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.schema.element;
|
11
|
|
|
12
|
|
import java.util.Iterator;
|
13
|
|
import java.util.Map;
|
14
|
|
|
15
|
|
import org.jbind.util.collection.FilterListIterator;
|
16
|
|
import org.jbind.util.other.StrBuffer;
|
17
|
|
import org.jbind.xml.base.IAttribute;
|
18
|
|
import org.jbind.xml.base.IAttributes;
|
19
|
|
import org.jbind.xml.base.ILocation;
|
20
|
|
import org.jbind.xml.base.INamespaces;
|
21
|
|
import org.jbind.xml.base.IQName;
|
22
|
|
import org.jbind.xml.base.IRef;
|
23
|
|
import org.jbind.xml.base.ISymbolspace;
|
24
|
|
import org.jbind.xml.base.Ref;
|
25
|
|
import org.jbind.xml.core.cmp.IAnnotation;
|
26
|
|
import org.jbind.xml.core.cmp.IAppInfoVisitor;
|
27
|
|
import org.jbind.xml.core.cmp.ISchema;
|
28
|
|
import org.jbind.xml.msg.IConstraintViolations;
|
29
|
|
import org.jbind.xml.msg.XmlException;
|
30
|
|
import org.jbind.xml.msg.XmlMessages;
|
31
|
|
import org.jbind.xml.schema.instantiation.IComponentJobHelper;
|
32
|
|
import org.jbind.xml.schema.instantiation.IElemValHelper;
|
33
|
|
|
34
|
|
public abstract class Element extends Parent implements IElement {
|
35
|
|
|
36
|
|
private ILocation myLocation = null;
|
37
|
|
|
38
|
|
/**
|
39
|
|
* Maps prefixes to namespace uris.
|
40
|
|
*/
|
41
|
|
private Map myPrefixToNamespaceMapping = null;
|
42
|
|
|
43
|
|
private IElement myParent = null;
|
44
|
|
private IQName myQName = null;
|
45
|
|
private IAttributes myAttributes = null;
|
46
|
|
|
47
|
|
private ISchemaDocument mySchemaDocument = null;
|
48
|
|
|
49
|
6540
|
public Element(CreationParams aCreationParams) {
|
50
|
6540
|
myLocation = aCreationParams.myLocation;
|
51
|
6540
|
myParent = aCreationParams.myParent;
|
52
|
6540
|
myPrefixToNamespaceMapping = aCreationParams.myPrefixMappings;
|
53
|
6540
|
myQName = aCreationParams.myQName;
|
54
|
6540
|
mySchemaDocument = aCreationParams.mySchemaDocument;
|
55
|
6540
|
myAttributes = new Attributes();
|
56
|
|
}
|
57
|
|
|
58
|
8816
|
public boolean isTopLevelComponent() {
|
59
|
8816
|
Object parent = getParent_();
|
60
|
8816
|
return (parent instanceof SchemaElement) || (parent instanceof Redefine) || (parent instanceof ModelGroupDeclaration);
|
61
|
|
}
|
62
|
|
|
63
|
14074
|
public ISchemaDocument getSchemaDocument() {
|
64
|
14074
|
return mySchemaDocument;
|
65
|
|
}
|
66
|
|
|
67
|
5685
|
public final IElement createChild(CreationParams aCreationParams, IConstraintViolations aViolations) throws XmlException {
|
68
|
5685
|
IElement res = null;
|
69
|
5685
|
try {
|
70
|
5685
|
res = doCreateChild(aCreationParams);
|
71
|
5681
|
if (null == res) {
|
72
|
3
|
aViolations.add(XmlMessages.unexpectedElement(aCreationParams.myQName, aCreationParams.myLocation));
|
73
|
|
}
|
74
|
|
} catch (XmlException e) {
|
75
|
4
|
aViolations.add(e.getXmlMessage());
|
76
|
|
}
|
77
|
5685
|
if (null == res) {
|
78
|
7
|
res = new GenericSchemaElement(aCreationParams);
|
79
|
|
}
|
80
|
5685
|
return res;
|
81
|
|
}
|
82
|
|
|
83
|
7304
|
public final void addAttribute(ACParams anACParams) throws XmlException {
|
84
|
7304
|
IAttribute a = doCreateAttribute(anACParams);
|
85
|
7299
|
if (null == a) {
|
86
|
1
|
throw new XmlException(XmlMessages.unexpectedAttribute(anACParams.qName, this));
|
87
|
|
} else {
|
88
|
7298
|
getAttributes().addAttribute(a);
|
89
|
|
}
|
90
|
|
}
|
91
|
|
|
92
|
|
/**
|
93
|
|
* Hook method to create an attribute.
|
94
|
|
*
|
95
|
|
* @param anACParams <i>(required)</i>.
|
96
|
|
* @return <i>(optional)</i>. The created attribute.
|
97
|
|
* @throws XmlException Thrown if the creation was not successful.
|
98
|
|
*/
|
99
|
1
|
protected IAttribute doCreateAttribute(ACParams anACParams) throws XmlException {
|
100
|
1
|
return null;
|
101
|
|
}
|
102
|
|
|
103
|
|
/**
|
104
|
|
* Hook method to create a child element.
|
105
|
|
*
|
106
|
|
* @param aCreationParams <i>(required)</i>.
|
107
|
|
* @return <i>(optional)</i>. The created element.
|
108
|
|
* @throws XmlException Raised if the creation was not successful.
|
109
|
|
*/
|
110
|
3
|
protected IElement doCreateChild(CreationParams aCreationParams) throws XmlException {
|
111
|
3
|
return null;
|
112
|
|
}
|
113
|
|
|
114
|
|
|
115
|
5928
|
public void validateElement(IElemValHelper aHelper, IConstraintViolations aViolations) {}
|
116
|
|
|
117
|
|
|
118
|
14074
|
protected SchemaElement getSchemaElement() {
|
119
|
14074
|
return getSchemaDocument().getSchemaElement();
|
120
|
|
}
|
121
|
|
|
122
|
798
|
protected String getTargetNamespace() {
|
123
|
798
|
return getSchemaElement().getTargetNamespace();
|
124
|
|
}
|
125
|
|
|
126
|
0
|
protected Boolean getBoolean(String anAttributeName) {
|
127
|
0
|
Boolean res = null;
|
128
|
0
|
String s = getAttributes().getStringValue(anAttributeName);
|
129
|
0
|
if (null != s) {
|
130
|
0
|
res = new Boolean(s);
|
131
|
|
}
|
132
|
0
|
return res;
|
133
|
|
}
|
134
|
|
|
135
|
25
|
protected IRef createGlobalRef(IQName aQName, ISymbolspace aSymbolSpace) {
|
136
|
25
|
IRef res = null;
|
137
|
25
|
if (null != aQName) {
|
138
|
25
|
res = new Ref(aQName.getNamespace(), aSymbolSpace, aQName.getLocalPart());
|
139
|
|
}
|
140
|
25
|
return res;
|
141
|
|
}
|
142
|
|
|
143
|
4867
|
public IElement getSubElement(Class aClass, Object aMessage, IConstraintViolations aViolations) {
|
144
|
4867
|
IElement res = null;
|
145
|
4867
|
res = (IElement)getChildByClass(aClass, aMessage, aViolations);
|
146
|
4867
|
return res;
|
147
|
|
}
|
148
|
|
|
149
|
12537
|
public Iterator iterChildrenByClass(final Class aClass) {
|
150
|
12537
|
return new FilterListIterator(iterChildren(), new FilterListIterator.Test() {
|
151
|
20543
|
public boolean test(Object anObject) {
|
152
|
20543
|
return aClass.isInstance(anObject);
|
153
|
|
}
|
154
|
|
});
|
155
|
|
}
|
156
|
|
|
157
|
4867
|
public IElement getChildByClass(Class aClass, Object aMessage, IConstraintViolations aViolations) {
|
158
|
4867
|
IElement res = null;
|
159
|
4867
|
Iterator i = iterChildrenByClass(aClass);
|
160
|
4867
|
if (i.hasNext()) {
|
161
|
1336
|
res = (IElement)i.next();
|
162
|
1336
|
if (i.hasNext()) {
|
163
|
9
|
aViolations.add(XmlMessages.childElementNotUnique(aMessage, this));
|
164
|
|
}
|
165
|
|
}
|
166
|
4867
|
return res;
|
167
|
|
}
|
168
|
|
|
169
|
1190
|
public ISchema getSchema() {
|
170
|
1190
|
return getSchemaElement().getSchema();
|
171
|
|
}
|
172
|
|
|
173
|
506
|
public String getStringBindingAttribute(String aName) {
|
174
|
506
|
IAttribute a = getBindingAttribute(aName);
|
175
|
506
|
return (a != null) ? a.getStringValue() : null;
|
176
|
|
}
|
177
|
|
|
178
|
22291
|
public IAttribute getBindingAttribute(String aName) {
|
179
|
22291
|
IAttribute res = null;
|
180
|
22291
|
for (IElement e = this; (res == null) && (e != null);
|
181
|
|
e = (IElement)e.getParent_()) {
|
182
|
81707
|
res = (IAttribute)e.getAttributes().getAttribute(INamespaces.JBIND, aName);
|
183
|
|
}
|
184
|
22291
|
return res;
|
185
|
|
}
|
186
|
|
|
187
|
97
|
public String getLocalStringBindingAttribute(String aName) {
|
188
|
97
|
String res = null;
|
189
|
97
|
IAttribute a = getLocalBindingAttribute(aName);
|
190
|
97
|
if (a != null) {
|
191
|
4
|
res = a.getStringValue();
|
192
|
|
}
|
193
|
97
|
return res;
|
194
|
|
}
|
195
|
|
|
196
|
18106
|
public IAttribute getLocalBindingAttribute(String aName) {
|
197
|
18106
|
IAttribute res = (IAttribute)getAttributes().getAttribute(INamespaces.JBIND, aName);
|
198
|
18106
|
return res;
|
199
|
|
}
|
200
|
|
|
201
|
0
|
public String getDefaultedLocalStringBindingAttribute(String aName) {
|
202
|
0
|
String res = null;
|
203
|
0
|
IAttribute a = getDefaultedLocalBindingAttribute(aName);
|
204
|
0
|
if (a != null) {
|
205
|
0
|
res = a.getStringValue();
|
206
|
|
}
|
207
|
0
|
return res;
|
208
|
|
}
|
209
|
|
|
210
|
6189
|
public IAttribute getDefaultedLocalBindingAttribute(String aName) {
|
211
|
6189
|
IAttribute res = getLocalBindingAttribute(aName);
|
212
|
6189
|
if (null == res) {
|
213
|
6189
|
res = getSchemaElement().getBindingAttribute(aName);
|
214
|
|
}
|
215
|
6189
|
return res;
|
216
|
|
}
|
217
|
|
|
218
|
0
|
public IComponentJobHelper getParentHelper() {
|
219
|
0
|
IComponentJobHelper res = null;
|
220
|
0
|
for (IElement e = (IElement)getParent_(); e != null;
|
221
|
|
e = (IElement)e.getParent_()) {
|
222
|
0
|
if (e instanceof IComponentJobHelper) {
|
223
|
0
|
res = (IComponentJobHelper)e;
|
224
|
0
|
break;
|
225
|
|
}
|
226
|
|
}
|
227
|
0
|
return res;
|
228
|
|
}
|
229
|
|
|
230
|
|
// method copied from base class
|
231
|
|
|
232
|
93164
|
public IElement getParent_() {
|
233
|
93164
|
return myParent;
|
234
|
|
}
|
235
|
|
|
236
|
0
|
public void setParent_(IElement anElement) {
|
237
|
0
|
myParent = anElement;
|
238
|
|
}
|
239
|
|
|
240
|
0
|
public String getNamespaceUri() {
|
241
|
0
|
return myQName.getNamespace();
|
242
|
|
}
|
243
|
|
|
244
|
0
|
public String getPrefix() {
|
245
|
0
|
return myQName.getPrefix();
|
246
|
|
}
|
247
|
|
|
248
|
0
|
public String getLocalName() {
|
249
|
0
|
return myQName.getLocalPart();
|
250
|
|
}
|
251
|
|
|
252
|
0
|
public IQName getQName() {
|
253
|
0
|
return myQName;
|
254
|
|
}
|
255
|
|
|
256
|
113597
|
public IAttributes getAttributes() {
|
257
|
113597
|
return myAttributes;
|
258
|
|
}
|
259
|
|
|
260
|
362
|
public ILocation getLocation() {
|
261
|
362
|
return myLocation;
|
262
|
|
}
|
263
|
|
|
264
|
0
|
public void setLocation(ILocation aLocation) {
|
265
|
0
|
myLocation = aLocation;
|
266
|
|
}
|
267
|
|
|
268
|
0
|
protected String getNamespace(String aPrefix) {
|
269
|
0
|
String uri = (String)myPrefixToNamespaceMapping.get(aPrefix);
|
270
|
0
|
return uri;
|
271
|
|
}
|
272
|
|
|
273
|
478
|
public Map getPrefixToNamespaceMapping() {
|
274
|
478
|
return myPrefixToNamespaceMapping;
|
275
|
|
}
|
276
|
|
|
277
|
0
|
public String toString() {
|
278
|
0
|
StrBuffer sb = new StrBuffer();
|
279
|
0
|
sb.append("<").append(myQName);
|
280
|
0
|
for (Iterator i = getAttributes().iterAttributes(); i.hasNext(); ) {
|
281
|
0
|
sb.append(' ').append(i.next());
|
282
|
|
}
|
283
|
0
|
if (hasChildren()) {
|
284
|
0
|
sb.append(">\n");
|
285
|
0
|
for (Iterator i = iterChildren(); i.hasNext(); ) {
|
286
|
0
|
i.next().toString();
|
287
|
|
}
|
288
|
0
|
sb.append("</").append(myQName).append(">\n");
|
289
|
|
} else {
|
290
|
0
|
sb.append("/>\n");
|
291
|
|
}
|
292
|
0
|
return sb.toString();
|
293
|
|
}
|
294
|
|
|
295
|
|
// method is overloaded in class Annotated
|
296
|
0
|
public IAnnotation getAnnotation() {
|
297
|
0
|
return null;
|
298
|
|
}
|
299
|
|
|
300
|
0
|
public void acceptAppInfoVisitor(IAppInfoVisitor aVisitor) {
|
301
|
0
|
aVisitor.startVisitXmlElement(this);
|
302
|
0
|
visitChildren(aVisitor);
|
303
|
0
|
aVisitor.endVisitXmlEelement(this);
|
304
|
|
}
|
305
|
|
|
306
|
|
}
|
307
|
|
|