Class XMLUtil


  • public class XMLUtil
    extends java.lang.Object
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.text.DateFormat dateFormat  
    • Constructor Summary

      Constructors 
      Constructor Description
      XMLUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void closeElement​(java.io.PrintWriter out, java.lang.String element)
      Writes </element>
      static javax.xml.parsers.DocumentBuilderFactory createSafeDocumentBuilderFactory()
      The createSafeDocumentBuilderFactory() method returns a new DocumentBuilderFactory Instance that has been configured to prevent XXE attacks possible through weakly configured XML parsers processing external entities.
      static javax.xml.parsers.SAXParserFactory createSafeSAXParserFactory()
      The createSafeSAXParserFactory() method returns a new DocumentBuilderFactory Instance that has been configured to prevent XXE attacks possible through weakly configured XML parsers processing external entities.
      static javax.xml.transform.TransformerFactory createSafeTransformerFactory()  
      static org.xml.sax.XMLReader createSafeXMLReader()
      The createSafeXMLReader() method returns a new XMLReader Instance that has been configured to prevent XXE attacks possible through weakly configured XML parsers processing external entities.
      static org.xml.sax.XMLReader createSafeXMLReader​(java.lang.String className)
      The createSafeXMLReader() method returns a new XMLReader Instance that has been configured to prevent XXE attacks possible through weakly configured XML parsers processing external entities.
      static void main​(java.lang.String[] args)  
      static void openElement​(java.io.PrintWriter out, java.lang.String element)
      Writes <element>
      static void printXMLHead​(java.io.PrintWriter out)  
      static void printXMLHead​(java.io.Writer out)  
      static void printXMLHead​(java.lang.StringBuilder out)  
      static void printXMLHead​(java.util.Formatter out)  
      static java.lang.String quoteCharacters​(java.lang.String s)  
      static void setSafetyFeatures​(org.xml.sax.XMLReader reader)
      Sets safety features on the provided XMLReader instance
      static void writeElement​(java.io.PrintWriter out, java.lang.String element, java.lang.String contents)
      Writes <element>contents</element> , quoting the contents.
      • Methods inherited from class java.lang.Object

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

      • dateFormat

        public static final java.text.DateFormat dateFormat
    • Constructor Detail

      • XMLUtil

        public XMLUtil()
    • Method Detail

      • createSafeDocumentBuilderFactory

        public static javax.xml.parsers.DocumentBuilderFactory createSafeDocumentBuilderFactory()
        The createSafeDocumentBuilderFactory() method returns a new DocumentBuilderFactory Instance that has been configured to prevent XXE attacks possible through weakly configured XML parsers processing external entities.

        See: https://www.owasp.org/index.php/XML_External_Entity_%28XXE%29_Processing for more info

      • createSafeTransformerFactory

        public static javax.xml.transform.TransformerFactory createSafeTransformerFactory()
      • createSafeSAXParserFactory

        public static javax.xml.parsers.SAXParserFactory createSafeSAXParserFactory()
        The createSafeSAXParserFactory() method returns a new DocumentBuilderFactory Instance that has been configured to prevent XXE attacks possible through weakly configured XML parsers processing external entities.

        See: https://www.owasp.org/index.php/XML_External_Entity_%28XXE%29_Processing for more info

      • createSafeXMLReader

        public static org.xml.sax.XMLReader createSafeXMLReader()
                                                         throws org.xml.sax.SAXException
        The createSafeXMLReader() method returns a new XMLReader Instance that has been configured to prevent XXE attacks possible through weakly configured XML parsers processing external entities.

        See: https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html#xmlreader for more info

        Calls XMLReaderFactory.createXMLReader()

        Throws:
        org.xml.sax.SAXException
      • createSafeXMLReader

        public static org.xml.sax.XMLReader createSafeXMLReader​(java.lang.String className)
                                                         throws org.xml.sax.SAXException
        The createSafeXMLReader() method returns a new XMLReader Instance that has been configured to prevent XXE attacks possible through weakly configured XML parsers processing external entities.

        See: https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html#xmlreader for more info

        Calls XMLReaderFactory.createXMLReader(String) and provides the given className

        Throws:
        org.xml.sax.SAXException
      • setSafetyFeatures

        public static void setSafetyFeatures​(org.xml.sax.XMLReader reader)
        Sets safety features on the provided XMLReader instance

        See: https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html#xmlreader for more info

      • printXMLHead

        public static void printXMLHead​(java.io.Writer out)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • printXMLHead

        public static void printXMLHead​(java.io.PrintWriter out)
      • printXMLHead

        public static void printXMLHead​(java.util.Formatter out)
      • printXMLHead

        public static void printXMLHead​(java.lang.StringBuilder out)
      • openElement

        public static void openElement​(java.io.PrintWriter out,
                                       java.lang.String element)
        Writes <element>
      • closeElement

        public static void closeElement​(java.io.PrintWriter out,
                                        java.lang.String element)
        Writes </element>
      • writeElement

        public static void writeElement​(java.io.PrintWriter out,
                                        java.lang.String element,
                                        java.lang.String contents)
        Writes <element>contents</element> , quoting the contents. If contents is null, writes <element/>
      • main

        public static void main​(java.lang.String[] args)
      • quoteCharacters

        public static java.lang.String quoteCharacters​(java.lang.String s)