Here you can find the source of quoteXMLValue(final Object o)
Parameter | Description |
---|---|
o | The object |
public static String quoteXMLValue(final Object o)
//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("""); break; case '<': sb.append("<"); break; case '>': sb.append(">"); break; case '&': sb.append("&"); break; case ASCII_26: sb.append("_"); break; default: sb.append("&#"); sb.append(Integer.toString(ch)); sb.append(';'); } } }