001    /**
002     * Copyright (c) 2000-2010 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.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                    return content;
145            }
146    
147            public static String toString(String xml)
148                    throws DocumentException, IOException {
149    
150                    return toString(xml, StringPool.TAB);
151            }
152    
153            public static String toString(String xml, String indent)
154                    throws DocumentException, IOException {
155    
156                    SAXReader reader = new SAXReader();
157    
158                    Document doc = reader.read(new UnsyncStringReader(xml));
159    
160                    return toString(doc, indent);
161            }
162    
163    }