Converts the raw characters to XML escape characters. - Java XML

Java examples for XML:XML String Escape

Description

Converts the raw characters to XML escape characters.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String rawContent = "java2s.com";
        String charset = "java2s.com";
        boolean isNoLines = true;
        System.out.println(escapeXML(rawContent, charset, isNoLines));
    }// w ww.  ja  v  a 2 s .  c  om

    /**
     * Converts the raw characters to XML escape characters.
     * 
     * @param rawContent
     * @param charset Null when charset is not known, so we assume it's unicode
     * @param isNoLines
     * @return escape string
     */
    public static String escapeXML(String rawContent, String charset,
            boolean isNoLines) {
        if (rawContent == null)
            return ""; //$NON-NLS-1$
        else {
            StringBuffer sb = new StringBuffer();

            for (int i = 0; i < rawContent.length(); i++) {
                char ch = rawContent.charAt(i);
                if (ch == '\'')
                    sb.append("&#39;"); //$NON-NLS-1$
                else if (ch == '&')
                    sb.append("&amp;"); //$NON-NLS-1$
                else if (ch == '"')
                    sb.append("&quot;"); //$NON-NLS-1$
                else if (ch == '<')
                    sb.append("&lt;"); //$NON-NLS-1$
                else if (ch == '>')
                    sb.append("&gt;"); //$NON-NLS-1$
                else if (ch > '~' && charset != null
                        && charSetImpliesAscii(charset))
                    // TODO - why is hashcode the only way to get the unicode number for the character
                    // in jre 5.0?
                    sb.append("&#x" + Integer.toHexString(new Character(ch).hashCode()).toUpperCase() + ";"); //$NON-NLS-1$ //$NON-NLS-2$
                else if (isNoLines) {
                    if (ch == '\r')
                        sb.append("&#xA;"); //$NON-NLS-1$
                    else if (ch != '\n')
                        sb.append(ch);
                } else
                    sb.append(ch);
            }
            return sb.toString();
        }
    }

    public static boolean charSetImpliesAscii(String charset) {
        return charset.equals("ISO-8859-1") || charset.equals("US-ASCII"); //$NON-NLS-1$ //$NON-NLS-2$
    }
}

Related Tutorials