Somewhere in my net-travels I picked up this version which performs pretty well:

/**
 * Returns an HTML rendition of the given <code>String</code>.  This was
 * written by <a href=mailto:[EMAIL PROTECTED]>Kimbo Mundy</a>.
 * @param  text  A <code>String</code> to be used in an HTML page.
 * @return  A <code>String</code> that quotes any HTML markup
 *           characters.  If no quoting is needed, this will be
 *           the same as <code>text</code>.
 */
public static String asHTML(String text) {
    if (text == null)
      return "";
    StringBuffer results = null;
    char[] orig = null;
    int beg = 0, len = text.length();
    for (int i = 0;  i < len;  ++i){
      char c = text.charAt(i);
      switch (c){
      case 0:
      case '&':
      case '<':
      case '>':
      case '"':
        if (results == null){
          orig = text.toCharArray();
          results = new StringBuffer(len+10);
        }
        if (i > beg)
          results.append(orig, beg, i-beg);
        beg = i + 1;
        switch (c){
        default:                // case 0:
          continue;
        case '&':
          results.append("&amp;");
          break;
        case '<':
          results.append("&lt;");
          break;
        case '>':
          results.append("&gt;");
          break;
        case '"':
          results.append("&quot;");
          break;
        }
        break;
      }
    }
    if (results == null)
      return text;
    results.append(orig, beg, len-beg);
    return results.toString();
}

--
Eric Lunt
mailto:[EMAIL PROTECTED] Ph: 312-642-9050 Fax: 312-642-9051
Burning Door, LLC (http://www.burningdoor.com)
"Helping our clients build effective web applications"

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JSP-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to