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    import com.liferay.portal.security.xml.SecureXMLFactoryProviderUtil;
024    
025    import java.io.IOException;
026    
027    import org.dom4j.Document;
028    import org.dom4j.DocumentException;
029    import org.dom4j.Node;
030    import org.dom4j.io.OutputFormat;
031    import org.dom4j.io.SAXReader;
032    import org.dom4j.io.XMLWriter;
033    
034    import org.xml.sax.XMLReader;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     * @author Alan Zimmerman
039     */
040    public class XMLFormatter {
041    
042            public static String fixProlog(String xml) {
043    
044                    // LEP-1921
045    
046                    if (xml != null) {
047                            int pos = xml.indexOf(CharPool.LESS_THAN);
048    
049                            if (pos > 0) {
050                                    xml = xml.substring(pos);
051                            }
052                    }
053    
054                    return xml;
055            }
056    
057            public static String fromCompactSafe(String xml) {
058                    return StringUtil.replace(xml, "[$NEW_LINE$]", StringPool.NEW_LINE);
059            }
060    
061            public static String stripInvalidChars(String xml) {
062                    if (Validator.isNull(xml)) {
063                            return xml;
064                    }
065    
066                    // Strip characters that are not valid in the 1.0 XML spec
067                    // http://www.w3.org/TR/2000/REC-xml-20001006#NT-Char
068    
069                    StringBuilder sb = new StringBuilder();
070    
071                    for (int i = 0; i < xml.length(); i++) {
072                            char c = xml.charAt(i);
073    
074                            if ((c == 0x9) || (c == 0xA) || (c == 0xD) ||
075                                    ((c >= 0x20) && (c <= 0xD7FF)) ||
076                                    ((c >= 0xE000) && (c <= 0xFFFD)) ||
077                                    ((c >= 0x10000) && (c <= 0x10FFFF))) {
078    
079                                    sb.append(c);
080                            }
081                    }
082    
083                    return sb.toString();
084            }
085    
086            public static String toCompactSafe(String xml) {
087                    return StringUtil.replace(
088                            xml,
089                            new String[] {
090                                    StringPool.RETURN_NEW_LINE, StringPool.NEW_LINE,
091                                    StringPool.RETURN
092                            },
093                            new String[] {
094                                    "[$NEW_LINE$]", "[$NEW_LINE$]", "[$NEW_LINE$]"
095                            });
096            }
097    
098            public static String toString(Node node) throws IOException {
099                    return toString(node, StringPool.TAB);
100            }
101    
102            public static String toString(Node node, String indent) throws IOException {
103                    return toString(node, StringPool.TAB, false);
104            }
105    
106            public static String toString(
107                            Node node, String indent, boolean expandEmptyElements)
108                    throws IOException {
109    
110                    return toString(node, indent, expandEmptyElements, true);
111            }
112    
113            public static String toString(
114                            Node node, String indent, boolean expandEmptyElements,
115                            boolean trimText)
116                    throws IOException {
117    
118                    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
119                            new UnsyncByteArrayOutputStream();
120    
121                    OutputFormat outputFormat = OutputFormat.createPrettyPrint();
122    
123                    outputFormat.setExpandEmptyElements(expandEmptyElements);
124                    outputFormat.setIndent(indent);
125                    outputFormat.setLineSeparator(StringPool.NEW_LINE);
126                    outputFormat.setTrimText(trimText);
127    
128                    XMLWriter xmlWriter = new XMLWriter(
129                            unsyncByteArrayOutputStream, outputFormat);
130    
131                    xmlWriter.write(node);
132    
133                    String content = unsyncByteArrayOutputStream.toString(StringPool.UTF8);
134    
135                    // LEP-4257
136    
137                    //content = StringUtil.replace(content, "\n\n\n", "\n\n");
138    
139                    if (content.endsWith("\n\n")) {
140                            content = content.substring(0, content.length() - 2);
141                    }
142    
143                    if (content.endsWith("\n")) {
144                            content = content.substring(0, content.length() - 1);
145                    }
146    
147                    while (content.contains(" \n")) {
148                            content = StringUtil.replace(content, " \n", "\n");
149                    }
150    
151                    if (content.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")) {
152                            content = StringUtil.replaceFirst(
153                                    content, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
154                                    "<?xml version=\"1.0\"?>");
155                    }
156    
157                    return content;
158            }
159    
160            public static String toString(String xml)
161                    throws DocumentException, IOException {
162    
163                    return toString(xml, StringPool.TAB);
164            }
165    
166            public static String toString(String xml, String indent)
167                    throws DocumentException, IOException {
168    
169                    XMLReader xmlReader = null;
170    
171                    if (SecureXMLFactoryProviderUtil.getSecureXMLFactoryProvider()
172                                    != null) {
173    
174                            xmlReader = SecureXMLFactoryProviderUtil.newXMLReader();
175                    }
176    
177                    SAXReader saxReader = new SAXReader(xmlReader);
178    
179                    Document document = saxReader.read(new UnsyncStringReader(xml));
180    
181                    return toString(document, indent);
182            }
183    
184    }