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 org.eclipse.persistence.oxm.XMLMarshaller
@see org.eclipse.persistence.oxm.XMLUnmarshaller
@see org.eclipse.persistence.oxm.XMLValidator
return xmlDescriptor.wrapObjectInXMLRoot(unmarshalRecord, this.isResultAlwaysXMLRoot);
}
public Object unmarshal(org.xml.sax.XMLReader xmlReader, InputSource inputSource) {
try {
XMLContext xmlContext = xmlUnmarshaller.getXMLContext();
if (xmlContext.hasDocumentPreservation()) {
SAXDocumentBuilder saxDocumentBuilder = new SAXDocumentBuilder();
xmlReader.setContentHandler(saxDocumentBuilder);
xmlReader.parse(inputSource);
return unmarshal(saxDocumentBuilder.getDocument().getDocumentElement());
}
}
}
public Object unmarshal(org.xml.sax.XMLReader xmlReader, InputSource inputSource, Class clazz) {
try {
XMLContext xmlContext = xmlUnmarshaller.getXMLContext();
if (xmlContext.hasDocumentPreservation()) {
SAXDocumentBuilder saxDocumentBuilder = new SAXDocumentBuilder();
xmlReader.setContentHandler(saxDocumentBuilder);
xmlReader.parse(inputSource);
return unmarshal(saxDocumentBuilder.getDocument().getDocumentElement(), clazz);
}
boolean isPrimitiveWrapper = isPrimitiveWrapper(clazz);
UnmarshalRecord unmarshalRecord;
XMLDescriptor xmlDescriptor = null;
AbstractSession session = null;
// check for case where the reference class is a primitive wrapper - in this case, we
// need to use the conversion manager to convert the node's value to the primitive
// wrapper class, then create, populate and return an XMLRoot. This will be done
// via XMLRootRecord.
if (isPrimitiveWrapper) {
unmarshalRecord = new XMLRootRecord(clazz);
unmarshalRecord.setSession((AbstractSession) xmlUnmarshaller.getXMLContext().getSession(0));
} else if(clazz == ClassConstants.OBJECT) {
SAXUnmarshallerHandler saxUnmarshallerHandler = new SAXUnmarshallerHandler(xmlUnmarshaller.getXMLContext());
saxUnmarshallerHandler.setXMLReader((XMLReader)xmlReader);
saxUnmarshallerHandler.setUnmarshaller(xmlUnmarshaller);
saxUnmarshallerHandler.setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
xmlReader.setContentHandler(saxUnmarshallerHandler);
xmlReader.parse(inputSource);
// resolve any mapping references
saxUnmarshallerHandler.resolveReferences();
return saxUnmarshallerHandler.getObject();
} else {
// for XMLObjectReferenceMappings we need a non-shared cache, so
// try and get a Unit Of Work from the XMLContext
session = xmlContext.getReadSession(clazz);
xmlDescriptor = (XMLDescriptor) session.getDescriptor(clazz);
unmarshalRecord = (UnmarshalRecord) xmlDescriptor.getObjectBuilder().createRecord(session);
}
XMLReader extendedXMLReader;
if(xmlReader instanceof XMLReader) {
if (null == contextInput) {
return null;
}
synchronized(this) {
JAXBContextState newState = contextInput.createContextState();
XMLContext xmlContext = getXMLContext();
xmlContext.setXMLContextState(newState.getXMLContext().getXMLContextState());
newState.setXMLContext(xmlContext);
newState.setTypeToTypeMappingInfo(contextState.getTypeToTypeMappingInfo());
return newState;
}
currentJAXBContextState = newContextState();
} catch (Exception e) {
throw JAXBException.exceptionDuringSchemaGeneration(e);
}
}
XMLContext xmlContext = currentJAXBContextState.getXMLContext();
Generator generator = currentJAXBContextState.getGenerator();
if (generator == null) {
List<Descriptor> descriptorsToProcess = new ArrayList<Descriptor>();
List<Session> sessions = xmlContext.getSessions();
for (Session session : sessions) {
List<Descriptor> descriptors = (List<Descriptor>)(List)session.getProject().getOrderedDescriptors();
for (Descriptor xDesc : descriptors) {
descriptorsToProcess.add(xDesc);
}
return createContextState(classArray, xmlBindingMap);
}
Exception sessionLoadingException = null;
try {
XMLContext xmlContext = new XMLContext(contextPath, classLoader);
return new JAXBContextState(xmlContext);
} catch (Exception exception) {
sessionLoadingException = exception;
}
JAXBException jaxbException = JAXBException.noObjectFactoryOrJaxbIndexInPath(contextPath);
}
XMLPlatform platform = new SAXPlatform();
platform.getConversionManager().setLoader(loader);
XMLContext xmlContext = new XMLContext((Project)proj, loader, sessionEventListeners());
((XMLLogin)xmlContext.getSession().getDatasourceLogin()).setEqualNamespaceResolvers(true);
return new JAXBContextState(xmlContext, generator, typesToBeBound, properties);
}
// Get the schema validation flag if present in the persistence unit properties
boolean validateORMSchema = isORMSchemaValidationPerformed(properties);
// -------------- Until bug 218047 is fixed. -----------------
if (m_orm1_0Project == null) {
m_orm1_0Project = new XMLContext(new XMLEntityMappingsMappingProject(ORM_1_0_NAMESPACE, ORM_1_0_XSD));
m_eclipseLinkOrmProject = new XMLContext(new XMLEntityMappingsMappingProject(ECLIPSELINK_ORM_NAMESPACE, ECLIPSELINK_ORM_XSD));
}
// Unmarshall JPA format.
XMLEntityMappings xmlEntityMappings;
/**
* INTENAL:
*/
public static void write(XMLEntityMappings entityMappings, Writer writer) {
entityMappings.setVersion(XMLEntityMappingsReader.ECLIPSELINK_ORM_XSD_VERSION);
XMLContext context = new XMLContext(new XMLEntityMappingsMappingProject(XMLEntityMappingsReader.ECLIPSELINK_ORM_NAMESPACE, XMLEntityMappingsReader.ECLIPSELINK_ORM_XSD_NAME));
XMLMarshaller marshaller = context.createMarshaller();
marshaller.setSchemaLocation(XMLEntityMappingsReader.ECLIPSELINK_ORM_NAMESPACE + " " + XMLEntityMappingsReader.ECLIPSELINK_ORM_XSD);
marshaller.marshal(entityMappings, writer);
try {
writer.flush();
XMLPlatform xmlPlatform = XMLPlatformFactory.getInstance().getXMLPlatform();
String url = xmlPlatform.resolveNamespacePrefix(next, prefix);
frag.setNamespaceURI(url);
schemaTypeQName = new QName(url, frag.getLocalName());
}
XMLContext xmlContext = nestedRecord.getUnmarshaller().getXMLContext();
referenceDescriptor = xmlContext.getDescriptorByGlobalType(frag);
}
if (referenceDescriptor == null) {
try {
QName qname = new QName(nestedRecord.getNamespaceURI(), nestedRecord.getLocalName());
referenceDescriptor = getDescriptor(nestedRecord, session, qname);
protected XMLDescriptor getDescriptor(XMLRecord xmlRecord, AbstractSession session, QName rootQName) throws XMLMarshalException {
if (rootQName == null) {
rootQName = new QName(xmlRecord.getNamespaceURI(), xmlRecord.getLocalName());
}
XMLContext xmlContext = xmlRecord.getUnmarshaller().getXMLContext();
XMLDescriptor xmlDescriptor = xmlContext.getDescriptor(rootQName);
if (null == xmlDescriptor) {
if (!((keepAsElementPolicy == UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT) || (keepAsElementPolicy == UnmarshalKeepAsElementPolicy.KEEP_ALL_AS_ELEMENT))) {
throw XMLMarshalException.noDescriptorWithMatchingRootElement(xmlRecord.getLocalName());
}
}
Related Classes of org.eclipse.persistence.oxm.XMLContext
Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.