/** * Escape <code>input</code> for XML. * * @param input the input to escape. * @return the XML escaped variant of <code>input</code>. */ public static CharSequence escapeForXml(CharSequence input) { return escapeForXml(input, XmlEscapeMode.safe); }
/** * Escape <code>input</code> for XML. * * @param input the input to escape. * @return the XML escaped variant of <code>input</code>. * @since 4.2 */ public static CharSequence escapeForXmlAttribute(CharSequence input) { return escapeForXml(input, XmlEscapeMode.forAttribute); }
/** * Escape <code>input</code> for XML. * * @param input the input to escape. * @return the XML escaped variant of <code>input</code>. * @since 4.2 */ public static CharSequence escapeForXmlText(CharSequence input) { return escapeForXml(input, XmlEscapeMode.forText); }
/** * Escape <code>input</code> for XML. * <p> * This is an optimized variant of {@link #escapeForXmlAttribute(CharSequence)} for XML where the * XML attribute is quoted using ''' (Apos). * </p> * * @param input the input to escape. * @return the XML escaped variant of <code>input</code>. * @since 4.2 */ public static CharSequence escapeForXmlAttributeApos(CharSequence input) { return escapeForXml(input, XmlEscapeMode.forAttributeApos); }
private void updateFN() { StringBuilder sb = new StringBuilder(); if (firstName != null) { sb.append(StringUtils.escapeForXml(firstName)).append(' '); } if (middleName != null) { sb.append(StringUtils.escapeForXml(middleName)).append(' '); } if (lastName != null) { sb.append(StringUtils.escapeForXml(lastName)); } setField("FN", sb.toString()); }
public XmlStringBuilder escape(String text) { assert text != null; sb.append(StringUtils.escapeForXml(text)); return this; }
private static void appendXmlAttributes(XmlPullParser parser, StringBuilder builder) { // NOTE: for now we ignore namespaces int count = parser.getAttributeCount(); if (count > 0) { for (int i = 0; i < count; i++) { builder.append(' '); builder.append(parser.getAttributeName(i)); builder.append("=\""); builder.append(StringUtils.escapeForXml(parser.getAttributeValue(i))); builder.append('"'); } } }
@Test public void testEscapeForXml() { String input = null; assertNull(StringUtils.escapeForXml(null)); input = "<b>"; assertCharSequenceEquals("<b>", StringUtils.escapeForXml(input)); input = "\""; assertCharSequenceEquals(""", StringUtils.escapeForXml(input)); input = "&"; assertCharSequenceEquals("&", StringUtils.escapeForXml(input)); input = "<b>\n\t\r</b>"; assertCharSequenceEquals("<b>\n\t\r</b>", StringUtils.escapeForXml(input)); input = " & "; assertCharSequenceEquals(" & ", StringUtils.escapeForXml(input)); input = " \" "; assertCharSequenceEquals(" " ", StringUtils.escapeForXml(input)); input = "> of me <"; assertCharSequenceEquals("> of me <", StringUtils.escapeForXml(input)); input = "> of me & you<"; assertCharSequenceEquals("> of me & you<", StringUtils.escapeForXml(input)); input = "& <"; assertCharSequenceEquals("& <", StringUtils.escapeForXml(input)); input = "&"; assertCharSequenceEquals("&", StringUtils.escapeForXml(input)); input = "It's a good day today"; assertCharSequenceEquals("It's a good day today", StringUtils.escapeForXml(input)); }
/** * Escape <code>input</code> for XML. * * @param input the input to escape. * @return the XML escaped variant of <code>input</code>. * @since 4.2 */ public static CharSequence escapeForXmlAttribute(CharSequence input) { return escapeForXml(input, XmlEscapeMode.forAttribute); }
/** * Escape <code>input</code> for XML. * * @param input the input to escape. * @return the XML escaped variant of <code>input</code>. */ public static CharSequence escapeForXml(CharSequence input) { return escapeForXml(input, XmlEscapeMode.safe); }
/** * Escape <code>input</code> for XML. * * @param input the input to escape. * @return the XML escaped variant of <code>input</code>. * @since 4.2 */ public static CharSequence escapeForXmlText(CharSequence input) { return escapeForXml(input, XmlEscapeMode.forText); }
/** * Escape <code>input</code> for XML. * <p> * This is an optimized variant of {@link #escapeForXmlAttribute(CharSequence)} for XML where the * XML attribute is quoted using ''' (Apos). * </p> * * @param input the input to escape. * @return the XML escaped variant of <code>input</code>. * @since 4.2 */ public static CharSequence escapeForXmlAttributeApos(CharSequence input) { return escapeForXml(input, XmlEscapeMode.forAttributeApos); }
public XmlStringBuilder escape(String text) { assert text != null; sb.append(StringUtils.escapeForXml(text)); return this; }
private static void appendXmlAttributes(XmlPullParser parser, StringBuilder builder) { // NOTE: for now we ignore namespaces int count = parser.getAttributeCount(); if (count > 0) { for (int i = 0; i < count; i++) { builder.append(' '); builder.append(parser.getAttributeName(i)); builder.append("=\""); builder.append(StringUtils.escapeForXml(parser.getAttributeValue(i))); builder.append('"'); } } }