moved to util

This commit is contained in:
Alexey Kudravtsev
2013-04-26 15:06:32 +04:00
parent 461df77664
commit 3f92614689
2 changed files with 60 additions and 61 deletions
@@ -15,69 +15,10 @@
*/
package com.intellij.xml.util;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;
public class XmlTagUtilBase {
public static String escapeString(@Nullable final String str, final boolean escapeWhiteSpace) {
if (str == null) return null;
StringBuffer buffer = null;
for (int i = 0; i < str.length(); i++) {
@NonNls String entity;
char ch = str.charAt(i);
switch (ch) {
case '\n':
entity = escapeWhiteSpace ? "&#10;" : null;
break;
case '\r':
entity = escapeWhiteSpace ? "&#13;" : null;
break;
case '\t':
entity = escapeWhiteSpace ? "&#9;" : null;
break;
case'\"':
entity = "&quot;";
break;
case'<':
entity = "&lt;";
break;
case'>':
entity = "&gt;";
break;
case'&':
entity = "&amp;";
break;
case 160: // unicode char for &nbsp;
entity = "&nbsp;";
break;
default:
entity = null;
break;
}
if (buffer == null) {
if (entity != null) {
// An entity occurred, so we'll have to use StringBuffer
// (allocate room for it plus a few more entities).
buffer = new StringBuffer(str.length() + 20);
// Copy previous skipped characters and fall through
// to pickup current character
buffer.append(str.substring(0, i));
buffer.append(entity);
}
}
else {
if (entity == null) {
buffer.append(ch);
}
else {
buffer.append(entity);
}
}
}
// If there were any entities, return the escaped characters
// that we put in the StringBuffer. Otherwise, just return
// the unmodified input string.
return buffer == null ? str : buffer.toString();
return XmlStringUtil.escapeString(str, escapeWhiteSpace);
}
}
@@ -38,7 +38,65 @@ public class XmlStringUtil {
}
public static String escapeString(@Nullable String str, final boolean escapeWhiteSpace) {
return XmlTagUtilBase.escapeString(str, escapeWhiteSpace);
if (str == null) return null;
StringBuilder buffer = null;
for (int i = 0; i < str.length(); i++) {
@NonNls String entity;
char ch = str.charAt(i);
switch (ch) {
case '\n':
entity = escapeWhiteSpace ? "&#10;" : null;
break;
case '\r':
entity = escapeWhiteSpace ? "&#13;" : null;
break;
case '\t':
entity = escapeWhiteSpace ? "&#9;" : null;
break;
case'\"':
entity = "&quot;";
break;
case'<':
entity = "&lt;";
break;
case'>':
entity = "&gt;";
break;
case'&':
entity = "&amp;";
break;
case 160: // unicode char for &nbsp;
entity = "&nbsp;";
break;
default:
entity = null;
break;
}
if (buffer == null) {
if (entity != null) {
// An entity occurred, so we'll have to use StringBuffer
// (allocate room for it plus a few more entities).
buffer = new StringBuilder(str.length() + 20);
// Copy previous skipped characters and fall through
// to pickup current character
buffer.append(str.substring(0, i));
buffer.append(entity);
}
}
else {
if (entity == null) {
buffer.append(ch);
}
else {
buffer.append(entity);
}
}
}
// If there were any entities, return the escaped characters
// that we put in the StringBuffer. Otherwise, just return
// the unmodified input string.
return buffer == null ? str : buffer.toString();
}
@NotNull