use of org.eclipse.persistence.oxm.XMLRoot 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.oxm.XMLRoot in project eclipselink by eclipse-ee4j.
the class XMLChoiceCollectionMapping method writeFromObjectIntoRow.
@Override
public void writeFromObjectIntoRow(Object object, AbstractRecord row, AbstractSession session, WriteType writeType) throws DescriptorException {
if (this.isReadOnly()) {
return;
}
Object attributeValue = getAttributeValueFromObject(object);
List<XMLEntry> nestedRows = new ArrayList<>();
XMLRecord record = (XMLRecord) row;
// First determine which Field is associated with each value:
if (null != attributeValue) {
ContainerPolicy cp = getContainerPolicy();
Object iterator = cp.iteratorFor(attributeValue);
if (null != iterator) {
while (cp.hasNext(iterator)) {
Object value = cp.next(iterator, session);
value = convertObjectValueToDataValue(value, session, record.getMarshaller());
NodeValue associatedNodeValue = null;
XMLField associatedField = null;
Object fieldValue = value;
if (value instanceof XMLRoot) {
XMLRoot rootValue = (XMLRoot) value;
String localName = rootValue.getLocalName();
String namespaceUri = rootValue.getNamespaceURI();
fieldValue = rootValue.getObject();
associatedField = getFieldForName(localName, namespaceUri);
if (associatedField == null) {
associatedField = getClassToFieldMappings().get(fieldValue.getClass());
}
} else {
associatedField = getClassToFieldMappings().get(value.getClass());
}
if (associatedField == null) {
// this may be a reference mapping
List<XMLField> sourceFields = classToSourceFieldsMappings.get(value.getClass());
if (sourceFields != null && sourceFields.size() > 0) {
DatabaseMapping xmlMapping = (DatabaseMapping) this.choiceElementMappings.get(sourceFields.get(0));
for (XMLField next : sourceFields) {
fieldValue = ((XMLCollectionReferenceMapping) xmlMapping).buildFieldValue(value, next, session);
XMLEntry entry = new XMLEntry();
entry.setValue(fieldValue);
entry.setXMLField(next);
nestedRows.add(entry);
}
}
} else {
DatabaseMapping xmlMapping = (DatabaseMapping) this.choiceElementMappings.get(associatedField);
if (xmlMapping.isAbstractCompositeCollectionMapping()) {
fieldValue = ((XMLCompositeCollectionMapping) xmlMapping).buildCompositeRow(fieldValue, session, row, writeType);
}
XMLEntry entry = new XMLEntry();
entry.setValue(fieldValue);
entry.setXMLField(associatedField);
nestedRows.add(entry);
}
}
}
}
row.put(getFields(), nestedRows);
}
use of org.eclipse.persistence.oxm.XMLRoot in project eclipselink by eclipse-ee4j.
the class ChoiceCollectionReuseTestCases method getControlObject.
@Override
protected Object getControlObject() {
Employee employee = new Employee();
employee.name = "Jane Doe";
employee.choice = new LinkedList<Object>();
employee.choice.add("123 Fake Street");
employee.choice.add(12);
Address addr = new Address();
addr.city = "Ottawa";
addr.street = "45 O'Connor";
employee.choice.add(addr);
employee.choice.add(14);
XMLRoot xmlRoot = new XMLRoot();
xmlRoot.setLocalName("simpleAddress");
xmlRoot.setObject("addressString");
employee.choice.add(xmlRoot);
employee.phone = "123-4567";
return employee;
}
use of org.eclipse.persistence.oxm.XMLRoot in project eclipselink by eclipse-ee4j.
the class XMLChoiceCollectionMappingMixedTestCases method getControlObject.
@Override
protected Object getControlObject() {
Employee employee = new Employee();
employee.name = "Jane Doe";
employee.choice = new java.util.Vector<Object>();
employee.choice.add("123 Fake Street");
employee.choice.add(12);
Address addr = new Address();
addr.city = "Ottawa";
addr.street = "45 O'Connor";
employee.choice.add(addr);
employee.choice.add(14);
XMLRoot xmlRoot = new XMLRoot();
xmlRoot.setLocalName("simpleAddress");
xmlRoot.setObject("addressString");
employee.choice.add(xmlRoot);
employee.phone = "123-4567";
return employee;
}
use of org.eclipse.persistence.oxm.XMLRoot in project eclipselink by eclipse-ee4j.
the class Root method equals.
public boolean equals(Object o) {
try {
Root theRoot = (Root) o;
if (theRoot.getCompanies().size() != getCompanies().size()) {
return false;
}
/*if ((!theRoot.getCompanies().containsAll(getCompanies())) || (!getCompanies().containsAll(theRoot.getCompanies()))) {
return false;
}*/
for (int i = 0; i < getCompanies().size(); i++) {
Object first = getCompanies().get(i);
Object second = theRoot.getCompanies().get(i);
if (first instanceof Company && second instanceof Company) {
if (!first.equals(second)) {
return false;
}
} else if (first instanceof XMLRoot && second instanceof XMLRoot) {
if (!(((XMLRoot) first).getLocalName().equals(((XMLRoot) second).getLocalName()))) {
return false;
}
if (!(((XMLRoot) first).getNamespaceURI().equals(((XMLRoot) second).getNamespaceURI()))) {
return false;
}
} else {
return false;
}
}
} catch (ClassCastException e) {
return false;
}
return true;
}
Aggregations