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.ArrayList;
|
13
|
|
import java.util.Collections;
|
14
|
|
import java.util.Iterator;
|
15
|
|
import java.util.List;
|
16
|
|
import java.util.ListIterator;
|
17
|
|
|
18
|
|
import org.jbind.util.collection.FilterIterator;
|
19
|
|
import org.jbind.util.collection.FilterListIterator;
|
20
|
|
import org.jbind.xml.dom3.types.IDomDocument;
|
21
|
|
import org.jbind.xml.dom3.types.IDomNodeList;
|
22
|
|
import org.w3c.dom.Node;
|
23
|
|
|
24
|
|
public class DomNodeList extends DomObject implements IDomNodeList {
|
25
|
|
|
26
|
|
private ArrayList myNodes = new ArrayList();
|
27
|
|
|
28
|
|
private DomNode myOwnerNode = null;
|
29
|
|
private short[] myAllowedChildNodeTypes;
|
30
|
|
private List myDefaultNodes = Collections.EMPTY_LIST;
|
31
|
|
|
32
|
0
|
public DomNodeList(DomNode anOwnerNode, IDomDocument aDomDocument, short[] anAllowedChildNodeTypes) {
|
33
|
0
|
super(aDomDocument);
|
34
|
0
|
myOwnerNode = anOwnerNode;
|
35
|
0
|
myAllowedChildNodeTypes = anAllowedChildNodeTypes;
|
36
|
|
}
|
37
|
|
|
38
|
0
|
public DomNodeList(IDomNodeList aDomNodeList, boolean aDeep) {
|
39
|
0
|
super(aDomNodeList, aDeep);
|
40
|
0
|
for (int i = 0; i < aDomNodeList.getLength(); i++) {
|
41
|
0
|
Node n = aDomNodeList.item(i);
|
42
|
0
|
if (aDeep) {
|
43
|
0
|
myNodes.add(n.cloneNode(aDeep));
|
44
|
|
} else {
|
45
|
0
|
myNodes.add(n);
|
46
|
|
}
|
47
|
|
}
|
48
|
|
}
|
49
|
|
|
50
|
0
|
public int getLength() {
|
51
|
0
|
return getDefaultedNodeList().size();
|
52
|
|
}
|
53
|
|
|
54
|
0
|
public int getNonDefaultedLength() {
|
55
|
0
|
return myNodes.size();
|
56
|
|
}
|
57
|
|
|
58
|
0
|
public boolean isDefault() {
|
59
|
0
|
return (myNodes.size() == 0) && (myDefaultNodes.size() > 0);
|
60
|
|
}
|
61
|
|
|
62
|
0
|
public Node item(int index) {
|
63
|
0
|
Node res = null;
|
64
|
0
|
List l = getDefaultedNodeList();
|
65
|
0
|
if ((index >= 0) && (index < l.size())) {
|
66
|
0
|
res = (Node)l.get(index);
|
67
|
|
}
|
68
|
0
|
return res;
|
69
|
|
}
|
70
|
|
|
71
|
0
|
public Node getPreviousNode(Node aNode) {
|
72
|
0
|
return item(getIndex(aNode) - 1);
|
73
|
|
}
|
74
|
|
|
75
|
0
|
public Node getNextNode(Node aNode) {
|
76
|
0
|
return item(getIndex(aNode) + 1);
|
77
|
|
}
|
78
|
|
|
79
|
0
|
public void insertNodeAt(int anIndex, Node aNode) throws DomException {
|
80
|
0
|
short s = aNode.getNodeType();
|
81
|
0
|
boolean found = false;
|
82
|
0
|
for (int i = 0; !found && (i < myAllowedChildNodeTypes.length); i++) {
|
83
|
0
|
found = s == myAllowedChildNodeTypes[i];
|
84
|
|
}
|
85
|
0
|
if (!found) {
|
86
|
0
|
throw new DomException(DomException.HIERARCHY_REQUEST_ERR, "node of type " + s + " must not be inserted here");
|
87
|
|
}
|
88
|
0
|
if (anIndex == myNodes.size()) {
|
89
|
0
|
myNodes.add(anIndex, aNode);
|
90
|
|
} else {
|
91
|
0
|
myNodes.add(aNode);
|
92
|
|
}
|
93
|
|
}
|
94
|
|
|
95
|
0
|
public void add(Node aNode) {
|
96
|
0
|
insertNodeAt(myNodes.size(), aNode);
|
97
|
|
}
|
98
|
|
|
99
|
0
|
public void removeAll() {
|
100
|
0
|
myNodes.clear();
|
101
|
|
}
|
102
|
|
|
103
|
0
|
public void removeNode(Node aNode) throws DomException {
|
104
|
0
|
if (!myNodes.remove(aNode)) {
|
105
|
0
|
throw new DomException(DomException.NOT_FOUND_ERR);
|
106
|
|
}
|
107
|
|
}
|
108
|
|
|
109
|
0
|
public int getIndex(Node aNode) throws DomException {
|
110
|
0
|
int res = getDefaultedNodeList().indexOf(aNode);
|
111
|
0
|
if ((res < 0) || (res >= getLength())) {
|
112
|
0
|
throw new DomException(DomException.NOT_FOUND_ERR);
|
113
|
|
}
|
114
|
0
|
return res;
|
115
|
|
}
|
116
|
|
|
117
|
0
|
public ListIterator iterNodes() {
|
118
|
0
|
return getDefaultedNodeList().listIterator();
|
119
|
|
}
|
120
|
|
|
121
|
0
|
public ListIterator iterNodes(final String aNamespace, final String aName) {
|
122
|
0
|
return new FilterListIterator(iterNodes(), new FilterListIterator.Test() {
|
123
|
|
|
124
|
0
|
public boolean test(Object anObject) {
|
125
|
0
|
Node n = (Node)anObject;
|
126
|
0
|
boolean res = "*".equals(aNamespace) || ((null == aNamespace) && (null == n.getNamespaceURI())) || ((null != aNamespace) && aNamespace.equals(n.getNamespaceURI()));
|
127
|
0
|
if (res) {
|
128
|
0
|
if (!"*".equals(aName)) {
|
129
|
0
|
if (null == n.getLocalName()) {
|
130
|
0
|
res = aName.equals(n.getNodeName());
|
131
|
|
} else {
|
132
|
0
|
res = aName.equals(n.getLocalName());
|
133
|
|
}
|
134
|
|
}
|
135
|
|
}
|
136
|
0
|
return res;
|
137
|
|
}
|
138
|
|
});
|
139
|
|
}
|
140
|
|
|
141
|
0
|
public Node getNodeByClass(final Class aClass) {
|
142
|
0
|
Iterator i = new FilterIterator(iterNodes(), new FilterIterator.ICondition() {
|
143
|
0
|
public boolean evaluate(Object anObject) {
|
144
|
0
|
return aClass.isInstance(anObject);
|
145
|
|
}
|
146
|
|
});
|
147
|
0
|
Node res = null;
|
148
|
0
|
if (i.hasNext()) {
|
149
|
0
|
res = (Node)i.next();
|
150
|
|
}
|
151
|
|
assert !i.hasNext() : "node not unique";
|
152
|
0
|
return res;
|
153
|
|
}
|
154
|
|
|
155
|
0
|
public List cloneNodeList() {
|
156
|
0
|
return (List)myNodes.clone();
|
157
|
|
}
|
158
|
|
|
159
|
0
|
public void addAll(List aNodeList) {
|
160
|
0
|
myNodes.addAll(aNodeList);
|
161
|
|
}
|
162
|
|
|
163
|
0
|
public void setDefaultTextContent(String aString) {
|
164
|
0
|
if (null == aString) {
|
165
|
0
|
myDefaultNodes = Collections.EMPTY_LIST;
|
166
|
|
} else {
|
167
|
0
|
DomText text = (DomText)myOwnerDocument.createTextNode(aString);
|
168
|
0
|
text.myParentNode = myOwnerNode;
|
169
|
0
|
myDefaultNodes = Collections.singletonList(text);
|
170
|
|
}
|
171
|
|
}
|
172
|
|
|
173
|
0
|
public List getDefaultedNodeList() {
|
174
|
0
|
List res = null;
|
175
|
0
|
if (myNodes.size() > 0) {
|
176
|
0
|
res = myNodes;
|
177
|
|
} else {
|
178
|
0
|
res = myDefaultNodes;
|
179
|
|
}
|
180
|
0
|
return res;
|
181
|
|
}
|
182
|
|
}
|
183
|
|
|