1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43:
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50:
51:
57: public class DomElement
58: extends DomNsNode
59: implements Element
60: {
61:
62:
66: Set userIdAttrs;
67:
68:
69:
70: private DomNamedNodeMap attributes;
71:
72:
73: String xmlSpace = "";
74:
75:
87: protected DomElement(DomDocument owner, String namespaceURI, String name)
88: {
89: super(ELEMENT_NODE, owner, namespaceURI, name);
90: }
91:
92:
96: public NamedNodeMap getAttributes()
97: {
98: if (attributes == null)
99: {
100: attributes = new DomNamedNodeMap(this, Node.ATTRIBUTE_NODE);
101: }
102: return attributes;
103: }
104:
105:
109: public boolean hasAttributes()
110: {
111: return attributes != null && attributes.length != 0;
112: }
113:
114:
118: public Object clone()
119: {
120: DomElement node = (DomElement) super.clone();
121:
122: if (attributes != null)
123: {
124: node.attributes = new DomNamedNodeMap(node, Node.ATTRIBUTE_NODE);
125: for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next)
126: {
127: node.attributes.setNamedItem(ctx.cloneNode(true), true, true);
128: }
129: }
130: return node;
131: }
132:
133: void setOwner(DomDocument doc)
134: {
135: if (attributes != null)
136: {
137: for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next)
138: {
139: ctx.setOwner(doc);
140: }
141: }
142: super.setOwner(doc);
143: }
144:
145:
149: public void makeReadonly()
150: {
151: super.makeReadonly();
152: if (attributes != null)
153: {
154: attributes.makeReadonly();
155: }
156: }
157:
158:
162: final public String getTagName()
163: {
164: return getNodeName();
165: }
166:
167:
172: public String getAttribute(String name)
173: {
174: if ("xml:space" == name)
175: {
176:
177: return xmlSpace;
178: }
179: Attr attr = getAttributeNode(name);
180: return (attr == null) ? "" : attr.getValue();
181: }
182:
183:
188: public boolean hasAttribute(String name)
189: {
190: return getAttributeNode(name) != null;
191: }
192:
193:
198: public boolean hasAttributeNS(String namespaceURI, String local)
199: {
200: return getAttributeNodeNS(namespaceURI, local) != null;
201: }
202:
203:
208: public String getAttributeNS(String namespaceURI, String local)
209: {
210: Attr attr = getAttributeNodeNS(namespaceURI, local);
211: return (attr == null) ? "" : attr.getValue();
212: }
213:
214:
219: public Attr getAttributeNode(String name)
220: {
221: return (attributes == null) ? null :
222: (Attr) attributes.getNamedItem(name);
223: }
224:
225:
230: public Attr getAttributeNodeNS(String namespace, String localPart)
231: {
232: return (attributes == null) ? null :
233: (Attr) attributes.getNamedItemNS(namespace, localPart);
234: }
235:
236:
242: public void setAttribute(String name, String value)
243: {
244: Attr attr = getAttributeNode(name);
245: if (attr != null)
246: {
247: attr.setNodeValue(value);
248: ((DomAttr) attr).setSpecified(true);
249: return;
250: }
251: attr = owner.createAttribute(name);
252: attr.setNodeValue(value);
253: setAttributeNode(attr);
254: }
255:
256:
261: public void setAttributeNS(String uri, String aname, String value)
262: {
263: if (("xmlns".equals (aname) || aname.startsWith ("xmlns:"))
264: && !XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals (uri))
265: {
266: throw new DomDOMException(DOMException.NAMESPACE_ERR,
267: "setting xmlns attribute to illegal value", this, 0);
268: }
269:
270: Attr attr = getAttributeNodeNS(uri, aname);
271: if (attr != null)
272: {
273: attr.setNodeValue(value);
274: return;
275: }
276: attr = owner.createAttributeNS(uri, aname);
277: attr.setNodeValue(value);
278: setAttributeNodeNS(attr);
279: }
280:
281:
286: public Attr setAttributeNode(Attr attr)
287: {
288: return (Attr) getAttributes().setNamedItem(attr);
289: }
290:
291:
296: public Attr setAttributeNodeNS(Attr attr)
297: {
298: return (Attr) getAttributes().setNamedItemNS(attr);
299: }
300:
301:
311: public void removeAttribute(String name)
312: {
313: if (attributes == null)
314: {
315: return;
316: }
317:
318: try
319: {
320: attributes.removeNamedItem(name);
321: }
322: catch (DomDOMException e)
323: {
324: if (e.code != DOMException.NOT_FOUND_ERR)
325: {
326: throw e;
327: }
328: }
329: }
330:
331:
340: public Attr removeAttributeNode(Attr node)
341: {
342: if (attributes == null)
343: {
344: throw new DomDOMException(DOMException.NOT_FOUND_ERR, null, node, 0);
345: }
346: return (Attr) attributes.removeNamedItem(node.getNodeName());
347: }
348:
349:
358: public void removeAttributeNS(String namespace, String localPart)
359: {
360: if (attributes == null)
361: {
362: throw new DomDOMException(DOMException.NOT_FOUND_ERR, localPart, null, 0);
363: }
364: attributes.removeNamedItemNS (namespace, localPart);
365: }
366:
367:
368:
369: public String lookupPrefix(String namespaceURI)
370: {
371: if (namespaceURI == null)
372: {
373: return null;
374: }
375: String namespace = getNamespaceURI();
376: if (namespace != null && namespace.equals(namespaceURI))
377: {
378: return getPrefix();
379: }
380: if (attributes != null)
381: {
382: for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next)
383: {
384: if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI
385: .equals(ctx.getNamespaceURI()))
386: {
387: String value = ctx.getNodeValue();
388: if (value.equals(namespaceURI))
389: {
390: return ctx.getLocalName();
391: }
392: }
393: }
394: }
395: return super.lookupPrefix(namespaceURI);
396: }
397:
398: public boolean isDefaultNamespace(String namespaceURI)
399: {
400: String namespace = getNamespaceURI();
401: if (namespace != null && namespace.equals(namespaceURI))
402: {
403: return getPrefix() == null;
404: }
405: if (attributes != null)
406: {
407: for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next)
408: {
409: if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI
410: .equals(ctx.getNamespaceURI()))
411: {
412: String qName = ctx.getNodeName();
413: return (XMLConstants.XMLNS_ATTRIBUTE.equals(qName));
414: }
415: }
416: }
417: return super.isDefaultNamespace(namespaceURI);
418: }
419:
420: public String lookupNamespaceURI(String prefix)
421: {
422: String namespace = getNamespaceURI();
423: if (namespace != null && equal(prefix, getPrefix()))
424: {
425: return namespace;
426: }
427: if (attributes != null)
428: {
429: for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next)
430: {
431: if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI
432: .equals(ctx.getNamespaceURI()))
433: {
434: if (prefix == null)
435: {
436: if (XMLConstants.XMLNS_ATTRIBUTE.equals(ctx.getNodeName()))
437: {
438: return ctx.getNodeValue();
439: }
440: }
441: else
442: {
443: if (prefix.equals(ctx.getLocalName()))
444: {
445: return ctx.getNodeValue();
446: }
447: }
448: }
449: }
450: }
451: return super.lookupNamespaceURI(prefix);
452: }
453:
454: public String getBaseURI()
455: {
456: if (attributes != null)
457: {
458: Node xmlBase =
459: attributes.getNamedItemNS(XMLConstants.XML_NS_URI, "base");
460: if (xmlBase != null)
461: {
462: return xmlBase.getNodeValue();
463: }
464: }
465: return super.getBaseURI();
466: }
467:
468: public TypeInfo getSchemaTypeInfo()
469: {
470:
471: DomDoctype doctype = (DomDoctype) owner.getDoctype();
472: if (doctype != null)
473: {
474: return doctype.getElementTypeInfo(getNodeName());
475: }
476:
477: return null;
478: }
479:
480: public void setIdAttribute(String name, boolean isId)
481: {
482: NamedNodeMap attrs = getAttributes();
483: Attr attr = (Attr) attrs.getNamedItem(name);
484: setIdAttributeNode(attr, isId);
485: }
486:
487: public void setIdAttributeNode(Attr attr, boolean isId)
488: {
489: if (readonly)
490: {
491: throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
492: }
493: if (attr == null || attr.getOwnerElement() != this)
494: {
495: throw new DomDOMException(DOMException.NOT_FOUND_ERR);
496: }
497: if (isId)
498: {
499: if (userIdAttrs == null)
500: {
501: userIdAttrs = new HashSet();
502: }
503: userIdAttrs.add(attr);
504: }
505: else if (userIdAttrs != null)
506: {
507: userIdAttrs.remove(attr);
508: if (userIdAttrs.isEmpty())
509: {
510: userIdAttrs = null;
511: }
512: }
513: }
514:
515: public void setIdAttributeNS(String namespaceURI, String localName,
516: boolean isId)
517: {
518: NamedNodeMap attrs = getAttributes();
519: Attr attr = (Attr) attrs.getNamedItemNS(namespaceURI, localName);
520: setIdAttributeNode(attr, isId);
521: }
522:
523: public boolean isEqualNode(Node arg)
524: {
525: if (!super.isEqualNode(arg))
526: return false;
527: getAttributes();
528: NamedNodeMap argAttrs = arg.getAttributes();
529: int len = argAttrs.getLength();
530: if (argAttrs == null || (len != attributes.length))
531: return false;
532: for (int i = 0; i < len; i++)
533: {
534: Node argCtx = argAttrs.item(i);
535:
536: if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI
537: .equals(argCtx.getNamespaceURI()))
538: continue;
539:
540: DomNode ctx = attributes.first;
541: for (; ctx != null; ctx = ctx.next)
542: {
543: if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI
544: .equals(ctx.getNamespaceURI()))
545: continue;
546: if (!ctx.isEqualNode(argCtx))
547: continue;
548: break;
549: }
550: if (ctx == null)
551: return false;
552: }
553: return true;
554: }
555:
556: }