Class XmlTransformer
-
Constructor Summary
ConstructorDescriptionXmlTransformer
(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.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. -
Method Summary
Modifier and TypeMethodDescriptionReturns new instance ofXmlFragmentMarshaller
.static Schema
loadXmlSchemas
(List<String> schemaFilenames) Creates a singleSchema
from multiple.xsd
files.void
marshal
(Object root, OutputStream out, Charset charset, ValidationMode validation) Validates and streamsroot
as formatted XML bytes with XML declaration.void
marshal
(Object root, Writer writer, ValidationMode validation) Streamsroot
without XML declaration, optionally validating against the schema.void
marshalStrict
(Object root, Result result) Validates and streamsroot
as characters, always using strict validation.static String
prettyPrint
(byte[] xmlBytes) Pretty print XML bytes.static String
prettyPrint
(String xmlString) Pretty print XML.<T> T
unmarshal
(Class<T> clazz, InputStream stream) Turns XML text into an object, validating against hard-coded xmlschema
s.void
Validates XML text againstschema
without marshalling.
-
Constructor Details
-
XmlTransformer
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
Validates XML text againstschema
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 matchexpect
.
-
unmarshal
Turns XML text into an object, validating against hard-coded xmlschema
s.- Parameters:
clazz
- the XML class you expect to receive as the root element- Throws:
XmlException
- if failed to read frombytes
, XML input is invalid, or root element doesn't matchexpect
.- See Also:
-
marshal
Streamsroot
without XML declaration, optionally validating against the schema.The root object must be annotated with
XmlRootElement
. If the validation parameter is set toValidationMode.STRICT
this method will verify that your object strictly conforms toschema
. Because the output is streamed,XmlException
will most likely be thrown after output has been written.- Parameters:
root
- the object to writewriter
- to write the output tovalidation
- whether to validate while marshaling- Throws:
XmlException
- to rethrowJAXBException
.
-
marshal
public void marshal(Object root, OutputStream out, Charset charset, ValidationMode validation) throws XmlException Validates and streamsroot
as formatted XML bytes with XML declaration.The root object must be annotated with
XmlRootElement
. If the validation parameter is set toValidationMode.STRICT
this method will verify that your object strictly conforms toschema
. Because the output is streamed,XmlException
will most likely be thrown after output has been written.- Parameters:
root
- the object to writeout
- 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 rethrowJAXBException
.- See Also:
-
marshalStrict
Validates and streamsroot
as characters, always using strict validation.The root object must be annotated with
XmlRootElement
. This method will verify that your object strictly conforms toschema
. Because the output is streamed,XmlException
will most likely be thrown after output has been written.- Parameters:
root
- the object to writeresult
- to write the output to- Throws:
XmlException
- to rethrowJAXBException
.
-
createFragmentMarshaller
Returns new instance ofXmlFragmentMarshaller
. -
loadXmlSchemas
Creates a singleSchema
from multiple.xsd
files. -
prettyPrint
Pretty print XML. -
prettyPrint
Pretty print XML bytes.
-