Line 0
Link Here
|
|
|
1 |
package org.eclipse.persistence.internal.oxm; |
2 |
|
3 |
import java.util.List; |
4 |
import javax.xml.namespace.QName; |
5 |
|
6 |
import org.eclipse.persistence.internal.oxm.record.MarshalContext; |
7 |
import org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext; |
8 |
import org.eclipse.persistence.internal.queries.ContainerPolicy; |
9 |
import org.eclipse.persistence.internal.sessions.AbstractSession; |
10 |
import org.eclipse.persistence.oxm.NamespaceResolver; |
11 |
import org.eclipse.persistence.oxm.XMLDescriptor; |
12 |
import org.eclipse.persistence.oxm.XMLField; |
13 |
import org.eclipse.persistence.oxm.mappings.XMLCollectionReferenceMapping; |
14 |
import org.eclipse.persistence.oxm.record.MarshalRecord; |
15 |
|
16 |
public class XMLCollectionReferenceMappingMarshalNodeValue extends MappingNodeValue implements ContainerValue { |
17 |
|
18 |
private XMLCollectionReferenceMapping xmlCollectionReferenceMapping; |
19 |
private XPathNode branchNode; |
20 |
|
21 |
public XMLCollectionReferenceMappingMarshalNodeValue(XMLCollectionReferenceMapping xmlCollectionReferenceMapping) { |
22 |
this.xmlCollectionReferenceMapping = xmlCollectionReferenceMapping; |
23 |
branchNode = new XPathNode(); |
24 |
NamespaceResolver namespaceResolver = ((XMLDescriptor) xmlCollectionReferenceMapping.getDescriptor()).getNamespaceResolver(); |
25 |
List fkFields = xmlCollectionReferenceMapping.getFields(); |
26 |
for(int x=0, fkFieldsSize=fkFields.size(); x<fkFieldsSize; x++) { |
27 |
XMLField fkField = (XMLField) fkFields.get(x); |
28 |
branchNode.addChild(fkField.getXPathFragment(), new XMLCollectionReferenceMappingFKMarshalNodeValue(xmlCollectionReferenceMapping, fkField), namespaceResolver); |
29 |
} |
30 |
} |
31 |
|
32 |
public Object getContainerInstance() { |
33 |
return getContainerPolicy().containerInstance(); |
34 |
} |
35 |
|
36 |
public ContainerPolicy getContainerPolicy() { |
37 |
return xmlCollectionReferenceMapping.getContainerPolicy(); |
38 |
} |
39 |
|
40 |
@Override |
41 |
public XMLCollectionReferenceMapping getMapping() { |
42 |
return xmlCollectionReferenceMapping; |
43 |
} |
44 |
|
45 |
public boolean getReuseContainer() { |
46 |
return xmlCollectionReferenceMapping.getReuseContainer(); |
47 |
} |
48 |
|
49 |
public void setContainerInstance(Object object, Object containerInstance) { |
50 |
xmlCollectionReferenceMapping.setAttributeValueInObject(object, containerInstance); |
51 |
} |
52 |
|
53 |
@Override |
54 |
public boolean marshal(XPathFragment xPathFragment, MarshalRecord marshalRecord, Object object, AbstractSession session, NamespaceResolver namespaceResolver) { |
55 |
if (xmlCollectionReferenceMapping.isReadOnly()) { |
56 |
return false; |
57 |
} |
58 |
|
59 |
Object collection = xmlCollectionReferenceMapping.getAttributeAccessor().getAttributeValueFromObject(object); |
60 |
if (null == collection) { |
61 |
return false; |
62 |
} |
63 |
ContainerPolicy cp = getContainerPolicy(); |
64 |
Object iterator = cp.iteratorFor(collection); |
65 |
if (cp.hasNext(iterator)) { |
66 |
XPathFragment groupingFragment = marshalRecord.openStartGroupingElements(namespaceResolver); |
67 |
marshalRecord.closeStartGroupingElements(groupingFragment); |
68 |
} else { |
69 |
return false; |
70 |
} |
71 |
if(xPathFragment != XPathFragment.SELF_FRAGMENT) { |
72 |
marshalRecord.openStartElement(xPathFragment, namespaceResolver); |
73 |
} |
74 |
while (cp.hasNext(iterator)) { |
75 |
Object objectValue = cp.next(iterator, session); |
76 |
marshalSingleValue(xPathFragment, marshalRecord, object, objectValue, session, namespaceResolver, ObjectMarshalContext.getInstance()); |
77 |
} |
78 |
if(xPathFragment != XPathFragment.SELF_FRAGMENT) { |
79 |
marshalRecord.endElement(xPathFragment, namespaceResolver); |
80 |
} |
81 |
return true; |
82 |
} |
83 |
|
84 |
@Override |
85 |
public boolean marshalSingleValue(XPathFragment xPathFragment, MarshalRecord marshalRecord, Object object, Object value, AbstractSession session, NamespaceResolver namespaceResolver, MarshalContext marshalContext) { |
86 |
if (xmlCollectionReferenceMapping.usesSingleNode()) { |
87 |
XPathFragment groupingFragment = marshalRecord.openStartGroupingElements(namespaceResolver); |
88 |
if (xPathFragment.isAttribute()) { |
89 |
marshalRecord.attribute(xPathFragment, namespaceResolver, (String) value); |
90 |
marshalRecord.closeStartGroupingElements(groupingFragment); |
91 |
} else { |
92 |
marshalRecord.closeStartGroupingElements(groupingFragment); |
93 |
marshalRecord.characters((String) value); |
94 |
} |
95 |
} else { |
96 |
for (int x = 0, size = marshalContext.getNonAttributeChildrenSize(branchNode); x < size; x++) { |
97 |
XPathNode xPathNode = (XPathNode)marshalContext.getNonAttributeChild(x, branchNode); |
98 |
xPathNode.marshal((MarshalRecord)marshalRecord, value, session, namespaceResolver, marshalRecord.getMarshaller(), marshalContext.getMarshalContext(x)); |
99 |
} |
100 |
|
101 |
} |
102 |
return true; |
103 |
} |
104 |
|
105 |
@Override |
106 |
public boolean isMarshalNodeValue() { |
107 |
return true; |
108 |
} |
109 |
|
110 |
@Override |
111 |
public boolean isUnmarshalNodeValue() { |
112 |
return false; |
113 |
} |
114 |
|
115 |
private static class XMLCollectionReferenceMappingFKMarshalNodeValue extends MappingNodeValue { |
116 |
|
117 |
private XMLCollectionReferenceMapping xmlCollectionReferenceMapping; |
118 |
private XMLField xmlField; |
119 |
|
120 |
public XMLCollectionReferenceMappingFKMarshalNodeValue(XMLCollectionReferenceMapping xmlCollectionReferenceMapping, XMLField xmlField) { |
121 |
this.xmlCollectionReferenceMapping = xmlCollectionReferenceMapping; |
122 |
this.xmlField = xmlField; |
123 |
} |
124 |
|
125 |
@Override |
126 |
public boolean marshal(XPathFragment xPathFragment, MarshalRecord marshalRecord, Object object, AbstractSession session, NamespaceResolver namespaceResolver) { |
127 |
XPathFragment groupingFragment = marshalRecord.openStartGroupingElements(namespaceResolver); |
128 |
marshalRecord.closeStartGroupingElements(groupingFragment); |
129 |
return marshalSingleValue(xPathFragment, marshalRecord, null, object, session, namespaceResolver, ObjectMarshalContext.getInstance()); |
130 |
} |
131 |
|
132 |
@Override |
133 |
public boolean marshalSingleValue(XPathFragment xPathFragment, MarshalRecord marshalRecord, Object object, Object value, AbstractSession session, NamespaceResolver namespaceResolver, MarshalContext marshalContext) { |
134 |
Object fieldValue = xmlCollectionReferenceMapping.buildFieldValue(value, xmlField, session); |
135 |
if (fieldValue == null) { |
136 |
return false; |
137 |
} |
138 |
QName schemaType = getSchemaType(xmlField, fieldValue, session); |
139 |
String stringValue = getValueToWrite(schemaType, fieldValue, (XMLConversionManager) session.getDatasourcePlatform().getConversionManager(), namespaceResolver); |
140 |
if (stringValue != null) { |
141 |
if (xPathFragment.isAttribute()) { |
142 |
marshalRecord.attribute(xPathFragment, namespaceResolver, stringValue); |
143 |
marshalRecord.closeStartElement(); |
144 |
} else { |
145 |
marshalRecord.closeStartElement(); |
146 |
marshalRecord.characters(stringValue); |
147 |
} |
148 |
return true; |
149 |
} |
150 |
return false; |
151 |
} |
152 |
|
153 |
@Override |
154 |
public XMLCollectionReferenceMapping getMapping() { |
155 |
return xmlCollectionReferenceMapping; |
156 |
} |
157 |
|
158 |
} |
159 |
|
160 |
} |