001
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
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
078
079
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 }