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.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.security.xml.SecureXMLFactoryProviderUtil;
022    
023    import java.io.IOException;
024    
025    import org.dom4j.Document;
026    import org.dom4j.DocumentException;
027    import org.dom4j.Node;
028    import org.dom4j.io.OutputFormat;
029    import org.dom4j.io.SAXReader;
030    import org.dom4j.io.XMLWriter;
031    
032    import org.xml.sax.XMLReader;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     * @author Alan Zimmerman
037     */
038    public class Dom4jUtil {
039    
040            public static String toString(Node node) throws IOException {
041                    return toString(node, StringPool.TAB);
042            }
043    
044            public static String toString(Node node, String indent) throws IOException {
045                    return toString(node, StringPool.TAB, false);
046            }
047    
048            public static String toString(
049                            Node node, String indent, boolean expandEmptyElements)
050                    throws IOException {
051    
052                    return toString(node, indent, expandEmptyElements, true);
053            }
054    
055            public static String toString(
056                            Node node, String indent, boolean expandEmptyElements,
057                            boolean trimText)
058                    throws IOException {
059    
060                    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
061                            new UnsyncByteArrayOutputStream();
062    
063                    OutputFormat outputFormat = OutputFormat.createPrettyPrint();
064    
065                    outputFormat.setExpandEmptyElements(expandEmptyElements);
066                    outputFormat.setIndent(indent);
067                    outputFormat.setLineSeparator(StringPool.NEW_LINE);
068                    outputFormat.setTrimText(trimText);
069    
070                    XMLWriter xmlWriter = new XMLWriter(
071                            unsyncByteArrayOutputStream, outputFormat);
072    
073                    xmlWriter.write(node);
074    
075                    String content = unsyncByteArrayOutputStream.toString(StringPool.UTF8);
076    
077                    // LEP-4257
078    
079                    //content = StringUtil.replace(content, "\n\n\n", "\n\n");
080    
081                    if (content.endsWith("\n\n")) {
082                            content = content.substring(0, content.length() - 2);
083                    }
084    
085                    if (content.endsWith("\n")) {
086                            content = content.substring(0, content.length() - 1);
087                    }
088    
089                    while (content.contains(" \n")) {
090                            content = StringUtil.replace(content, " \n", "\n");
091                    }
092    
093                    if (content.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")) {
094                            content = StringUtil.replaceFirst(
095                                    content, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
096                                    "<?xml version=\"1.0\"?>");
097                    }
098    
099                    return content;
100            }
101    
102            public static String toString(String xml)
103                    throws DocumentException, IOException {
104    
105                    return toString(xml, StringPool.TAB);
106            }
107    
108            public static String toString(String xml, String indent)
109                    throws DocumentException, IOException {
110    
111                    XMLReader xmlReader = null;
112    
113                    if (SecureXMLFactoryProviderUtil.getSecureXMLFactoryProvider()
114                                    != null) {
115    
116                            xmlReader = SecureXMLFactoryProviderUtil.newXMLReader();
117                    }
118    
119                    SAXReader saxReader = new SAXReader(xmlReader);
120    
121                    Document document = saxReader.read(new UnsyncStringReader(xml));
122    
123                    return toString(document, indent);
124            }
125    
126    }