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
|
|
import java.util.Map;
|
15
|
|
|
16
|
|
import org.jbind.util.other.StrBuffer;
|
17
|
|
import org.jbind.xml.base.ILocation;
|
18
|
|
import org.jbind.xml.core.base.ITextContentMemento;
|
19
|
|
import org.jbind.xml.core.base.ITextContentProvider;
|
20
|
|
import org.jbind.xml.core.bridge.IElementImpl;
|
21
|
|
import org.jbind.xml.core.bridge.IImplContainer;
|
22
|
|
import org.jbind.xml.core.bridge.ITextImpl;
|
23
|
|
import org.jbind.xml.core.data.IImpl;
|
24
|
|
import org.jbind.xml.dom3.types.IDomComment;
|
25
|
|
import org.jbind.xml.dom3.types.IDomDocument;
|
26
|
|
import org.jbind.xml.dom3.types.IDomElement;
|
27
|
|
import org.jbind.xml.dom3.types.IDomNamedNodeMap;
|
28
|
|
import org.jbind.xml.dom3.types.IDomNode;
|
29
|
|
import org.jbind.xml.dom3.types.IDomNodeList;
|
30
|
|
import org.jbind.xml.dom3.types.IDomProcessingInstruction;
|
31
|
|
import org.jbind.xml.dom3.types.IDomText;
|
32
|
|
import org.jbind.xml.dom3.types.IDomVisitor;
|
33
|
|
import org.w3c.dom.Node;
|
34
|
|
import org.w3c.dom.NodeList;
|
35
|
|
|
36
|
|
public abstract class DomNode extends DomObject implements IDomNode {
|
37
|
|
|
38
|
|
/**
|
39
|
|
* Recursively collects the text content of the nodes in the specified node list.
|
40
|
|
*/
|
41
|
0
|
protected static final void collectTextContent(IDomNodeList aDomNodeList, StrBuffer aStrBuffer) {
|
42
|
0
|
for (Iterator i = aDomNodeList.iterNodes(); i.hasNext(); ) {
|
43
|
0
|
IDomNode node = (IDomNode)i.next();
|
44
|
0
|
if (!(node instanceof IDomProcessingInstruction) && !(node instanceof IDomComment)) {
|
45
|
0
|
aStrBuffer.append(node.getTextContent());
|
46
|
|
}
|
47
|
|
}
|
48
|
|
}
|
49
|
|
|
50
|
0
|
protected static final String collectTextContent(IDomNodeList aDomNodeList) {
|
51
|
0
|
StrBuffer sb = new StrBuffer();
|
52
|
0
|
collectTextContent(aDomNodeList, sb);
|
53
|
0
|
return sb.toString();
|
54
|
|
}
|
55
|
|
|
56
|
|
/**
|
57
|
|
* <i>(required)</i>.
|
58
|
|
* The parent of this node. All nodes, except <code>Attr</code>,
|
59
|
|
* <code>Document</code>, <code>DocumentFragment</code>,
|
60
|
|
* <code>Entity</code>, and <code>Notation</code> may have a parent.
|
61
|
|
* However, if a node has just been created and not yet added to the
|
62
|
|
* tree, or if it has been removed from the tree, this is
|
63
|
|
* <code>null</code>.
|
64
|
|
*/
|
65
|
|
protected DomNode myParentNode = null;
|
66
|
|
// Allow protected access because the getter method looses some type information.
|
67
|
|
|
68
|
|
/**
|
69
|
|
* A <code>NodeList</code> that contains all children of this node. If
|
70
|
|
* there are no children, this is a <code>NodeList</code> containing no
|
71
|
|
* nodes.
|
72
|
|
*/
|
73
|
|
protected IDomNodeList myChildNodes = null;
|
74
|
|
// Allow protected access because the getter method looses some type information.
|
75
|
|
|
76
|
|
private Map myPrefixToNamespaceMapping = null;
|
77
|
|
|
78
|
|
private ILocation myLocation = null;
|
79
|
|
|
80
|
0
|
public DomNode(IDomDocument aDocument) {
|
81
|
0
|
super(aDocument);
|
82
|
0
|
IDomDocument dd = aDocument;
|
83
|
0
|
if ((null == dd) && (this instanceof IDomDocument)) {
|
84
|
0
|
dd = (IDomDocument)this;
|
85
|
|
}
|
86
|
0
|
myChildNodes = new DomNodeList(this, dd, DomUtil.getAllowedChildNodeTypes(getNodeType()));
|
87
|
|
}
|
88
|
|
|
89
|
0
|
protected DomNode(IDomNode aDomNode, boolean aDeep) {
|
90
|
0
|
super(aDomNode, aDeep);
|
91
|
0
|
myParentNode = null;// Specification: the parent node of a clone is null.
|
92
|
0
|
myChildNodes = doCloneChildNodes((IDomNodeList)aDomNode.getChildNodes(), aDeep);
|
93
|
|
}
|
94
|
|
|
95
|
|
/**
|
96
|
|
* Hook method for cloning the child nodes.
|
97
|
|
*/
|
98
|
|
protected abstract IDomNodeList doCloneChildNodes(IDomNodeList aDomNodeList, boolean aDeep);
|
99
|
|
|
100
|
0
|
public Node getPreviousSibling() {
|
101
|
0
|
Node res = null;
|
102
|
0
|
if (null != myParentNode) {
|
103
|
0
|
res = myParentNode.myChildNodes.getPreviousNode(this);
|
104
|
|
}
|
105
|
0
|
return res;
|
106
|
|
}
|
107
|
|
|
108
|
0
|
public Node getNextSibling() {
|
109
|
0
|
Node res = null;
|
110
|
0
|
if (null != myParentNode) {
|
111
|
0
|
res = myParentNode.myChildNodes.getNextNode(this);
|
112
|
|
}
|
113
|
0
|
return res;
|
114
|
|
}
|
115
|
|
|
116
|
0
|
public boolean hasChildNodes() {
|
117
|
0
|
return myChildNodes.getLength() > 0;
|
118
|
|
}
|
119
|
|
|
120
|
0
|
public Node removeChild(Node oldChild) throws DomException {
|
121
|
0
|
checkMutability();
|
122
|
|
|
123
|
0
|
myChildNodes.removeNode(oldChild);
|
124
|
0
|
((DomNode)oldChild).myParentNode = null;
|
125
|
|
|
126
|
0
|
return oldChild;
|
127
|
|
}
|
128
|
|
|
129
|
0
|
public Node appendChild(Node newChild) throws DomException {
|
130
|
0
|
return insertChildAt(myChildNodes.getLength(), newChild);
|
131
|
|
}
|
132
|
|
|
133
|
0
|
public Node insertBefore(Node newChild, Node refChild) throws DomException {
|
134
|
0
|
Node res = null;
|
135
|
0
|
if (null == refChild) {
|
136
|
0
|
res = appendChild(newChild);
|
137
|
|
} else {
|
138
|
0
|
int idx = myChildNodes.getIndex(refChild);
|
139
|
0
|
res = insertChildAt(idx, newChild);
|
140
|
|
}
|
141
|
0
|
return res;
|
142
|
|
}
|
143
|
|
|
144
|
0
|
public Node insertAfter(Node newChild, Node refChild) throws DomException {
|
145
|
0
|
Node res = null;
|
146
|
0
|
if (null == refChild) {
|
147
|
0
|
res = appendChild(newChild);
|
148
|
|
} else {
|
149
|
0
|
int idx = myChildNodes.getIndex(refChild);
|
150
|
0
|
res = insertChildAt(idx + 1, newChild);
|
151
|
|
}
|
152
|
0
|
return res;
|
153
|
|
}
|
154
|
|
|
155
|
0
|
public Node replaceChild(Node newChild, Node oldChild) throws DomException {
|
156
|
0
|
insertBefore(newChild, oldChild);
|
157
|
0
|
removeChild(oldChild);
|
158
|
0
|
return oldChild;
|
159
|
|
}
|
160
|
|
|
161
|
|
|
162
|
|
|
163
|
0
|
private Node insertChildAt(int anIndex, Node newChild) throws DomException {
|
164
|
|
assert null != newChild : "child must not be null";
|
165
|
|
|
166
|
0
|
if (newChild instanceof DomDocumentFragment) {
|
167
|
0
|
for (int i = 0; i < newChild.getChildNodes().getLength(); i++) {
|
168
|
0
|
insertChildAt(anIndex + i, newChild.getChildNodes().item(i));
|
169
|
|
}
|
170
|
|
|
171
|
|
} else {
|
172
|
0
|
checkMutability();
|
173
|
0
|
checkOwnerDocument((DomObject)newChild);
|
174
|
|
|
175
|
|
// check if this node or one of its parents is the newChild
|
176
|
0
|
Node n;
|
177
|
0
|
for (n = this; (null != n) && (newChild != n); n = n.getParentNode());
|
178
|
0
|
if (null != n) {
|
179
|
0
|
throw new DomException(DomException.HIERARCHY_REQUEST_ERR, "tried to append ancestor as child");
|
180
|
|
}
|
181
|
|
|
182
|
0
|
if (null != newChild.getParentNode()) {
|
183
|
0
|
newChild.getParentNode().removeChild(newChild);
|
184
|
|
}
|
185
|
|
|
186
|
0
|
myChildNodes.insertNodeAt(anIndex, newChild);
|
187
|
0
|
((DomNode)newChild).myParentNode = this;
|
188
|
|
}
|
189
|
|
|
190
|
0
|
return newChild;
|
191
|
|
}
|
192
|
|
|
193
|
0
|
public NodeList getChildNodes() {
|
194
|
0
|
return myChildNodes;
|
195
|
|
}
|
196
|
|
|
197
|
0
|
public void setChildNodes(IDomNodeList aNodeList) {
|
198
|
0
|
myChildNodes = aNodeList;
|
199
|
|
}
|
200
|
|
|
201
|
0
|
public Node getParentNode() {
|
202
|
0
|
return myParentNode;
|
203
|
|
}
|
204
|
|
|
205
|
0
|
public Node getFirstChild() {
|
206
|
0
|
return myChildNodes.item(0);
|
207
|
|
}
|
208
|
|
|
209
|
0
|
public Node getLastChild() {
|
210
|
0
|
return myChildNodes.item(myChildNodes.getLength() - 1);
|
211
|
|
}
|
212
|
|
|
213
|
0
|
protected Node getChildByClass(Class aClass) {
|
214
|
0
|
return myChildNodes.getNodeByClass(aClass);
|
215
|
|
}
|
216
|
|
|
217
|
|
/**
|
218
|
|
* Puts all <code>Text</code> nodes in the full depth of the sub-tree
|
219
|
|
* underneath this <code>Node</code>, including attribute nodes, into a
|
220
|
|
* "normal" form where only structure (e.g., elements, comments,
|
221
|
|
* processing instructions, CDATA sections, and entity references)
|
222
|
|
* separates <code>Text</code> nodes, i.e., there are neither adjacent
|
223
|
|
* <code>Text</code> nodes nor empty <code>Text</code> nodes. This can
|
224
|
|
* be used to ensure that the DOM view of a document is the same as if
|
225
|
|
* it were saved and re-loaded, and is useful when operations (such as
|
226
|
|
* XPointer lookups) that depend on a particular document tree
|
227
|
|
* structure are to be used.In cases where the document contains
|
228
|
|
* <code>CDATASections</code>, the normalize operation alone may not be
|
229
|
|
* sufficient, since XPointers do not differentiate between
|
230
|
|
* <code>Text</code> nodes and <code>CDATASection</code> nodes.
|
231
|
|
*/
|
232
|
0
|
public void normalize() {
|
233
|
|
// TODO
|
234
|
|
assert false : "nyi";
|
235
|
|
}
|
236
|
|
|
237
|
0
|
public boolean isSupported(String aFeature, String aVersion) {
|
238
|
|
// TODO
|
239
|
|
assert false : "nyi";
|
240
|
0
|
return false;
|
241
|
|
}
|
242
|
|
|
243
|
0
|
public String getBaseURI() {
|
244
|
|
// TODO
|
245
|
|
assert false : "nyi";
|
246
|
0
|
return null;
|
247
|
|
}
|
248
|
|
|
249
|
0
|
public short compareTreePosition(Node other) {
|
250
|
|
// TODO
|
251
|
|
assert false : "nyi";
|
252
|
0
|
return 0;
|
253
|
|
}
|
254
|
|
|
255
|
0
|
public boolean isSameNode(Node aNode) {
|
256
|
0
|
return equals(aNode);
|
257
|
|
}
|
258
|
|
|
259
|
0
|
public boolean isEqualNode(Node aNode, boolean aDeep) {
|
260
|
|
// TODO
|
261
|
|
assert false : "nyi";
|
262
|
0
|
return false;
|
263
|
|
}
|
264
|
|
|
265
|
0
|
public String lookupNamespacePrefix(String aNamespaceUri) {
|
266
|
|
// TODO
|
267
|
|
assert false : "nyi";
|
268
|
0
|
return null;
|
269
|
|
}
|
270
|
|
|
271
|
0
|
public String getNamespaceForPrefix(String aPrefix) {
|
272
|
0
|
return (String)myPrefixToNamespaceMapping.get(aPrefix);
|
273
|
|
}
|
274
|
|
|
275
|
0
|
public String getPrefixForNamespace(String aNamespace, boolean aCreate) {
|
276
|
0
|
for (Iterator i = myPrefixToNamespaceMapping.entrySet().iterator();
|
277
|
0
|
i.hasNext(); ) {
|
278
|
0
|
Map.Entry me = (Map.Entry)i.next();
|
279
|
0
|
if (aNamespace.equals(me.getValue())) {
|
280
|
0
|
return (String)me.getKey();
|
281
|
|
}
|
282
|
|
}
|
283
|
0
|
if (!aCreate) {
|
284
|
0
|
return null;
|
285
|
|
}
|
286
|
0
|
int i = 0;
|
287
|
0
|
while (true) {
|
288
|
0
|
String p = "p" + i++;
|
289
|
0
|
if (!myPrefixToNamespaceMapping.containsKey(p)) {
|
290
|
0
|
myPrefixToNamespaceMapping.put(p, aNamespace);
|
291
|
0
|
return p;
|
292
|
|
}
|
293
|
|
}
|
294
|
|
}
|
295
|
|
|
296
|
0
|
public String lookupNamespaceURI(String aPrefix) {
|
297
|
0
|
return getNamespaceForPrefix(aPrefix);
|
298
|
|
}
|
299
|
|
|
300
|
|
/**
|
301
|
|
* Gets the parent or owner element.
|
302
|
|
*/
|
303
|
0
|
protected Node getAncestor() {
|
304
|
0
|
return getParentNode();
|
305
|
|
}
|
306
|
|
|
307
|
0
|
public Node getInterface(String aFeature) {
|
308
|
|
// TODO
|
309
|
|
assert false : "nyi";
|
310
|
0
|
return null;
|
311
|
|
}
|
312
|
|
|
313
|
0
|
public Object getUserData(String aKey) {
|
314
|
|
// TODO
|
315
|
|
assert false : "nyi";
|
316
|
0
|
return null;
|
317
|
|
}
|
318
|
|
|
319
|
|
// public Object setUserData(String aKey, Object aData, UserDataHandler aHandler) {
|
320
|
|
// // TODO
|
321
|
|
// assert false : "nyi";
|
322
|
|
// return null;
|
323
|
|
// }
|
324
|
|
//
|
325
|
0
|
public final void accept(IDomVisitor aVisitor) {
|
326
|
0
|
doBeforeAccept(aVisitor);
|
327
|
0
|
visitAttributes(aVisitor);
|
328
|
0
|
visitChildren(aVisitor);
|
329
|
0
|
doAfterAccept(aVisitor);
|
330
|
|
}
|
331
|
|
|
332
|
|
protected abstract void doBeforeAccept(IDomVisitor aVisitor);
|
333
|
|
protected abstract void doAfterAccept(IDomVisitor aVisitor);
|
334
|
|
|
335
|
0
|
protected final void visitChildren(IDomVisitor aVisitor) {
|
336
|
0
|
for (Iterator i = myChildNodes.iterNodes(); i.hasNext(); ) {
|
337
|
0
|
IDomNode node = (IDomNode)i.next();
|
338
|
0
|
node.accept(aVisitor);
|
339
|
|
}
|
340
|
|
}
|
341
|
|
|
342
|
0
|
private void visitAttributes(IDomVisitor aVisitor) {
|
343
|
0
|
IDomNamedNodeMap nnm = (IDomNamedNodeMap)getAttributes();
|
344
|
0
|
if (null != nnm) {
|
345
|
0
|
for (Iterator i = nnm.iterNodes(); i.hasNext(); ) {
|
346
|
0
|
IDomNode n = (IDomNode)i.next();
|
347
|
0
|
n.accept(aVisitor);
|
348
|
|
}
|
349
|
|
}
|
350
|
|
}
|
351
|
|
|
352
|
0
|
public ILocation getLocation() {
|
353
|
0
|
return myLocation;
|
354
|
|
}
|
355
|
|
|
356
|
0
|
public void setLocation(ILocation aLocation) {
|
357
|
0
|
myLocation = aLocation;
|
358
|
|
}
|
359
|
|
|
360
|
|
//
|
361
|
|
// Implementation of IDataContainer
|
362
|
|
//
|
363
|
|
|
364
|
0
|
public ITextImpl addText(String aText, ILocation aLocation) {
|
365
|
0
|
IDomText text = (IDomText)myOwnerDocument.createTextNode(aText);
|
366
|
0
|
text.setLocation(aLocation);
|
367
|
0
|
appendChild(text);
|
368
|
0
|
return text;
|
369
|
|
}
|
370
|
|
|
371
|
0
|
public IElementImpl addElement(String aNamespace, String aQName, Map aPrefixMappings, ILocation aLocation) {
|
372
|
0
|
IDomElement element = (IDomElement)myOwnerDocument.createElementNS(aNamespace, aQName);
|
373
|
0
|
element.setPrefixToNamespaceMapping(aPrefixMappings);
|
374
|
0
|
element.setLocation(aLocation);
|
375
|
0
|
appendChild(element);
|
376
|
0
|
return element;
|
377
|
|
}
|
378
|
|
|
379
|
0
|
public IElementImpl addElement(String aNamespace, String aQName) {
|
380
|
0
|
return addElement(aNamespace, aQName, myPrefixToNamespaceMapping, null);
|
381
|
|
}
|
382
|
|
|
383
|
0
|
public IImplContainer getParentImpl() {
|
384
|
0
|
return (IImplContainer)myParentNode;
|
385
|
|
}
|
386
|
|
|
387
|
0
|
public void setTextContent(ITextContentProvider aTextContentProvider) {
|
388
|
|
// This method is overloaded in DomCharacterData and DomProcessingInstruction
|
389
|
0
|
myChildNodes.removeAll();
|
390
|
0
|
if (null != aTextContentProvider) {
|
391
|
0
|
DomText text = (DomText)myOwnerDocument.createTextNode(aTextContentProvider);
|
392
|
0
|
text.myParentNode = this;
|
393
|
0
|
myChildNodes.add(text);
|
394
|
|
}
|
395
|
|
}
|
396
|
|
|
397
|
0
|
public void setTextContent(String aString) {
|
398
|
|
// This method is overloaded in DomCharacterData and DomProcessingInstruction
|
399
|
0
|
DomText text = (DomText)myOwnerDocument.createTextNode(aString);
|
400
|
0
|
text.myParentNode = this;
|
401
|
0
|
myChildNodes.removeAll();
|
402
|
0
|
myChildNodes.add(text);
|
403
|
|
}
|
404
|
|
|
405
|
0
|
public ITextContentMemento saveTextContent() {
|
406
|
0
|
return new NodeListMemento(myChildNodes.cloneNodeList());
|
407
|
|
}
|
408
|
|
|
409
|
0
|
public void restoreTextContent(ITextContentMemento aMemento) {
|
410
|
0
|
myChildNodes.removeAll();
|
411
|
0
|
myChildNodes.addAll(((NodeListMemento)aMemento).getList());
|
412
|
|
}
|
413
|
|
|
414
|
0
|
public void setPrefixToNamespaceMapping(Map aMap) {
|
415
|
0
|
myPrefixToNamespaceMapping = aMap;
|
416
|
|
}
|
417
|
|
|
418
|
0
|
public ListIterator iterChildren() {
|
419
|
0
|
return myChildNodes.iterNodes();
|
420
|
|
}
|
421
|
|
|
422
|
0
|
public int getNbChildren() {
|
423
|
0
|
return myChildNodes.getLength();
|
424
|
|
}
|
425
|
|
|
426
|
0
|
public int getNonDefaultedNbChildren() {
|
427
|
0
|
return myChildNodes.getNonDefaultedLength();
|
428
|
|
}
|
429
|
|
|
430
|
0
|
public IImpl getChild(int anIndex) {
|
431
|
0
|
return (IImpl)myChildNodes.item(anIndex);
|
432
|
|
}
|
433
|
|
|
434
|
0
|
public Map getPrefixToNamespaceMapping() {
|
435
|
0
|
return myPrefixToNamespaceMapping;
|
436
|
|
}
|
437
|
|
}
|
438
|
|
|