1
|
|
/*
|
2
|
|
* JBind
|
3
|
|
*
|
4
|
|
* Copyright (c) by Stefan Wachter. All rights reserved.
|
5
|
|
*
|
6
|
|
* Usage, modification, and redistribution is subject to license terms that are
|
7
|
|
* available at 'http://www.jbind.org'. The JBind license is like the
|
8
|
|
* 'Apache Software License V 1.1'.
|
9
|
|
*/
|
10
|
|
package org.jbind.xml.instance.data;
|
11
|
|
|
12
|
|
import java.util.Collections;
|
13
|
|
import java.util.Iterator;
|
14
|
|
import java.util.ListIterator;
|
15
|
|
|
16
|
|
import org.jbind.util.collection.FilterIterator;
|
17
|
|
import org.jbind.util.collection.TransformingIterator;
|
18
|
|
import org.jbind.xml.base.INamespaces;
|
19
|
|
import org.jbind.xml.base.ISymbolspaces;
|
20
|
|
import org.jbind.xml.base.Ref;
|
21
|
|
import org.jbind.xml.core.base.ITextContentProvider;
|
22
|
|
import org.jbind.xml.core.bridge.IAttributeImpl;
|
23
|
|
import org.jbind.xml.core.bridge.IDataImpl;
|
24
|
|
import org.jbind.xml.core.bridge.IElementImpl;
|
25
|
|
import org.jbind.xml.core.bridge.IImplChild;
|
26
|
|
import org.jbind.xml.core.bridge.IXPathMethod;
|
27
|
|
import org.jbind.xml.core.content.IAttrDesc;
|
28
|
|
import org.jbind.xml.core.content.IDataDesc;
|
29
|
|
import org.jbind.xml.core.content.IDataRefOrDecl;
|
30
|
|
import org.jbind.xml.core.content.IElemDesc;
|
31
|
|
import org.jbind.xml.core.content.IElemRefOrDecl;
|
32
|
|
import org.jbind.xml.core.data.IAnyTypeData;
|
33
|
|
import org.jbind.xml.core.data.IComplexData;
|
34
|
|
import org.jbind.xml.core.data.IDataVisitor;
|
35
|
|
import org.jbind.xml.core.data.ISimpleData;
|
36
|
|
import org.jbind.xml.core.type.IAnyType;
|
37
|
|
import org.jbind.xml.msg.XmlException;
|
38
|
|
import org.jbind.xml.msg.XmlMessages;
|
39
|
|
|
40
|
|
/**
|
41
|
|
* Base class for data objects.
|
42
|
|
*/
|
43
|
|
public abstract class AbstractData implements IAnyTypeData {
|
44
|
|
|
45
|
|
private IDataRefOrDecl myMatchedAttrOrElem = null;
|
46
|
|
|
47
|
|
private IAnyType myType = null;
|
48
|
|
|
49
|
|
private IDataImpl myImpl = null;
|
50
|
|
|
51
|
|
private IReferencedData myReferencedData = null;
|
52
|
|
|
53
|
|
private ISimpleData myFixed = null;
|
54
|
|
|
55
|
|
private IDataDesc myDataDesc = null;
|
56
|
|
private IElemRefOrDecl mySubstitutionHead = null;
|
57
|
|
|
58
|
2049
|
public IDataDesc getDataDesc_() {
|
59
|
2049
|
return myDataDesc;
|
60
|
|
}
|
61
|
3987
|
public void setDataDesc_(IDataDesc aDataDesc) {
|
62
|
3987
|
myDataDesc = aDataDesc;
|
63
|
|
}
|
64
|
|
|
65
|
2196
|
public IElemRefOrDecl getSubstitutionHead_() {
|
66
|
2196
|
return mySubstitutionHead;
|
67
|
|
}
|
68
|
2338
|
public void setSubstitutionHead_(IElemRefOrDecl aDecl) {
|
69
|
2338
|
mySubstitutionHead = aDecl;
|
70
|
|
}
|
71
|
|
|
72
|
18
|
protected ISimpleData getFixed_() {
|
73
|
18
|
return (null != myDataDesc) ? myDataDesc.getFixed() : null;
|
74
|
|
}
|
75
|
|
|
76
|
12235
|
public IAnyType getType_() {
|
77
|
12235
|
return myType;
|
78
|
|
}
|
79
|
4530
|
public void setType_(IAnyType aType) {
|
80
|
4530
|
myType = aType;
|
81
|
|
}
|
82
|
|
|
83
|
13029
|
public IDataImpl getImpl_() {
|
84
|
13029
|
return myImpl;
|
85
|
|
}
|
86
|
|
|
87
|
4622
|
public final void setImpl_(IDataImpl anImpl) {
|
88
|
4622
|
myImpl = anImpl;
|
89
|
|
}
|
90
|
|
|
91
|
1270
|
public IAnyTypeData getAttribute_(String aNamespace, String aName) {
|
92
|
|
assert null != aNamespace : "namespace must not be null";
|
93
|
|
assert null != aName : "name must not be null";
|
94
|
1270
|
return getImpl_().getAttributeData(aNamespace, aName);
|
95
|
|
}
|
96
|
|
|
97
|
30
|
public Iterator iterAttributes_(String aNamespace, String aName) {
|
98
|
30
|
return ((IDataImpl)getImpl_()).iterAttributes(aNamespace, aName);
|
99
|
|
}
|
100
|
|
|
101
|
0
|
public final boolean isDefault_() {
|
102
|
0
|
return getImpl_().isDefault();
|
103
|
|
}
|
104
|
|
|
105
|
5766
|
public final boolean isNil_() {
|
106
|
5766
|
return getImpl_().isNil();
|
107
|
|
}
|
108
|
|
|
109
|
2081
|
public final void accept_(IDataVisitor aVisitor) {
|
110
|
2081
|
doAcceptStarts_(aVisitor);
|
111
|
2081
|
for (Iterator i = iterElements_(null, null); i.hasNext(); ) {
|
112
|
1982
|
IAnyTypeData d = (IAnyTypeData)i.next();
|
113
|
1982
|
d.accept_(aVisitor);
|
114
|
|
}
|
115
|
2081
|
doAcceptEnds_(aVisitor);
|
116
|
|
}
|
117
|
|
|
118
|
|
protected abstract void doAcceptStarts_(IDataVisitor aVisitor);
|
119
|
|
protected abstract void doAcceptEnds_(IDataVisitor aVisitor);
|
120
|
|
|
121
|
61
|
public void addReferencedData_(String aKey, IAnyTypeData aData) {
|
122
|
61
|
if (null == myReferencedData) {
|
123
|
25
|
myReferencedData = new ReferencedData();
|
124
|
|
}
|
125
|
61
|
myReferencedData.add(aKey, aData);
|
126
|
|
}
|
127
|
|
|
128
|
10
|
protected ListIterator iterReferencedData_(String aKey) {
|
129
|
10
|
ListIterator res = null;
|
130
|
10
|
if (null != myReferencedData) {
|
131
|
10
|
res = myReferencedData.iter(aKey);
|
132
|
|
} else {
|
133
|
0
|
res = Collections.EMPTY_LIST.listIterator();
|
134
|
|
}
|
135
|
10
|
return res;
|
136
|
|
}
|
137
|
|
|
138
|
2081
|
public ListIterator iterElements_(String aNamespace, String aName) {
|
139
|
2081
|
return getImpl_().iterElementData(aNamespace, aName);
|
140
|
|
}
|
141
|
|
|
142
|
0
|
public IComplexData getParent_() {
|
143
|
0
|
IImplChild child = (IImplChild)getImpl_();
|
144
|
0
|
IElementImpl parent = (IElementImpl)child.getParentImpl();
|
145
|
0
|
return (IComplexData)parent.getData();
|
146
|
|
}
|
147
|
|
|
148
|
|
/**
|
149
|
|
* Creates unchecked data objects for attributes. This method is used by the
|
150
|
|
* generated data classes when an attribute is assigned a value.
|
151
|
|
* The assignement uses an unchecked data object into which an (already exisiting)
|
152
|
|
* simple storage value is set. By this procedure it is avoided that the assigned
|
153
|
|
* value is ever transformed into a string. This would be the case if the
|
154
|
|
* data object would be created using the {@link IAnyType#createData createData}
|
155
|
|
* method.
|
156
|
|
* <p>
|
157
|
|
* The supplied text content provider is used only if the textual content of
|
158
|
|
* the assigned value is really needed in the creation of the data object.
|
159
|
|
* This happens if a data object of a union type is created because an intelligent
|
160
|
|
* guess is made about which member type of the union to choose.
|
161
|
|
*
|
162
|
|
* @param aNamespace <i>(required)</i>.
|
163
|
|
* @param aName <i>(required)</i>.
|
164
|
|
* @param aTextContentProvider <i>(required)</i>.
|
165
|
|
*/
|
166
|
6
|
protected IAnyTypeData createUncheckedAttributeData_(String aNamespace, String aName, ITextContentProvider aProvider) throws XmlException {
|
167
|
6
|
IAttrDesc desc = getType_().getAttrDesc(aNamespace, aName, null);
|
168
|
6
|
IAttributeImpl impl = ((IElementImpl)getImpl_()).addAttribute(aNamespace, aName, aProvider, false, null);
|
169
|
6
|
IAnyTypeData res = desc.createUncheckedData(impl, null);
|
170
|
6
|
impl.setData(res);
|
171
|
6
|
return res;
|
172
|
|
}
|
173
|
|
|
174
|
0
|
public IAnyTypeData setAttribute_(String aNamespace, String aName, IAnyType anOverloadingType, String aTextContent) throws XmlException {
|
175
|
0
|
IAttrDesc desc = getType_().getAttributesModel().getAttrDesc(aNamespace, aName, null);
|
176
|
0
|
IAttributeImpl impl = ((IElementImpl)getImpl_()).addAttribute(aNamespace, aName, aTextContent, false, null);
|
177
|
0
|
IAnyTypeData res = desc.createData(impl, anOverloadingType);
|
178
|
0
|
impl.setData(res);
|
179
|
0
|
return res;
|
180
|
|
}
|
181
|
|
|
182
|
10
|
protected void removeAttributeData_(String aNamespace, String aName) throws XmlException {
|
183
|
10
|
((IElementImpl)getImpl_()).removeAttribute(aNamespace, aName);
|
184
|
|
}
|
185
|
|
|
186
|
0
|
public IAnyTypeData createElement_(String aNamespace, String aName, IAnyType anOverloadingType, final String aTextContent) throws XmlException {
|
187
|
0
|
ITextContentProvider p = null;
|
188
|
0
|
if (null != aTextContent) {
|
189
|
0
|
p = new ITextContentProvider() {
|
190
|
0
|
public String getTextContent_() {
|
191
|
0
|
return aTextContent;
|
192
|
|
}
|
193
|
|
};
|
194
|
|
}
|
195
|
0
|
return newElementData_(aNamespace, aName, anOverloadingType, p);
|
196
|
|
}
|
197
|
|
|
198
|
0
|
public IAnyTypeData createElement_(IElemRefOrDecl anElemRefOrDecl, IAnyType anOverloadingType, final String aTextContent) throws XmlException {
|
199
|
|
assert null != anElemRefOrDecl;
|
200
|
0
|
ITextContentProvider p = null;
|
201
|
0
|
if (null != aTextContent) {
|
202
|
0
|
p = new ITextContentProvider() {
|
203
|
0
|
public String getTextContent_() {
|
204
|
0
|
return aTextContent;
|
205
|
|
}
|
206
|
|
};
|
207
|
|
}
|
208
|
0
|
IElementImpl impl = ((IElementImpl)getImpl_()).addElement(anElemRefOrDecl.getNamespace(), anElemRefOrDecl.getName());
|
209
|
0
|
impl.setTextContent(p);
|
210
|
0
|
IAnyTypeData res = anElemRefOrDecl.createUncheckedData(impl, anOverloadingType);
|
211
|
0
|
impl.setData(res);
|
212
|
0
|
setOverloadingTypeAttribute(impl, anOverloadingType);
|
213
|
0
|
return res;
|
214
|
|
}
|
215
|
|
|
216
|
6
|
protected IAnyTypeData newElementData_(String aNamespace, String aName, IAnyType anOverloadingType, ITextContentProvider aProvider) throws XmlException {
|
217
|
6
|
IElementImpl impl = ((IElementImpl)getImpl_()).addElement(aNamespace, aName);
|
218
|
6
|
impl.setTextContent(aProvider);
|
219
|
6
|
IElemDesc desc = (IElemRefOrDecl)getType_().getElemDesc(aNamespace, aName);
|
220
|
6
|
if (null == desc) {
|
221
|
0
|
search:
|
222
|
|
{
|
223
|
0
|
for (Iterator i = getType_().getContentModel().iterElements();
|
224
|
0
|
i.hasNext(); ) {
|
225
|
0
|
IElemRefOrDecl erd = (IElemRefOrDecl)i.next();
|
226
|
0
|
for (Iterator j = erd.iterSubstitutes(); j.hasNext(); ) {
|
227
|
0
|
IElemRefOrDecl e = (IElemRefOrDecl)j.next();
|
228
|
0
|
if (aNamespace.equals(e.getNamespace()) && aName.equals(e.getName())) {
|
229
|
0
|
desc = e;
|
230
|
0
|
break search;
|
231
|
|
}
|
232
|
|
}
|
233
|
|
}
|
234
|
|
}
|
235
|
0
|
if (null == desc) {
|
236
|
0
|
Ref ref = new Ref(aNamespace, ISymbolspaces.ELEMENT, aName);
|
237
|
0
|
throw new XmlException(XmlMessages.unexpectedElement(ref, null));
|
238
|
|
}
|
239
|
|
}
|
240
|
6
|
IAnyTypeData res = desc.createUncheckedData(impl, anOverloadingType);
|
241
|
6
|
impl.setData(res);
|
242
|
6
|
setOverloadingTypeAttribute(impl, anOverloadingType);
|
243
|
6
|
return res;
|
244
|
|
}
|
245
|
|
|
246
|
6
|
private void setOverloadingTypeAttribute(IElementImpl anImpl, IAnyType anOverloadingType) {
|
247
|
|
//
|
248
|
|
// Add an attribute for the overloading type.
|
249
|
|
//
|
250
|
6
|
if (null != anOverloadingType) {
|
251
|
0
|
String xsiPrefix = anImpl.getPrefixForNamespace(INamespaces.XML_SCHEMA_INSTANCE, true);
|
252
|
0
|
String xsiTypeAttrQName = null;
|
253
|
0
|
if ("".equals(xsiPrefix)) {
|
254
|
0
|
xsiTypeAttrQName = "type";
|
255
|
|
} else {
|
256
|
0
|
xsiTypeAttrQName = xsiPrefix + ":type";
|
257
|
|
}
|
258
|
0
|
String typePrefix = anImpl.getPrefixForNamespace(anOverloadingType.getGlobalRef().getNamespace(), true);
|
259
|
0
|
String typeQName = null;
|
260
|
0
|
if ("".equals(typePrefix)) {
|
261
|
0
|
typeQName = anOverloadingType.getGlobalRef().getLocalPart();
|
262
|
|
} else {
|
263
|
0
|
typeQName = typePrefix + ":" + anOverloadingType.getGlobalRef().getLocalPart();
|
264
|
|
}
|
265
|
0
|
anImpl.addAttribute(INamespaces.XML_SCHEMA_INSTANCE, xsiTypeAttrQName, typeQName, true, null);
|
266
|
|
}
|
267
|
|
}
|
268
|
|
|
269
|
3
|
protected Iterator selectByXPath_(String anXPathMethodName) throws XmlException {
|
270
|
3
|
IXPathMethod m = (IXPathMethod)getType_().getXPathMethods().get(anXPathMethodName);
|
271
|
3
|
return new TransformingIterator(new FilterIterator(m.getXPath().select((IDataImpl)getImpl_()), new FilterIterator.ICondition() {
|
272
|
9
|
public boolean evaluate(Object anObject) {
|
273
|
9
|
return (anObject != null) && (anObject instanceof IDataImpl) && (null != ((IDataImpl)anObject).getData());
|
274
|
|
}
|
275
|
|
}), new TransformingIterator.ITransformation() {
|
276
|
9
|
public Object transform(Object anObject) {
|
277
|
|
// TODO remove
|
278
|
9
|
if (anObject == null) {
|
279
|
0
|
System.err.println("anObject was null !!!!!!!!!!!!!!!!!11");
|
280
|
|
}
|
281
|
9
|
return ((IDataImpl)anObject).getData();
|
282
|
|
}
|
283
|
|
});
|
284
|
|
}
|
285
|
|
|
286
|
1
|
protected IAnyTypeData fetchByXPath_(String anXPathMethodName) throws XmlException {
|
287
|
1
|
IAnyTypeData res = null;
|
288
|
1
|
Iterator i = selectByXPath_(anXPathMethodName);
|
289
|
1
|
if (i.hasNext()) {
|
290
|
1
|
res = (IAnyTypeData)i.next();
|
291
|
1
|
if (i.hasNext()) {
|
292
|
0
|
IXPathMethod m = (IXPathMethod)getType_().getXPathMethods().get(anXPathMethodName);
|
293
|
0
|
throw new XmlException(XmlMessages.xPathSelectedMoreThanOneNode(anXPathMethodName, m.getXPath().asString(), getImpl_()));
|
294
|
|
}
|
295
|
|
}
|
296
|
1
|
return res;
|
297
|
|
}
|
298
|
|
|
299
|
1
|
protected boolean testByXPath_(String anXPathMethodName) throws XmlException {
|
300
|
1
|
IXPathMethod m = (IXPathMethod)getType_().getXPathMethods().get(anXPathMethodName);
|
301
|
1
|
return m.getXPath().test((IDataImpl)getImpl_());
|
302
|
|
}
|
303
|
|
|
304
|
1
|
protected double numberByXPath_(String anXPathMethodName) throws XmlException {
|
305
|
1
|
IXPathMethod m = (IXPathMethod)getType_().getXPathMethods().get(anXPathMethodName);
|
306
|
1
|
return m.getXPath().number((IDataImpl)getImpl_());
|
307
|
|
}
|
308
|
|
|
309
|
1
|
protected String stringByXPath_(String anXPathMethodName) throws XmlException {
|
310
|
1
|
IXPathMethod m = (IXPathMethod)getType_().getXPathMethods().get(anXPathMethodName);
|
311
|
1
|
return m.getXPath().string((IDataImpl)getImpl_());
|
312
|
|
}
|
313
|
|
|
314
|
363
|
public final boolean equals(Object anObject) {
|
315
|
363
|
if (!(anObject instanceof IAnyTypeData)) {
|
316
|
0
|
return false;
|
317
|
|
}
|
318
|
363
|
IAnyTypeData data2 = (IAnyTypeData)anObject;
|
319
|
363
|
if (getType_().isSimple() && !getType_().isComplex() && data2.getType_().isSimple() && !data2.getType_().isComplex()) {
|
320
|
362
|
return ((ISimpleData)this).simpleStorageValueEquals((ISimpleData)anObject);
|
321
|
|
} else {
|
322
|
1
|
return this == anObject;
|
323
|
|
}
|
324
|
|
}
|
325
|
|
|
326
|
1083
|
public final int hashCode() {
|
327
|
1083
|
int hc;
|
328
|
1083
|
if (getType_().isComplex()) {
|
329
|
0
|
hc = super.hashCode();
|
330
|
|
} else {
|
331
|
1083
|
hc = ((ISimpleData)this).simpleStorageValueHashCode();
|
332
|
|
}
|
333
|
1083
|
return hc;
|
334
|
|
}
|
335
|
|
|
336
|
|
}
|
337
|
|
|