tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Christopher Williams" <ccwillia...@ntlworld.com>
Subject Re: HTML quoting
Date Fri, 03 Oct 2003 14:30:41 GMT
Here's a simple method to quote the most important character entities:

    /**
     * Handles a couple of problematic characters in strings that are
printed to
     * an HTML stream, replacing them with their escaped equivalents
     * @param s an input string
     * @return the escaped string
     */
    public static String escapeSpaces(String s)
    {
        StringBuffer sb = new StringBuffer();
        int nChars = s.length();
        for (int i = 0; i < nChars; i++)
        {
            char c = s.charAt(i);
            if (' ' == c)
            {
                sb.append("&#032;");
            }
            else if ('>' == c)
            {
                sb.append("&gt;");
            }
            else if ('<' == c)
            {
                sb.append("&lt;");
            }
            else if ('\"' == c)
            {
                sb.append("&quot;");
            }
            else if ('&' == c)
            {
                sb.append("&amp;");
            }
            else
            {
                sb.append(c);
            }
        }
        return sb.toString();
    }

A more complete solution would be to look up the complete list of character
entities (e.g 'HTML and XHTML The Definitive Guide'), build a lookup table
and use each character as an index into that table.

Chris Williams.



---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message