Class XMLUtils

java.lang.Object
com.priint.pubserver.util.XMLUtils

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

    • getXMLDocument

      public static Document getXMLDocument(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

      public static Document getXMLDocument(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 Document getXMLDocument(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:
    • validateXML

      public static boolean validateXML(InputStream xmlStream, 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(Reader reader, Class<T> T, Class<?>... types) throws jakarta.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:
      jakarta.xml.bind.JAXBException - (actually never thrown)
      Since:
      4.0.0
    • objectFromXMLUnchecked

      public static <T> T objectFromXMLUnchecked(Reader reader, Class<T> T, Class<?>... types) throws jakarta.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:
      jakarta.xml.bind.JAXBException
      Since:
      4.0.0
    • objectFromXML

      public static <T> T objectFromXML(String xmlstring, Class<T> T, Class<?>... types) throws jakarta.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:
      jakarta.xml.bind.JAXBException
      Since:
      4.0.0
    • objectFromXMLUnchecked

      public static <T> T objectFromXMLUnchecked(String xmlstring, Class<T> T, Class<?>... types) throws jakarta.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:
      jakarta.xml.bind.JAXBException
      Since:
      4.0.0
    • objectFromXML

      public static <T> T objectFromXML(byte[] bytes, Class<T> T, Class<?>... types) throws jakarta.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:
      jakarta.xml.bind.JAXBException
      Since:
      4.0.0
    • objectFromXMLUnchecked

      public static <T> T objectFromXMLUnchecked(byte[] bytes, Class<T> T, Class<?>... types) throws jakarta.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:
      jakarta.xml.bind.JAXBException
      Since:
      4.0.0
    • objectFromXML

      public static <T> T objectFromXML(Source source, Class<T> T, Class<?>... types) throws jakarta.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:
      jakarta.xml.bind.JAXBException
      Since:
      4.1.6
    • objectToXML

      public static String objectToXML(Serializable serializable, 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:
    • objectToUnformattedXML

      public static String objectToUnformattedXML(Serializable serializable, 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 StringWriter objectToXMLWriter(Serializable serializable, 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 StringWriter objectToUnformattedXMLWriter(Serializable serializable, 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 Element getFirstChildElement(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 List<Element> getChildElements(Node node, 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(File xmlFile, File xslFile, File outFile, Map<String,Object> args) throws TransformerFactoryConfigurationError, TransformerException, 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:
      TransformerFactoryConfigurationError
      TransformerException
      IOException
      Since:
      4.0.0
      See Also:
    • xslTransform

      public static void xslTransform(InputStream xmlStream, InputStream xslStream, Map<String,Object> args, Writer outputWriter) throws TransformerFactoryConfigurationError, 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:
      TransformerFactoryConfigurationError
      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(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 String createSchema(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(Class<?> clazz, 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