An XMLContext is created based on EclipseLink sessions or projects and can then used to create instances of XMLMarshaller, XMLUnmarshaller and XMLValidator.
There are constructors to create an XMLContext with a single EclipseLink project or with a String which is a single EclipseLink session name or a ':' separated list of EclipseLink session names.
Code Sample
XMLContext context = new XMLContext("mySessionName");
XMLMarshaller marshaller = context.createMarshaller();
XMLUnmarshaller unmarshaller = context.createUnmarshaller();
XMLValidator validator = context.createValidator();
The XMLContext is thread-safe. If multiple threads accessing the same XMLContext object request an XMLMarshaller, each will receive their own instance of XMLMarshaller, so any state that the XMLMarshaller maintains will be unique to that process. The same is true of instances of XMLUnmarshaller and XMLValidator.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.eclipse.persistence.internal.oxm.Context
Context.ContextState<ABSTRACT_SESSION extends CoreAbstractSession,
DESCRIPTOR extends Descriptor, PROJECT extends CoreProject, SESSION extends CoreSession, SESSION_EVENT_LISTENER extends CoreSessionEventListener> -
Field Summary
Fields inherited from class org.eclipse.persistence.internal.oxm.Context
contextState
-
Constructor Summary
ConstructorDescriptionXMLContext
(String sessionNames) Create a new XMLContext based on the specified session name or list of session namesXMLContext
(String sessionNames, ClassLoader classLoader) Create a new XMLContext based on the specified session name or list of session namesXMLContext
(String sessionNames, ClassLoader classLoader, String xmlResource) Create a new XMLContext based on passed in session names, classloader and session meta XML.XMLContext
(String sessionNames, String xmlResource) Create a new XMLContext based on passed in session names and session meta XML.XMLContext
(Collection projects) XMLContext
(Collection projects, ClassLoader classLoader) XMLContext
(Project project) Create a new XMLContext based on the specified projectXMLContext
(Project project, ClassLoader classLoader) Create a new XMLContext based on the specified Project and ClassLoader.XMLContext
(Project project, ClassLoader classLoader, Collection<SessionEventListener> sessionEventListeners) Create a new XMLContext based on the specified Project and ClassLoader.XMLContext
(Project project, ClassLoader classLoader, SessionEventListener sessionEventListener) Create a new XMLContext based on the specified Project and ClassLoader. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addDescriptorByQName
(QName qName, XMLDescriptor descriptor) void
addSession
(DatabaseSession sessionToAdd) INTERNAL: Add and initialize a new session to the list of sessions associated with this XMLContext.void
applyORMMetadata
(AbstractSession ormSession) ADVANCED: Adjust the OXM metadata to take into account ORM mapping metadata,Create a new XMLBindercreateBinder
(XMLMarshaller marshaller, XMLUnmarshaller unmarshaller) Create a new XMLBinder with supplied marshaller and unmarshaller instances.createByQualifiedName
(String namespace, String typeName, boolean isGlobalType) Create a new object instance for a given XML namespace and name.<T> T
createByXPath
(Object parentObject, String xPath, NamespaceResolver namespaceResolver, Class<T> returnType) Create a new object instance for a given XPath, relative to the parentObject.protected XMLField
createField
(String path) Create a new XMLMarshallerCreate a new XMLUnmarshallercreateUnmarshaller
(Map<String, Boolean> parserFeatures) Create a new XMLUnmarshallerCreate a new XMLValidatorgetDescriptor
(QName qName) INTERNAL: Return the XMLDescriptor with the default root mapping matching the QName parameter.getDescriptor
(XPathQName xpathQName) INTERNAL: Return the XMLDescriptor with the default root mapping matching the QName parameter.getDescriptorByGlobalType
(XPathFragment xPathFragment) INTERNAL: Return the XMLDescriptor mapped to the global type matching the XPathFragment parameter.getDescriptorForObject
(Object object) Returns descriptor for given object.Returns descriptors from all sessions.INTERNAL: Return the DocumentPreservationPolicy associated with this sessionReturns conversion manager from session datasource platform.getReadSession
(Class<?> clazz) INTERNAL: Return the session corresponding to this class.getReadSession
(Object object) INTERNAL: Return the session corresponding to this object.getReadSession
(XMLDescriptor xmlDescriptor) INTERNAL: Return the session corresponding to this XMLDescriptor.getSession
(int index) INTERNAL:XMLContext xmlContext = new XMLContext("path0:path1");
DatabaseSession session = xmlContext.getSession(0); // returns session for path0
getSession
(Class<?> clazz) INTERNAL: Return the session corresponding to this class.getSession
(Object object) INTERNAL: Return the session corresponding to this object.getSession
(XMLDescriptor xmlDescriptor) INTERNAL: Return the session corresponding to this XMLDescriptor.INTERNAL: Return the EclipseLink session used to marshal.<T> T
getValueByXPath
(Object object, String xPath, NamespaceResolver namespaceResolver, Class<T> returnType) Query the object model based on the corresponding XML document.org.eclipse.persistence.oxm.XMLContext.XMLContextState
INTERNAL: Return the XMLContextState that represents the XMLContexts stateful information.boolean
INTERNAL: Return true if any session held onto by this context has a document preservation policy that requires unmarshalling from a Node.void
void
setValueByXPath
(Object object, String xPath, NamespaceResolver namespaceResolver, Object value) Set values in the object model based on the corresponding XML document.void
setXMLContextState
(org.eclipse.persistence.oxm.XMLContext.XMLContextState xcs) INTERNAL: Set the stateful information for this XMLContext.void
storeXMLDescriptorByQName
(XMLDescriptor xmlDescriptor) INTERNAL:Methods inherited from class org.eclipse.persistence.internal.oxm.Context
getSession
-
Constructor Details
-
XMLContext
Create a new XMLContext based on the specified session name or list of session names- Parameters:
sessionNames
- A single session name or multiple session names separated by a :
-
XMLContext
Create a new XMLContext based on the specified session name or list of session names- Parameters:
sessionNames
- A single session name or multiple session names separated by a :classLoader
- classloader for loading sessions.xml
-
XMLContext
Create a new XMLContext based on passed in session names and session meta XML.- Parameters:
sessionNames
- A single session name or multiple session names separated by a:xmlResource
- path to XML file containing session meta data to initialize and load sessions.
-
XMLContext
Create a new XMLContext based on passed in session names, classloader and session meta XML.- Parameters:
sessionNames
- A single session name or multiple session names separated by a :classLoader
- classloader for loading sessions.xmlxmlResource
- path to XML file containing session meta data to initialize and load sessions.
-
XMLContext
Create a new XMLContext based on the specified project- Parameters:
project
- An EclipseLink project
-
XMLContext
Create a new XMLContext based on the specified Project and ClassLoader.- Parameters:
project
- An EclipseLink projectclassLoader
- The ClassLoader to be used
-
XMLContext
public XMLContext(Project project, ClassLoader classLoader, SessionEventListener sessionEventListener) Create a new XMLContext based on the specified Project and ClassLoader.- Parameters:
project
- An EclipseLink projectclassLoader
- The ClassLoader to be usedsessionEventListener
- If non-null, this listener will be registered with the SessionEventManager- See Also:
-
XMLContext
public XMLContext(Project project, ClassLoader classLoader, Collection<SessionEventListener> sessionEventListeners) Create a new XMLContext based on the specified Project and ClassLoader.- Parameters:
project
- An EclipseLink projectclassLoader
- The ClassLoader to be usedsessionEventListeners
- If non-null, these listeners will be registered with the SessionEventManager- See Also:
-
XMLContext
-
XMLContext
-
-
Method Details
-
getXMLContextState
public org.eclipse.persistence.oxm.XMLContext.XMLContextState getXMLContextState()INTERNAL: Return the XMLContextState that represents the XMLContexts stateful information. This method is provided for the benefits of layers that build on top of the core OXM layer such as MOXy's JAXB implementation. -
setXMLContextState
public void setXMLContextState(org.eclipse.persistence.oxm.XMLContext.XMLContextState xcs) INTERNAL: Set the stateful information for this XMLContext. Once the new state has been set, OXM operations (marshal, unmarshal, etc) will be based on this new state. This method is provided for the benefit of layers that build on top of the core OXM layer such as MOXy's JAXB -
addSession
INTERNAL: Add and initialize a new session to the list of sessions associated with this XMLContext. -
createUnmarshaller
Create a new XMLUnmarshaller- Specified by:
createUnmarshaller
in classContext<AbstractSession,
XMLDescriptor, XMLField, NamespaceResolver, Project, DatabaseSession, SessionEventListener> - Returns:
- An XMLUnmarshaller based on this XMLContext
-
createUnmarshaller
Create a new XMLUnmarshallerMap<String, Boolean> parserFeatures = new HashMap<String, Boolean>(1); parserFeatures.put("http://apache.org/xml/features/validation/schema/normalized-value", false); XMLUnmarshaller unmarshaller = xmlContext.createUnmarshaller(parserFeatures);
- Returns:
- An XMLUnmarshaller based on this XMLContext, the underlying parser will use the passed in parser features.
-
createBinder
Create a new XMLBinder- Returns:
- an XMLBinder based on this XMLContext
-
createBinder
Create a new XMLBinder with supplied marshaller and unmarshaller instances.- Returns:
- an XMLBinder based on this XMLContext,
-
createMarshaller
Create a new XMLMarshaller- Specified by:
createMarshaller
in classContext<AbstractSession,
XMLDescriptor, XMLField, NamespaceResolver, Project, DatabaseSession, SessionEventListener> - Returns:
- An XMLMarshaller based on this XMLContext
-
createValidator
Create a new XMLValidator- Returns:
- An XMLValidator based on this XMLContext
-
getReadSession
INTERNAL: Return the session corresponding to this object. Since the object may be mapped by more that one of the projects used to create the XML Context, this method will return the first match. The session will be a unit of work if document preservation is not enabled. This method will typically be used for unmarshalling when a non-shared cache is desired. -
getReadSession
INTERNAL: Return the session corresponding to this class. Since the class may be mapped by more that one of the projects used to create the XML Context, this method will return the first match. The session will be a unit of work if document preservation is not enabled. This method will typically be used for unmarshalling when a non-shared cache is desired. -
getReadSession
INTERNAL: Return the session corresponding to this XMLDescriptor. Since the class may be mapped by more that one of the projects used to create the XML Context, this method will return the first match. The session will be a unit of work if document preservation is not enabled. This method will typically be used for unmarshalling when a non-shared cache is desired. -
getSessions
INTERNAL: Return the EclipseLink session used to marshal. -
getSession
INTERNAL:XMLContext xmlContext = new XMLContext("path0:path1");
DatabaseSession session = xmlContext.getSession(0); // returns session for path0
-
getSession
INTERNAL: Return the session corresponding to this object. Since the object may be mapped by more that one of the projects used to create the XML Context, this method will return the first match.- Overrides:
getSession
in classContext<AbstractSession,
XMLDescriptor, XMLField, NamespaceResolver, Project, DatabaseSession, SessionEventListener>
-
getSession
INTERNAL: Return the session corresponding to this class. Since the class may be mapped by more that one of the projects used to create the XML Context, this method will return the first match.- Overrides:
getSession
in classContext<AbstractSession,
XMLDescriptor, XMLField, NamespaceResolver, Project, DatabaseSession, SessionEventListener>
-
getSession
INTERNAL: Return the session corresponding to this XMLDescriptor. Since the class may be mapped by more that one of the projects used to create the XML Context, this method will return the first match.- Overrides:
getSession
in classContext<AbstractSession,
XMLDescriptor, XMLField, NamespaceResolver, Project, DatabaseSession, SessionEventListener>
-
storeXMLDescriptorByQName
INTERNAL: -
getDescriptor
INTERNAL: Return the XMLDescriptor with the default root mapping matching the QName parameter.- Overrides:
getDescriptor
in classContext<AbstractSession,
XMLDescriptor, XMLField, NamespaceResolver, Project, DatabaseSession, SessionEventListener>
-
getDescriptor
INTERNAL: Return the XMLDescriptor with the default root mapping matching the QName parameter.- Overrides:
getDescriptor
in classContext<AbstractSession,
XMLDescriptor, XMLField, NamespaceResolver, Project, DatabaseSession, SessionEventListener>
-
addDescriptorByQName
-
getDescriptorByGlobalType
INTERNAL: Return the XMLDescriptor mapped to the global type matching the XPathFragment parameter. -
getDocumentPreservationPolicy
INTERNAL: Return the DocumentPreservationPolicy associated with this session -
setupDocumentPreservationPolicy
-
hasDocumentPreservation
public boolean hasDocumentPreservation()INTERNAL: Return true if any session held onto by this context has a document preservation policy that requires unmarshalling from a Node.- Specified by:
hasDocumentPreservation
in classContext<AbstractSession,
XMLDescriptor, XMLField, NamespaceResolver, Project, DatabaseSession, SessionEventListener>
-
applyORMMetadata
ADVANCED: Adjust the OXM metadata to take into account ORM mapping metadata, -
getValueByXPath
public <T> T getValueByXPath(Object object, String xPath, NamespaceResolver namespaceResolver, Class<T> returnType) Query the object model based on the corresponding XML document. The following pairings are equivalent:
Return the Customer's IDInteger id = xmlContext.getValueByXPath(customer, "@id", null, Integer.class); Integer id = customer.getId();
Return the Customer's NameString name = xmlContext.getValueByXPath(customer, "ns:personal-info/ns:name/text()", null, String.class); String name = customer.getName();
Return the Customer's AddressAddress address = xmlContext.getValueByXPath(customer, "ns:contact-info/ns:address", aNamespaceResolver, Address.class); Address address = customer.getAddress();
Return all the Customer's PhoneNumbersList phoneNumbers = xmlContext.getValueByXPath(customer, "ns:contact-info/ns:phone-number", aNamespaceResolver, List.class); List phoneNumbers = customer.getPhoneNumbers();
Return the Customer's second PhoneNumberPhoneNumber phoneNumber = xmlContext.getValueByXPath(customer, "ns:contact-info/ns:phone-number[2]", aNamespaceResolver, PhoneNumber.class); PhoneNumber phoneNumber = customer.getPhoneNumbers().get(1);
Return the base objectCustomer customer = xmlContext.getValueByXPath(customer, ".", aNamespaceResolver, Customer.class); Customer customer = customer;
- Overrides:
getValueByXPath
in classContext<AbstractSession,
XMLDescriptor, XMLField, NamespaceResolver, Project, DatabaseSession, SessionEventListener> - Type Parameters:
T
- The return type of this method corresponds to the returnType parameter.- Parameters:
object
- The XPath will be executed relative to this object.xPath
- The XPath statementnamespaceResolver
- A NamespaceResolver containing the prefix/URI pairings from the XPath statement.returnType
- The return type.- Returns:
- The object corresponding to the XPath or null if no result was found.
-
setValueByXPath
public void setValueByXPath(Object object, String xPath, NamespaceResolver namespaceResolver, Object value) Set values in the object model based on the corresponding XML document. The following pairings are equivalent:
Set the Customer's IDxmlContext.setValueByXPath(customer, "@id", null, Integer.valueOf(123)); customer.setId(Integer.valueOf(123));
Set the Customer's NamexmlContext.setValueByXPath(customer, "ns:personal-info/ns:name/text()", aNamespaceResolver, "Jane Doe"); customer.setName("Jane Doe");
Set the Customer's AddressxmlContext.setValueByXPath(customer, "ns:contact-info/ns:address", aNamespaceResolver, anAddress); customer.setAddress(anAddress);
Set the Customer's PhoneNumbersxmlContext.setValueByXPath(customer, "ns:contact-info/ns:phone-number", aNamespaceResolver, phoneNumbers); customer.setPhoneNumbers(phoneNumbers);
Set the Customer's second PhoneNumberxmlContext.setValueByXPath(customer, "ns:contact-info/ns:phone-number[2]", aNamespaceResolver, aPhoneNumber); customer.getPhoneNumbers().get(1);
- Overrides:
setValueByXPath
in classContext<AbstractSession,
XMLDescriptor, XMLField, NamespaceResolver, Project, DatabaseSession, SessionEventListener> - Parameters:
object
- The XPath will be executed relative to this object.xPath
- The XPath statementnamespaceResolver
- A NamespaceResolver containing the prefix/URI pairings from the XPath statement.value
- The value to be set.
-
createByQualifiedName
public Object createByQualifiedName(String namespace, String typeName, boolean isGlobalType) throws IllegalArgumentException Create a new object instance for a given XML namespace and name.- Parameters:
namespace
- The namespace of the complex type to create a new Java instance of.typeName
- The XML type name to create a new Java instance of.isGlobalType
- True if the object to be created represents a global type, false if it represents a global element.- Returns:
- An instance of the Java class mapped to the indicated XML type, or null if no result was found.
- Throws:
IllegalArgumentException
-
createByXPath
public <T> T createByXPath(Object parentObject, String xPath, NamespaceResolver namespaceResolver, Class<T> returnType) Create a new object instance for a given XPath, relative to the parentObject.- Overrides:
createByXPath
in classContext<AbstractSession,
XMLDescriptor, XMLField, NamespaceResolver, Project, DatabaseSession, SessionEventListener> - Type Parameters:
T
- The return type of this method corresponds to the returnType parameter.- Parameters:
parentObject
- The XPath will be executed relative to this object.xPath
- The XPath statement.namespaceResolver
- A NamespaceResolver containing the prefix/URI pairings from the XPath statement.returnType
- The return type.- Returns:
- An instance of the Java class mapped to the supplied XML type, or null if no result was found.
-
createField
- Specified by:
createField
in classContext<AbstractSession,
XMLDescriptor, XMLField, NamespaceResolver, Project, DatabaseSession, SessionEventListener>
-
getDescriptors
Returns descriptors from all sessions.- Returns:
- descriptors from all sessions
-
getOxmConversionManager
Returns conversion manager from session datasource platform.- Returns:
- conversion manager
-
getDescriptorForObject
Returns descriptor for given object.- Returns:
- descriptor for given object
-