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.dom3.core;
|
11
|
|
|
12
|
|
import java.util.Iterator;
|
13
|
|
import java.util.ListIterator;
|
14
|
|
|
15
|
|
import org.jbind.util.collection.FilterIterator;
|
16
|
|
import org.jbind.util.collection.FilterListIterator;
|
17
|
|
import org.jbind.util.collection.TransformingIterator;
|
18
|
|
import org.jbind.util.collection.TransformingListIterator;
|
19
|
|
import org.jbind.xml.base.ILocation;
|
20
|
|
import org.jbind.xml.base.INamespaces;
|
21
|
|
import org.jbind.xml.core.base.ITextContentProvider;
|
22
|
|
import org.jbind.xml.core.bridge.IAttributeImpl;
|
23
|
|
import org.jbind.xml.core.bridge.IDataImplVisitor;
|
24
|
|
import org.jbind.xml.core.bridge.IDefaultAttrCreator;
|
25
|
|
import org.jbind.xml.core.content.IElemRefOrDecl;
|
26
|
|
import org.jbind.xml.core.data.IAnyTypeData;
|
27
|
|
import org.jbind.xml.core.data.IImpl;
|
28
|
|
import org.jbind.xml.dom3.types.IDomAttr;
|
29
|
|
import org.jbind.xml.dom3.types.IDomAttrsMap;
|
30
|
|
import org.jbind.xml.dom3.types.IDomDocument;
|
31
|
|
import org.jbind.xml.dom3.types.IDomElement;
|
32
|
|
import org.jbind.xml.dom3.types.IDomNamedNodeMap;
|
33
|
|
import org.jbind.xml.dom3.types.IDomNode;
|
34
|
|
import org.jbind.xml.dom3.types.IDomNodeList;
|
35
|
|
import org.jbind.xml.dom3.types.IDomText;
|
36
|
|
import org.jbind.xml.dom3.types.IDomVisitor;
|
37
|
|
import org.jbind.xml.msg.XmlException;
|
38
|
|
import org.w3c.dom.Attr;
|
39
|
|
import org.w3c.dom.NamedNodeMap;
|
40
|
|
import org.w3c.dom.Node;
|
41
|
|
import org.w3c.dom.NodeList;
|
42
|
|
|
43
|
|
public class DomElement extends DomAttrOrElm implements IDomElement {
|
44
|
|
|
45
|
|
private IDomAttrsMap myAttrs = null;
|
46
|
|
|
47
|
0
|
public DomElement(IDomDocument aDomDocument, String aNamespaceUri, String aQName) {
|
48
|
0
|
super(aDomDocument, aNamespaceUri, aQName);
|
49
|
0
|
myAttrs = new DomAttrsMap(this);
|
50
|
|
}
|
51
|
|
|
52
|
0
|
public DomElement(IDomDocument aDomDocument, String aName) {
|
53
|
0
|
super(aDomDocument, aName);
|
54
|
0
|
myAttrs = new DomAttrsMap(this);
|
55
|
|
}
|
56
|
|
|
57
|
0
|
protected DomElement(IDomElement aDomElement, boolean aDeep) {
|
58
|
0
|
super(aDomElement, aDeep);
|
59
|
0
|
myAttrs = new DomAttrsMap((IDomAttrsMap)aDomElement.getAttributes(), this);
|
60
|
|
}
|
61
|
|
|
62
|
0
|
public Node cloneNode(boolean aDeep) {
|
63
|
0
|
return new DomElement(this, aDeep);
|
64
|
|
}
|
65
|
|
|
66
|
0
|
protected IDomNodeList doCloneChildNodes(IDomNodeList aDomNodeList, boolean aDeep) {
|
67
|
0
|
return new DomNodeList(aDomNodeList, aDeep);
|
68
|
|
}
|
69
|
|
|
70
|
0
|
public String getAttribute(String aName) {
|
71
|
0
|
String res = null;
|
72
|
0
|
Node n = myAttrs.getNamedItem(aName);
|
73
|
0
|
if (null != n) {
|
74
|
0
|
res = n.getNodeValue();
|
75
|
|
} else {
|
76
|
0
|
res = "";
|
77
|
|
}
|
78
|
0
|
return res;
|
79
|
|
}
|
80
|
|
|
81
|
0
|
public String getAttributeNS(String aNamespaceUri, String aLocalName) {
|
82
|
0
|
String res = null;
|
83
|
0
|
Node n = myAttrs.getNamedItemNS(aNamespaceUri, aLocalName);
|
84
|
0
|
if (null != n) {
|
85
|
0
|
res = n.getNodeValue();
|
86
|
|
} else {
|
87
|
0
|
res = "";
|
88
|
|
}
|
89
|
0
|
return res;
|
90
|
|
}
|
91
|
|
|
92
|
0
|
public Attr getAttributeNode(String aName) {
|
93
|
0
|
return (Attr)myAttrs.getNamedItem(aName);
|
94
|
|
}
|
95
|
|
|
96
|
0
|
public Attr getAttributeNodeNS(String aNamespaceUri, String aLocalName) {
|
97
|
0
|
return (Attr)myAttrs.getNamedItemNS(aNamespaceUri, aLocalName);
|
98
|
|
}
|
99
|
|
|
100
|
0
|
public NamedNodeMap getAttributes() {
|
101
|
0
|
return myAttrs;
|
102
|
|
}
|
103
|
|
|
104
|
0
|
public short getNodeType() {
|
105
|
0
|
return ELEMENT_NODE;
|
106
|
|
}
|
107
|
|
|
108
|
0
|
public String getTagName() {
|
109
|
0
|
return getNodeName();
|
110
|
|
}
|
111
|
|
|
112
|
0
|
public boolean hasAttribute(String aName) {
|
113
|
0
|
return null != getAttributeNode(aName);
|
114
|
|
}
|
115
|
|
|
116
|
0
|
public boolean hasAttributeNS(String aNamespaceUri, String aLocalName) {
|
117
|
0
|
return null != getAttributeNodeNS(aNamespaceUri, aLocalName);
|
118
|
|
}
|
119
|
|
|
120
|
0
|
public boolean hasAttributes() {
|
121
|
0
|
return myAttrs.getLength() > 0;
|
122
|
|
}
|
123
|
|
|
124
|
0
|
public void removeAttribute(String aName) throws DomException {
|
125
|
0
|
myAttrs.removeNamedItem(aName);
|
126
|
|
}
|
127
|
|
|
128
|
0
|
public void removeAttributeNS(String aNamespaceUri, String aLocalName) throws DomException {
|
129
|
0
|
myAttrs.removeNamedItemNS(aNamespaceUri, aLocalName);
|
130
|
|
}
|
131
|
|
|
132
|
0
|
public Attr removeAttributeNode(Attr oldAttr) throws DomException {
|
133
|
0
|
myAttrs.removeNode(oldAttr);
|
134
|
0
|
return oldAttr;
|
135
|
|
}
|
136
|
|
|
137
|
0
|
public void setAttribute(String aName, String aValue) throws DomException {
|
138
|
0
|
IDomAttr a = (IDomAttr)myOwnerDocument.createAttribute(aName);
|
139
|
0
|
a.setPrefixToNamespaceMapping(getPrefixToNamespaceMapping());
|
140
|
0
|
a.setValue(aValue);
|
141
|
0
|
setAttributeNode(a);
|
142
|
|
}
|
143
|
|
|
144
|
0
|
public void setAttributeNS(String aNamespaceUri, String aQName, String aValue) throws DomException {
|
145
|
0
|
IDomAttr a = (IDomAttr)myOwnerDocument.createAttributeNS(aNamespaceUri, aQName);
|
146
|
0
|
a.setPrefixToNamespaceMapping(getPrefixToNamespaceMapping());
|
147
|
0
|
a.setValue(aValue);
|
148
|
0
|
setAttributeNodeNS(a);
|
149
|
|
}
|
150
|
|
|
151
|
0
|
public Attr setAttributeNode(Attr newAttr) throws DomException {
|
152
|
0
|
return (Attr)myAttrs.setNamedItem(newAttr);
|
153
|
|
}
|
154
|
|
|
155
|
0
|
public Attr setAttributeNodeNS(Attr newAttr) throws DomException {
|
156
|
0
|
return (Attr)myAttrs.setNamedItemNS(newAttr);
|
157
|
|
}
|
158
|
|
|
159
|
0
|
public String getNodeValue() throws DomException {
|
160
|
0
|
return null;
|
161
|
|
}
|
162
|
|
|
163
|
0
|
public void setNodeValue(String aString) throws DomException {
|
164
|
|
// The specification says that setting the node value of an element has no effect
|
165
|
|
}
|
166
|
|
|
167
|
0
|
public NodeList getElementsByTagName(String aName) {
|
168
|
0
|
ElementCollector v = new ElementCollector(myOwnerDocument, aName);
|
169
|
0
|
visitChildren(v);
|
170
|
0
|
return v.getElements();
|
171
|
|
}
|
172
|
|
|
173
|
0
|
public NodeList getElementsByTagNameNS(String aNamespaceUri, String aLocalName) {
|
174
|
0
|
ElementCollector v = new ElementCollector(myOwnerDocument, aNamespaceUri, aLocalName);
|
175
|
0
|
visitChildren(v);
|
176
|
0
|
return v.getElements();
|
177
|
|
}
|
178
|
|
|
179
|
0
|
protected void doBeforeAccept(IDomVisitor aDomVisitor) {
|
180
|
0
|
aDomVisitor.visitElementStart(this);
|
181
|
|
}
|
182
|
0
|
protected void doAfterAccept(IDomVisitor aDomVisitor) {
|
183
|
0
|
aDomVisitor.visitElementEnd(this);
|
184
|
|
}
|
185
|
|
|
186
|
0
|
public boolean isNil() {
|
187
|
0
|
boolean res = false;
|
188
|
0
|
IDomNode n = (IDomNode)getAttributes().getNamedItemNS(INamespaces.XML_SCHEMA_INSTANCE, "nil");
|
189
|
0
|
if (null != n) {
|
190
|
0
|
res = "true".equals(n.getTextContent());
|
191
|
|
}
|
192
|
0
|
return res;
|
193
|
|
}
|
194
|
|
|
195
|
0
|
public IAnyTypeData getAttributeData(String aNamespace, String aName) {
|
196
|
0
|
IAnyTypeData res = null;
|
197
|
0
|
IDomAttr attr = (IDomAttr)getAttributes().getNamedItemNS(aNamespace, aName);
|
198
|
0
|
if (null != attr) {
|
199
|
0
|
res = attr.getData();
|
200
|
|
}
|
201
|
0
|
return res;
|
202
|
|
}
|
203
|
|
|
204
|
0
|
public Iterator iterAttributes(final String aNamespace, final String aName) {
|
205
|
0
|
return new TransformingIterator(new FilterIterator(((IDomNamedNodeMap)getAttributes()).iterNodes(), new FilterIterator.ICondition() {
|
206
|
|
|
207
|
0
|
public boolean evaluate(Object anObject) {
|
208
|
0
|
boolean res = anObject instanceof IDomAttr;
|
209
|
0
|
if (res) {
|
210
|
0
|
res = false;
|
211
|
0
|
IDomAttr node = (IDomAttr)anObject;
|
212
|
0
|
if (null != node.getData()) {
|
213
|
0
|
res = ((null == aNamespace) || aNamespace.equals(node.getNamespaceURI())) && ((null == aName) || aName.equals(node.getLocalName()));
|
214
|
|
}
|
215
|
|
}
|
216
|
0
|
return res;
|
217
|
|
}
|
218
|
|
}), new TransformingIterator.ITransformation() {
|
219
|
0
|
public Object transform(Object anObject) {
|
220
|
0
|
return ((IDomAttr)anObject).getData();
|
221
|
|
}
|
222
|
|
});
|
223
|
|
}
|
224
|
|
|
225
|
0
|
public IAttributeImpl getAttributeImpl(String aNamespace, String aLocalName) {
|
226
|
0
|
return (IAttributeImpl)getAttributes().getNamedItemNS(aNamespace, aLocalName);
|
227
|
|
}
|
228
|
|
|
229
|
0
|
public Iterator iterAttributeImpls() {
|
230
|
0
|
return ((IDomNamedNodeMap)getAttributes()).iterNodes();
|
231
|
|
}
|
232
|
|
|
233
|
0
|
public IAttributeImpl addAttribute(String aNamespace, String aQName, String aValue, boolean anIsDefault, ILocation aLocation) {
|
234
|
0
|
IDomAttr a = (IDomAttr)myOwnerDocument.createAttributeNS(aNamespace, aQName);
|
235
|
0
|
a.setPrefixToNamespaceMapping(getPrefixToNamespaceMapping());
|
236
|
0
|
a.setValue(aValue);
|
237
|
0
|
a.setLocation(aLocation);
|
238
|
0
|
getAttributes().setNamedItemNS(a);
|
239
|
0
|
return a;
|
240
|
|
}
|
241
|
|
|
242
|
0
|
public IAttributeImpl addAttribute(String aNamespace, String aQName, ITextContentProvider aProvider, boolean anIsDefault, ILocation aLocation) {
|
243
|
0
|
IDomAttr a = (IDomAttr)myOwnerDocument.createAttributeNS(aNamespace, aQName);
|
244
|
0
|
a.setPrefixToNamespaceMapping(getPrefixToNamespaceMapping());
|
245
|
0
|
a.setTextContent(aProvider);
|
246
|
0
|
a.setLocation(aLocation);
|
247
|
0
|
getAttributes().setNamedItemNS(a);
|
248
|
0
|
return a;
|
249
|
|
}
|
250
|
|
|
251
|
0
|
public ListIterator iterElementData(final String aNamespace, final String aLocalName) {
|
252
|
0
|
return new TransformingListIterator(new FilterListIterator(myChildNodes.iterNodes(), new FilterListIterator.Test() {
|
253
|
|
|
254
|
0
|
public boolean test(Object anObject) {
|
255
|
0
|
boolean res = anObject instanceof IDomElement;
|
256
|
0
|
if (res) {
|
257
|
0
|
IDomElement node = (IDomElement)anObject;
|
258
|
0
|
res = false;
|
259
|
0
|
IAnyTypeData data = node.getData();
|
260
|
0
|
if (null != data) {
|
261
|
0
|
String namespace = null;
|
262
|
0
|
String name = null;
|
263
|
0
|
IElemRefOrDecl elem = data.getSubstitutionHead_();
|
264
|
0
|
if (null != elem) {
|
265
|
0
|
namespace = elem.getNamespace();
|
266
|
0
|
name = elem.getName();
|
267
|
|
} else {
|
268
|
0
|
namespace = node.getNamespace();
|
269
|
0
|
name = node.getLocalName();
|
270
|
0
|
if (null == name) {
|
271
|
0
|
name = node.getNodeName();
|
272
|
|
}
|
273
|
|
}
|
274
|
0
|
res = ((null == aNamespace) || aNamespace.equals(namespace)) && ((null == aLocalName) || aLocalName.equals(name));
|
275
|
|
}
|
276
|
|
}
|
277
|
0
|
return res;
|
278
|
|
}
|
279
|
|
}), new TransformingIterator.ITransformation() {
|
280
|
0
|
public Object transform(Object anObject) {
|
281
|
0
|
return ((IDomElement)anObject).getData();
|
282
|
|
}
|
283
|
|
});
|
284
|
|
}
|
285
|
|
|
286
|
0
|
public void setDefaultAttrCreator(IDefaultAttrCreator aCreator) {
|
287
|
0
|
myAttrs.setDefaultAttrCreator(aCreator);
|
288
|
|
}
|
289
|
|
|
290
|
0
|
public void setDefaultTextContent(String aString) {
|
291
|
0
|
myChildNodes.setDefaultTextContent(aString);
|
292
|
|
}
|
293
|
|
|
294
|
0
|
public boolean hasTextContent() {
|
295
|
0
|
boolean res = false;
|
296
|
0
|
for (Iterator i = myChildNodes.iterNodes(); !res && i.hasNext(); ) {
|
297
|
0
|
Object n = i.next();
|
298
|
0
|
res = (n instanceof IDomText) && ((IDomText)n).hasTextContent();
|
299
|
|
}
|
300
|
0
|
return res;
|
301
|
|
}
|
302
|
|
|
303
|
0
|
public void removeAttribute(String aNamespace, String aName) throws XmlException {
|
304
|
0
|
myAttrs.removeAttribute(aNamespace, aName);
|
305
|
|
}
|
306
|
|
|
307
|
0
|
public void accept(IDataImplVisitor aVisitor) throws XmlException {
|
308
|
0
|
aVisitor.visitElementImplStart(this);
|
309
|
0
|
for (Iterator i = iterChildren(); i.hasNext(); ) {
|
310
|
0
|
IImpl impl = (IImpl)i.next();
|
311
|
0
|
impl.accept(aVisitor);
|
312
|
|
}
|
313
|
0
|
aVisitor.visitElementImplEnd(this);
|
314
|
|
}
|
315
|
|
|
316
|
|
|
317
|
|
}
|
318
|
|
|