Class ContextUtils

java.lang.Object
org.jvnet.basicjaxb.lang.ContextUtils

public class ContextUtils extends Object
Utility class for JAXB Context helpers.
  • Field Details

    • XML_SCHEMA_FACTORY

      public static final SchemaFactory XML_SCHEMA_FACTORY
    • DEFAULT_JAXB_FORMATTED_OUTPUT

      public static final boolean DEFAULT_JAXB_FORMATTED_OUTPUT
      See Also:
  • Constructor Details

    • ContextUtils

      public ContextUtils()
  • Method Details

    • getContextPath

      public static String getContextPath(Class<?>... classes)
      Build a JAXB context path of colon (":") delimited package names.
      Parameters:
      classes - One or more classes to include on the path.
      Returns:
      A colon (":") delimited path of package names.
    • createMarshaller

      public static jakarta.xml.bind.Marshaller createMarshaller(jakarta.xml.bind.JAXBContext context) throws jakarta.xml.bind.JAXBException
      Throws:
      jakarta.xml.bind.JAXBException
    • createMarshaller

      public static jakarta.xml.bind.Marshaller createMarshaller(jakarta.xml.bind.JAXBContext context, boolean formatted) throws jakarta.xml.bind.JAXBException
      Create a Marshaller from the given JAXB context.
      Parameters:
      context - The JAXB context.
      formatted - True when the marshalled XML data is to be formatted with linefeeds and indentation.
      Returns:
      A JAXB Marshaller
      Throws:
      jakarta.xml.bind.JAXBException - When the Marshaller cannot be created.
    • createUnmarshaller

      public static jakarta.xml.bind.Unmarshaller createUnmarshaller(jakarta.xml.bind.JAXBContext context) throws jakarta.xml.bind.JAXBException
      Create a Unmarshaller from the given JAXB context.
      Parameters:
      context - The JAXB context.
      Returns:
      A JAXB Unmarshaller
      Throws:
      jakarta.xml.bind.JAXBException - When the Unmarshaller cannot be created.
    • marshalToString

      public static String marshalToString(jakarta.xml.bind.JAXBContext context, Object object) throws jakarta.xml.bind.JAXBException
      Marshal the given object to a formatted XML representation.
      Parameters:
      context - The JAXB context.
      object - The object to be marshaled.
      Returns:
      An XML representation of the given object.
      Throws:
      jakarta.xml.bind.JAXBException - When the object cannot be marshalled.
    • marshalToString

      public static String marshalToString(jakarta.xml.bind.Marshaller marshaller, Object object) throws jakarta.xml.bind.JAXBException
      Marshal the given object to a XML representation.
      Parameters:
      marshaller - The JAXB marshaller.
      object - The object to be marshaled.
      Returns:
      An XML representation of the given object.
      Throws:
      jakarta.xml.bind.JAXBException - When the object cannot be marshalled.
    • unmarshalFromString

      public static <T> T unmarshalFromString(jakarta.xml.bind.JAXBContext context, String xml, Class<?> clazz) throws jakarta.xml.bind.JAXBException
      Unmarshal an XML representaion to an object of the given class type.
      Type Parameters:
      T - The object's class type.
      Parameters:
      context - The JAXB context.
      xml - The XML representation.
      clazz - The unmarshalled object class type.
      Returns:
      An unmarshalled object of the given type.
      Throws:
      jakarta.xml.bind.JAXBException - When the object cannot be unmarshalled.
    • unmarshalFromString

      public static <T> T unmarshalFromString(jakarta.xml.bind.Unmarshaller unmarshaller, String xml) throws jakarta.xml.bind.JAXBException
      Throws:
      jakarta.xml.bind.JAXBException
    • unmarshalFromString

      public static <T> T unmarshalFromString(jakarta.xml.bind.Unmarshaller unmarshaller, String xml, Class<?> clazz) throws jakarta.xml.bind.JAXBException
      Unmarshal an XML representaion to an object of the given class type.
      Type Parameters:
      T - The object's class type.
      Parameters:
      unmarshaller - The JAXB unmarshaller.
      xml - The XML representation.
      clazz - The unmarshalled object class type.
      Returns:
      An unmarshalled object of the given type.
      Throws:
      jakarta.xml.bind.JAXBException - When the object cannot be unmarshalled.
    • enableXmlSchemaValidator

      public static Schema enableXmlSchemaValidator(jakarta.xml.bind.Unmarshaller unmarshaller, jakarta.xml.bind.Marshaller marshaller, String... schemaUrls) throws IOException, SAXException
      Enable XML Schema Validation for the given schema URL(s). URL(s) can be "file:", "classpath:", etc.
      Parameters:
      unmarshaller - An optional unmarshaller.
      marshaller - An optional marshaller.
      schemaUrls - A list of XML schema URL(s).
      Returns:
      Immutable in-memory representation of grammar.
      Throws:
      IOException - When an InputStream cannot be used.
      SAXException - When an XML schema cannot be parsed.
    • createSchemaOutputStringResolver

      public static SchemaOutputStringResolver createSchemaOutputStringResolver(jakarta.xml.bind.JAXBContext jaxbContext) throws IOException
      Create a SchemaOutputStringResolver from a JAXBContext
      Parameters:
      jaxbContext - The JAXB context.
      Returns:
      A SchemaOutputStringResolver instance.
      Throws:
      IOException - When JAXB context cannot generate (output) a schema.
    • createSchemaOutputDomResolver

      public static SchemaOutputDomResolver createSchemaOutputDomResolver(jakarta.xml.bind.JAXBContext jaxbContext) throws IOException
      Create a SchemaOutputDomResolver from a JAXBContext
      Parameters:
      jaxbContext - The JAXB context.
      Returns:
      A SchemaOutputDomResolver instance.
      Throws:
      IOException - When JAXB context cannot generate (output) a schema.
    • enableXmlSchemaValidator

      public static Schema enableXmlSchemaValidator(jakarta.xml.bind.Unmarshaller unmarshaller, jakarta.xml.bind.Marshaller marshaller, jakarta.xml.bind.JAXBContext jaxbContext) throws IOException, SAXException
      Enable XML Schema Validation for the given JAXBContext.
      Parameters:
      unmarshaller - An optional unmarshaller.
      marshaller - An optional marshaller.
      jaxbContext - A JAXB context with one or more packages.
      Returns:
      Immutable in-memory representation of grammar.
      Throws:
      IOException - When an SchemaOutputDomResolver cannot be used.
      SAXException - When an XML schema cannot be parsed.
    • enableSchemaValidation

      public static void enableSchemaValidation(jakarta.xml.bind.Unmarshaller unmarshaller, jakarta.xml.bind.Marshaller marshaller, String... schemaUrls) throws IOException, SAXException, jakarta.xml.bind.JAXBException
      Enable XML Schema Validation for the given schema URL(s). URL(s) can be "file:", "classpath:", etc.
      Parameters:
      unmarshaller - Governs the process of deserializing XML data.
      marshaller - Governs the process of serializing XML data.
      schemaUrls - A list of XML schema URL(s).
      Throws:
      IOException - When an InputStream cannot be used.
      SAXException - When an XML schema cannot be parsed.
      jakarta.xml.bind.JAXBException - When marshaller and unmarshaller are not available.
    • createJAXBElement

      public static <T> jakarta.xml.bind.JAXBElement<T> createJAXBElement(jakarta.xml.bind.JAXBContext context, T value) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
      Throws:
      IllegalAccessException
      IllegalArgumentException
      InvocationTargetException
    • createJAXBElement

      public static <T> jakarta.xml.bind.JAXBElement<T> createJAXBElement(T value) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException, SecurityException
      Create a JAXBElement to wrap the given value object. Looks for ObjectFactory in the value object's package.
      Type Parameters:
      T - The generic value type.
      Parameters:
      value - The object to wrap.
      Returns:
      a JAXBElement to wrap the given value object or null.
      Throws:
      IllegalAccessException
      IllegalArgumentException
      InvocationTargetException
      InstantiationException
      NoSuchMethodException
      SecurityException
    • createJAXBElement

      public static <T> jakarta.xml.bind.JAXBElement<T> createJAXBElement(Object objectFactory, T value)
      Use ObjectFactory to create a JAXBElement to wrap the given value object.
      Type Parameters:
      T - The generic value type.
      Parameters:
      objectFactory - The ObjectFactory for the JAXB context.
      value - The object to wrap.
      Returns:
      a JAXBElement to wrap the given value object or null.