1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.jeuclid.dom;
20
21 import java.util.List;
22 import java.util.Vector;
23
24 import org.w3c.dom.DOMException;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.NamedNodeMap;
27 import org.w3c.dom.Node;
28 import org.w3c.dom.UserDataHandler;
29
30
31
32
33
34
35
36
37
38
39 public abstract class AbstractPartialNodeImpl implements Node {
40
41 private static final String COULD_NOT_FIND_NODE = "Could not find node: ";
42
43 private final List<Node> children = new Vector<Node>();
44
45 private Node parent;
46
47
48
49
50 public static class NodeList implements org.w3c.dom.NodeList {
51 private final List<Node> children;
52
53
54
55
56
57
58
59 protected NodeList(final List<Node> childs) {
60 this.children = childs;
61 }
62
63
64 public final int getLength() {
65 return this.children.size();
66 }
67
68
69 public final Node item(final int index) {
70 return this.children.get(index);
71 }
72 }
73
74
75 public final String lookupNamespaceURI(final String prefix) {
76 throw new UnsupportedOperationException("lookupNamespaceURI");
77 }
78
79
80 public final void normalize() {
81 throw new UnsupportedOperationException("normalize");
82 }
83
84
85 public final org.w3c.dom.NodeList getChildNodes() {
86 return new NodeList(this.children);
87 }
88
89
90 public final Node getFirstChild() {
91 try {
92 return this.children.get(0);
93 } catch (final IndexOutOfBoundsException e) {
94 return null;
95 }
96 }
97
98
99 public final boolean isSameNode(final Node other) {
100 return this.equals(other);
101 }
102
103
104 public Node appendChild(final Node newChild) {
105 if (newChild instanceof AbstractPartialNodeImpl) {
106 this.children.add(newChild);
107 ((AbstractPartialNodeImpl) newChild).parent = this;
108 return newChild;
109 } else {
110 throw new IllegalArgumentException(
111 "Can only add children of type "
112 + AbstractPartialNodeImpl.class.getName()
113 + " to " + this.getClass().getName());
114 }
115 }
116
117
118 public String getTextContent() {
119 final StringBuilder builder = new StringBuilder();
120 for (final Node n : this.children) {
121 builder.append(n.getTextContent());
122 }
123 return builder.toString();
124 }
125
126
127 public void setTextContent(final String newTextContent) {
128 this.children.clear();
129 if (newTextContent != null && newTextContent.length() > 0) {
130 this.children.add(new PartialTextImpl(newTextContent));
131 }
132 }
133
134
135 public final Node cloneNode(final boolean deep) {
136 throw new UnsupportedOperationException("cloneNode");
137 }
138
139
140 public String getNodeValue() {
141 return null;
142 }
143
144
145 public final String getNamespaceURI() {
146 return null;
147 }
148
149
150 public final Node getParentNode() {
151 return this.parent;
152 }
153
154
155 public final String getBaseURI() {
156 throw new UnsupportedOperationException("getBaseURI");
157 }
158
159
160 public final String getPrefix() {
161 throw new UnsupportedOperationException("getPrefix");
162 }
163
164
165 public NamedNodeMap getAttributes() {
166 return null;
167 }
168
169
170 public final boolean hasChildNodes() {
171 return !this.children.isEmpty();
172 }
173
174
175 public final boolean isDefaultNamespace(final String namespaceURI) {
176 throw new UnsupportedOperationException("isDefaultNamespace");
177 }
178
179
180 public Node replaceChild(final Node newChild, final Node oldChild) {
181
182
183 for (int i = 0; i < this.children.size(); i++) {
184 final Node oldChildAtIndex = this.children.get(i);
185 if (oldChildAtIndex.equals(oldChild)) {
186 this.children.set(i, newChild);
187 return oldChildAtIndex;
188 }
189 }
190 throw new DOMException(DOMException.NOT_FOUND_ERR,
191 AbstractPartialNodeImpl.COULD_NOT_FIND_NODE + oldChild);
192 }
193
194
195 public final Node insertBefore(final Node newChild, final Node refChild) {
196 throw new UnsupportedOperationException("insertBefore");
197 }
198
199
200 public final boolean isEqualNode(final Node arg) {
201 throw new UnsupportedOperationException("isEqualNode");
202 }
203
204
205 public final Node getNextSibling() {
206 Node retval;
207 try {
208 final List<Node> parentsChildren = ((AbstractPartialNodeImpl) this.parent).children;
209 retval = parentsChildren.get(parentsChildren.indexOf(this) + 1);
210 } catch (final NullPointerException ne) {
211 retval = null;
212 } catch (final IndexOutOfBoundsException iobe) {
213 retval = null;
214 }
215 return retval;
216 }
217
218
219 public final boolean hasAttributes() {
220 return this.getAttributes().getLength() > 0;
221 }
222
223
224 public final short compareDocumentPosition(final Node other) {
225 throw new UnsupportedOperationException("compareDocumentPosition");
226 }
227
228
229 public final Node removeChild(final Node oldChild) {
230 for (int i = 0; i < this.children.size(); i++) {
231 final Node oldChildAtIndex = this.children.get(i);
232 if (oldChildAtIndex.equals(oldChild)) {
233 this.children.remove(i);
234 return oldChildAtIndex;
235 }
236 }
237 throw new DOMException(DOMException.NOT_FOUND_ERR,
238 AbstractPartialNodeImpl.COULD_NOT_FIND_NODE + oldChild);
239
240 }
241
242
243 public final Object getFeature(final String feature, final String version) {
244 throw new UnsupportedOperationException("getFeature");
245 }
246
247
248 public final Node getLastChild() {
249 final List<Node> theChildren = this.children;
250 final int size = theChildren.size();
251 if (size == 0) {
252 return null;
253 } else {
254 return theChildren.get(size - 1);
255 }
256 }
257
258
259 public String getLocalName() {
260 return null;
261 }
262
263
264 public final Document getOwnerDocument() {
265 throw new UnsupportedOperationException("getOwnerDocument");
266 }
267
268
269 public final Node getPreviousSibling() {
270 throw new UnsupportedOperationException("getPreviousSibling");
271 }
272
273
274 public final boolean isSupported(final String feature,
275 final String version) {
276 throw new UnsupportedOperationException("isSupported");
277 }
278
279
280 public final String lookupPrefix(final String namespaceURI) {
281 throw new UnsupportedOperationException("lookupPrefix");
282 }
283
284
285 public final void setNodeValue(final String nodeValue) {
286 throw new UnsupportedOperationException("setNodeValue");
287 }
288
289
290 public final void setPrefix(final String prefix) {
291 throw new UnsupportedOperationException("setPrefix");
292 }
293
294
295 public final Object getUserData(final String key) {
296 throw new UnsupportedOperationException("getUserData");
297 }
298
299
300 public final Object setUserData(final String key, final Object data,
301 final UserDataHandler handler) {
302 throw new UnsupportedOperationException("setUserData");
303 }
304
305 }