001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.util.xml;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
018    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
019    import com.liferay.portal.kernel.util.CharPool;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    import java.io.IOException;
025    
026    import org.dom4j.Branch;
027    import org.dom4j.Document;
028    import org.dom4j.DocumentException;
029    import org.dom4j.io.OutputFormat;
030    import org.dom4j.io.SAXReader;
031    import org.dom4j.io.XMLWriter;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Alan Zimmerman
036     */
037    public class XMLFormatter {
038    
039            public static String fixProlog(String xml) {
040    
041                    // LEP-1921
042    
043                    if (xml != null) {
044                            int pos = xml.indexOf(CharPool.LESS_THAN);
045    
046                            if (pos > 0) {
047                                    xml = xml.substring(pos);
048                            }
049                    }
050    
051                    return xml;
052            }
053    
054            public static String fromCompactSafe(String xml) {
055                    return StringUtil.replace(xml, "[$NEW_LINE$]", StringPool.NEW_LINE);
056            }
057    
058            public static String stripInvalidChars(String xml) {
059                    if (Validator.isNull(xml)) {
060                            return xml;
061                    }
062    
063                    // Strip characters that are not valid in the 1.0 XML spec
064                    // http://www.w3.org/TR/2000/REC-xml-20001006#NT-Char
065    
066                    StringBuilder sb = new StringBuilder();
067    
068                    for (int i = 0; i < xml.length(); i++) {
069                            char c = xml.charAt(i);
070    
071                            if ((c == 0x9) || (c == 0xA) || (c == 0xD) ||
072                                    ((c >= 0x20) && (c <= 0xD7FF)) ||
073                                    ((c >= 0xE000) && (c <= 0xFFFD)) ||
074                                    ((c >= 0x10000) && (c <= 0x10FFFF))) {
075    
076                                    sb.append(c);
077                            }
078                    }
079    
080                    return sb.toString();
081            }
082    
083            public static String toCompactSafe(String xml) {
084                    return StringUtil.replace(
085                            xml,
086                            new String[] {
087                                    StringPool.RETURN_NEW_LINE,
088                                    StringPool.NEW_LINE,
089                                    StringPool.RETURN
090                            },
091                            new String[] {
092                                    "[$NEW_LINE$]",
093                                    "[$NEW_LINE$]",
094                                    "[$NEW_LINE$]"
095                            });
096            }
097    
098            public static String toString(Branch branch) throws IOException {
099                    return toString(branch, StringPool.TAB);
100            }
101    
102            public static String toString(Branch branch, String indent)
103                    throws IOException {
104    
105                    return toString(branch, StringPool.TAB, false);
106            }
107    
108            public static String toString(
109                            Branch branch, String indent, boolean expandEmptyElements)
110                    throws IOException {
111    
112                    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
113                            new UnsyncByteArrayOutputStream();
114    
115                    OutputFormat outputFormat = OutputFormat.createPrettyPrint();
116    
117                    outputFormat.setExpandEmptyElements(expandEmptyElements);
118                    outputFormat.setIndent(indent);
119                    outputFormat.setLineSeparator(StringPool.NEW_LINE);
120    
121                    XMLWriter writer = new XMLWriter(
122                            unsyncByteArrayOutputStream, outputFormat);
123    
124                    writer.write(branch);
125    
126                    String content = unsyncByteArrayOutputStream.toString(StringPool.UTF8);
127    
128                    // LEP-4257
129    
130                    //content = StringUtil.replace(content, "\n\n\n", "\n\n");
131    
132                    if (content.endsWith("\n\n")) {
133                            content = content.substring(0, content.length() - 2);
134                    }
135    
136                    if (content.endsWith("\n")) {
137                            content = content.substring(0, content.length() - 1);
138                    }
139    
140                    while (content.indexOf(" \n") != -1) {
141                            content = StringUtil.replace(content, " \n", "\n");
142                    }
143    
144                    if (content.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")) {
145                            content = StringUtil.replaceFirst(
146                                    content, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
147                                    "<?xml version=\"1.0\"?>");
148                    }
149    
150                    return content;
151            }
152    
153            public static String toString(String xml)
154                    throws DocumentException, IOException {
155    
156                    return toString(xml, StringPool.TAB);
157            }
158    
159            public static String toString(String xml, String indent)
160                    throws DocumentException, IOException {
161    
162                    SAXReader saxReader = new SAXReader();
163    
164                    Document document = saxReader.read(new UnsyncStringReader(xml));
165    
166                    return toString(document, indent);
167            }
168    
169    }