Package org.jvnet.basicjaxb.lang
Class ContextUtils
java.lang.Object
org.jvnet.basicjaxb.lang.ContextUtils
Utility class for JAXB Context helpers.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> jakarta.xml.bind.JAXBElement<T> createJAXBElement(jakarta.xml.bind.JAXBContext context, T value) static <T> jakarta.xml.bind.JAXBElement<T> createJAXBElement(Object objectFactory, T value) UseObjectFactoryto create a JAXBElement to wrap the given value object.static <T> jakarta.xml.bind.JAXBElement<T> createJAXBElement(T value) Create a JAXBElement to wrap the given value object.static jakarta.xml.bind.MarshallercreateMarshaller(jakarta.xml.bind.JAXBContext context) static jakarta.xml.bind.MarshallercreateMarshaller(jakarta.xml.bind.JAXBContext context, boolean formatted) Create aMarshallerfrom the given JAXB context.static SchemaOutputDomResolvercreateSchemaOutputDomResolver(jakarta.xml.bind.JAXBContext jaxbContext) Create aSchemaOutputDomResolverfrom aJAXBContextstatic SchemaOutputStringResolvercreateSchemaOutputStringResolver(jakarta.xml.bind.JAXBContext jaxbContext) Create aSchemaOutputStringResolverfrom aJAXBContextstatic jakarta.xml.bind.UnmarshallercreateUnmarshaller(jakarta.xml.bind.JAXBContext context) Create aUnmarshallerfrom the given JAXB context.static voidenableSchemaValidation(jakarta.xml.bind.Unmarshaller unmarshaller, jakarta.xml.bind.Marshaller marshaller, String... schemaUrls) Enable XML Schema Validation for the given schema URL(s).static SchemaenableXmlSchemaValidator(jakarta.xml.bind.Unmarshaller unmarshaller, jakarta.xml.bind.Marshaller marshaller, jakarta.xml.bind.JAXBContext jaxbContext) Enable XML Schema Validation for the given JAXBContext.static SchemaenableXmlSchemaValidator(jakarta.xml.bind.Unmarshaller unmarshaller, jakarta.xml.bind.Marshaller marshaller, String... schemaUrls) Enable XML Schema Validation for the given schema URL(s).static StringgetContextPath(Class<?>... classes) Build a JAXB context path of colon (":") delimited package names.static StringmarshalToString(jakarta.xml.bind.JAXBContext context, Object object) Marshal the given object to a formatted XML representation.static StringmarshalToString(jakarta.xml.bind.Marshaller marshaller, Object object) Marshal the given object to a XML representation.static <T> TunmarshalFromString(jakarta.xml.bind.JAXBContext context, String xml, Class<?> clazz) Unmarshal an XML representaion to an object of the given class type.static <T> TunmarshalFromString(jakarta.xml.bind.Unmarshaller unmarshaller, String xml) static <T> TunmarshalFromString(jakarta.xml.bind.Unmarshaller unmarshaller, String xml, Class<?> clazz) Unmarshal an XML representaion to an object of the given class type.
-
Field Details
-
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
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 aMarshallerfrom 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 theMarshallercannot be created.
-
createUnmarshaller
public static jakarta.xml.bind.Unmarshaller createUnmarshaller(jakarta.xml.bind.JAXBContext context) throws jakarta.xml.bind.JAXBException Create aUnmarshallerfrom the given JAXB context.- Parameters:
context- The JAXB context.- Returns:
- A JAXB
Unmarshaller - Throws:
jakarta.xml.bind.JAXBException- When theUnmarshallercannot 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 aSchemaOutputStringResolverfrom aJAXBContext- Parameters:
jaxbContext- The JAXB context.- Returns:
- A
SchemaOutputStringResolverinstance. - Throws:
IOException- When JAXB context cannot generate (output) a schema.
-
createSchemaOutputDomResolver
public static SchemaOutputDomResolver createSchemaOutputDomResolver(jakarta.xml.bind.JAXBContext jaxbContext) throws IOException Create aSchemaOutputDomResolverfrom aJAXBContext- Parameters:
jaxbContext- The JAXB context.- Returns:
- A
SchemaOutputDomResolverinstance. - 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 -
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 forObjectFactoryin 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:
IllegalAccessExceptionIllegalArgumentExceptionInvocationTargetExceptionInstantiationExceptionNoSuchMethodExceptionSecurityException
-
createJAXBElement
UseObjectFactoryto create a JAXBElement to wrap the given value object.- Type Parameters:
T- The generic value type.- Parameters:
objectFactory- TheObjectFactoryfor the JAXB context.value- The object to wrap.- Returns:
- a JAXBElement to wrap the given value object or null.
-