001    /**
002     * Copyright (c) 2000-2013 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) throws IOException {
100                    return toString(node, StringPool.TAB, false);
101            }
102    
103            public static String toString(
104                            Node node, String indent, boolean expandEmptyElements)
105                    throws IOException {
106    
107                    return toString(node, indent, expandEmptyElements, true);
108            }
109    
110            public static String toString(
111                            Node node, String indent, boolean expandEmptyElements,
112                            boolean trimText)
113                    throws IOException {
114    
115                    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
116                            new UnsyncByteArrayOutputStream();
117    
118                    OutputFormat outputFormat = OutputFormat.createPrettyPrint();
119    
120                    outputFormat.setExpandEmptyElements(expandEmptyElements);
121                    outputFormat.setIndent(indent);
122                    outputFormat.setLineSeparator(StringPool.NEW_LINE);
123                    outputFormat.setTrimText(trimText);
124    
125                    XMLWriter xmlWriter = new XMLWriter(
126                            unsyncByteArrayOutputStream, outputFormat);
127    
128                    xmlWriter.write(node);
129    
130                    String content = unsyncByteArrayOutputStream.toString(StringPool.UTF8);
131    
132                    // LEP-4257
133    
134                    //content = StringUtil.replace(content, "\n\n\n", "\n\n");
135    
136                    if (content.endsWith("\n\n")) {
137                            content = content.substring(0, content.length() - 2);
138                    }
139    
140                    if (content.endsWith("\n")) {
141                            content = content.substring(0, content.length() - 1);
142                    }
143    
144                    while (content.contains(" \n")) {
145                            content = StringUtil.replace(content, " \n", "\n");
146                    }
147    
148                    if (content.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")) {
149                            content = StringUtil.replaceFirst(
150                                    content, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
151                                    "<?xml version=\"1.0\"?>");
152                    }
153    
154                    return content;
155            }
156    
157            public static String toString(String xml)
158                    throws DocumentException, IOException {
159    
160                    return toString(xml, StringPool.TAB);
161            }
162    
163            public static String toString(String xml, String indent)
164                    throws DocumentException, IOException {
165    
166                    SAXReader saxReader = new SAXReader();
167    
168                    Document document = saxReader.read(new UnsyncStringReader(xml));
169    
170                    return toString(document, indent);
171            }
172    
173    }