001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.xml;
022:
023: import com.liferay.portal.kernel.util.ByteArrayMaker;
024: import com.liferay.portal.kernel.util.StringUtil;
025:
026: import java.io.IOException;
027: import java.io.StringReader;
028:
029: import org.dom4j.Branch;
030: import org.dom4j.Document;
031: import org.dom4j.DocumentException;
032: import org.dom4j.io.OutputFormat;
033: import org.dom4j.io.SAXReader;
034: import org.dom4j.io.XMLWriter;
035:
036: /**
037: * <a href="XMLFormatter.java.html"><b><i>View Source</i></b></a>
038: *
039: * @author Brian Wing Shun Chan
040: * @author Alan Zimmerman
041: *
042: */
043: public class XMLFormatter {
044:
045: public static final String ENCODING = "UTF-8";
046:
047: public static final String INDENT = "\t";
048:
049: public static String fixProlog(String xml) {
050:
051: // LEP-1921
052:
053: if (xml != null) {
054: char[] charArray = xml.toCharArray();
055:
056: for (int i = 0; i < charArray.length; i++) {
057: if (charArray[i] == '<') {
058: if (i != 0) {
059: xml = xml.substring(i, xml.length());
060: }
061:
062: break;
063: }
064: }
065: }
066:
067: return xml;
068: }
069:
070: public static String fromCompactSafe(String xml) {
071: return StringUtil.replace(xml, "[$NEW_LINE$]", "\n");
072: }
073:
074: public static String toCompactSafe(String xml) {
075: return StringUtil.replace(xml, "\n", "[$NEW_LINE$]");
076: }
077:
078: public static String toString(String xml) throws DocumentException,
079: IOException {
080:
081: return toString(xml, INDENT);
082: }
083:
084: public static String toString(String xml, String indent)
085: throws DocumentException, IOException {
086:
087: SAXReader reader = new SAXReader();
088:
089: Document doc = reader.read(new StringReader(xml));
090:
091: return toString(doc, indent);
092: }
093:
094: public static String toString(Branch branch) throws IOException {
095: return toString(branch, INDENT);
096: }
097:
098: public static String toString(Branch branch, String indent)
099: throws IOException {
100:
101: return toString(branch, INDENT, false);
102: }
103:
104: public static String toString(Branch branch, String indent,
105: boolean expandEmptyElements) throws IOException {
106:
107: ByteArrayMaker bam = new ByteArrayMaker();
108:
109: OutputFormat format = OutputFormat.createPrettyPrint();
110:
111: format.setExpandEmptyElements(expandEmptyElements);
112: format.setIndent(indent);
113: format.setLineSeparator("\n");
114:
115: XMLWriter writer = new XMLWriter(bam, format);
116:
117: writer.write(branch);
118:
119: String content = bam.toString(ENCODING);
120:
121: // LEP-4257
122:
123: //content = StringUtil.replace(content, "\n\n\n", "\n\n");
124:
125: if (content.endsWith("\n\n")) {
126: content = content.substring(0, content.length() - 2);
127: }
128:
129: if (content.endsWith("\n")) {
130: content = content.substring(0, content.length() - 1);
131: }
132:
133: while (content.indexOf(" \n") != -1) {
134: content = StringUtil.replace(content, " \n", "\n");
135: }
136:
137: return content;
138: }
139:
140: }
|