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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to