Class XMLUtils


  • public class XMLUtils
    extends java.lang.Object
    Utility class with static methods to handle and serialize/deserialize XML documents.
    Since:
    4.0.0
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String createSchema​(java.lang.Class<?> clazz)
      Create XML Schema (XSD) for JAXB class and return as XML string.
      static void createSchema​(java.lang.Class<?> clazz, java.io.PrintStream printStream)
      Create XML Schema (XSD) for JAXB class and save into given print stream.
      static java.util.List<org.w3c.dom.Element> getChildElements​(org.w3c.dom.Node node, java.lang.String nodeName)
      Searches for all element children from a node having a specific node name.
      static org.w3c.dom.Element getFirstChildElement​(org.w3c.dom.Node node)
      Searches for the first element child from a node - if existing.
      static org.w3c.dom.Document getXMLDocument​(java.io.InputStream input)
      Parses a string into an XML document.
      static org.w3c.dom.Document getXMLDocument​(java.lang.String input)
      Parses a string into an XML document.
      static org.w3c.dom.Document getXMLDocument​(org.xml.sax.InputSource input)
      Parses an input stream into an XML document.
      static <T> T objectFromXML​(byte[] bytes, java.lang.Class<T> T, java.lang.Class<?>... types)
      Imports a data file (i.e.
      static <T> T objectFromXML​(java.io.Reader reader, java.lang.Class<T> T, java.lang.Class<?>... types)
      Imports a data file (i.e.
      static <T> T objectFromXML​(java.lang.String xmlstring, java.lang.Class<T> T, java.lang.Class<?>... types)
      Imports a data file (i.e.
      static <T> T objectFromXML​(javax.xml.transform.Source source, java.lang.Class<T> T, java.lang.Class<?>... types)
      Imports a data file (i.e.
      static <T> T objectFromXMLUnchecked​(byte[] bytes, java.lang.Class<T> T, java.lang.Class<?>... types)
      Imports a data file (i.e.
      static <T> T objectFromXMLUnchecked​(java.io.Reader reader, java.lang.Class<T> T, java.lang.Class<?>... types)
      Imports a data file (i.e.
      static <T> T objectFromXMLUnchecked​(java.lang.String xmlstring, java.lang.Class<T> T, java.lang.Class<?>... types)
      Imports a data file (i.e.
      static java.lang.String objectToUnformattedXML​(java.io.Serializable serializable, java.lang.Class<?>... types)
      Serialize an object into an unformatted XML string representation
      static java.io.StringWriter objectToUnformattedXMLWriter​(java.io.Serializable serializable, java.lang.Class<?>... types)
      Serialize an object as unformatted XML into an string writer
      static java.lang.String objectToXML​(java.io.Serializable serializable, java.lang.Class<?>... types)
      Serialize an object into an XML string representation
      static java.io.StringWriter objectToXMLWriter​(java.io.Serializable serializable, java.lang.Class<?>... types)
      Serialize an object as XML into an string writer
      static void validateXML​(byte[] bytes)
      Validates a XML (i.e.
      static void validateXML​(java.io.InputStream input)
      Validates an XML (i.e.
      static boolean validateXML​(java.io.InputStream xmlStream, java.io.InputStream... schemaStreams)
      Validate an XML input stream
      static void xslTransform​(java.io.File xmlFile, java.io.File xslFile, java.io.File outFile, java.util.Map<java.lang.String,​java.lang.Object> args)
      Transform an XML Document given as a file reference into an output file using an XSL Document given as a file reference.
      static void xslTransform​(java.io.InputStream xmlStream, java.io.InputStream xslStream, java.util.Map<java.lang.String,​java.lang.Object> args, java.io.Writer outputWriter)
      Transform an XML Document given as stream using an XSL Document given as another stream.
      • Methods inherited from class java.lang.Object

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

      • getXMLDocument

        public static org.w3c.dom.Document getXMLDocument​(java.lang.String input)
        Parses a string into an XML document. Returns the document or null, if anything fails.
        Parameters:
        input - XML String
        Returns:
        new Document or NULL
        Since:
        4.0.0
        See Also:
        getXMLDocument(InputSource)
      • getXMLDocument

        public static org.w3c.dom.Document getXMLDocument​(org.xml.sax.InputSource input)
        Parses an input stream into an XML document. Returns the document or null, if anything fails.
        Parameters:
        input - XML String
        Returns:
        new Document or NULL
        Since:
        4.0.0
      • getXMLDocument

        public static org.w3c.dom.Document getXMLDocument​(java.io.InputStream input)
        Parses a string into an XML document. Returns the document or null, if anything fails.
        Parameters:
        input - XML String
        Returns:
        new Document or NULL
        Since:
        4.0.0
        See Also:
        getXMLDocument(InputSource)
      • validateXML

        public static boolean validateXML​(java.io.InputStream xmlStream,
                                          java.io.InputStream... schemaStreams)
        Validate an XML input stream
        Parameters:
        xmlStream - the XML stream
        schemaStreams - e.g. stream of "/path/to/schema1.xsd"
        Returns:
        true if input is a valid instance of XML schema
        Since:
        4.0.0
      • objectFromXML

        public static <T> T objectFromXML​(java.io.Reader reader,
                                          java.lang.Class<T> T,
                                          java.lang.Class<?>... types)
                                   throws javax.xml.bind.JAXBException

        Imports a data file (i.e. as a character reader) into an object.

        If exceptions during unmarshalling or type casting occur, the method returns null.

        This method might be deprecated in future versions of the SDK.
        If exceptions should be handled on client side (which we recommend), use objectFromXMLUnchecked(Reader, Class, Class...) instead.

        Parameters:
        reader - reader
        T - type to be returned
        types - list of additional types for unmarshalling
        Returns:
        new object of type T or null
        Throws:
        javax.xml.bind.JAXBException - (actually never thrown)
        Since:
        4.0.0
      • objectFromXMLUnchecked

        public static <T> T objectFromXMLUnchecked​(java.io.Reader reader,
                                                   java.lang.Class<T> T,
                                                   java.lang.Class<?>... types)
                                            throws javax.xml.bind.JAXBException

        Imports a data file (i.e. as a character reader) into an object.

        Any exception during unmarshalling or type casting is raised and must be handled on client side.

        Parameters:
        reader -
        T - type to be returned
        types - list of additional types for unmarshalling
        Returns:
        new object of type T
        Throws:
        javax.xml.bind.JAXBException
        Since:
        4.0.0
      • objectFromXML

        public static <T> T objectFromXML​(java.lang.String xmlstring,
                                          java.lang.Class<T> T,
                                          java.lang.Class<?>... types)
                                   throws javax.xml.bind.JAXBException

        Imports a data file (i.e. as a xml string) into an object.

        If exceptions during unmarshalling or type casting occur, the method returns null.

        This method might be deprecated in future versions of the SDK.
        If exceptions should be handled on client side (which we recommend), use objectFromXMLUnchecked(String, Class, Class...) instead.

        Parameters:
        xmlstring - data file (XML String)
        T - type to be returned
        types - list of additional types for unmarshalling
        Returns:
        new object of type T or null
        Throws:
        javax.xml.bind.JAXBException
        Since:
        4.0.0
      • objectFromXMLUnchecked

        public static <T> T objectFromXMLUnchecked​(java.lang.String xmlstring,
                                                   java.lang.Class<T> T,
                                                   java.lang.Class<?>... types)
                                            throws javax.xml.bind.JAXBException

        Imports a data file (i.e. as a xml string) into an object.

        Any exception during unmarshalling or type casting is raised and must be handled on client side.

        Parameters:
        xmlstring - xml String to be imported
        T - type to be returned
        types - list of additional types for unmarshalling
        Returns:
        new object of type T or null
        Throws:
        javax.xml.bind.JAXBException
        Since:
        4.0.0
      • objectFromXML

        public static <T> T objectFromXML​(byte[] bytes,
                                          java.lang.Class<T> T,
                                          java.lang.Class<?>... types)
                                   throws javax.xml.bind.JAXBException

        Imports a data file (i.e. as byte array) into an object.

        Expects data to use default encoding of JVM. You may have to set "-dfile.encoding" of your JVM accordingly. PubServer expects all data in UTF-8.

        If exceptions during unmarshalling or type casting occur, the method returns null.

        This method might be deprecated in future versions of the SDK.
        If exceptions should be handled on client side (which we recommend), use objectFromXMLUnchecked(byte[], Class, Class...) instead.

        Parameters:
        bytes - data file (byte array)
        T - type to be returned
        types - list of additional types for unmarshalling
        Returns:
        new object of type T or null
        Throws:
        javax.xml.bind.JAXBException
        Since:
        4.0.0
      • objectFromXMLUnchecked

        public static <T> T objectFromXMLUnchecked​(byte[] bytes,
                                                   java.lang.Class<T> T,
                                                   java.lang.Class<?>... types)
                                            throws javax.xml.bind.JAXBException

        Imports a data file (i.e. as byte array) into an object.

        Expects data to use default encoding of JVM. You may have to set "-dfile.encoding" of your JVM accordingly. PubServer expects all data in UTF-8.

        Any exception during unmarshalling or type casting is raised and must be handled on client side.

        Parameters:
        bytes -
        T - type to be returned
        types - list of additional types for unmarshalling
        Returns:
        new object of type T or null
        Throws:
        javax.xml.bind.JAXBException
        Since:
        4.0.0
      • objectFromXML

        public static <T> T objectFromXML​(javax.xml.transform.Source source,
                                          java.lang.Class<T> T,
                                          java.lang.Class<?>... types)
                                   throws javax.xml.bind.JAXBException

        Imports a data file (i.e. as javax.xml.transform.Source) into an object.

        Expects data to use default encoding of JVM. You may have to set "-dfile.encoding" of your JVM accordingly. PubServer expects all data in UTF-8.

        Any exception during unmarshalling or type casting is raised and must be handled on client side.

        Parameters:
        source - the xml source
        T - type to be returned
        types - list of additional types for unmarshalling
        Returns:
        new object of type T or null
        Throws:
        javax.xml.bind.JAXBException
        Since:
        4.1.6
      • objectToXML

        public static java.lang.String objectToXML​(java.io.Serializable serializable,
                                                   java.lang.Class<?>... types)
        Serialize an object into an XML string representation
        Parameters:
        serializable - object
        types - list of additional types for unmarshalling
        Returns:
        XML string
        Since:
        4.0.0
        See Also:
        objectToXMLWriter(Serializable, Class...)
      • objectToUnformattedXML

        public static java.lang.String objectToUnformattedXML​(java.io.Serializable serializable,
                                                              java.lang.Class<?>... types)
        Serialize an object into an unformatted XML string representation
        Parameters:
        serializable - object
        types - list of additional types for unmarshalling
        Returns:
        XML string
      • objectToXMLWriter

        public static java.io.StringWriter objectToXMLWriter​(java.io.Serializable serializable,
                                                             java.lang.Class<?>... types)
        Serialize an object as XML into an string writer
        Parameters:
        serializable - object
        types - list of additional types for unmarshalling
        Returns:
        XML string
        Since:
        4.0.0
      • objectToUnformattedXMLWriter

        public static java.io.StringWriter objectToUnformattedXMLWriter​(java.io.Serializable serializable,
                                                                        java.lang.Class<?>... types)
        Serialize an object as unformatted XML into an string writer
        Parameters:
        serializable - object
        types - list of additional types for unmarshalling
        Returns:
        XML string
      • getFirstChildElement

        public static org.w3c.dom.Element getFirstChildElement​(org.w3c.dom.Node node)
        Searches for the first element child from a node - if existing.
        Parameters:
        node - parent node
        Returns:
        element child found or null.
        Throws:
        NullArgumentException - if node is null.
        Since:
        4.0.0
      • getChildElements

        public static java.util.List<org.w3c.dom.Element> getChildElements​(org.w3c.dom.Node node,
                                                                           java.lang.String nodeName)
        Searches for all element children from a node having a specific node name.

        To retrieve all elements please use nodeName="*".

        No support for namespaces. Only local name is compared.

        Parameters:
        node - parent node
        nodeName - name of the children nodes to be searched for
        Returns:
        list of matching elements (never null).
        Throws:
        NullArgumentException - if node is null.
        Since:
        4.1.0
      • xslTransform

        public static void xslTransform​(java.io.File xmlFile,
                                        java.io.File xslFile,
                                        java.io.File outFile,
                                        java.util.Map<java.lang.String,​java.lang.Object> args)
                                 throws javax.xml.transform.TransformerFactoryConfigurationError,
                                        javax.xml.transform.TransformerException,
                                        java.io.IOException
        Transform an XML Document given as a file reference into an output file using an XSL Document given as a file reference. You pipe a hashtable of string arguments into the transformation.
        Parameters:
        xmlFile -
        xslFile -
        outFile -
        args -
        Throws:
        javax.xml.transform.TransformerFactoryConfigurationError
        javax.xml.transform.TransformerException
        java.io.IOException
        Since:
        4.0.0
        See Also:
        xslTransform(InputStream, InputStream, Map, Writer)
      • xslTransform

        public static void xslTransform​(java.io.InputStream xmlStream,
                                        java.io.InputStream xslStream,
                                        java.util.Map<java.lang.String,​java.lang.Object> args,
                                        java.io.Writer outputWriter)
                                 throws javax.xml.transform.TransformerFactoryConfigurationError,
                                        javax.xml.transform.TransformerException
        Transform an XML Document given as stream using an XSL Document given as another stream. You pipe a hash table of string arguments into the transformation. Writes the result to a Writer
        Parameters:
        xmlStream -
        xslStream -
        args -
        outputWriter -
        Throws:
        javax.xml.transform.TransformerFactoryConfigurationError
        javax.xml.transform.TransformerException
        Since:
        4.0.0
      • validateXML

        public static void validateXML​(byte[] bytes)
                                throws PubServerException

        Validates a XML (i.e. as a array of bytes) using the JAXB XML parser, optionally matching against JAXB classes

        Parameters:
        bytes - byte array to parse
        Throws:
        PubServerException
      • validateXML

        public static void validateXML​(java.io.InputStream input)
                                throws PubServerException

        Validates an XML (i.e. as a Reader) using the JAXB XML parser, optionally matching against JAXB classes

        Parameters:
        input - reader to parse from
        Throws:
        PubServerException
      • createSchema

        public static java.lang.String createSchema​(java.lang.Class<?> clazz)
                                             throws PluginException
        Create XML Schema (XSD) for JAXB class and return as XML string.
        Parameters:
        clazz - annotated with @XmlRootElement
        Returns:
        Schema as XML string
        Throws:
        PluginException - on any error
      • createSchema

        public static void createSchema​(java.lang.Class<?> clazz,
                                        java.io.PrintStream printStream)
                                 throws PluginException
        Create XML Schema (XSD) for JAXB class and save into given print stream.
        Parameters:
        clazz - annotated with @XmlRootElement
        printStream - target stream to write string data to
        Throws:
        PluginException - on any error