001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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.Document;
027    import org.dom4j.DocumentException;
028    import org.dom4j.Node;
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, StringPool.NEW_LINE,
088                                    StringPool.RETURN
089                            },
090                            new String[] {
091                                    "[$NEW_LINE$]", "[$NEW_LINE$]", "[$NEW_LINE$]"
092                            });
093            }
094    
095            public static String toString(Node node) throws IOException {
096                    return toString(node, StringPool.TAB);
097            }
098    
099            public static String toString(Node node, String indent)
100                    throws IOException {
101    
102                    return toString(node, StringPool.TAB, false);
103            }
104    
105            public static String toString(
106                            Node node, String indent, boolean expandEmptyElements)
107                    throws IOException {
108    
109                    return toString(node, indent, expandEmptyElements, true);
110            }
111    
112            public static String toString(
113                            Node node, String indent, boolean expandEmptyElements,
114                            boolean trimText)
115                    throws IOException {
116    
117                    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
118                            new UnsyncByteArrayOutputStream();
119    
120                    OutputFormat outputFormat = OutputFormat.createPrettyPrint();
121    
122                    outputFormat.setExpandEmptyElements(expandEmptyElements);
123                    outputFormat.setIndent(indent);
124                    outputFormat.setLineSeparator(StringPool.NEW_LINE);
125                    outputFormat.setTrimText(trimText);
126    
127                    XMLWriter xmlWriter = new XMLWriter(
128                            unsyncByteArrayOutputStream, outputFormat);
129    
130                    xmlWriter.write(node);
131    
132                    String content = unsyncByteArrayOutputStream.toString(StringPool.UTF8);
133    
134                    // LEP-4257
135    
136                    //content = StringUtil.replace(content, "\n\n\n", "\n\n");
137    
138                    if (content.endsWith("\n\n")) {
139                            content = content.substring(0, content.length() - 2);
140                    }
141    
142                    if (content.endsWith("\n")) {
143                            content = content.substring(0, content.length() - 1);
144                    }
145    
146                    while (content.indexOf(" \n") != -1) {
147                            content = StringUtil.replace(content, " \n", "\n");
148                    }
149    
150                    if (content.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")) {
151                            content = StringUtil.replaceFirst(
152                                    content, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
153                                    "<?xml version=\"1.0\"?>");
154                    }
155    
156                    return content;
157            }
158    
159            public static String toString(String xml)
160                    throws DocumentException, IOException {
161    
162                    return toString(xml, StringPool.TAB);
163            }
164    
165            public static String toString(String xml, String indent)
166                    throws DocumentException, IOException {
167    
168                    SAXReader saxReader = new SAXReader();
169    
170                    Document document = saxReader.read(new UnsyncStringReader(xml));
171    
172                    return toString(document, indent);
173            }
174    
175    }