Class XMLUtils

Object
XMLUtils

public class XMLUtils extends Object
XML related utilities not otherwise found in base libraries
Author:
Andrea Aime - GeoSolutions
  • Constructor Details

    • XMLUtils

      public XMLUtils()
  • Method Details

    • newXMLInputFactory

      public static XMLInputFactory newXMLInputFactory()
      Creates an XMLInputFactory configured with GeoTools library defaults.
      Returns:
      XMLInputFactory
    • newXMLInputFactory

      public static XMLInputFactory newXMLInputFactory(Hints hints)
      Creates an XMLInputFactory configured with GeoTools library defaults.

      The XMLInputFactory provided will be configured to be more forgiving when NullEntityResolver is used as this indicates the library is configured to work with local files.

      When working with EntityResolvers allowing `http` access, such as `DefaultEntityResolver` more restrictions are enforced.

      Parameters:
      hints - Factory hints
      Returns:
      XMLInputFactory
    • newSAXParserFactory

      public static SAXParserFactory newSAXParserFactory()
      Create a new SAXParserFactory that respects library configuration.
      Returns:
      SAX Parser Factory
    • newSAXParserFactory

      public static SAXParserFactory newSAXParserFactory(Hints hints)
      Create a new SAXParserFactory that respects library configuration.
      Parameters:
      hints - Factory hints
      Returns:
      SAX Parser Factory
    • newSAXParserFactory

      public static SAXParserFactory newSAXParserFactory(SAXParserFactory factory, Hints hints)
      Create a new SAXParser that respects library configuration.
      Parameters:
      hints - Factory hints
      Returns:
      SAX Parser Factory
    • toSAXParserFactory

      public static XMLUtils.GTSAXParserFactory toSAXParserFactory(SAXParserFactory factory, Hints hints)
      Cast / wraps to GTSAXParserFactory respecting GeoTools configuration.
      Parameters:
      factory - SAXParserFactory factory, or null
      hints - Factory configuration
      Returns:
      GTSAXParserFactory respecting GeoTools configuration.
    • newSAXParser

      public static SAXParser newSAXParser() throws ParserConfigurationException, SAXException
      Creates a new instance of a SAXParser using GeoTools configuration.
      Returns:
      SAXParser setup with library configuration
      Throws:
      ParserConfigurationException
      SAXException
    • newSAXParser

      public static SAXParser newSAXParser(SAXParserFactory factory) throws ParserConfigurationException, SAXException
      Creates a new instance of a SAXParser provided factory, applying GeoTools configuration.
      Parameters:
      factory - SAXParserFactory factory, or null
      Returns:
      SAXParser
      Throws:
      ParserConfigurationException
      SAXException
    • newSAXParser

      public static SAXParser newSAXParser(SAXParserFactory factory, Hints hints) throws ParserConfigurationException, SAXException
      Creates a new instance of a SAXParser provided factory, applying GeoTools configuration.
      Parameters:
      factory - SAXParserFactory factory, or null
      hints - Factory configuration
      Returns:
      SAXParser
      Throws:
      ParserConfigurationException
      SAXException
    • newTransformerFactory

      public static TransformerFactory newTransformerFactory()
      Create a new TransformerFactory that respects library configuration.
      Returns:
      Transformer Factory
    • newTransformerFactory

      public static TransformerFactory newTransformerFactory(Hints hints)
      Create a new TransformerFactory that respects library configuration.
      Parameters:
      hints - Factory hints
      Returns:
      Transformer Factory
    • newSaxTransformerFactory

      public static SAXTransformerFactory newSaxTransformerFactory()
    • newSaxTransformerFactory

      public static SAXTransformerFactory newSaxTransformerFactory(Hints hints)
    • newTransformer

      public static Transformer newTransformer() throws TransformerConfigurationException
      Creates a new Transformer as an alternative to direct use of TransformerFactory.newTransformer().
      Throws:
      TransformerConfigurationException
    • newTransformer

      public static Transformer newTransformer(Hints hints) throws TransformerConfigurationException
      Creates a new Transformer as an alternative to direct use of TransformerFactory.newTransformer().
      Throws:
      TransformerConfigurationException
    • newTransformer

      public static Transformer newTransformer(TransformerFactory factory) throws TransformerConfigurationException
      Creates a new Transformer as an alternative to direct use of TransformerFactory.newTransformer().
      Parameters:
      factory -
      Throws:
      TransformerConfigurationException
    • newTransformer

      public static Transformer newTransformer(TransformerFactory factory, Hints hints) throws TransformerConfigurationException
      Creates a new Transformer as an alternative to direct use of TransformerFactory.newTransformer().
      Parameters:
      factory - Factory pre-configured with information such as indenting
      hints - Factory configuration
      Returns:
      XML transformer
      Throws:
      TransformerConfigurationException
    • toTransformerFactory

      protected static TransformerFactory toTransformerFactory(TransformerFactory factory, Hints hints)
      Cast /wraps to GTTransformerFactory or GTSAXTransformFactory respecting GeoTools configuration.
      Parameters:
      factory - Transformer factory, or null
      hints - Factory configuration
      Returns:
      GTTransformerFactory or GTSAXTransformFactory respecting GeoTools configuration.
    • newTransformer

      public static Transformer newTransformer(Source source) throws TransformerConfigurationException
      Creates a new Transformer as an alternative to direct use of TransformerFactory.newTransformer().
      Parameters:
      source - Source input
      Returns:
      XML transformer
      Throws:
      TransformerConfigurationException
    • newTransformer

      public static Transformer newTransformer(Source source, Hints hints) throws TransformerConfigurationException
      Creates a new Transformer as an alternative to direct use of TransformerFactory.newTransformer().
      Parameters:
      source - Source input
      hints - Factory configuration
      Returns:
      XML transformer
      Throws:
      TransformerConfigurationException
    • newTransformer

      public static Transformer newTransformer(TransformerFactory factory, Source source) throws TransformerConfigurationException
      Creates a new Transformer as an alternative to direct use of TransformerFactory.newTransformer().
      Parameters:
      source - Source input
      factory - Factory pre-configured with information such as indenting
      Returns:
      XML transformer
      Throws:
      TransformerConfigurationException
    • newTransformer

      public static Transformer newTransformer(TransformerFactory factory, Source source, Hints hints) throws TransformerConfigurationException
      Creates a new Transformer as an alternative to direct use of TransformerFactory.newTransformer().
      Parameters:
      source - Source input
      factory - Factory pre-configured with information such as indenting
      hints - Factory configuration
      Returns:
      XML transformer
      Throws:
      TransformerConfigurationException
    • source

      public static Source source(Node dom)
      Alternative to direct use of DOMSOurce, allowing GeoTools library configuration to be applied when traversal is required.
      Parameters:
      dom - Document node
      Returns:
      Source with any XML streaming subject to GeoTools configuration including GeoTools.getEntityResolver(Hints).
    • source

      public static Source source(Source source)
      Alternative to direct use of Source, allowing GeoTools library configuration to be applied when traversal is required.
      Parameters:
      source -
      Returns:
      Source with any XML streaming subject to GeoTools configuration including GeoTools.getEntityResolver(Hints).
    • source

      public static Source source(Source source, Hints hints)
      Alternative to direct use of Source, allowing GeoTools library configuration to be applied when traversal is required.
      Parameters:
      source - transform source
      hints - GeoTools library configuration
      Returns:
      Source with any XML streaming subject to GeoTools configuration including GeoTools.getEntityResolver(Hints).
    • sax

      public static SAXSource sax(InputSource inputSource, Hints hints)
      Alternative to direct use of InputSource allowing SAXSource setup with GeoTools configuration to be applied.
      Parameters:
      inputSource - InputSource
      hints - GeoTools library configuration
      Returns:
      Source with any XML streaming subject to GeoTools configuration including GeoTools.getEntityResolver(Hints).
    • newDocumentBuilder

      public static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException
      Creates a new document builder, as an alternative to DocumentBuilder.newDocument(), allowing GeoTools library configuration to be applied.

      The document builder is configured using GeoTools configuration including GeoTools.getEntityResolver(Hints) .

      Returns:
      DocumentBuilder configured with Ge
      Throws:
      ParserConfigurationException
    • newDocumentBuilder

      public static DocumentBuilder newDocumentBuilder(Hints hints) throws ParserConfigurationException
      Creates a new document builder, as an alternative to direct use of DocumentBuilder.newDocument(), allowing GeoTools library configuration to be applied.

      The document builder is configured using GeoTools configuration including GeoTools.getEntityResolver(Hints) .

      Returns:
      DocumentBuilder configured using GeoTools Hints
      Throws:
      ParserConfigurationException
    • newDocumentBuilder

      public static DocumentBuilder newDocumentBuilder(DocumentBuilderFactory factory) throws ParserConfigurationException
      Create a new DocumentBuilder using a supplied Factory, allowing GeoTools library configuration to be applied.

      This apporach allows document build factory to be configured to be namespace aware, validating, etc. while still applying GeoTools configuration including GeoTools.getEntityResolver(Hints).

       DocumentBuilderFactory factory = XMLUtils.newDocumentBuilderFactory();
       factory.setValidating(true);
       factory.setNamespaceAware(true);
       DocumentBuilder builder = XMLUtils.newDocumentBuilder(factory);
       Document document = builder.parse(source);
      Parameters:
      factory - Configured DocumentBuildFactory
      Throws:
      ParserConfigurationException
    • newDocumentBuilder

      public static DocumentBuilder newDocumentBuilder(DocumentBuilderFactory factory, Hints hints) throws ParserConfigurationException
      Create a new DocumentBuilder using a supplied Factory, allowing GeoTools library configuration to be applied.

      This approach allows document build factory to be configured to be namespace aware, validating, etc. while still applying GeoTools configuration including GeoTools.getEntityResolver(Hints).

       Hints hints = GeoTools.getDefaultHints();
       hints.put(Hints.ENTITY_RESOLVER, NullEntityResolver.INSTANCE);
      
       DocumentBuilderFactory factory = XMLUtils.newDocumentBuilderFactory(hints);
       factory.setValidating(true);
       factory.setNamespaceAware(true);
       DocumentBuilder builder = XMLUtils.newDocumentBuilder(factory, hints);
       Document document = builder.parse(source);
      The above example shows use of this method with NullEntityResolver.
      Parameters:
      factory - Configured DocumentBuildFactory
      hints - Factory hints
      Throws:
      ParserConfigurationException
    • newDocumentBuilderFactory

      public static DocumentBuilderFactory newDocumentBuilderFactory()
      Create a new DocumentBuilderFactory allowing GeoTools library configuration to be applied.
    • newDocumentBuilderFactory

      public static DocumentBuilderFactory newDocumentBuilderFactory(Hints hints)
      Create a new DocumentBuilder using a supplied Factory, allowing GeoTools library configuration to be applied.
      Parameters:
      hints - Factory hints
    • newSchemaFactory

      public static SchemaFactory newSchemaFactory(String schemaLanguage)
      Creates a new SchemaFactory allowing GeoTools configuration to be applied.
      Parameters:
      schemaLanguage - Schema language which the factory will understand
      Returns:
      New instance of schema factory supporting schemaLanguage
    • newSchemaFactory

      public static SchemaFactory newSchemaFactory(String schemaLanguage, Hints hints)
      Creates a new SchemaFactory allowing GeoTools configuration to be applied.
      Parameters:
      schemaLanguage - Schema language which the factory will understand
      hints - Factory hints
      Returns:
      New instance of schema factory supporting schemaLanguage
    • checkSupportForJAXP15Properties

      public static void checkSupportForJAXP15Properties()
      Tests whether the TransformerFactory and SchemaFactory implementations support JAXP 1.5 properties to protect against XML external entity injection (XXE) attacks. The internal JDK XML processors starting with JDK 7u40 would support these properties but outdated versions of XML libraries (e.g., Xalan, Xerces) that do not support these properties may be included in GeoServer's classpath or provided by the web application server.

      GeoTools uses these properties internally.

      Throws:
      IllegalStateException - if the JAXP 1.5 properties are not supported or if there was an error checking for JAXP 1.5 support
    • removeXMLInvalidChars

      public static String removeXMLInvalidChars(String in)
      Checks the string for XML invalid chars, and in case any is found, create a copy with the invalid ones removed.
    • qName

      public static QName qName(String name, NamespaceSupport namespaces)
      Creates a qualified name from a string by parsing out the colon as the prefix / local separator.
      Parameters:
      name - The possibly qualified name.
      namespaces - The namespace prefix uri mappings.