Class XmlTransformer

java.lang.Object
google.registry.xml.XmlTransformer

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

    • XmlTransformer

      public XmlTransformer(List<String> schemaFilenames, 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(Package pakkage, com.google.common.collect.ImmutableMap<String,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 Details

    • validate

      public void validate(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(Class<T> clazz, 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:
    • marshal

      public void marshal(Object root, 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(Object root, OutputStream out, 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:
    • marshalStrict

      public void marshalStrict(Object root, 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.
    • createFragmentMarshaller

      public XmlFragmentMarshaller createFragmentMarshaller()
      Returns new instance of XmlFragmentMarshaller.
    • loadXmlSchemas

      public static Schema loadXmlSchemas(List<String> schemaFilenames)
      Creates a single Schema from multiple .xsd files.
    • prettyPrint

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

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