Package com.helger.jaxb
Class GenericJAXBMarshaller<JAXBTYPE>
java.lang.Object
com.helger.jaxb.GenericJAXBMarshaller<JAXBTYPE>
- Type Parameters:
JAXBTYPE- The JAXB type to be marshaled
- All Implemented Interfaces:
com.helger.commons.lang.IHasClassLoader,IJAXBReader<JAXBTYPE>,IJAXBValidator<JAXBTYPE>,IJAXBWriter<JAXBTYPE>
@NotThreadSafe
public class GenericJAXBMarshaller<JAXBTYPE>
extends Object
implements com.helger.commons.lang.IHasClassLoader, IJAXBReader<JAXBTYPE>, IJAXBWriter<JAXBTYPE>, IJAXBValidator<JAXBTYPE>
This is the generic reader and writer base class for JAXB enabled document
types.
- Author:
- Philip Helger
-
Field Summary
Fields inherited from interface com.helger.jaxb.IJAXBWriter
USE_JAXB_CHARSET_FIX -
Constructor Summary
ConstructorsConstructorDescriptionGenericJAXBMarshaller(Class<JAXBTYPE> aType, Function<? super JAXBTYPE, ? extends jakarta.xml.bind.JAXBElement<JAXBTYPE>> aWrapper) Constructor without XSD paths.GenericJAXBMarshaller(Class<JAXBTYPE> aType, List<? extends com.helger.commons.io.resource.ClassPathResource> aXSDs, Function<? super JAXBTYPE, ? extends jakarta.xml.bind.JAXBElement<JAXBTYPE>> aJAXBElementWrapper) Constructor with XSD paths.GenericJAXBMarshaller(Class<JAXBTYPE> aType, QName aQName) Constructor without XSD paths. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Function<T, jakarta.xml.bind.JAXBElement<T>> createSimpleJAXBElement(QName aQName, Class<T> aClass) Helper function to create a supplier forJAXBElementobjects.protected Schemaprotected voidcustomizeMarshaller(jakarta.xml.bind.Marshaller aMarshaller) Customize the passed marshaller before marshalling something.protected voidcustomizeMarshallerForValidation(jakarta.xml.bind.Marshaller aMarshaller) Customize the passed marshaller before marshalling something.protected voidcustomizeUnmarshaller(jakarta.xml.bind.Unmarshaller aUnmarshaller) Customize the passed unmarshaller before unmarshalling (reading) something.final Charsetfinal ClassLoaderfinal Stringprotected jakarta.xml.bind.JAXBContextgetJAXBContext(ClassLoader aClassLoader) Create the JAXBContext - cached or uncached.final NamespaceContextfinal Stringfinal com.helger.commons.collection.impl.ICommonsList<com.helger.commons.io.resource.ClassPathResource> final StringgetType()final jakarta.xml.bind.ValidationEventHandlerfinal booleanfinal booleanfinal booleanfinal JAXBTYPEread(IJAXBUnmarshaller<JAXBTYPE> aHandler) Read a document using the passed handler.final com.helger.commons.callback.CallbackList<com.helger.commons.callback.exception.IExceptionCallback<jakarta.xml.bind.JAXBException>> final GenericJAXBMarshaller<JAXBTYPE> setCharset(Charset aCharset) Set the charset to be used for writing JAXB objects.final GenericJAXBMarshaller<JAXBTYPE> setCollectErrors(com.helger.commons.error.list.ErrorList aErrorList) Special overload ofsetValidationEventHandler(ValidationEventHandler)for the easy version of just collecting the errors and additionally invoking the old validation handler.final GenericJAXBMarshaller<JAXBTYPE> setFormattedOutput(boolean bWriteFormatted) Change the way formatting happens when calling write.final GenericJAXBMarshaller<JAXBTYPE> setIndentString(String sIndentString) Set the indent string to be used for writing JAXB objects.final GenericJAXBMarshaller<JAXBTYPE> setNamespaceContext(NamespaceContext aNSContext) Set the namespace context (prefix to namespace URL mapping) to be used.final GenericJAXBMarshaller<JAXBTYPE> setNoNamespaceSchemaLocation(String sNoNamespaceSchemaLocation) Set the no namespace schema location to be used for writing JAXB objects.final GenericJAXBMarshaller<JAXBTYPE> setSchemaLocation(String sSchemaLocation) Set the schema location to be used for writing JAXB objects.final GenericJAXBMarshaller<JAXBTYPE> setUseContextCache(boolean bUseContextCache) Change whether the context cache should be used or not.final GenericJAXBMarshaller<JAXBTYPE> setUseSchema(boolean bUseSchema) Enable or disable the usage of an eventually configured XML Schema.final GenericJAXBMarshaller<JAXBTYPE> setValidationEventHandler(jakarta.xml.bind.ValidationEventHandler aEventHandler) Set the JAXB validation event handler to be used.final GenericJAXBMarshaller<JAXBTYPE> Deprecated, for removal: This API element is subject to removal in a future version.Set the validation event handler manually.toString()voidValidate the passed JAXB document.final com.helger.commons.state.ESuccesswrite(JAXBTYPE aObject, IJAXBMarshaller<JAXBTYPE> aMarshallerFunc) Convert the passed object to XML.final com.helger.commons.callback.CallbackList<com.helger.commons.callback.exception.IExceptionCallback<jakarta.xml.bind.JAXBException>> Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface com.helger.commons.lang.IHasClassLoader
hasClassLoaderMethods inherited from interface com.helger.jaxb.IJAXBReader
read, read, read, read, read, read, read, read, read, read, read, read, read, read, read, read, readMethods inherited from interface com.helger.jaxb.IJAXBValidator
isValid, validateMethods inherited from interface com.helger.jaxb.IJAXBWriter
getAsByteBuffer, getAsBytes, getAsDocument, getAsElement, getAsInputStream, getAsMicroDocument, getAsMicroElement, getAsString, getXMLWriterSettings, hasCharset, hasIndentString, hasNoNamespaceSchemaLocation, hasSchemaLocation, write, write, write, write, write, write, write, write, write
-
Constructor Details
-
GenericJAXBMarshaller
Constructor without XSD paths.- Parameters:
aType- The class of the JAXB document implementation type. May not benull.aQName- The qualified name in which the object should be wrapped. May not benull.- Since:
- 9.1.5
- See Also:
-
GenericJAXBMarshaller
public GenericJAXBMarshaller(@Nonnull Class<JAXBTYPE> aType, @Nonnull Function<? super JAXBTYPE, ? extends jakarta.xml.bind.JAXBElement<JAXBTYPE>> aWrapper) Constructor without XSD paths.- Parameters:
aType- The class of the JAXB document implementation type. May not benull.aWrapper- Wrap the passed domain object into aJAXBElementfor marshalling (writing). This can usually be done using the respective's package ObjectFactory implementation. May not benull.
-
GenericJAXBMarshaller
public GenericJAXBMarshaller(@Nonnull Class<JAXBTYPE> aType, @Nullable List<? extends com.helger.commons.io.resource.ClassPathResource> aXSDs, @Nonnull Function<? super JAXBTYPE, ? extends jakarta.xml.bind.JAXBElement<JAXBTYPE>> aJAXBElementWrapper) Constructor with XSD paths.- Parameters:
aType- The class of the JAXB document implementation type. May not benull.aXSDs- The XSDs used to validate document. May benullor empty indicating, that no XSD check is needed.aJAXBElementWrapper- Wrap the passed domain object into aJAXBElementfor marshalling (writing). This can usually be done using the respective's package ObjectFactory implementation. May not benull.
-
-
Method Details
-
getType
- Returns:
- The type as passed in the constructor. Never
null. - Since:
- v9.4.2
-
getClassLoader
- Specified by:
getClassLoaderin interfacecom.helger.commons.lang.IHasClassLoader
-
getValidationEventHandler
- Returns:
- The special JAXB validation event handler to be used. By default
JAXBBuilderDefaultSettings.getDefaultValidationEventHandler()is used.
-
setValidationEventHandler
@Nonnull public final GenericJAXBMarshaller<JAXBTYPE> setValidationEventHandler(@Nullable jakarta.xml.bind.ValidationEventHandler aEventHandler) Set the JAXB validation event handler to be used. May benull.- Parameters:
aEventHandler- The event handler to be used. May benull.- Returns:
- this for chaining
-
setValidationEventHandlerFactory
@Nonnull @Deprecated(forRemoval=true, since="11.0.0") public final GenericJAXBMarshaller<JAXBTYPE> setValidationEventHandlerFactory(@Nullable IValidationEventHandlerFactory aVEHFactory) Deprecated, for removal: This API element is subject to removal in a future version.Set the validation event handler manually. UsesetValidationEventHandler(ValidationEventHandler)instead.Set a factory to be used to createValidationEventHandlerobjects. By default none is present. Since v11 this is deprecated, because it was too complicated to use.- Parameters:
aVEHFactory- The new factory to be used. May benull.- Returns:
- this for chaining
-
setCollectErrors
@Nonnull public final GenericJAXBMarshaller<JAXBTYPE> setCollectErrors(@Nonnull com.helger.commons.error.list.ErrorList aErrorList) Special overload ofsetValidationEventHandler(ValidationEventHandler)for the easy version of just collecting the errors and additionally invoking the old validation handler.- Parameters:
aErrorList- The error list to fill. May not benull.- Returns:
- this for chaining
- Since:
- 11.0.0
-
getNamespaceContext
- Specified by:
getNamespaceContextin interfaceIJAXBWriter<JAXBTYPE>- Returns:
- The special JAXB namespace context to be used. May be
null.
-
setNamespaceContext
@Nonnull public final GenericJAXBMarshaller<JAXBTYPE> setNamespaceContext(@Nullable NamespaceContext aNSContext) Set the namespace context (prefix to namespace URL mapping) to be used.- Parameters:
aNSContext- The namespace context to be used. May benull.- Returns:
- this for chaining
- Since:
- 8.5.3
-
isFormattedOutput
public final boolean isFormattedOutput()- Specified by:
isFormattedOutputin interfaceIJAXBWriter<JAXBTYPE>- Returns:
trueif the JAXB output should be formatted. Default isfalse.
-
setFormattedOutput
Change the way formatting happens when calling write.- Parameters:
bWriteFormatted-trueto write formatted output.- Returns:
- this for chaining
- Since:
- 8.5.3
-
getCharset
- Specified by:
getCharsetin interfaceIJAXBWriter<JAXBTYPE>- Returns:
- The special JAXB Charset to be used for writing.
nullby default.
-
setCharset
Set the charset to be used for writing JAXB objects.- Parameters:
aCharset- The charset to be used by default. May benull.- Returns:
- this for chaining
- Since:
- 8.5.3
-
getIndentString
- Specified by:
getIndentStringin interfaceIJAXBWriter<JAXBTYPE>- Returns:
- The JAXB indentation string to be used for writing.
nullby default. Only used when formatted output is used.
-
setIndentString
@Nonnull public final GenericJAXBMarshaller<JAXBTYPE> setIndentString(@Nullable String sIndentString) Set the indent string to be used for writing JAXB objects.- Parameters:
sIndentString- The indent string to be used. May benull.- Returns:
- this for chaining
- Since:
- 8.5.3
-
isUseSchema
public final boolean isUseSchema()- Specified by:
isUseSchemain interfaceIJAXBReader<JAXBTYPE>- Specified by:
isUseSchemain interfaceIJAXBValidator<JAXBTYPE>- Specified by:
isUseSchemain interfaceIJAXBWriter<JAXBTYPE>- Returns:
trueif an eventually configured XML Schema should be used,falseto explicitly disable the usage of XML Schema.
-
setUseSchema
Enable or disable the usage of an eventually configured XML Schema.- Parameters:
bUseSchema-trueto use an XML Schema,falseto not use it.- Returns:
- this for chaining
- Since:
- 11.0.3
-
getSchemaLocation
- Specified by:
getSchemaLocationin interfaceIJAXBWriter<JAXBTYPE>- Returns:
- The schema location to be used for writing.
nullby default.
-
setSchemaLocation
@Nonnull public final GenericJAXBMarshaller<JAXBTYPE> setSchemaLocation(@Nullable String sSchemaLocation) Set the schema location to be used for writing JAXB objects.- Parameters:
sSchemaLocation- The schema location to be used. May benull.- Returns:
- this for chaining
- Since:
- 8.6.0
-
getNoNamespaceSchemaLocation
- Specified by:
getNoNamespaceSchemaLocationin interfaceIJAXBWriter<JAXBTYPE>- Returns:
- The no namespace schema location to be used for writing.
nullby default.
-
setNoNamespaceSchemaLocation
@Nonnull public final GenericJAXBMarshaller<JAXBTYPE> setNoNamespaceSchemaLocation(@Nullable String sNoNamespaceSchemaLocation) Set the no namespace schema location to be used for writing JAXB objects.- Parameters:
sNoNamespaceSchemaLocation- The no namespace schema location to be used. May benull.- Returns:
- this for chaining
- Since:
- 9.0.0
-
isUseContextCache
public final boolean isUseContextCache() -
setUseContextCache
Change whether the context cache should be used or not. Since creating the JAXB context is quite cost intensive it is recommended to leave it enabled.- Parameters:
bUseContextCache-trueto use it (default),falseif not.- Returns:
- this for chaining
-
readExceptionCallbacks
@Nonnull @ReturnsMutableObject public final com.helger.commons.callback.CallbackList<com.helger.commons.callback.exception.IExceptionCallback<jakarta.xml.bind.JAXBException>> readExceptionCallbacks()- Returns:
- Read exception callbacks. Never
null. - Since:
- 9.2.2
-
writeExceptionCallbacks
@Nonnull @ReturnsMutableObject public final com.helger.commons.callback.CallbackList<com.helger.commons.callback.exception.IExceptionCallback<jakarta.xml.bind.JAXBException>> writeExceptionCallbacks()- Returns:
- Write exception callbacks. Never
null. - Since:
- 9.2.2
-
getOriginalXSDs
@Nonnull @Nonempty @ReturnsMutableCopy public final com.helger.commons.collection.impl.ICommonsList<com.helger.commons.io.resource.ClassPathResource> getOriginalXSDs()- Returns:
- A list of all XSD resources used for validation. Never
nullbut maybe empty.
-
createValidationSchema
- Returns:
- The validation schema to be used. May be
nullindicating that no validation is required. - See Also:
-
getJAXBContext
@Nonnull @OverrideOnDemand protected jakarta.xml.bind.JAXBContext getJAXBContext(@Nullable ClassLoader aClassLoader) throws jakarta.xml.bind.JAXBException Create the JAXBContext - cached or uncached.- Parameters:
aClassLoader- The class loader to be used for XML schema resolving. May benull.- Returns:
- The created JAXBContext and never
null. - Throws:
jakarta.xml.bind.JAXBException- In case creation fails- Since:
- 9.4.2
-
customizeUnmarshaller
@OverrideOnDemand protected void customizeUnmarshaller(@Nonnull jakarta.xml.bind.Unmarshaller aUnmarshaller) Customize the passed unmarshaller before unmarshalling (reading) something.- Parameters:
aUnmarshaller- The object to customize. Nevernull.
-
read
Description copied from interface:IJAXBReaderRead a document using the passed handler.- Specified by:
readin interfaceIJAXBReader<JAXBTYPE>- Parameters:
aHandler- The unmarshalling handler. May not benull.- Returns:
nullin case reading fails.
-
customizeMarshaller
@OverrideOnDemand protected void customizeMarshaller(@Nonnull jakarta.xml.bind.Marshaller aMarshaller) Customize the passed marshaller before marshalling something.- Parameters:
aMarshaller- The object to customize. Nevernull.
-
write
@Nonnull public final com.helger.commons.state.ESuccess write(@Nonnull JAXBTYPE aObject, @Nonnull IJAXBMarshaller<JAXBTYPE> aMarshallerFunc) Description copied from interface:IJAXBWriterConvert the passed object to XML.- Specified by:
writein interfaceIJAXBWriter<JAXBTYPE>- Parameters:
aObject- The object to be converted. May not benull.aMarshallerFunc- The marshalling function. May not benull.- Returns:
ESuccess
-
customizeMarshallerForValidation
@OverrideOnDemand protected void customizeMarshallerForValidation(@Nonnull jakarta.xml.bind.Marshaller aMarshaller) Customize the passed marshaller before marshalling something.- Parameters:
aMarshaller- The object to customize. Nevernull.
-
validate
public void validate(@Nonnull JAXBTYPE aObject, @Nonnull com.helger.commons.error.list.ErrorList aErrorList) Description copied from interface:IJAXBValidatorValidate the passed JAXB document.- Specified by:
validatein interfaceIJAXBValidator<JAXBTYPE>- Parameters:
aObject- The JAXB document to be validated. May not benull.aErrorList- The error list to be filled. May not benull.
-
toString
-
createSimpleJAXBElement
@Nonnull public static <T> Function<T,jakarta.xml.bind.JAXBElement<T>> createSimpleJAXBElement(@Nonnull QName aQName, @Nonnull Class<T> aClass) Helper function to create a supplier forJAXBElementobjects.- Type Parameters:
T- the type to wrap- Parameters:
aQName- QName to use. May not benull.aClass- The implementation class to use.- Returns:
- Never
null. - Since:
- 9.1.5
- See Also:
-