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:
hns 2001-09-14 16:04:23 +00:00
parent 7a6b473e22
commit 4f28990143

View file

@ -124,33 +124,33 @@ 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
StringBuffer ret = new StringBuffer (Math.round (what.length()*1.4f));
encode (what, ret);
StringBuffer ret = new StringBuffer (Math.round (str.length()*1.4f));
encode (str, ret);
return ret.toString();
}
/**
*
*/
public final static void encode (String what, StringBuffer ret) {
if (what == null || what.length() == 0) {
public final static void encode (String str, StringBuffer ret) {
if (str == null)
return;
}
StringReader in = new StringReader (what);
int c;
int l = str.length();
boolean closeTag=false, readTag=false, tagOpen=false;
// the difference between swallowOneNewline and ignoreNewline is that swallowOneNewline is just effective once (for the next newline)
boolean ignoreNewline = false;
boolean swallowOneNewline = false;
StringBuffer tag = new StringBuffer ();
try {
while ((c = in.read()) != -1) {
for (int i=0; i<l; i++) {
char c = str.charAt (i);
if (readTag) {
if (Character.isLetterOrDigit ((char) c))
tag.append ((char) c);
if (Character.isLetterOrDigit (c))
tag.append (c);
else if ('/' == c)
closeTag = true;
else {
@ -197,28 +197,28 @@ public final class HtmlEncoder {
ret.append ('>');
break;
default:
if (c < 160)
ret.append ((char) c);
ret.append (c);
// 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 (";");
}
if (!tagOpen && !Character.isWhitespace ((char)c))
// else {
// ret.append ("&#");
// ret.append (c);
// ret.append (";");
// }
if (!tagOpen && !Character.isWhitespace (c))
swallowOneNewline = false;
}
}
} catch (IOException e) {}
}
/**
*
*/
public final static String encodeFormValue (String what) {
StringBuffer ret = new StringBuffer (Math.round (what.length()*1.4f));
encodeAll (what, ret, false);
public final static String encodeFormValue (String str) {
StringBuffer ret = new StringBuffer (Math.round (str.length()*1.2f));
encodeAll (str, ret, false);
return ret.toString();
}
@ -226,17 +226,17 @@ public final class HtmlEncoder {
/**
*
*/
public final static String encodeAll (String what) {
StringBuffer ret = new StringBuffer (Math.round (what.length()*1.4f));
encodeAll (what, ret, true);
public final static String encodeAll (String str) {
StringBuffer ret = new StringBuffer (Math.round (str.length()*1.2f));
encodeAll (str, ret, true);
return ret.toString();
}
/**
*
*/
public final static String encodeAll (String what, StringBuffer ret) {
encodeAll (what, ret, true);
public final static String encodeAll (String str, StringBuffer ret) {
encodeAll (str, ret, true);
return ret.toString();
}
@ -244,15 +244,13 @@ public final class HtmlEncoder {
/**
*
*/
public final static void encodeAll (String what, StringBuffer ret, boolean encodeNewline) {
if (what == null || what.length() == 0) {
public final static void encodeAll (String str, StringBuffer ret, boolean encodeNewline) {
if (str == null)
return;
}
StringReader in = new StringReader (what);
int c;
try {
while ((c = in.read()) != -1) {
int l = str.length();
for (int i=0; i<l; i++) {
char c = str.charAt (i);
switch (c) {
case '<' :
ret.append ("&lt;");
@ -273,70 +271,34 @@ public final class HtmlEncoder {
}
break;
default:
if (c < 160)
ret.append ((char) c);
ret.append (c);
// 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 (";");
// else {
// ret.append ("&#");
// ret.append (c);
// 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);
public final static String encodeXml (String str) {
StringBuffer ret = new StringBuffer (Math.round (str.length()*1.2f));
encodeXml (str, ret);
return ret.toString();
}
public final static void encodeSoft (String what, StringBuffer ret) {
if (what == null || what.length() == 0) {
public final static void encodeXml (String str, StringBuffer ret) {
if (str == null)
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 ("&#8364;");
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) {
StringBuffer ret = new StringBuffer (Math.round (what.length()*1.4f));
encodeXml (what, ret);
return ret.toString();
}
public final static void encodeXml (String what, StringBuffer ret) {
if (what == null || what.length() == 0) {
return;
}
StringReader in = new StringReader (what);
int c;
try {
while ((c = in.read()) != -1) {
int l = str.length();
for (int i=0; i<l; i++) {
char c = str.charAt (i);
switch (c) {
case '<' :
ret.append ("&lt;");
@ -348,12 +310,10 @@ public final class HtmlEncoder {
ret.append ("&amp;");
break;
default:
ret.append ((char) c);
ret.append (c);
}
}
} catch (IOException e) {}
}
}
} // end of class