Skip to content

Commit

Permalink
LPS-56451 Move methods not specific to dom4j
Browse files Browse the repository at this point in the history
  • Loading branch information
marcellustavares authored and brianchandotcom committed Jun 19, 2015
1 parent 21eeb28 commit 55f35d3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 56 deletions.
56 changes: 0 additions & 56 deletions util-java/src/com/liferay/util/xml/XMLFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@

import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
import com.liferay.portal.kernel.util.CharPool;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.security.xml.SecureXMLFactoryProviderUtil;

import java.io.IOException;
Expand All @@ -39,60 +37,6 @@
*/
public class XMLFormatter {

public static String fixProlog(String xml) {

// LEP-1921

if (xml != null) {
int pos = xml.indexOf(CharPool.LESS_THAN);

if (pos > 0) {
xml = xml.substring(pos);
}
}

return xml;
}

public static String fromCompactSafe(String xml) {
return StringUtil.replace(xml, "[$NEW_LINE$]", StringPool.NEW_LINE);
}

public static String stripInvalidChars(String xml) {
if (Validator.isNull(xml)) {
return xml;
}

// Strip characters that are not valid in the 1.0 XML spec
// http://www.w3.org/TR/2000/REC-xml-20001006#NT-Char

StringBuilder sb = new StringBuilder();

for (int i = 0; i < xml.length(); i++) {
char c = xml.charAt(i);

if ((c == 0x9) || (c == 0xA) || (c == 0xD) ||
((c >= 0x20) && (c <= 0xD7FF)) ||
((c >= 0xE000) && (c <= 0xFFFD)) ||
((c >= 0x10000) && (c <= 0x10FFFF))) {

sb.append(c);
}
}

return sb.toString();
}

public static String toCompactSafe(String xml) {
return StringUtil.replace(
xml,
new String[] {
StringPool.RETURN_NEW_LINE, StringPool.NEW_LINE,
StringPool.RETURN
},
new String[] {"[$NEW_LINE$]", "[$NEW_LINE$]", "[$NEW_LINE$]"});
}

public static String toString(Node node) throws IOException {
return toString(node, StringPool.TAB);
}
Expand Down
57 changes: 57 additions & 0 deletions util-java/src/com/liferay/util/xml/XMLUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
package com.liferay.util.xml;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.CharPool;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.xml.Document;

import java.io.IOException;
Expand All @@ -25,6 +28,21 @@
*/
public class XMLUtil {

public static String fixProlog(String xml) {

// LEP-1921

if (xml != null) {
int pos = xml.indexOf(CharPool.LESS_THAN);

if (pos > 0) {
xml = xml.substring(pos);
}
}

return xml;
}

public static String formatXML(Document document) {
try {
return document.formattedString(_XML_INDENT);
Expand Down Expand Up @@ -55,6 +73,45 @@ public static String formatXML(String xml) {
}
}

public static String fromCompactSafe(String xml) {
return StringUtil.replace(xml, "[$NEW_LINE$]", StringPool.NEW_LINE);
}

public static String stripInvalidChars(String xml) {
if (Validator.isNull(xml)) {
return xml;
}

// Strip characters that are not valid in the 1.0 XML spec
// http://www.w3.org/TR/2000/REC-xml-20001006#NT-Char

StringBuilder sb = new StringBuilder();

for (int i = 0; i < xml.length(); i++) {
char c = xml.charAt(i);

if ((c == 0x9) || (c == 0xA) || (c == 0xD) ||
((c >= 0x20) && (c <= 0xD7FF)) ||
((c >= 0xE000) && (c <= 0xFFFD)) ||
((c >= 0x10000) && (c <= 0x10FFFF))) {

sb.append(c);
}
}

return sb.toString();
}

public static String toCompactSafe(String xml) {
return StringUtil.replace(
xml,
new String[] {
StringPool.RETURN_NEW_LINE, StringPool.NEW_LINE,
StringPool.RETURN
},
new String[] {"[$NEW_LINE$]", "[$NEW_LINE$]", "[$NEW_LINE$]"});
}

private static final String _XML_INDENT = " ";

}

0 comments on commit 55f35d3

Please sign in to comment.