Directly loop through characters from the source string instead of
using a StringReader. Dropped character entity encoding, since this limits us to western encoding and should not be necessary because browsers usually default to western encoding.
This commit is contained in:
parent
7a6b473e22
commit
4f28990143
1 changed files with 94 additions and 134 deletions
|
@ -124,60 +124,60 @@ public final class HtmlEncoder {
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public final static String encode (String what) {
|
public final static String encode (String str) {
|
||||||
// try to make stringbuffer large enough from the start
|
// try to make stringbuffer large enough from the start
|
||||||
StringBuffer ret = new StringBuffer (Math.round (what.length()*1.4f));
|
StringBuffer ret = new StringBuffer (Math.round (str.length()*1.4f));
|
||||||
encode (what, ret);
|
encode (str, ret);
|
||||||
return ret.toString();
|
return ret.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public final static void encode (String what, StringBuffer ret) {
|
public final static void encode (String str, StringBuffer ret) {
|
||||||
if (what == null || what.length() == 0) {
|
if (str == null)
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
StringReader in = new StringReader (what);
|
int l = str.length();
|
||||||
int c;
|
|
||||||
boolean closeTag=false, readTag=false, tagOpen=false;
|
boolean closeTag=false, readTag=false, tagOpen=false;
|
||||||
// the difference between swallowOneNewline and ignoreNewline is that swallowOneNewline is just effective once (for the next newline)
|
// the difference between swallowOneNewline and ignoreNewline is that swallowOneNewline is just effective once (for the next newline)
|
||||||
boolean ignoreNewline = false;
|
boolean ignoreNewline = false;
|
||||||
boolean swallowOneNewline = false;
|
boolean swallowOneNewline = false;
|
||||||
StringBuffer tag = new StringBuffer ();
|
StringBuffer tag = new StringBuffer ();
|
||||||
try {
|
|
||||||
while ((c = in.read()) != -1) {
|
|
||||||
if (readTag) {
|
|
||||||
if (Character.isLetterOrDigit ((char) c))
|
|
||||||
tag.append ((char) c);
|
|
||||||
else if ('/' == c)
|
|
||||||
closeTag = true;
|
|
||||||
else {
|
|
||||||
String t = tag.toString ();
|
|
||||||
// set ignoreNewline on some tags, depending on wheather they're
|
|
||||||
// being opened or closed.
|
|
||||||
// what's going on here? we switch newline encoding on inside some tags, for
|
|
||||||
// others we switch it on when they're closed
|
|
||||||
if ("td".equalsIgnoreCase (t) || "th".equalsIgnoreCase (t) || "li".equalsIgnoreCase (t)) {
|
|
||||||
ignoreNewline = closeTag;
|
|
||||||
swallowOneNewline = true;
|
|
||||||
} else if ("table".equalsIgnoreCase (t) || "ul".equalsIgnoreCase (t) || "ol".equalsIgnoreCase (t) || "pre".equalsIgnoreCase (t)) {
|
|
||||||
ignoreNewline = !closeTag;
|
|
||||||
swallowOneNewline = true;
|
|
||||||
} else if ("p".equalsIgnoreCase (t)) {
|
|
||||||
swallowOneNewline = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
readTag = false;
|
for (int i=0; i<l; i++) {
|
||||||
closeTag = false;
|
char c = str.charAt (i);
|
||||||
tag.setLength (0);
|
if (readTag) {
|
||||||
}
|
if (Character.isLetterOrDigit (c))
|
||||||
} // if (readTag)
|
tag.append (c);
|
||||||
|
else if ('/' == c)
|
||||||
|
closeTag = true;
|
||||||
|
else {
|
||||||
|
String t = tag.toString ();
|
||||||
|
// set ignoreNewline on some tags, depending on wheather they're
|
||||||
|
// being opened or closed.
|
||||||
|
// what's going on here? we switch newline encoding on inside some tags, for
|
||||||
|
// others we switch it on when they're closed
|
||||||
|
if ("td".equalsIgnoreCase (t) || "th".equalsIgnoreCase (t) || "li".equalsIgnoreCase (t)) {
|
||||||
|
ignoreNewline = closeTag;
|
||||||
|
swallowOneNewline = true;
|
||||||
|
} else if ("table".equalsIgnoreCase (t) || "ul".equalsIgnoreCase (t) || "ol".equalsIgnoreCase (t) || "pre".equalsIgnoreCase (t)) {
|
||||||
|
ignoreNewline = !closeTag;
|
||||||
|
swallowOneNewline = true;
|
||||||
|
} else if ("p".equalsIgnoreCase (t)) {
|
||||||
|
swallowOneNewline = true;
|
||||||
|
}
|
||||||
|
|
||||||
switch (c) {
|
readTag = false;
|
||||||
|
closeTag = false;
|
||||||
|
tag.setLength (0);
|
||||||
|
}
|
||||||
|
} // if (readTag)
|
||||||
|
|
||||||
|
switch (c) {
|
||||||
// case '&':
|
// case '&':
|
||||||
// ret.append ("&");
|
// ret.append ("&");
|
||||||
// break;
|
// break;
|
||||||
case '\n':
|
case '\n':
|
||||||
ret.append ('\n');
|
ret.append ('\n');
|
||||||
|
@ -197,28 +197,28 @@ public final class HtmlEncoder {
|
||||||
ret.append ('>');
|
ret.append ('>');
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (c < 160)
|
ret.append (c);
|
||||||
ret.append ((char) c);
|
// if (c < 160)
|
||||||
|
// ret.append ((char) c);
|
||||||
// else if (c >= 160 && c <= 255)
|
// else if (c >= 160 && c <= 255)
|
||||||
// ret.append (convertor.get(new Integer(c)));
|
// ret.append (convertor.get(new Integer(c)));
|
||||||
else {
|
// else {
|
||||||
ret.append ("&#");
|
// ret.append ("&#");
|
||||||
ret.append (c);
|
// ret.append (c);
|
||||||
ret.append (";");
|
// ret.append (";");
|
||||||
}
|
// }
|
||||||
if (!tagOpen && !Character.isWhitespace ((char)c))
|
if (!tagOpen && !Character.isWhitespace (c))
|
||||||
swallowOneNewline = false;
|
swallowOneNewline = false;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (IOException e) {}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public final static String encodeFormValue (String what) {
|
public final static String encodeFormValue (String str) {
|
||||||
StringBuffer ret = new StringBuffer (Math.round (what.length()*1.4f));
|
StringBuffer ret = new StringBuffer (Math.round (str.length()*1.2f));
|
||||||
encodeAll (what, ret, false);
|
encodeAll (str, ret, false);
|
||||||
return ret.toString();
|
return ret.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -226,17 +226,17 @@ public final class HtmlEncoder {
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public final static String encodeAll (String what) {
|
public final static String encodeAll (String str) {
|
||||||
StringBuffer ret = new StringBuffer (Math.round (what.length()*1.4f));
|
StringBuffer ret = new StringBuffer (Math.round (str.length()*1.2f));
|
||||||
encodeAll (what, ret, true);
|
encodeAll (str, ret, true);
|
||||||
return ret.toString();
|
return ret.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public final static String encodeAll (String what, StringBuffer ret) {
|
public final static String encodeAll (String str, StringBuffer ret) {
|
||||||
encodeAll (what, ret, true);
|
encodeAll (str, ret, true);
|
||||||
return ret.toString();
|
return ret.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -244,116 +244,76 @@ public final class HtmlEncoder {
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public final static void encodeAll (String what, StringBuffer ret, boolean encodeNewline) {
|
public final static void encodeAll (String str, StringBuffer ret, boolean encodeNewline) {
|
||||||
if (what == null || what.length() == 0) {
|
if (str == null)
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
StringReader in = new StringReader (what);
|
int l = str.length();
|
||||||
int c;
|
for (int i=0; i<l; i++) {
|
||||||
try {
|
char c = str.charAt (i);
|
||||||
while ((c = in.read()) != -1) {
|
switch (c) {
|
||||||
switch (c) {
|
case '<' :
|
||||||
case '<' :
|
|
||||||
ret.append ("<");
|
ret.append ("<");
|
||||||
break;
|
break;
|
||||||
case '>':
|
case '>':
|
||||||
ret.append (">");
|
ret.append (">");
|
||||||
break;
|
break;
|
||||||
case '&':
|
case '&':
|
||||||
ret.append ("&");
|
ret.append ("&");
|
||||||
break;
|
break;
|
||||||
case '"':
|
case '"':
|
||||||
ret.append (""");
|
ret.append (""");
|
||||||
break;
|
break;
|
||||||
case '\n':
|
case '\n':
|
||||||
ret.append ('\n');
|
ret.append ('\n');
|
||||||
if (encodeNewline) {
|
if (encodeNewline) {
|
||||||
ret.append ("<br>");
|
ret.append ("<br>");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (c < 160)
|
ret.append (c);
|
||||||
ret.append ((char) c);
|
// if (c < 160)
|
||||||
|
// ret.append ((char) c);
|
||||||
// else if (c >= 160 && c <= 255)
|
// else if (c >= 160 && c <= 255)
|
||||||
// ret.append (convertor.get(new Integer(c)));
|
// ret.append (convertor.get(new Integer(c)));
|
||||||
else {
|
// else {
|
||||||
ret.append ("&#");
|
// ret.append ("&#");
|
||||||
ret.append (c);
|
// ret.append (c);
|
||||||
ret.append (";");
|
// ret.append (";");
|
||||||
}
|
// }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (IOException e) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
public final static String encodeSoft (String what) {
|
|
||||||
StringBuffer ret = new StringBuffer (Math.round (what.length()*1.4f));
|
|
||||||
encodeSoft (what, ret);
|
|
||||||
return ret.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public final static void encodeSoft (String what, StringBuffer ret) {
|
|
||||||
if (what == null || what.length() == 0) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
StringReader in = new StringReader (what);
|
|
||||||
int c;
|
|
||||||
try {
|
|
||||||
while ((c = in.read()) != -1) {
|
|
||||||
switch (c) {
|
|
||||||
case 128: // Euro-Symbol. This is for missing Unicode support in TowerJ.
|
|
||||||
ret.append ("€");
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
if (c < 160)
|
|
||||||
ret.append ((char) c);
|
|
||||||
// else if (c >= 160 && c <= 255)
|
|
||||||
// ret.append (convertor.get(new Integer(c)));
|
|
||||||
else {
|
|
||||||
ret.append ("&#");
|
|
||||||
ret.append (c);
|
|
||||||
ret.append (";");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (IOException e) {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public final static String encodeXml (String what) {
|
public final static String encodeXml (String str) {
|
||||||
StringBuffer ret = new StringBuffer (Math.round (what.length()*1.4f));
|
StringBuffer ret = new StringBuffer (Math.round (str.length()*1.2f));
|
||||||
encodeXml (what, ret);
|
encodeXml (str, ret);
|
||||||
return ret.toString();
|
return ret.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public final static void encodeXml (String what, StringBuffer ret) {
|
public final static void encodeXml (String str, StringBuffer ret) {
|
||||||
if (what == null || what.length() == 0) {
|
if (str == null)
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
StringReader in = new StringReader (what);
|
int l = str.length();
|
||||||
int c;
|
for (int i=0; i<l; i++) {
|
||||||
try {
|
char c = str.charAt (i);
|
||||||
while ((c = in.read()) != -1) {
|
switch (c) {
|
||||||
switch (c) {
|
case '<' :
|
||||||
case '<' :
|
|
||||||
ret.append ("<");
|
ret.append ("<");
|
||||||
break;
|
break;
|
||||||
case '>':
|
case '>':
|
||||||
ret.append (">");
|
ret.append (">");
|
||||||
break;
|
break;
|
||||||
case '&':
|
case '&':
|
||||||
ret.append ("&");
|
ret.append ("&");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
ret.append ((char) c);
|
ret.append (c);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (IOException e) {}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} // end of class
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue