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