use of org.eclipse.persistence.internal.oxm.XPathFragment in project eclipselink by eclipse-ee4j.
the class XMLAnyObjectMapping method writeSingleValue.
@Override
public void writeSingleValue(Object value, Object parent, XMLRecord row, AbstractSession session) {
DOMRecord record = (DOMRecord) row;
Node root = record.getDOM();
Object objectValue = value;
objectValue = convertObjectValueToDataValue(objectValue, session, row.getMarshaller());
if (field != null) {
root = XPathEngine.getInstance().create((XMLField) getField(), root, session);
}
org.w3c.dom.Document doc = record.getDocument();
XMLField xmlRootField = null;
boolean wasXMLRoot = false;
Object originalObject = objectValue;
Node toReplace = getNodeToReplace(root);
if (usesXMLRoot() && (objectValue instanceof XMLRoot)) {
xmlRootField = new XMLField();
wasXMLRoot = true;
XPathFragment frag = new XPathFragment();
if ((((XMLRoot) objectValue)).getNamespaceURI() != null) {
frag.setNamespaceURI(((XMLRoot) objectValue).getNamespaceURI());
}
frag.setXPath(((XMLRoot) objectValue).getLocalName());
xmlRootField.setXPathFragment(frag);
xmlRootField.setNamespaceResolver(row.getNamespaceResolver());
objectValue = ((XMLRoot) objectValue).getObject();
}
if (objectValue instanceof String) {
writeSimpleValue(xmlRootField, record, session, originalObject, objectValue, root, toReplace, wasXMLRoot);
} else if (objectValue instanceof org.w3c.dom.Node) {
Node importedCopy = doc.importNode((Node) objectValue, true);
root.appendChild(importedCopy);
} else {
XMLDescriptor referenceDescriptor = (XMLDescriptor) session.getDescriptor(objectValue.getClass());
if (referenceDescriptor == null) {
writeSimpleValue(xmlRootField, record, session, originalObject, objectValue, root, toReplace, wasXMLRoot);
return;
}
if (wasXMLRoot) {
if (((XMLRoot) originalObject).getNamespaceURI() != null) {
String prefix = referenceDescriptor.getNonNullNamespaceResolver().resolveNamespaceURI(((XMLRoot) originalObject).getNamespaceURI());
if ((prefix == null) || prefix.length() == 0) {
prefix = row.getNamespaceResolver().resolveNamespaceURI(((XMLRoot) originalObject).getNamespaceURI());
}
if ((prefix == null) || prefix.length() == 0) {
xmlRootField.getXPathFragment().setGeneratedPrefix(true);
prefix = row.getNamespaceResolver().generatePrefix();
}
xmlRootField.getXPathFragment().setXPath(prefix + XMLConstants.COLON + ((XMLRoot) originalObject).getLocalName());
}
}
DOMRecord nestedRecord = (DOMRecord) buildCompositeRow(objectValue, session, referenceDescriptor, row, xmlRootField, originalObject, wasXMLRoot);
if ((nestedRecord != null) && (toReplace != null)) {
if (nestedRecord.getDOM() != toReplace) {
root.replaceChild(nestedRecord.getDOM(), toReplace);
}
} else if (nestedRecord != null) {
root.appendChild(nestedRecord.getDOM());
} else if (toReplace != null) {
root.removeChild(toReplace);
}
}
}
use of org.eclipse.persistence.internal.oxm.XPathFragment in project eclipselink by eclipse-ee4j.
the class XMLCompositeObjectMapping method convertToSimpleTypeIfPresent.
private Object convertToSimpleTypeIfPresent(Object toReturn, XMLRecord nestedRow, AbstractSession executionSession) {
Node textchild = nestedRow.getDOM().getFirstChild();
String stringValue = null;
if ((textchild != null) && (textchild.getNodeType() == Node.TEXT_NODE)) {
stringValue = textchild.getNodeValue();
if (stringValue != null && getKeepAsElementPolicy() != UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT && getKeepAsElementPolicy() != UnmarshalKeepAsElementPolicy.KEEP_ALL_AS_ELEMENT) {
toReturn = stringValue;
}
}
if ((stringValue == null) || stringValue.length() == 0) {
return toReturn;
}
String type = ((Element) nestedRow.getDOM()).getAttributeNS(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, XMLConstants.SCHEMA_TYPE_ATTRIBUTE);
if ((null != type) && type.length() > 0) {
XPathFragment typeFragment = new XPathFragment(type);
String namespaceURI = nestedRow.resolveNamespacePrefix(typeFragment.getPrefix());
QName schemaTypeQName = new QName(namespaceURI, typeFragment.getLocalName());
ConversionManager conversionManager = (ConversionManager) executionSession.getDatasourcePlatform().getConversionManager();
Class<Object> theClass = conversionManager.javaType(schemaTypeQName);
if (theClass != null) {
toReturn = conversionManager.convertObject(stringValue, theClass, schemaTypeQName);
}
}
return toReturn;
}
use of org.eclipse.persistence.internal.oxm.XPathFragment in project eclipselink by eclipse-ee4j.
the class XMLAnyCollectionMapping method buildObjectValuesFromDOMRecord.
private Object buildObjectValuesFromDOMRecord(DOMRecord record, AbstractSession session, ObjectBuildingQuery query, JoinedAttributeManager joinManager) {
// This DOMRecord represents the root node of the AnyType instance
// Grab ALL children to populate the collection.
Node root = record.getDOM();
NodeList children = root.getChildNodes();
ContainerPolicy cp = getContainerPolicy();
Object container = null;
if (reuseContainer) {
Object currentObject = record.getCurrentObject();
Object value = getAttributeAccessor().getAttributeValueFromObject(currentObject);
container = value != null ? value : cp.containerInstance();
} else {
container = cp.containerInstance();
}
int length = children.getLength();
Node next = null;
if (length > 0) {
next = record.getDOM().getFirstChild();
}
while (next != null) {
Object objectValue = null;
if (isUnmappedContent(next)) {
if ((next.getNodeType() == Node.TEXT_NODE) && this.isMixedContent()) {
if (next.getNodeValue().trim().length() > 0) {
objectValue = next.getNodeValue();
objectValue = convertDataValueToObjectValue(objectValue, session, record.getUnmarshaller());
cp.addInto(objectValue, container, session);
}
} else if (next.getNodeType() == Node.ELEMENT_NODE) {
XMLDescriptor referenceDescriptor = null;
// In this case it must be an element so we need to dig up the descriptor
// make a nested record and build an object from it.
DOMRecord nestedRecord = (DOMRecord) record.buildNestedRow((Element) next);
if (!useXMLRoot) {
referenceDescriptor = getDescriptor(nestedRecord, session, null);
objectValue = buildObjectForNonXMLRoot(referenceDescriptor, getConverter(), query, record, nestedRecord, joinManager, session, next, container, cp);
} else {
String schemaType = ((Element) next).getAttributeNS(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, XMLConstants.SCHEMA_TYPE_ATTRIBUTE);
QName schemaTypeQName = null;
XPathFragment frag = new XPathFragment();
if ((null != schemaType) && (schemaType.length() > 0)) {
frag.setXPath(schemaType);
if (frag.hasNamespace()) {
String prefix = frag.getPrefix();
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);
} catch (XMLMarshalException e) {
referenceDescriptor = null;
}
// Check if descriptor is for a wrapper, if it is null it out and let continue
XMLDescriptor xmlReferenceDescriptor = referenceDescriptor;
if (referenceDescriptor != null && xmlReferenceDescriptor.isWrapper()) {
referenceDescriptor = null;
}
}
// is set, then we want to return either an Element or an XMLRoot wrapping an Element
if (getKeepAsElementPolicy() == UnmarshalKeepAsElementPolicy.KEEP_ALL_AS_ELEMENT || (referenceDescriptor == null && getKeepAsElementPolicy() == UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT)) {
Object objVal = buildObjectNoReferenceDescriptor(record, getConverter(), session, next, null, null);
// if we know the descriptor use it to wrap the Element in an XMLRoot (if necessary)
if (referenceDescriptor != null) {
objVal = referenceDescriptor.wrapObjectInXMLRoot(objVal, next.getNamespaceURI(), next.getLocalName(), next.getPrefix(), false, record.isNamespaceAware(), record.getUnmarshaller());
cp.addInto(objVal, container, session);
} else {
// no descriptor, so manually build the XMLRoot
cp.addInto(buildXMLRoot(next, objVal), container, session);
}
} else // if we have a descriptor use it to build the object and wrap it in an XMLRoot
if (referenceDescriptor != null) {
buildObjectAndWrapInXMLRoot(referenceDescriptor, getConverter(), query, record, nestedRecord, joinManager, session, next, container, cp);
} else {
// no descriptor, but could be TEXT to wrap and return
XMLRoot rootValue;
if ((rootValue = buildXMLRootForText(next, schemaTypeQName, getConverter(), session, record)) != null) {
cp.addInto(rootValue, container, session);
}
}
}
}
}
next = next.getNextSibling();
}
return container;
}
use of org.eclipse.persistence.internal.oxm.XPathFragment in project eclipselink by eclipse-ee4j.
the class XMLChoiceCollectionMapping method getFieldForName.
private XMLField getFieldForName(String localName, String namespaceUri) {
Iterator<DatabaseField> fields = getFields().iterator();
while (fields.hasNext()) {
XMLField nextField = (XMLField) fields.next();
XPathFragment fragment = nextField.getXPathFragment();
while (fragment != null && (!fragment.nameIsText())) {
if (fragment.getNextFragment() == null || fragment.getHasText()) {
if (fragment.getLocalName().equals(localName)) {
String fragUri = fragment.getNamespaceURI();
if ((namespaceUri == null && fragUri == null) || (namespaceUri != null && fragUri != null && namespaceUri.equals(fragUri))) {
return nextField;
}
}
}
fragment = fragment.getNextFragment();
}
}
return null;
}
use of org.eclipse.persistence.internal.oxm.XPathFragment in project eclipselink by eclipse-ee4j.
the class XMLVariableXPathCollectionMapping method getXPathFragmentForValue.
@Override
public XPathFragment getXPathFragmentForValue(Object obj, NamespaceResolver nr, boolean isNamespaceAware, char sep) {
Object value = getVariableAttributeAccessor().getAttributeValueFromObject(obj);
if (value == null) {
throw XMLMarshalException.nullValueNotAllowed(getVariableAttributeName(), getReferenceClassName());
}
String returnString;
String uri = null;
if (value instanceof QName) {
returnString = ((QName) value).getLocalPart();
uri = ((QName) value).getNamespaceURI();
} else {
returnString = (String) value;
}
XPathFragment frag = new XPathFragment();
frag.setLocalName(returnString);
if (isNamespaceAware && uri != null && uri.length() > 0) {
String prefix = nr.resolveNamespaceURI(uri);
if (prefix == null) {
prefix = nr.generatePrefix();
// marshalRecord.namespaceDeclaration(prefix, uri);
frag.setGeneratedPrefix(true);
}
if (prefix != null && prefix.length() > 0) {
frag.setPrefix(prefix);
// returnString = prefix + sep + returnString;
}
}
// frag.setXPath(returnString);
// frag.setLocalName(localName);
frag.setNamespaceURI(uri);
return frag;
}
Aggregations