Class XmlUtil

java.lang.Object
com.pnfsoftware.jeb.util.encoding.xml.XmlUtil

public class XmlUtil extends Object
XML utility methods.
  • Field Details

    • PREDEFINED_ENTITIES

      public static final Map<String,String> PREDEFINED_ENTITIES
      The five well-known XML entities (lt, gt, amp, apos, quot) to their String representations (<, >, &, ', ").
  • Method Details

    • createSecureDocumentBuilderFactory

      public static DocumentBuilderFactory createSecureDocumentBuilderFactory(boolean disallowDoctypeDecl) throws ParserConfigurationException
      Create a safe DocumentBuilderFactory object.
      Parameters:
      disallowDoctypeDecl -
      Returns:
      the factory object
      Throws:
      ParserConfigurationException
    • isSpace

      public static boolean isSpace(int codePoint)
      Determine whether the provided code point is a white space character.
      Parameters:
      codePoint - Unicode code point
      Returns:
      true if the code point is XML whitespace or a Java space character
    • skipSomeSpaces

      public static int skipSomeSpaces(String str, int idx)
      Skip 1+ WSP characters.
      Parameters:
      str - input string
      idx - start index
      Returns:
      new index after skipped whitespace
    • skipSpaces

      public static int skipSpaces(String str, int idx)
      Skip 0+ WSP characters.
      Parameters:
      str - input string
      idx - start index
      Returns:
      new index after skipped whitespace
    • skipWSP

      public static int skipWSP(String str, int idx, int minSpaceCount)
      Skip WSP characters.
      Parameters:
      str - string
      idx - start index
      minSpaceCount - the method will raise if fewer WSP chars were skipped
      Returns:
      new index, after skipping WSP chars
    • readToken

      public static int readToken(String str, int idx, Collection<Integer> stoppers, String[] atoken)
      Read a token.
      Parameters:
      str - input string
      idx - token start (cannot be a wsp); may be a single-quote or double-quote
      stoppers - optional code points that terminate an unquoted token
      atoken - output token
      Returns:
      index immediately after the token
    • readToken

      public static int readToken(String str, int idx, Collection<Integer> stoppers, String[] atoken, boolean mustBeQuoted, boolean handleBackslashAxmlStyle)
      Read a token.
      Parameters:
      str - input string
      idx - token start (cannot be a wsp); may be a single-quote or double-quote
      stoppers - optional code points that terminate an unquoted token
      atoken - output token
      mustBeQuoted - true if the token must start with a quote
      handleBackslashAxmlStyle - true to decode Android resource backslash escapes
      Returns:
      index immediately after the token
    • compare

      public static boolean compare(String str, int idx, String expected)
      Compare a substring with an expected literal.
      Parameters:
      str - input string
      idx - substring start index
      expected - expected literal
      Returns:
      true if expected starts at idx
    • compareCI

      public static boolean compareCI(String str, int idx, String expected)
      Compare a substring with an expected literal, ignoring case.
      Parameters:
      str - input string
      idx - substring start index
      expected - expected literal
      Returns:
      true if expected starts at idx, ignoring case
    • getFirstChildElement

      public static Element getFirstChildElement(Element e, String childName)
      Find the first direct child element with the requested name.
      Parameters:
      e - parent element
      childName - child tag name
      Returns:
      first matching child element, or null
    • getChildrenElements

      public static List<Element> getChildrenElements(Element e, String childName)
      Find all direct child elements with the requested name.
      Parameters:
      e - parent element
      childName - child tag name
      Returns:
      list of matching child elements
    • format

      public static String format(Document doc)
      Format a document as text.
      Parameters:
      doc - input document
      Returns:
      textual representation of the document
    • isXmlChar

      public static boolean isXmlChar(int c)
    • isXmlNameStartChar

      public static boolean isXmlNameStartChar(int c)
    • isXmlNameChar

      public static boolean isXmlNameChar(int c)
    • isLegalXmlName

      public static boolean isLegalXmlName(String s)
    • isLegalXmlName

      public static boolean isLegalXmlName(String s, int beg, int end)
    • isLegalEscape

      public static boolean isLegalEscape(String s)
      Parameters:
      s -
      Returns:
    • isLegalEscape

      public static boolean isLegalEscape(String s, Map<String,String> allowedEntities)
      Parameters:
      s - the entity located between & and ;
      allowedEntities - null means accept any syntactically valid entity; else, a set of accepted entities beyond the 5 predefined entities
      Returns:
    • escapeAttributeValue

      public static String escapeAttributeValue(String s, boolean sanitizeBadChars)
      Escape an XLM attribute value.
      Parameters:
      s - the attribute value
      sanitizeBadChars - true to also sanitize bad chars (illegal XML characters will be replaced by the underscore char)
      Returns:
    • escapeTextData

      public static String escapeTextData(String s, boolean sanitizeBadChars)
      Escape XLM text data.
      Parameters:
      s - the text data
      sanitizeBadChars - true to also sanitize bad chars (illegal XML characters will be replaced by the underscore char)
      Returns:
    • unescape

      public static String unescape(String s)
      Unescape an escaped XML string. Illegal chars are replaced by underscore. Unknown entities are resolved to the empty string.
      Parameters:
      s - the escaped string
      Returns:
    • unescape

      public static String unescape(String s, Map<String,String> allowedEntities)
      Unescape an escaped XML string. Illegal chars are replaced by underscore.
      Parameters:
      s - the escaped string
      allowedEntities - additional map of entities allowed (on top of the 5 standard entities); if null is passed, all non-predefined entities resolve to the empty string
      Returns:
    • sanitizeName

      public static String sanitizeName(String s)
      Sanitize a supposedly legal XML name (e.g. tag or attribute name) into a legal name.
      Parameters:
      s -
      Returns:
    • sanitizeAttributeValue

      public static String sanitizeAttributeValue(String s)
      Sanitize a supposedly legal (escaped if necessary) XML attribute value.
      Parameters:
      s - an unsafe, supposedly escaped attribute value
      Returns:
      an attribute value string that should not crash XML parsers
    • sanitizeAttributeValue

      public static String sanitizeAttributeValue(String s, Map<String,String> allowedEntities)
      Sanitize a supposedly legal (escaped if necessary) XML attribute value.
      Parameters:
      s - an unsafe, supposedly escaped attribute value
      allowedEntities -
      Returns:
      an attribute value string that should not crash XML parsers
    • sanitizeTextData

      public static String sanitizeTextData(String s)
      Sanitize a supposedly legal (escaped if necessary) XML text data.
      Parameters:
      s - an unsafe, supposedly escaped text data
      Returns:
      a text data string that should not crash XML parsers
    • sanitizeTextData

      public static String sanitizeTextData(String s, Map<String,String> allowedEntities)
      Sanitize a supposedly legal (escaped if necessary) XML text data.
      Parameters:
      s - an unsafe, supposedly escaped text data
      allowedEntities -
      Returns:
      a text data string that should not crash XML parsers