Class XmlTransformer


  • public class XmlTransformer
    extends java.lang.Object
    Static methods for marshaling, unmarshaling, and validating XML.
    • Constructor Summary

      Constructors 
      Constructor Description
      XmlTransformer​(java.lang.Package pakkage, com.google.common.collect.ImmutableMap<java.lang.String,​java.lang.String> schemaNamesToFilenames)
      Create a new XmlTransformer that validates using the given schemas and marshals to and from classes generated off of those schemas.
      XmlTransformer​(java.util.List<java.lang.String> schemaFilenames, java.lang.Class<?>... recognizedClasses)
      Create a new XmlTransformer that validates using the given schemas, but uses the given classes (rather than generated ones) for marshaling and unmarshaling.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      XmlFragmentMarshaller createFragmentMarshaller()
      Returns new instance of XmlFragmentMarshaller.
      static javax.xml.validation.Schema loadXmlSchemas​(java.util.List<java.lang.String> schemaFilenames)
      Creates a single Schema from multiple .xsd files.
      void marshal​(java.lang.Object root, java.io.OutputStream out, java.nio.charset.Charset charset, ValidationMode validation)
      Validates and streams root as formatted XML bytes with XML declaration.
      void marshal​(java.lang.Object root, java.io.Writer writer, ValidationMode validation)
      Streams root without XML declaration, optionally validating against the schema.
      void marshalStrict​(java.lang.Object root, javax.xml.transform.Result result)
      Validates and streams root as characters, always using strict validation.
      static java.lang.String prettyPrint​(byte[] xmlBytes)
      Pretty print XML bytes.
      static java.lang.String prettyPrint​(java.lang.String xmlString)
      Pretty print XML.
      <T> T unmarshal​(java.lang.Class<T> clazz, java.io.InputStream stream)
      Turns XML text into an object, validating against hard-coded xml schemas.
      void validate​(java.lang.String xml)
      Validates XML text against schema without marshalling.
      • Methods inherited from class java.lang.Object

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

      • XmlTransformer

        public XmlTransformer​(java.util.List<java.lang.String> schemaFilenames,
                              java.lang.Class<?>... recognizedClasses)
        Create a new XmlTransformer that validates using the given schemas, but uses the given classes (rather than generated ones) for marshaling and unmarshaling.
        Parameters:
        schemaFilenames - schema files, used only for validating, and relative to this package.
        recognizedClasses - the classes that can be used to marshal to and from
      • XmlTransformer

        public XmlTransformer​(java.lang.Package pakkage,
                              com.google.common.collect.ImmutableMap<java.lang.String,​java.lang.String> schemaNamesToFilenames)
        Create a new XmlTransformer that validates using the given schemas and marshals to and from classes generated off of those schemas.
        Parameters:
        schemaNamesToFilenames - map of schema names to filenames, immutable because ordering is significant and ImmutableMap preserves insertion order. The filenames are relative to this package.
    • Method Detail

      • validate

        public void validate​(java.lang.String xml)
                      throws XmlException
        Validates XML text against schema without marshalling.

        You must specify the XML class you expect to receive as the root element. Validation is performed in accordance with the hard-coded XML schemas.

        Throws:
        XmlException - if XML input was invalid or root element doesn't match expect.
      • unmarshal

        public <T> T unmarshal​(java.lang.Class<T> clazz,
                               java.io.InputStream stream)
                        throws XmlException
        Turns XML text into an object, validating against hard-coded xml schemas.
        Parameters:
        clazz - the XML class you expect to receive as the root element
        Throws:
        XmlException - if failed to read from bytes, XML input is invalid, or root element doesn't match expect.
        See Also:
        Files.asByteSource(java.io.File), Resources.asByteSource(java.net.URL), TypeParameterUnusedInFormals
      • marshal

        public void marshal​(java.lang.Object root,
                            java.io.Writer writer,
                            ValidationMode validation)
                     throws XmlException
        Streams root without XML declaration, optionally validating against the schema.

        The root object must be annotated with XmlRootElement. If the validation parameter is set to ValidationMode.STRICT this method will verify that your object strictly conforms to schema. Because the output is streamed, XmlException will most likely be thrown after output has been written.

        Parameters:
        root - the object to write
        writer - to write the output to
        validation - whether to validate while marshaling
        Throws:
        XmlException - to rethrow JAXBException.
      • marshal

        public void marshal​(java.lang.Object root,
                            java.io.OutputStream out,
                            java.nio.charset.Charset charset,
                            ValidationMode validation)
                     throws XmlException
        Validates and streams root as formatted XML bytes with XML declaration.

        The root object must be annotated with XmlRootElement. If the validation parameter is set to ValidationMode.STRICT this method will verify that your object strictly conforms to schema. Because the output is streamed, XmlException will most likely be thrown after output has been written.

        Parameters:
        root - the object to write
        out - byte-oriented output for writing XML. This method won't close it.
        charset - should almost always be set to "utf-8".
        validation - whether to validate while marshaling
        Throws:
        XmlException - to rethrow JAXBException.
        See Also:
        unmarshal(java.lang.Class<T>, java.io.InputStream)
      • marshalStrict

        public void marshalStrict​(java.lang.Object root,
                                  javax.xml.transform.Result result)
                           throws XmlException
        Validates and streams root as characters, always using strict validation.

        The root object must be annotated with XmlRootElement. This method will verify that your object strictly conforms to schema. Because the output is streamed, XmlException will most likely be thrown after output has been written.

        Parameters:
        root - the object to write
        result - to write the output to
        Throws:
        XmlException - to rethrow JAXBException.
      • loadXmlSchemas

        public static javax.xml.validation.Schema loadXmlSchemas​(java.util.List<java.lang.String> schemaFilenames)
        Creates a single Schema from multiple .xsd files.
      • prettyPrint

        public static java.lang.String prettyPrint​(java.lang.String xmlString)
        Pretty print XML.
      • prettyPrint

        public static java.lang.String prettyPrint​(byte[] xmlBytes)
        Pretty print XML bytes.