1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43:
44:
52: public class DomNamedNodeMap
53: implements NamedNodeMap
54: {
55:
56: final DomNode owner;
57: final short type;
58:
59: DomNode first;
60: int length;
61: boolean readonly;
62:
63:
64: DomNamedNodeMap(DomNode owner, short type)
65: {
66: this.owner = owner;
67: this.type = type;
68: }
69:
70:
76: public final boolean isReadonly()
77: {
78: return readonly;
79: }
80:
81:
85: public void makeReadonly()
86: {
87: readonly = true;
88: for (DomNode ctx = first; ctx != null; ctx = ctx.next)
89: {
90: ctx.makeReadonly();
91: }
92: }
93:
94:
99: public Node getNamedItem(String name)
100: {
101: for (DomNode ctx = first; ctx != null; ctx = ctx.next)
102: {
103: if (ctx.getNodeName().equals(name))
104: {
105: return ctx;
106: }
107: }
108: return null;
109: }
110:
111:
116: public Node getNamedItemNS(String namespaceURI, String localName)
117: {
118: if ("".equals(namespaceURI))
119: {
120: namespaceURI = null;
121: }
122: for (DomNode ctx = first; ctx != null; ctx = ctx.next)
123: {
124: String name = ctx.getLocalName();
125: if ((localName == null && name == null) ||
126: (localName != null && localName.equals(name)))
127: {
128: String uri = ctx.getNamespaceURI();
129: if ("".equals(uri))
130: {
131: uri = null;
132: }
133: if ((namespaceURI == null && uri == null) ||
134: (namespaceURI != null && namespaceURI.equals(uri)))
135: {
136: return ctx;
137: }
138: }
139: }
140: return null;
141: }
142:
143:
149: public Node setNamedItem(Node arg)
150: {
151: return setNamedItem(arg, false, false);
152: }
153:
154:
161: public Node setNamedItemNS(Node arg)
162: {
163: return setNamedItem(arg, true, false);
164: }
165:
166: Node setNamedItem(Node arg, boolean ns, boolean cloning)
167: {
168: if (readonly)
169: {
170: throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
171: }
172:
173: DomNode node = (DomNode) arg;
174: if (!cloning && node.owner != owner.owner)
175: {
176: throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR);
177: }
178: if (node.nodeType != type)
179: {
180: throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR);
181: }
182: if (node.nodeType == Node.ATTRIBUTE_NODE)
183: {
184: DomNode element = node.parent;
185: if (element != null && element != owner)
186: {
187: throw new DomDOMException(DOMException.INUSE_ATTRIBUTE_ERR);
188: }
189: node.parent = owner;
190: node.depth = owner.depth + 1;
191: }
192:
193: String nodeName = node.getNodeName();
194: String localName = ns ? node.getLocalName() : null;
195: String namespaceURI = ns ? node.getNamespaceURI() : null;
196: if ("".equals(namespaceURI))
197: {
198: namespaceURI = null;
199: }
200:
201:
202: DomNode last = null;
203: for (DomNode ctx = first; ctx != null; ctx = ctx.next)
204: {
205: boolean test = false;
206: if (ns)
207: {
208: String tln = ctx.getLocalName();
209: if (tln == null)
210: {
211: tln = ctx.getNodeName();
212: }
213: if (tln.equals(localName))
214: {
215: String tu = ctx.getNamespaceURI();
216: if ((tu == null && namespaceURI == null) ||
217: (tu != null && tu.equals(namespaceURI)))
218: {
219: test = true;
220: }
221: }
222: }
223: else
224: {
225: test = ctx.getNodeName().equals(nodeName);
226: }
227: if (test)
228: {
229:
230: node.previous = ctx.previous;
231: node.next = ctx.next;
232: if (ctx.previous != null)
233: {
234: ctx.previous.next = node;
235: }
236: if (ctx.next != null)
237: {
238: ctx.next.previous = node;
239: }
240: if (first == ctx)
241: {
242: first = node;
243: }
244: reparent(node, nodeName, ctx.index);
245: ctx.parent = null;
246: ctx.next = null;
247: ctx.previous = null;
248: ctx.setDepth(0);
249: ctx.index = 0;
250: return ctx;
251: }
252: last = ctx;
253: }
254:
255: if (last != null)
256: {
257: last.next = node;
258: node.previous = last;
259: }
260: else
261: {
262: first = node;
263: }
264: length++;
265: reparent(node, nodeName, 0);
266: return null;
267: }
268:
269: void reparent(DomNode node, String nodeName, int i)
270: {
271: node.parent = owner;
272: node.setDepth(owner.depth + 1);
273:
274: for (DomNode ctx = node; ctx != null; ctx = ctx.next)
275: {
276: ctx.index = i++;
277: }
278:
279: boolean xmlSpace = "xml:space".equals(nodeName);
280: if (xmlSpace && owner instanceof DomElement)
281: {
282: ((DomElement) owner).xmlSpace = node.getNodeValue();
283: }
284: }
285:
286:
291: public Node removeNamedItem(String name)
292: {
293: return removeNamedItem(null, name, false);
294: }
295:
296:
301: public Node removeNamedItemNS(String namespaceURI, String localName)
302: {
303: return removeNamedItem(namespaceURI, localName, true);
304: }
305:
306: Node removeNamedItem(String uri, String name, boolean ns)
307: {
308: if (readonly)
309: {
310: throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
311: }
312:
313:
314:
315: for (DomNode ctx = first; ctx != null; ctx = ctx.next)
316: {
317: boolean test = false;
318: String nodeName = ctx.getNodeName();
319: if (ns)
320: {
321: String tln = ctx.getLocalName();
322: if (name != null && name.equals(tln))
323: {
324: String tu = ctx.getNamespaceURI();
325: if ((tu == null && uri == null) ||
326: (tu != null && tu.equals(uri)))
327: {
328: test = true;
329: }
330: }
331: }
332: else
333: {
334: test = nodeName.equals(name);
335: }
336: if (test)
337: {
338:
339: boolean xmlSpace = "xml:space".equals(nodeName);
340: if (xmlSpace && owner instanceof DomElement)
341: {
342: ((DomElement) owner).xmlSpace = "";
343: }
344:
345: if (ctx.nodeType == Node.ATTRIBUTE_NODE)
346: {
347: String def = getDefaultValue(ctx.getNodeName());
348: if (def != null)
349: {
350: ctx.setNodeValue(def);
351: ((DomAttr) ctx).setSpecified(false);
352: return null;
353: }
354: }
355:
356: if (ctx == first)
357: {
358: first = ctx.next;
359: }
360: if (ctx.previous != null)
361: {
362: ctx.previous.next = ctx.next;
363: }
364: if (ctx.next != null)
365: {
366: ctx.next.previous = ctx.previous;
367: }
368: length--;
369: ctx.previous = null;
370: ctx.next = null;
371: ctx.parent = null;
372: ctx.setDepth(0);
373: ctx.index = 0;
374: return ctx;
375: }
376: }
377: throw new DomDOMException(DOMException.NOT_FOUND_ERR);
378: }
379:
380: String getDefaultValue(String name)
381: {
382: DomDoctype doctype = (DomDoctype) owner.owner.getDoctype();
383: if (doctype == null)
384: {
385: return null;
386: }
387: DTDAttributeTypeInfo info =
388: doctype.getAttributeTypeInfo(owner.getNodeName(), name);
389: if (info == null)
390: {
391: return null;
392: }
393: return info.value;
394: }
395:
396:
400: public Node item(int index)
401: {
402: DomNode ctx = first;
403: int count = 0;
404: while (ctx != null && count < index)
405: {
406: ctx = ctx.next;
407: count++;
408: }
409: return ctx;
410: }
411:
412:
416: public int getLength()
417: {
418: return length;
419: }
420:
421: }