Class DynamicJAXBContextFactory
DynamicJAXBContextFactory allows the user to create a DynamicJAXBContext without having realized Java classes available on the classpath. During context creation, the user's metadata will be analyzed, and in-memory classes will be generated.
Objects that are returned by EclipseLink unmarshal methods will be subclasses of DynamicEntity. DynamicEntities offer a simple get(propertyName) / set(propertyName, propertyValue) API to manipulate their data.
Example:
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream iStream = classLoader.getResourceAsStream("resource/MySchema.xsd");
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(DynamicJAXBContextFactory.XML_SCHEMA_KEY, iStream);
DynamicJAXBContext jaxbContext = (DynamicJAXBContext) JAXBContext.newInstance("org.example", classLoader, properties);
DynamicEntity employee = jaxbContext.newDynamicEntity("org.example.Employee");
employee.set("firstName", "Bob");
employee.set("lastName", "Barker");
jaxbContext.createMarshaller().(employee, System.out);
- See Also:
-
JAXBContextDynamicJAXBContextDynamicEntityDynamicType
- Author:
- rbarkhouse
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic DynamicJAXBContextcreateContext(Class<?>[] classes, Map<String, Object> properties) Unsupported Operation.static DynamicJAXBContextcreateContext(String contextPath, ClassLoader classLoader, Map<String, Object> properties) Create aDynamicJAXBContext, using either an XML Schema, EclipseLink OXM file, or EclipseLinksessions.xmlas the metadata source.static DynamicJAXBContextcreateContextFromOXM(ClassLoader classLoader, Map<String, ?> properties) Create aDynamicJAXBContext, using an EclipseLink OXM file as the metadata source.static DynamicJAXBContextcreateContextFromXSD(InputStream schemaStream, EntityResolver resolver, ClassLoader classLoader, Map<String, ?> properties) Create aDynamicJAXBContext, using XML Schema as the metadata source.static DynamicJAXBContextcreateContextFromXSD(Source schemaSource, EntityResolver resolver, ClassLoader classLoader, Map<String, Object> properties) Create aDynamicJAXBContext, using XML Schema as the metadata source.static DynamicJAXBContextcreateContextFromXSD(Node schemaDOM, EntityResolver resolver, ClassLoader classLoader, Map<String, Object> properties) Create aDynamicJAXBContext, using XML Schema as the metadata source.
-
Field Details
-
XML_SCHEMA_KEY
- See Also:
-
ENTITY_RESOLVER_KEY
- See Also:
-
EXTERNAL_BINDINGS_KEY
- See Also:
-
SCHEMAMETADATA_CLASS_NAME
- See Also:
-
-
Constructor Details
-
DynamicJAXBContextFactory
public DynamicJAXBContextFactory()
-
-
Method Details
-
createContext
public static DynamicJAXBContext createContext(String contextPath, ClassLoader classLoader, Map<String, Object> properties) throws jakarta.xml.bind.JAXBExceptionCreate aDynamicJAXBContext, using either an XML Schema, EclipseLink OXM file, or EclipseLinksessions.xmlas the metadata source. This creation method will be called if the user calls thenewInstance()method onjakarta.xml.bind.JAXBContext, and has specifiedjakarta.xml.bind.JAXBContextFactory=org.eclipse.persistence.jaxb.DynamicJAXBContextFactoryin theirjaxb.propertiesfile.-- Context Creation From XML Schema --
The
propertiesmap must contain the following key/value pairs:- DynamicJAXBContextFactory.XML_SCHEMA_KEY
- Either a
org.w3c.dom.Node,javax.xml.transform.Source, orjava.io.InputStreampointing to the XML Schema - DynamicJAXBContextFactory.ENTITY_RESOLVER_KEY
- An
org.xml.sax.EntityResolver, used to resolve schema imports. Can be null.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); InputStream iStream = classLoader.getResourceAsStream("resource/MySchema.xsd"); Map<String, Object> properties = new HashMap<String, Object>(); properties.put(DynamicJAXBContextFactory.XML_SCHEMA_KEY, iStream); DynamicJAXBContext jaxbContext = (DynamicJAXBContext) JAXBContext.newInstance("org.example", classLoader, properties); DynamicEntity emp = jaxbContext.newDynamicEntity("org.example.Employee"); ...Context Creation From EclipseLink OXM:The
propertiesmap must contain the key JAXBContextProperties.OXM_METADATA_SOURCE, which can have several possible values:- One of the following, pointing to your OXM file:
java.io.File,java.io.InputStream,java.io.Reader,java.net.URL,
javax.xml.stream.XMLEventReader,javax.xml.stream.XMLStreamReader,javax.xml.transform.Source,
org.w3c.dom.Node, ororg.xml.sax.InputSource. - A
Listof objects from the set above. - A
Map<String, Object>, whereStringis a package name, andObjectis the pointer to the OXM file, from the set
of possibilities above. If using this option, apackage-nameelement is not required in thexml-bindingselement of your OXM file.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); InputStream iStream = classLoader.getResourceAsStream("resource/eclipselink-oxm.xml"); Map<String, Object> properties = new HashMap<String, Object>(); properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream); DynamicJAXBContext jaxbContext = (DynamicJAXBContext) JAXBContext.newInstance("org.example", classLoader, properties); DynamicEntity emp = jaxbContext.newDynamicEntity("org.example.Employee"); ...Context Creation From EclipseLink sessions.xml:The
sessionNamesparameter is a colon-delimited list of session names within thesessions.xmlfile.Descriptorsin this session'sProjectmust not havejavaClassset, but must havejavaClassNameset.Example:
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); DynamicJAXBContext jaxbContext = (DynamicJAXBContext) JAXBContext.newInstance("org.example", classLoader, null); DynamicEntity emp = jaxbContext.newDynamicEntity("org.example.Employee"); ...- Parameters:
contextPath- A colon-delimitedStringspecifying the packages containingjaxb.properties. If bootstrapping from EclipseLinksessions.xml, this will also be the name(s) of your sessions.classLoader- The application's current class loader, which will be used to first lookup classes to see if they exist before newDynamicTypesare generated. Can benull, in which caseThread.currentThread().getContextClassLoader()will be used.properties- Map of properties to use when creating a newDynamicJAXBContext. Can be null if bootstrapping from sessions.xml.- Returns:
- A new instance of
DynamicJAXBContext. - Throws:
jakarta.xml.bind.JAXBException- if an error was encountered while creating theDynamicJAXBContext.
-
createContext
public static DynamicJAXBContext createContext(Class<?>[] classes, Map<String, Object> properties) throws jakarta.xml.bind.JAXBExceptionUnsupported Operation. DynamicJAXBConexts can not be created from concrete classes. Use the standard JAXBContext to create a context from existing Classes.- Throws:
jakarta.xml.bind.JAXBException- See Also:
-
createContextFromXSD
public static DynamicJAXBContext createContextFromXSD(Node schemaDOM, EntityResolver resolver, ClassLoader classLoader, Map<String, Object> properties) throws jakarta.xml.bind.JAXBExceptionCreate aDynamicJAXBContext, using XML Schema as the metadata source.- Parameters:
schemaDOM-org.w3c.dom.Noderepresenting the XML Schema.resolver- Anorg.xml.sax.EntityResolver, used to resolve schema imports. Can be null.classLoader- The application's current class loader, which will be used to first lookup classes to see if they exist before newDynamicTypesare generated. Can benull, in which caseThread.currentThread().getContextClassLoader()will be used.properties- Map of properties to use when creating a newDynamicJAXBContext. Can be null.- Returns:
- A new instance of
DynamicJAXBContext. - Throws:
jakarta.xml.bind.JAXBException- if an error was encountered while creating theDynamicJAXBContext.
-
createContextFromXSD
public static DynamicJAXBContext createContextFromXSD(InputStream schemaStream, EntityResolver resolver, ClassLoader classLoader, Map<String, ?> properties) throws jakarta.xml.bind.JAXBExceptionCreate aDynamicJAXBContext, using XML Schema as the metadata source.- Parameters:
schemaStream-java.io.InputStreamfrom which to read the XML Schema.resolver- Anorg.xml.sax.EntityResolver, used to resolve schema imports. Can be null.classLoader- The application's current class loader, which will be used to first lookup classes to see if they exist before newDynamicTypesare generated. Can benull, in which caseThread.currentThread().getContextClassLoader()will be used.properties- Map of properties to use when creating a newDynamicJAXBContext. Can be null.- Returns:
- A new instance of
DynamicJAXBContext. - Throws:
jakarta.xml.bind.JAXBException- if an error was encountered while creating theDynamicJAXBContext.
-
createContextFromXSD
public static DynamicJAXBContext createContextFromXSD(Source schemaSource, EntityResolver resolver, ClassLoader classLoader, Map<String, Object> properties) throws jakarta.xml.bind.JAXBExceptionCreate aDynamicJAXBContext, using XML Schema as the metadata source.- Parameters:
schemaSource-javax.xml.transform.Sourcefrom which to read the XML Schema.resolver- Anorg.xml.sax.EntityResolver, used to resolve schema imports. Can be null.classLoader- The application's current class loader, which will be used to first lookup classes to see if they exist before newDynamicTypesare generated. Can benull, in which caseThread.currentThread().getContextClassLoader()will be used.properties- Map of properties to use when creating a newDynamicJAXBContext. Can be null.- Returns:
- A new instance of
DynamicJAXBContext. - Throws:
jakarta.xml.bind.JAXBException- if an error was encountered while creating theDynamicJAXBContext.
-
createContextFromOXM
public static DynamicJAXBContext createContextFromOXM(ClassLoader classLoader, Map<String, ?> properties) throws jakarta.xml.bind.JAXBExceptionCreate aDynamicJAXBContext, using an EclipseLink OXM file as the metadata source.- Parameters:
classLoader- The application's current class loader, which will be used to first lookup classes to see if they exist before newDynamicTypesare generated. Can benull, in which caseThread.currentThread().getContextClassLoader()will be used.properties- Map of properties to use when creating a newDynamicJAXBContext. This map must contain a key of JAXBContextProperties.OXM_METADATA_SOURCE, which can have several possible values:- One of the following, pointing to your OXM file:
java.io.File,java.io.InputStream,java.io.Reader,java.net.URL,
javax.xml.stream.XMLEventReader,javax.xml.stream.XMLStreamReader,javax.xml.transform.Source,
org.w3c.dom.Node, ororg.xml.sax.InputSource. - A
Listof objects from the set above. - A
Map<String, Object>, whereStringis a package name, andObjectis the pointer to the OXM file, from the set
of possibilities above. If using this option, apackage-nameelement is not required in thexml-bindingselement of your OXM file.
- One of the following, pointing to your OXM file:
- Returns:
- A new instance of
DynamicJAXBContext. - Throws:
jakarta.xml.bind.JAXBException- if an error was encountered while creating theDynamicJAXBContext.
-