Java XML Quote quoteXML(String string)

Here you can find the source of quoteXML(String string)

Description

Escapes specified string (that is, '<' is replaced by "&#60;", '&' is replaced by "&#38;", etc) then quotes the escaped string.

License

Open Source License

Parameter

Parameter Description
string string to be escaped and quoted

Return

escaped and quoted string

Declaration

public static final String quoteXML(String string) 

Method Source Code

//package com.java2s;
/*/* w w w  . j a v a2s .c o m*/
 *    Qizx/open 4.1
 *
 * This code is the open-source version of Qizx.
 * Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
 *
 * The contents of this file are subject to the Mozilla Public License 
 *  Version 1.1 (the "License"); you may not use this file except in 
 *  compliance with the License. You may obtain a copy of the License at
 *  http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 *  for the specific language governing rights and limitations under the
 *  License.
 *
 * The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
 *
 */

public class Main {
    /**
     * Escapes specified string (that is, <tt>'&lt;'</tt> is replaced by "<tt>&amp;#60</tt>;",
     * <tt>'&amp;'</tt> is replaced by "<tt>&amp;#38;</tt>", etc) then
     * quotes the escaped string.
     * 
     * @param string string to be escaped and quoted
     * @return escaped and quoted string
     */
    public static final String quoteXML(String string) {
        StringBuffer quoted = new StringBuffer();
        quoted.append('\"');
        escapeXML(string, quoted);
        quoted.append('\"');
        return quoted.toString();
    }

    /**
     * Escapes specified string (that is, <tt>'&lt;'</tt> is replaced by "<tt>&amp;#60</tt>;",
     * <tt>'&amp;'</tt> is replaced by "<tt>&amp;#38;</tt>", etc).
     * 
     * @param string string to be escaped
     * @return escaped string
     */
    public static final String escapeXML(String string) {
        StringBuffer escaped = new StringBuffer();
        escapeXML(string, escaped);
        return escaped.toString();
    }

    /**
     * Escapes specified string (that is, <tt>'&lt;'</tt> is replaced by "<tt>&amp;#60</tt>;",
     * <tt>'&amp;'</tt> is replaced by "<tt>&amp;#38;</tt>", etc).
     * 
     * @param string string to be escaped
     * @param escaped buffer used to store escaped string (characters are
     *        appended to this buffer)
     */
    public static final void escapeXML(String string, StringBuffer escaped) {
        char[] chars = string.toCharArray();
        escapeXML(chars, 0, chars.length, escaped);
    }

    /**
     * Escapes specified character array (that is, <tt>'&lt;'</tt> is
     * replaced by "<tt>&amp;#60</tt>;", <tt>'&amp;'</tt> is replaced by "<tt>&amp;#38;</tt>",
     * etc).
     * 
     * @param chars character array to be escaped
     * @param offset specifies first character in array to be escaped
     * @param length number of characters in array to be escaped
     * @param escaped buffer used to store escaped string (characters are
     *        appended to this buffer)
     */
    public static final void escapeXML(char[] chars, int offset, int length, StringBuffer escaped) {
        escapeXML(chars, offset, length, escaped, false);
    }

    /**
     * Escapes specified character array (that is, <tt>'&lt;'</tt> is
     * replaced by "<tt>&amp;#60</tt>;", <tt>'&amp;'</tt> is replaced by "<tt>&amp;#38;</tt>",
     * etc).
     * 
     * @param chars character array to be escaped
     * @param offset specifies first character in array to be escaped
     * @param length number of characters in array to be escaped
     * @param escaped buffer used to store escaped string (characters are
     *        appended to this buffer)
     * @param ascii if true, characters with code &gt; 127 are escaped as
     *        <tt>&amp;#<i>code</i>;</tt>
     */
    public static final void escapeXML(char[] chars, int offset, int length, StringBuffer escaped, boolean ascii) {
        int end = offset + length;
        for (int i = offset; i < end; ++i) {
            char c = chars[i];
            switch (c) {
            case '\'':
                escaped.append("&#39;");
                break;
            case '\"':
                escaped.append("&#34;");
                break;
            case '<':
                escaped.append("&#60;");
                break;
            case '>':
                escaped.append("&#62;");
                break;
            case '&':
                escaped.append("&#38;");
                break;
            case 0x00A0:
                // &nbsp;
                escaped.append("&#x00A0;");
                break;
            default:
                if (ascii && c > 127) {
                    escaped.append("&#");
                    escaped.append(Integer.toString((int) c));
                    escaped.append(';');
                } else {
                    escaped.append(c);
                }
            }
        }
    }
}

Related

  1. quoteXML(String string)
  2. quoteXML(String xmlFragment)
  3. quoteXmlContent(String x)
  4. quoteXMLValue(final Object o)
  5. quoteXMLValue(Object o)