* Check for null and empty string in all encoder methods.

* Encode macro tags and HTML comments inside <code> tag.
* Added simple main() method for command line tests.
This commit is contained in:
hns 2002-10-14 16:39:18 +00:00
parent 6a7e0ec908
commit 825415f526

View file

@ -282,8 +282,13 @@ public final class HtmlEncoder {
* *
*/ */
public final static String encode (String str) { public final static String encode (String str) {
if (str == null)
return null;
int l = str.length();
if (l == 0)
return "";
// try to make stringbuffer large enough from the start // try to make stringbuffer large enough from the start
StringBuffer ret = new StringBuffer (Math.round (str.length()*1.4f)); StringBuffer ret = new StringBuffer (Math.round (l*1.4f));
encode (str, ret); encode (str, ret);
return ret.toString(); return ret.toString();
} }
@ -353,7 +358,7 @@ public final class HtmlEncoder {
j++; j++;
// if we haven't gotten past the <, // if we haven't gotten past the <,
// check if it's an HTML comment or Helma macro tag // check if it's an HTML comment or Helma macro tag
if (j == tagStart) { if (j == tagStart && !insideCodeTag) {
if ('%' == chars[j]) { if ('%' == chars[j]) {
insideMacroTag = insideTag = true; insideMacroTag = insideTag = true;
ret.append ('<'); ret.append ('<');
@ -433,7 +438,12 @@ public final class HtmlEncoder {
* *
*/ */
public final static String encodeFormValue (String str) { public final static String encodeFormValue (String str) {
StringBuffer ret = new StringBuffer (Math.round (str.length()*1.2f)); if (str == null)
return null;
int l = str.length();
if (l == 0)
return "";
StringBuffer ret = new StringBuffer (Math.round (l*1.2f));
encodeAll (str, ret, false); encodeAll (str, ret, false);
return ret.toString(); return ret.toString();
} }
@ -443,7 +453,12 @@ public final class HtmlEncoder {
* *
*/ */
public final static String encodeAll (String str) { public final static String encodeAll (String str) {
StringBuffer ret = new StringBuffer (Math.round (str.length()*1.2f)); if (str == null)
return null;
int l = str.length();
if (l == 0)
return "";
StringBuffer ret = new StringBuffer (Math.round (l*1.2f));
encodeAll (str, ret, true); encodeAll (str, ret, true);
return ret.toString(); return ret.toString();
} }
@ -503,7 +518,12 @@ public final class HtmlEncoder {
public final static String encodeXml (String str) { public final static String encodeXml (String str) {
StringBuffer ret = new StringBuffer (Math.round (str.length()*1.2f)); if (str == null)
return null;
int l = str.length();
if (l == 0)
return "";
StringBuffer ret = new StringBuffer (Math.round (l*1.2f));
encodeXml (str, ret); encodeXml (str, ret);
return ret.toString(); return ret.toString();
} }
@ -560,4 +580,10 @@ public final class HtmlEncoder {
return response.toString(); return response.toString();
} }
// for testing...
public static void main (String[] args) {
for (int i=0; i<args.length; i++)
System.err.println (encode (args[i]));
}
} // end of class } // end of class