Class XmlUtil


  • public class XmlUtil
    extends java.lang.Object
    Utilities for working with JAXP and SAX.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  XmlUtil.JstlEntityResolver
      JSTL-specific implementation of EntityResolver, used by parsers.
      static class  XmlUtil.JstlUriResolver
      JSTL-specific implementation of URIResolver, used by transformers.
    • Constructor Summary

      Constructors 
      Constructor Description
      XmlUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static void checkProtocol​(java.lang.String allowedProtocols, java.lang.String uri)  
      private static java.lang.String initAllowedProtocols()  
      static javax.xml.parsers.DocumentBuilder newDocumentBuilder()
      Create a new DocumentBuilder configured for namespaces but not validating.
      static org.w3c.dom.Document newEmptyDocument()
      Create a new empty document.
      static org.xml.sax.InputSource newInputSource​(java.io.Reader reader, java.lang.String systemId)
      Create an InputSource from a Reader.
      static javax.xml.transform.sax.SAXSource newSAXSource​(java.io.Reader reader, java.lang.String systemId, XmlUtil.JstlEntityResolver entityResolver)
      Create a SAXSource from a Reader.
      static javax.xml.transform.Transformer newTransformer​(javax.xml.transform.Source source)
      Create a new Transformer from an XSLT.
      static javax.xml.transform.sax.TransformerHandler newTransformerHandler()
      Create a new TransformerHandler.
      static org.xml.sax.XMLReader newXMLReader​(XmlUtil.JstlEntityResolver entityResolver)
      Create an XMLReader that resolves entities using JSTL semantics.
      private static <T,​E extends java.lang.Exception>
      T
      runWithOurClassLoader​(java.util.concurrent.Callable<T> action, java.lang.Class<E> allowed)
      Performs an action using this Class's ClassLoader as the Thread context ClassLoader.
      private static java.lang.String wrapSystemId​(java.lang.String systemId)
      Wraps systemId with a "jstl:" prefix to prevent the parser from thinking that the URI is truly relative and resolving it against the current directory in the filesystem.
      • Methods inherited from class java.lang.Object

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

      • PARSER_FACTORY

        private static final javax.xml.parsers.DocumentBuilderFactory PARSER_FACTORY
      • TRANSFORMER_FACTORY

        private static final javax.xml.transform.sax.SAXTransformerFactory TRANSFORMER_FACTORY
      • SAXPARSER_FACTORY

        private static final javax.xml.parsers.SAXParserFactory SAXPARSER_FACTORY
      • SP_ALLOWED_PROTOCOLS

        private static final java.lang.String SP_ALLOWED_PROTOCOLS
        See Also:
        Constant Field Values
      • ALLOWED_PROTOCOLS

        private static final java.lang.String ALLOWED_PROTOCOLS
    • Constructor Detail

      • XmlUtil

        public XmlUtil()
    • Method Detail

      • initAllowedProtocols

        private static java.lang.String initAllowedProtocols()
      • checkProtocol

        static void checkProtocol​(java.lang.String allowedProtocols,
                                  java.lang.String uri)
      • newEmptyDocument

        public static org.w3c.dom.Document newEmptyDocument()
        Create a new empty document.
        Returns:
        a new empty document
      • newDocumentBuilder

        public static javax.xml.parsers.DocumentBuilder newDocumentBuilder()
        Create a new DocumentBuilder configured for namespaces but not validating.
        Returns:
        a new, configured DocumentBuilder
      • newTransformerHandler

        public static javax.xml.transform.sax.TransformerHandler newTransformerHandler()
                                                                                throws javax.xml.transform.TransformerConfigurationException
        Create a new TransformerHandler.
        Returns:
        a new TransformerHandler
        Throws:
        javax.xml.transform.TransformerConfigurationException
      • newTransformer

        public static javax.xml.transform.Transformer newTransformer​(javax.xml.transform.Source source)
                                                              throws javax.xml.transform.TransformerConfigurationException
        Create a new Transformer from an XSLT.
        Parameters:
        source - the source of the XSLT.
        Returns:
        a new Transformer
        Throws:
        javax.xml.transform.TransformerConfigurationException - if there was a problem creating the Transformer from the XSLT
      • newInputSource

        public static org.xml.sax.InputSource newInputSource​(java.io.Reader reader,
                                                             java.lang.String systemId)
        Create an InputSource from a Reader. The systemId will be wrapped for use with JSTL's EntityResolver and UriResolver.
        Parameters:
        reader - the source of the XML
        systemId - the system id
        Returns:
        a configured InputSource
      • newXMLReader

        public static org.xml.sax.XMLReader newXMLReader​(XmlUtil.JstlEntityResolver entityResolver)
                                                  throws javax.xml.parsers.ParserConfigurationException,
                                                         org.xml.sax.SAXException
        Create an XMLReader that resolves entities using JSTL semantics.
        Parameters:
        entityResolver - for resolving using JSTL semantics
        Returns:
        a new XMLReader
        Throws:
        javax.xml.parsers.ParserConfigurationException - if there was a configuration problem creating the reader
        org.xml.sax.SAXException - if there was a problem creating the reader
      • newSAXSource

        public static javax.xml.transform.sax.SAXSource newSAXSource​(java.io.Reader reader,
                                                                     java.lang.String systemId,
                                                                     XmlUtil.JstlEntityResolver entityResolver)
                                                              throws javax.xml.parsers.ParserConfigurationException,
                                                                     org.xml.sax.SAXException
        Create a SAXSource from a Reader. Any entities will be resolved using JSTL semantics.
        Parameters:
        reader - the source of the XML
        systemId - the system id
        entityResolver - for resolving using JSTL semamtics
        Returns:
        a new SAXSource
        Throws:
        javax.xml.parsers.ParserConfigurationException - if there was a configuration problem creating the source
        org.xml.sax.SAXException - if there was a problem creating the source
      • wrapSystemId

        private static java.lang.String wrapSystemId​(java.lang.String systemId)
        Wraps systemId with a "jstl:" prefix to prevent the parser from thinking that the URI is truly relative and resolving it against the current directory in the filesystem.
      • runWithOurClassLoader

        private static <T,​E extends java.lang.Exception> T runWithOurClassLoader​(java.util.concurrent.Callable<T> action,
                                                                                       java.lang.Class<E> allowed)
                                                                                throws E extends java.lang.Exception
        Performs an action using this Class's ClassLoader as the Thread context ClassLoader.
        Type Parameters:
        T - the type of the result
        E - the type of the allowed Exception
        Parameters:
        action - the action to perform
        allowed - an Exception that might be thrown by the action
        Returns:
        the result of the action
        Throws:
        E - if the action threw the allowed Exception
        E extends java.lang.Exception