Class XMLUtil


  • public class XMLUtil
    extends java.lang.Object
    Contains utility methods for XML.
    Since:
    5.0.6
    • Constructor Summary

      Constructors 
      Constructor Description
      XMLUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String escapeXML​(java.lang.String s, boolean onlyASCII)
      Escapes a string with the appropriated XML codes.
      static int findInArray​(char needle, char[] haystack, int start)
      Looks for a character in a character array, starting from a certain position
      static java.lang.String getEncodingName​(byte[] b4)
      Returns the IANA encoding name that is auto-detected from the bytes specified, with the endian-ness of that encoding where appropriate.
      static boolean isValidCharacterValue​(int c)
      Checks if a character value should be escaped/unescaped.
      static boolean isValidCharacterValue​(java.lang.String s)
      Checks if a character value should be escaped/unescaped.
      static int unescape​(java.lang.String s)
      Unescapes 'lt', 'gt', 'apos', 'quote' and 'amp' to the corresponding character values.
      static java.lang.String unescapeXML​(java.lang.String s)
      Unescapes a String, replacing &#nn;, <, >, &, ", and &apos to the corresponding characters.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • XMLUtil

        public XMLUtil()
    • Method Detail

      • escapeXML

        public static java.lang.String escapeXML​(java.lang.String s,
                                                 boolean onlyASCII)
        Escapes a string with the appropriated XML codes.
        Parameters:
        s - the string to be escaped
        onlyASCII - codes above 127 will always be escaped with &#nn; if true
        Returns:
        the escaped string
        Since:
        5.0.6
      • unescapeXML

        public static java.lang.String unescapeXML​(java.lang.String s)
        Unescapes a String, replacing &#nn;, <, >, &, ", and &apos to the corresponding characters.
        Parameters:
        s - a String with entities
        Returns:
        the unescaped string
      • unescape

        public static int unescape​(java.lang.String s)
        Unescapes 'lt', 'gt', 'apos', 'quote' and 'amp' to the corresponding character values.
        Parameters:
        s - a string representing a character
        Returns:
        a character value
      • isValidCharacterValue

        public static boolean isValidCharacterValue​(java.lang.String s)
        Checks if a character value should be escaped/unescaped.
        Parameters:
        s - the String representation of an integer
        Returns:
        true if it's OK to escape or unescape this value
      • isValidCharacterValue

        public static boolean isValidCharacterValue​(int c)
        Checks if a character value should be escaped/unescaped.
        Parameters:
        c - a character value
        Returns:
        true if it's OK to escape or unescape this value
      • findInArray

        public static int findInArray​(char needle,
                                      char[] haystack,
                                      int start)
        Looks for a character in a character array, starting from a certain position
        Parameters:
        needle - the character you're looking for
        haystack - the character array
        start - the start position
        Returns:
        the position where the character was found, or -1 if it wasn't found.
      • getEncodingName

        public static java.lang.String getEncodingName​(byte[] b4)
        Returns the IANA encoding name that is auto-detected from the bytes specified, with the endian-ness of that encoding where appropriate. (method found in org.apache.xerces.impl.XMLEntityManager, originally published by the Apache Software Foundation under the Apache Software License; now being used in iText under the MPL)
        Parameters:
        b4 - The first four bytes of the input.
        Returns:
        an IANA-encoding string
        Since:
        5.0.6