* 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:
parent
6a7e0ec908
commit
825415f526
1 changed files with 59 additions and 33 deletions
|
@ -282,8 +282,13 @@ public final class HtmlEncoder {
|
|||
*
|
||||
*/
|
||||
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
|
||||
StringBuffer ret = new StringBuffer (Math.round (str.length()*1.4f));
|
||||
StringBuffer ret = new StringBuffer (Math.round (l*1.4f));
|
||||
encode (str, ret);
|
||||
return ret.toString();
|
||||
}
|
||||
|
@ -353,7 +358,7 @@ public final class HtmlEncoder {
|
|||
j++;
|
||||
// if we haven't gotten past the <,
|
||||
// check if it's an HTML comment or Helma macro tag
|
||||
if (j == tagStart) {
|
||||
if (j == tagStart && !insideCodeTag) {
|
||||
if ('%' == chars[j]) {
|
||||
insideMacroTag = insideTag = true;
|
||||
ret.append ('<');
|
||||
|
@ -433,7 +438,12 @@ public final class HtmlEncoder {
|
|||
*
|
||||
*/
|
||||
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);
|
||||
return ret.toString();
|
||||
}
|
||||
|
@ -443,7 +453,12 @@ public final class HtmlEncoder {
|
|||
*
|
||||
*/
|
||||
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);
|
||||
return ret.toString();
|
||||
}
|
||||
|
@ -503,7 +518,12 @@ public final class HtmlEncoder {
|
|||
|
||||
|
||||
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);
|
||||
return ret.toString();
|
||||
}
|
||||
|
@ -539,25 +559,31 @@ public final class HtmlEncoder {
|
|||
}
|
||||
|
||||
// test method
|
||||
public static String printCharRange (int from, int to) {
|
||||
StringBuffer response = new StringBuffer();
|
||||
for (int i=from;i<to;i++) {
|
||||
response.append (i);
|
||||
response.append (" ");
|
||||
response.append ((char) i);
|
||||
response.append (" ");
|
||||
if (i < 128)
|
||||
response.append ((char) i);
|
||||
else if (i >= 128 && i < 256)
|
||||
response.append (transform[i-128]);
|
||||
else {
|
||||
response.append ("&#");
|
||||
response.append (i);
|
||||
response.append (";");
|
||||
}
|
||||
response.append ("\r\n");
|
||||
}
|
||||
return response.toString();
|
||||
public static String printCharRange (int from, int to) {
|
||||
StringBuffer response = new StringBuffer();
|
||||
for (int i=from;i<to;i++) {
|
||||
response.append (i);
|
||||
response.append (" ");
|
||||
response.append ((char) i);
|
||||
response.append (" ");
|
||||
if (i < 128)
|
||||
response.append ((char) i);
|
||||
else if (i >= 128 && i < 256)
|
||||
response.append (transform[i-128]);
|
||||
else {
|
||||
response.append ("&#");
|
||||
response.append (i);
|
||||
response.append (";");
|
||||
}
|
||||
response.append ("\r\n");
|
||||
}
|
||||
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
|
||||
|
|
Loading…
Add table
Reference in a new issue