Class XMLUtils
- java.lang.Object
-
- com.priint.pubserver.util.XMLUtils
-
public class XMLUtils extends java.lang.ObjectUtility 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.StringcreateSchema(java.lang.Class<?> clazz)Create XML Schema (XSD) for JAXB class and return as XML string.static voidcreateSchema(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.ElementgetFirstChildElement(org.w3c.dom.Node node)Searches for the first element child from a node - if existing.static org.w3c.dom.DocumentgetXMLDocument(java.io.InputStream input)Parses a string into an XML document.static org.w3c.dom.DocumentgetXMLDocument(java.lang.String input)Parses a string into an XML document.static org.w3c.dom.DocumentgetXMLDocument(org.xml.sax.InputSource input)Parses an input stream into an XML document.static <T> TobjectFromXML(byte[] bytes, java.lang.Class<T> T, java.lang.Class<?>... types)Imports a data file (i.e.static <T> TobjectFromXML(java.io.Reader reader, java.lang.Class<T> T, java.lang.Class<?>... types)Imports a data file (i.e.static <T> TobjectFromXML(java.lang.String xmlstring, java.lang.Class<T> T, java.lang.Class<?>... types)Imports a data file (i.e.static <T> TobjectFromXML(javax.xml.transform.Source source, java.lang.Class<T> T, java.lang.Class<?>... types)Imports a data file (i.e.static <T> TobjectFromXMLUnchecked(byte[] bytes, java.lang.Class<T> T, java.lang.Class<?>... types)Imports a data file (i.e.static <T> TobjectFromXMLUnchecked(java.io.Reader reader, java.lang.Class<T> T, java.lang.Class<?>... types)Imports a data file (i.e.static <T> TobjectFromXMLUnchecked(java.lang.String xmlstring, java.lang.Class<T> T, java.lang.Class<?>... types)Imports a data file (i.e.static java.lang.StringobjectToUnformattedXML(java.io.Serializable serializable, java.lang.Class<?>... types)Serialize an object into an unformatted XML string representationstatic java.io.StringWriterobjectToUnformattedXMLWriter(java.io.Serializable serializable, java.lang.Class<?>... types)Serialize an object as unformatted XML into an string writerstatic java.lang.StringobjectToXML(java.io.Serializable serializable, java.lang.Class<?>... types)Serialize an object into an XML string representationstatic java.io.StringWriterobjectToXMLWriter(java.io.Serializable serializable, java.lang.Class<?>... types)Serialize an object as XML into an string writerstatic voidvalidateXML(byte[] bytes)Validates a XML (i.e.static voidvalidateXML(java.io.InputStream input)Validates an XML (i.e.static booleanvalidateXML(java.io.InputStream xmlStream, java.io.InputStream... schemaStreams)Validate an XML input streamstatic voidxslTransform(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 voidxslTransform(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.
-
-
-
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 streamschemaStreams- 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.JAXBExceptionImports 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), useobjectFromXMLUnchecked(Reader, Class, Class...)instead.
- Parameters:
reader- readerT- type to be returnedtypes- 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.JAXBExceptionImports 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 returnedtypes- 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.JAXBExceptionImports 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), useobjectFromXMLUnchecked(String, Class, Class...)instead.
- Parameters:
xmlstring- data file (XML String)T- type to be returnedtypes- 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.JAXBExceptionImports 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 importedT- type to be returnedtypes- 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.JAXBExceptionImports 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), useobjectFromXMLUnchecked(byte[], Class, Class...)instead.
- Parameters:
bytes- data file (byte array)T- type to be returnedtypes- 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.JAXBExceptionImports 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 returnedtypes- 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.JAXBExceptionImports 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 sourceT- type to be returnedtypes- 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- objecttypes- 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- objecttypes- 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- objecttypes- 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- objecttypes- 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 nodenodeName- 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.IOExceptionTransform 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.TransformerFactoryConfigurationErrorjavax.xml.transform.TransformerExceptionjava.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.TransformerExceptionTransform 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 aWriter- Parameters:
xmlStream-xslStream-args-outputWriter-- Throws:
javax.xml.transform.TransformerFactoryConfigurationErrorjavax.xml.transform.TransformerException- Since:
- 4.0.0
-
validateXML
public static void validateXML(byte[] bytes) throws PubServerExceptionValidates 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 PubServerExceptionValidates 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 PluginExceptionCreate 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 PluginExceptionCreate XML Schema (XSD) for JAXB class and save into given print stream.- Parameters:
clazz- annotated with @XmlRootElementprintStream- target stream to write string data to- Throws:
PluginException- on any error
-
-