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