Java XML Quote quoteXMLValue(final Object o)

Here you can find the source of quoteXMLValue(final Object o)

Description

Convert the object into XML safe string.

License

LGPL

Parameter

Parameter Description
o The object

Return

The string

Declaration

public static String quoteXMLValue(final Object o) 

Method Source Code

//package com.java2s;
/**//www  . jav a2 s .  c  om
 * CC-LGPL 2.1
 * http://creativecommons.org/licenses/LGPL/2.1/
 */

public class Main {
    /**
     * ASCII (char) 26 constant.
     */
    private static final int ASCII_26 = 26;
    /**
     * ASCII 'pure' printable limit.
     */
    private static final int ASCII_LIMIT = 192;

    /**
     * Convert the object into XML safe string.
     *
     * @param o The object
     * @return The string
     */
    public static String quoteXMLValue(final Object o) {
        if (o == null) {
            return null;
        }

        StringBuilder input = new StringBuilder(o.toString());
        StringBuilder output = new StringBuilder(input.length());
        int seqStart = 0;
        int seqEnd = 0;

        for (int count = 0; count < input.length(); count++) {
            char ch = input.charAt(count);
            if (isTransformableEntity(ch)) {
                if (seqEnd > seqStart) {
                    output.append(input.substring(seqStart, seqEnd));
                }

                transformEntity(output, ch);
                seqStart = count + 1;
            } else {
                seqEnd = count + 1;
            }
        }
        if (seqEnd > seqStart) {
            output.append(input.substring(seqStart, seqEnd));
        }

        return output.toString();
    }

    /**
     * Returns true if the char is transformable.
     *
     * @param ch The char
     * @return True, if the char is transformable
     */
    protected static boolean isTransformableEntity(final char ch) {
        if ('"' == ch) {
            return true;
        } else if ('<' == ch) {
            return true;
        } else if ('>' == ch) {
            return true;
        } else if ('&' == ch) {
            return true;
        } else if (ASCII_26 == ch) {
            return true;
        } else if (((int) ch) > ASCII_LIMIT) {
            return true;
        }

        return false;
    }

    /**
     * Transforms the char and add the transformed entity to the StringBuilder
     * instance.
     *
     * @param sb The StringBuilder instance
     * @param ch The char
     */
    protected static void transformEntity(final StringBuilder sb, final char ch) {
        switch (ch) {
        case '"':
            sb.append("&quot;");
            break;
        case '<':
            sb.append("&lt;");
            break;
        case '>':
            sb.append("&gt;");
            break;
        case '&':
            sb.append("&amp;");
            break;
        case ASCII_26:
            sb.append("_");
            break;
        default:
            sb.append("&#");
            sb.append(Integer.toString(ch));
            sb.append(';');
        }
    }
}

Related

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