1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util.xml;
24  
25  import com.liferay.portal.kernel.util.ByteArrayMaker;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.StringUtil;
28  
29  import java.io.IOException;
30  import java.io.StringReader;
31  
32  import org.dom4j.Branch;
33  import org.dom4j.Document;
34  import org.dom4j.DocumentException;
35  import org.dom4j.io.OutputFormat;
36  import org.dom4j.io.SAXReader;
37  import org.dom4j.io.XMLWriter;
38  
39  /**
40   * <a href="XMLFormatter.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   * @author Alan Zimmerman
44   *
45   */
46  public class XMLFormatter {
47  
48      public static String fixProlog(String xml) {
49  
50          // LEP-1921
51  
52          if (xml != null) {
53              char[] charArray = xml.toCharArray();
54  
55              for (int i = 0; i < charArray.length; i++) {
56                  if (charArray[i] == '<') {
57                      if (i != 0) {
58                          xml = xml.substring(i, xml.length());
59                      }
60  
61                      break;
62                  }
63              }
64          }
65  
66          return xml;
67      }
68  
69      public static String fromCompactSafe(String xml) {
70          return StringUtil.replace(xml, "[$NEW_LINE$]", StringPool.NEW_LINE);
71      }
72  
73      public static String toCompactSafe(String xml) {
74          return StringUtil.replace(
75              xml,
76              new String[] {
77                  StringPool.RETURN_NEW_LINE,
78                  StringPool.NEW_LINE,
79                  StringPool.RETURN
80              },
81              new String[] {
82                  "[$NEW_LINE$]",
83                  "[$NEW_LINE$]",
84                  "[$NEW_LINE$]"
85              });
86      }
87  
88      public static String toString(String xml)
89          throws DocumentException, IOException {
90  
91          return toString(xml, StringPool.TAB);
92      }
93  
94      public static String toString(String xml, String indent)
95          throws DocumentException, IOException {
96  
97          SAXReader reader = new SAXReader();
98  
99          Document doc = reader.read(new StringReader(xml));
100 
101         return toString(doc, indent);
102     }
103 
104     public static String toString(Branch branch) throws IOException {
105         return toString(branch, StringPool.TAB);
106     }
107 
108     public static String toString(Branch branch, String indent)
109         throws IOException {
110 
111         return toString(branch, StringPool.TAB, false);
112     }
113 
114     public static String toString(
115             Branch branch, String indent, boolean expandEmptyElements)
116         throws IOException {
117 
118         ByteArrayMaker bam = new ByteArrayMaker();
119 
120         OutputFormat format = OutputFormat.createPrettyPrint();
121 
122         format.setExpandEmptyElements(expandEmptyElements);
123         format.setIndent(indent);
124         format.setLineSeparator("\n");
125 
126         XMLWriter writer = new XMLWriter(bam, format);
127 
128         writer.write(branch);
129 
130         String content = bam.toString(StringPool.UTF8);
131 
132         // LEP-4257
133 
134         //content = StringUtil.replace(content, "\n\n\n", "\n\n");
135 
136         if (content.endsWith("\n\n")) {
137             content = content.substring(0, content.length() - 2);
138         }
139 
140         if (content.endsWith("\n")) {
141             content = content.substring(0, content.length() - 1);
142         }
143 
144         while (content.indexOf(" \n") != -1) {
145             content = StringUtil.replace(content, " \n", "\n");
146         }
147 
148         return content;
149     }
150 
151 }