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
  • Constructor Details

    • GenericJAXBMarshaller

      public GenericJAXBMarshaller(@Nonnull Class<JAXBTYPE> aType, @Nonnull QName aQName)
      Constructor without XSD paths.
      Parameters:
      aType - The class of the JAXB document implementation type. May not be null.
      aQName - The qualified name in which the object should be wrapped. May not be null.
      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 be null.
      aWrapper - Wrap the passed domain object into a JAXBElement for marshalling (writing). This can usually be done using the respective's package ObjectFactory implementation. May not be null.
    • 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 be null.
      aXSDs - The XSDs used to validate document. May be null or empty indicating, that no XSD check is needed.
      aJAXBElementWrapper - Wrap the passed domain object into a JAXBElement for marshalling (writing). This can usually be done using the respective's package ObjectFactory implementation. May not be null.
  • Method Details

    • getType

      @Nonnull protected final Class<JAXBTYPE> getType()
      Returns:
      The type as passed in the constructor. Never null.
      Since:
      v9.4.2
    • getClassLoader

      @Nullable public final ClassLoader getClassLoader()
      Specified by:
      getClassLoader in interface com.helger.commons.lang.IHasClassLoader
    • getValidationEventHandler

      @Nullable public final jakarta.xml.bind.ValidationEventHandler 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 be null.
      Parameters:
      aEventHandler - The event handler to be used. May be null.
      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. Use setValidationEventHandler(ValidationEventHandler) instead.
      Set a factory to be used to create ValidationEventHandler objects. 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 be null.
      Returns:
      this for chaining
    • setCollectErrors

      @Nonnull public final GenericJAXBMarshaller<JAXBTYPE> setCollectErrors(@Nonnull com.helger.commons.error.list.ErrorList aErrorList)
      Special overload of setValidationEventHandler(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 be null.
      Returns:
      this for chaining
      Since:
      11.0.0
    • getNamespaceContext

      @Nullable public final NamespaceContext getNamespaceContext()
      Specified by:
      getNamespaceContext in interface IJAXBWriter<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 be null.
      Returns:
      this for chaining
      Since:
      8.5.3
    • isFormattedOutput

      public final boolean isFormattedOutput()
      Specified by:
      isFormattedOutput in interface IJAXBWriter<JAXBTYPE>
      Returns:
      true if the JAXB output should be formatted. Default is false.
    • setFormattedOutput

      @Nonnull public final GenericJAXBMarshaller<JAXBTYPE> setFormattedOutput(boolean bWriteFormatted)
      Change the way formatting happens when calling write.
      Parameters:
      bWriteFormatted - true to write formatted output.
      Returns:
      this for chaining
      Since:
      8.5.3
    • getCharset

      @Nullable public final Charset getCharset()
      Specified by:
      getCharset in interface IJAXBWriter<JAXBTYPE>
      Returns:
      The special JAXB Charset to be used for writing. null by default.
    • setCharset

      @Nonnull public final GenericJAXBMarshaller<JAXBTYPE> setCharset(@Nullable Charset aCharset)
      Set the charset to be used for writing JAXB objects.
      Parameters:
      aCharset - The charset to be used by default. May be null.
      Returns:
      this for chaining
      Since:
      8.5.3
    • getIndentString

      @Nullable public final String getIndentString()
      Specified by:
      getIndentString in interface IJAXBWriter<JAXBTYPE>
      Returns:
      The JAXB indentation string to be used for writing. null by 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 be null.
      Returns:
      this for chaining
      Since:
      8.5.3
    • isUseSchema

      public final boolean isUseSchema()
      Specified by:
      isUseSchema in interface IJAXBReader<JAXBTYPE>
      Specified by:
      isUseSchema in interface IJAXBValidator<JAXBTYPE>
      Specified by:
      isUseSchema in interface IJAXBWriter<JAXBTYPE>
      Returns:
      true if an eventually configured XML Schema should be used, false to explicitly disable the usage of XML Schema.
    • setUseSchema

      @Nonnull public final GenericJAXBMarshaller<JAXBTYPE> setUseSchema(boolean bUseSchema)
      Enable or disable the usage of an eventually configured XML Schema.
      Parameters:
      bUseSchema - true to use an XML Schema, false to not use it.
      Returns:
      this for chaining
      Since:
      11.0.3
    • getSchemaLocation

      @Nullable public final String getSchemaLocation()
      Specified by:
      getSchemaLocation in interface IJAXBWriter<JAXBTYPE>
      Returns:
      The schema location to be used for writing. null by 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 be null.
      Returns:
      this for chaining
      Since:
      8.6.0
    • getNoNamespaceSchemaLocation

      @Nullable public final String getNoNamespaceSchemaLocation()
      Specified by:
      getNoNamespaceSchemaLocation in interface IJAXBWriter<JAXBTYPE>
      Returns:
      The no namespace schema location to be used for writing. null by 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 be null.
      Returns:
      this for chaining
      Since:
      9.0.0
    • isUseContextCache

      public final boolean isUseContextCache()
    • setUseContextCache

      @Nonnull public final GenericJAXBMarshaller<JAXBTYPE> setUseContextCache(boolean bUseContextCache)
      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 - true to use it (default), false if 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 null but maybe empty.
    • createValidationSchema

      @Nullable @OverrideOnDemand protected Schema createValidationSchema()
      Returns:
      The validation schema to be used. May be null indicating 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 be null.
      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. Never null.
    • read

      @Nullable public final JAXBTYPE read(@Nonnull IJAXBUnmarshaller<JAXBTYPE> aHandler)
      Description copied from interface: IJAXBReader
      Read a document using the passed handler.
      Specified by:
      read in interface IJAXBReader<JAXBTYPE>
      Parameters:
      aHandler - The unmarshalling handler. May not be null.
      Returns:
      null in 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. Never null.
    • write

      @Nonnull public final com.helger.commons.state.ESuccess write(@Nonnull JAXBTYPE aObject, @Nonnull IJAXBMarshaller<JAXBTYPE> aMarshallerFunc)
      Description copied from interface: IJAXBWriter
      Convert the passed object to XML.
      Specified by:
      write in interface IJAXBWriter<JAXBTYPE>
      Parameters:
      aObject - The object to be converted. May not be null.
      aMarshallerFunc - The marshalling function. May not be null.
      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. Never null.
    • validate

      public void validate(@Nonnull JAXBTYPE aObject, @Nonnull com.helger.commons.error.list.ErrorList aErrorList)
      Description copied from interface: IJAXBValidator
      Validate the passed JAXB document.
      Specified by:
      validate in interface IJAXBValidator<JAXBTYPE>
      Parameters:
      aObject - The JAXB document to be validated. May not be null.
      aErrorList - The error list to be filled. May not be null.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • 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 for JAXBElement objects.
      Type Parameters:
      T - the type to wrap
      Parameters:
      aQName - QName to use. May not be null.
      aClass - The implementation class to use.
      Returns:
      Never null.
      Since:
      9.1.5
      See Also: