use of org.eclipse.persistence.oxm.record.UnmarshalRecord in project eclipselink by eclipse-ee4j.
the class JAXBUnmarshaller method unmarshal.
/**
* Unmarshal the object based on the binding metadata associated with the
* TypeMappingInfo.
*/
public JAXBElement unmarshal(XMLStreamReader streamReader, TypeMappingInfo type) throws JAXBException {
try {
Descriptor xmlDescriptor = type.getXmlDescriptor();
if (type.getType() instanceof Class) {
Class<?> javaClass = (Class) type.getType();
Class<?> componentClass = javaClass.getComponentType();
if (javaClass.isArray() && javaClass != CoreClassConstants.APBYTE && javaClass != CoreClassConstants.ABYTE && XMLConversionManager.getDefaultJavaTypes().get(componentClass) != null) {
// Top-level array. Descriptor will be for an EL-generated class, containing one DirectCollection mapping.
DirectCollectionMapping mapping = (DirectCollectionMapping) xmlDescriptor.getMappings().get(0);
XMLStreamReaderReader staxReader = new XMLStreamReaderReader(xmlUnmarshaller);
staxReader.setErrorHandler(xmlUnmarshaller.getErrorHandler());
PrimitiveArrayContentHandler primitiveArrayContentHandler = new PrimitiveArrayContentHandler(javaClass, componentClass, mapping.usesSingleNode());
staxReader.setContentHandler(primitiveArrayContentHandler);
XMLStreamReaderInputSource inputSource = new XMLStreamReaderInputSource(streamReader);
staxReader.parse(inputSource);
return primitiveArrayContentHandler.getJaxbElement();
}
}
if (null != xmlDescriptor && null == getSchema()) {
RootLevelXmlAdapter adapter = null;
if (jaxbContext.getTypeMappingInfoToJavaTypeAdapters().size() > 0) {
adapter = jaxbContext.getTypeMappingInfoToJavaTypeAdapters().get(type);
}
UnmarshalRecord wrapper = (UnmarshalRecord) xmlDescriptor.getObjectBuilder().createRecordFromXMLContext(xmlUnmarshaller.getXMLContext());
org.eclipse.persistence.internal.oxm.record.UnmarshalRecord unmarshalRecord = wrapper.getUnmarshalRecord();
XMLStreamReaderReader staxReader = new XMLStreamReaderReader(xmlUnmarshaller);
unmarshalRecord.setUnmarshaller(xmlUnmarshaller);
unmarshalRecord.setXMLReader(staxReader);
staxReader.setContentHandler(unmarshalRecord);
staxReader.parse(streamReader);
Object value = null;
if (unmarshalRecord.isNil()) {
value = null;
} else {
value = unmarshalRecord.getCurrentObject();
}
if (value instanceof WrappedValue) {
value = ((WrappedValue) value).getValue();
}
if (value instanceof ManyValue) {
value = ((ManyValue) value).getItem();
}
if (adapter != null) {
try {
value = adapter.getXmlAdapter().unmarshal(value);
} catch (Exception ex) {
throw new JAXBException(XMLMarshalException.marshalException(ex));
}
}
Class<?> declaredClass = null;
if (type.getType() instanceof Class) {
declaredClass = (Class) type.getType();
} else {
declaredClass = Object.class;
}
return new JAXBElement(new QName(unmarshalRecord.getRootElementNamespaceUri(), unmarshalRecord.getLocalName()), declaredClass, value);
}
if (jaxbContext.getTypeMappingInfoToGeneratedType() == null) {
return unmarshal(streamReader, type.getType());
}
RootLevelXmlAdapter adapter = null;
if (jaxbContext.getTypeMappingInfoToJavaTypeAdapters().size() > 0) {
adapter = jaxbContext.getTypeMappingInfoToJavaTypeAdapters().get(type);
}
Class<?> unmarshalClass = null;
if (jaxbContext.getTypeMappingInfoToGeneratedType().size() > 0) {
unmarshalClass = jaxbContext.getTypeMappingInfoToGeneratedType().get(type);
}
if (unmarshalClass != null) {
JAXBElement unmarshalled = unmarshal(streamReader, unmarshalClass);
Class<?> declaredClass = null;
if (type.getType() instanceof Class) {
declaredClass = (Class) type.getType();
} else {
declaredClass = Object.class;
}
Object value = unmarshalled.getValue();
if (adapter != null) {
try {
value = adapter.getXmlAdapter().unmarshal(value);
} catch (Exception ex) {
throw new JAXBException(XMLMarshalException.marshalException(ex));
}
}
JAXBElement returnVal = new JAXBElement(unmarshalled.getName(), declaredClass, unmarshalled.getScope(), value);
return returnVal;
} else if (type.getType() instanceof Class) {
if (adapter != null) {
JAXBElement element = unmarshal(streamReader, adapter.getBoundType());
try {
Object value = adapter.getXmlAdapter().unmarshal(element.getValue());
element.setValue(value);
return element;
} catch (Exception ex) {
throw new JAXBException(XMLMarshalException.marshalException(ex));
}
}
return unmarshal(streamReader, (Class) type.getType());
} else if (type.getType() instanceof ParameterizedType) {
return unmarshal(streamReader, ((ParameterizedType) type.getType()).getRawType());
}
return null;
} catch (XMLMarshalException xmlMarshalException) {
throw handleXMLMarshalException(xmlMarshalException);
} catch (SAXException e) {
throw new JAXBException(e);
}
}
use of org.eclipse.persistence.oxm.record.UnmarshalRecord in project eclipselink by eclipse-ee4j.
the class DBWSModelProject method buildResultDescriptor.
// result
//
// type|attachment
protected XMLDescriptor buildResultDescriptor() {
XMLDescriptor descriptor = new XMLDescriptor();
descriptor.setJavaClass(Result.class);
descriptor.setDefaultRootElement("result");
XMLTransformationMapping type = new XMLTransformationMapping();
type.setAttributeName("type");
QNameTransformer qNameTransformer = new QNameTransformer("type/text()");
type.addFieldTransformer("type/text()", qNameTransformer);
type.setAttributeTransformer(qNameTransformer);
descriptor.addMapping(type);
XMLCompositeObjectMapping attachment = new XMLCompositeObjectMapping();
attachment.setAttributeName("attachment");
attachment.setXPath("attachment");
attachment.setReferenceClass(Attachment.class);
descriptor.addMapping(attachment);
XMLCompositeObjectMapping sxf = new XMLCompositeObjectMapping();
sxf.setAttributeName("simpleXMLFormat");
sxf.setXPath("simple-xml-format");
sxf.setReferenceClass(SimpleXMLFormat.class);
descriptor.addMapping(sxf);
XMLDirectMapping isCollection = new XMLDirectMapping();
isCollection.setAttributeAccessor(new AttributeAccessor() {
@Override
public String getAttributeName() {
return "isCollection";
}
@Override
public Object getAttributeValueFromObject(Object object) throws DescriptorException {
if (object instanceof CollectionResult) {
return Boolean.TRUE;
}
return null;
}
@Override
public void setAttributeValueInObject(Object object, Object value) throws DescriptorException {
}
});
isCollection.setXPath("@isCollection");
descriptor.addMapping(isCollection);
XMLField isColl = new XMLField("@isCollection");
isColl.setSchemaType(BOOLEAN_QNAME);
descriptor.getInheritancePolicy().setClassIndicatorField(isColl);
descriptor.getInheritancePolicy().setClassExtractor(new ClassExtractor() {
@Override
@SuppressWarnings({ "unchecked" })
public <T> Class<T> extractClassFromRow(DataRecord dataRecord, Session session) {
Class<?> clz = Result.class;
UnmarshalRecord uRecord = (UnmarshalRecord) dataRecord;
Attributes attrs = uRecord.getAttributes();
if (attrs != null) {
for (int i = 0, l = attrs.getLength(); i < l; i++) {
String attributeName = attrs.getQName(i);
if (attributeName.equals("isCollection")) {
String value = attrs.getValue(i);
if (value.equalsIgnoreCase("true")) {
clz = CollectionResult.class;
break;
}
}
}
}
return (Class<T>) clz;
}
});
XMLDirectMapping jdbcTypeMapping = new XMLDirectMapping();
jdbcTypeMapping.setAttributeName("jdbcType");
jdbcTypeMapping.setXPath("@jdbcType");
descriptor.addMapping(jdbcTypeMapping);
return descriptor;
}
use of org.eclipse.persistence.oxm.record.UnmarshalRecord in project eclipselink by eclipse-ee4j.
the class PLSQLCallModelTestProject method buildDatabaseTypeWrapperDescriptor.
protected ClassDescriptor buildDatabaseTypeWrapperDescriptor() {
XMLDescriptor descriptor = new XMLDescriptor();
descriptor.setJavaClass(DatabaseTypeWrapper.class);
descriptor.getInheritancePolicy().setClassExtractor(new ClassExtractor() {
@Override
public Class<?> extractClassFromRow(DataRecord databaseRow, Session session) {
if (databaseRow instanceof UnmarshalRecord) {
UnmarshalRecord unmarshalRecord = (UnmarshalRecord) databaseRow;
Attributes attributes = unmarshalRecord.getAttributes();
for (int i = 0, l = attributes.getLength(); i < l; i++) {
String qName = attributes.getQName(i);
if (PLSQL_RECORD_INDICATOR.equals(qName)) {
return ComplexPLSQLTypeWrapper.class;
}
if (PLSQL_TYPE_INDICATOR.equals(qName)) {
return SimplePLSQLTypeWrapper.class;
}
return JDBCTypeWrapper.class;
}
}
Class<?> clz = null;
DOMRecord domRecord = (DOMRecord) databaseRow;
Object o = domRecord.get("@" + PLSQL_RECORD_INDICATOR);
if (o != null) {
clz = ComplexPLSQLTypeWrapper.class;
} else {
o = domRecord.get("@" + PLSQL_TYPE_INDICATOR);
if (o != null) {
clz = SimplePLSQLTypeWrapper.class;
} else {
o = domRecord.get("@" + JDBC_TYPE_INDICATOR);
if (o != null) {
clz = JDBCTypeWrapper.class;
}
}
}
return clz;
}
});
descriptor.descriptorIsAggregate();
return descriptor;
}
use of org.eclipse.persistence.oxm.record.UnmarshalRecord in project eclipselink by eclipse-ee4j.
the class SDOUnmappedContentHandler method giveToOXToProcess.
private void giveToOXToProcess(String namespaceURI, String localName, String qName, Attributes atts, XMLDescriptor xmlDescriptor) throws SAXException {
AbstractSession session = ((SDOXMLHelper) aHelperContext.getXMLHelper()).getXmlContext().getReadSession(xmlDescriptor);
// taken from SAXUnmarshallerHandler start Element
UnmarshalRecord unmarshalRecord;
if (xmlDescriptor.hasInheritance()) {
unmarshalRecord = new UnmarshalRecord((TreeObjectBuilder) xmlDescriptor.getObjectBuilder());
unmarshalRecord.setUnmarshalNamespaceResolver(unmarshalNamespaceResolver);
unmarshalRecord.setAttributes(atts);
if (parentRecord != null) {
unmarshalRecord.setXMLReader(parentRecord.getXMLReader());
}
Class<?> classValue = xmlDescriptor.getInheritancePolicy().classFromRow(unmarshalRecord, session);
if (classValue == null) {
// no xsi:type attribute - look for type indicator on the default root element
QName leafElementType = xmlDescriptor.getDefaultRootElementType();
// if we have a user-set type, try to get the class from the inheritance policy
if (leafElementType != null) {
Object indicator = xmlDescriptor.getInheritancePolicy().getClassIndicatorMapping().get(leafElementType);
if (indicator != null) {
classValue = (Class) indicator;
}
}
}
if (classValue != null) {
xmlDescriptor = (XMLDescriptor) session.getDescriptor(classValue);
} else {
// sure it is non-abstract
if (Modifier.isAbstract(xmlDescriptor.getJavaClass().getModifiers())) {
// need to throw an exception here
throw DescriptorException.missingClassIndicatorField((XMLRecord) unmarshalRecord, xmlDescriptor.getInheritancePolicy().getDescriptor());
}
}
}
unmarshalRecord = (UnmarshalRecord) xmlDescriptor.getObjectBuilder().createRecord(session);
unmarshalRecord.setParentRecord(parentRecord);
unmarshalRecord.setUnmarshaller(parentRecord.getUnmarshaller());
unmarshalRecord.setXMLReader(parentRecord.getXMLReader());
unmarshalRecord.startDocument();
unmarshalRecord.setUnmarshalNamespaceResolver(unmarshalNamespaceResolver);
unmarshalRecord.startElement(namespaceURI, localName, qName, atts);
parentRecord.getXMLReader().setContentHandler(unmarshalRecord);
try {
unmarshalRecord.getXMLReader().setProperty("http://xml.org/sax/properties/lexical-handler", unmarshalRecord);
} catch (SAXNotRecognizedException ex) {
} catch (SAXNotSupportedException ex) {
// if lexical handling is not supported by this parser, just ignore.
}
currentDataObjects.push(unmarshalRecord.getCurrentObject());
depth++;
}
Aggregations