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:
112: protected DomElement(DomDocument owner, String namespaceURI, String name,
113: String prefix, String localName)
114: {
115: super(ELEMENT_NODE, owner, namespaceURI, name, prefix, localName);
116: }
117:
118:
122: public NamedNodeMap getAttributes()
123: {
124: if (attributes == null)
125: {
126: attributes = new DomNamedNodeMap(this, Node.ATTRIBUTE_NODE);
127: }
128: return attributes;
129: }
130:
131:
135: public boolean hasAttributes()
136: {
137: return attributes != null && attributes.length != 0;
138: }
139:
140:
144: public Object clone()
145: {
146: DomElement node = (DomElement) super.clone();
147:
148: if (attributes != null)
149: {
150: node.attributes = new DomNamedNodeMap(node, Node.ATTRIBUTE_NODE);
151: for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next)
152: {
153: node.attributes.setNamedItem(ctx.cloneNode(true), true, true);
154: }
155: }
156: return node;
157: }
158:
159: void setOwner(DomDocument doc)
160: {
161: if (attributes != null)
162: {
163: for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next)
164: {
165: ctx.setOwner(doc);
166: }
167: }
168: super.setOwner(doc);
169: }
170:
171:
175: public void makeReadonly()
176: {
177: super.makeReadonly();
178: if (attributes != null)
179: {
180: attributes.makeReadonly();
181: }
182: }
183:
184:
188: final public String getTagName()
189: {
190: return getNodeName();
191: }
192:
193:
198: public String getAttribute(String name)
199: {
200: if ("xml:space" == name)
201: {
202:
203: return xmlSpace;
204: }
205: Attr attr = getAttributeNode(name);
206: return (attr == null) ? "" : attr.getValue();
207: }
208:
209:
214: public boolean hasAttribute(String name)
215: {
216: return getAttributeNode(name) != null;
217: }
218:
219:
224: public boolean hasAttributeNS(String namespaceURI, String local)
225: {
226: return getAttributeNodeNS(namespaceURI, local) != null;
227: }
228:
229:
234: public String getAttributeNS(String namespaceURI, String local)
235: {
236: Attr attr = getAttributeNodeNS(namespaceURI, local);
237: return (attr == null) ? "" : attr.getValue();
238: }
239:
240:
245: public Attr getAttributeNode(String name)
246: {
247: return (attributes == null) ? null :
248: (Attr) attributes.getNamedItem(name);
249: }
250:
251:
256: public Attr getAttributeNodeNS(String namespace, String localPart)
257: {
258: return (attributes == null) ? null :
259: (Attr) attributes.getNamedItemNS(namespace, localPart);
260: }
261:
262:
268: public void setAttribute(String name, String value)
269: {
270: Attr attr = getAttributeNode(name);
271: if (attr != null)
272: {
273: attr.setNodeValue(value);
274: ((DomAttr) attr).setSpecified(true);
275: return;
276: }
277: attr = owner.createAttribute(name);
278: attr.setNodeValue(value);
279: setAttributeNode(attr);
280: }
281:
282:
287: public void setAttributeNS(String uri, String aname, String value)
288: {
289: if (("xmlns".equals (aname) || aname.startsWith ("xmlns:"))
290: && !XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals (uri))
291: {
292: throw new DomDOMException(DOMException.NAMESPACE_ERR,
293: "setting xmlns attribute to illegal value", this, 0);
294: }
295:
296: Attr attr = getAttributeNodeNS(uri, aname);
297: if (attr != null)
298: {
299: attr.setNodeValue(value);
300: return;
301: }
302: attr = owner.createAttributeNS(uri, aname);
303: attr.setNodeValue(value);
304: setAttributeNodeNS(attr);
305: }
306:
307:
312: public Attr setAttributeNode(Attr attr)
313: {
314: return (Attr) getAttributes().setNamedItem(attr);
315: }
316:
317:
322: public Attr setAttributeNodeNS(Attr attr)
323: {
324: return (Attr) getAttributes().setNamedItemNS(attr);
325: }
326:
327:
337: public void removeAttribute(String name)
338: {
339: if (attributes == null)
340: {
341: return;
342: }
343:
344: try
345: {
346: attributes.removeNamedItem(name);
347: }
348: catch (DomDOMException e)
349: {
350: if (e.code != DOMException.NOT_FOUND_ERR)
351: {
352: throw e;
353: }
354: }
355: }
356:
357:
366: public Attr removeAttributeNode(Attr node)
367: {
368: if (attributes == null)
369: {
370: throw new DomDOMException(DOMException.NOT_FOUND_ERR, null, node, 0);
371: }
372: return (Attr) attributes.removeNamedItem(node.getNodeName());
373: }
374:
375:
384: public void removeAttributeNS(String namespace, String localPart)
385: {
386: if (attributes == null)
387: {
388: throw new DomDOMException(DOMException.NOT_FOUND_ERR, localPart, null, 0);
389: }
390: attributes.removeNamedItemNS (namespace, localPart);
391: }
392:
393:
394:
395: public String lookupPrefix(String namespaceURI)
396: {
397: if (namespaceURI == null)
398: {
399: return null;
400: }
401: String namespace = getNamespaceURI();
402: if (namespace != null && namespace.equals(namespaceURI))
403: {
404: return getPrefix();
405: }
406: if (attributes != null)
407: {
408: for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next)
409: {
410: if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI
411: .equals(ctx.getNamespaceURI()))
412: {
413: String value = ctx.getNodeValue();
414: if (value.equals(namespaceURI))
415: {
416: return ctx.getLocalName();
417: }
418: }
419: }
420: }
421: return super.lookupPrefix(namespaceURI);
422: }
423:
424: public boolean isDefaultNamespace(String namespaceURI)
425: {
426: String namespace = getNamespaceURI();
427: if (namespace != null && namespace.equals(namespaceURI))
428: {
429: return getPrefix() == null;
430: }
431: if (attributes != null)
432: {
433: for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next)
434: {
435: if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI
436: .equals(ctx.getNamespaceURI()))
437: {
438: String qName = ctx.getNodeName();
439: return (XMLConstants.XMLNS_ATTRIBUTE.equals(qName));
440: }
441: }
442: }
443: return super.isDefaultNamespace(namespaceURI);
444: }
445:
446: public String lookupNamespaceURI(String prefix)
447: {
448: String namespace = getNamespaceURI();
449: if (namespace != null && equal(prefix, getPrefix()))
450: {
451: return namespace;
452: }
453: if (attributes != null)
454: {
455: for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next)
456: {
457: if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI
458: .equals(ctx.getNamespaceURI()))
459: {
460: if (prefix == null)
461: {
462: if (XMLConstants.XMLNS_ATTRIBUTE.equals(ctx.getNodeName()))
463: {
464: return ctx.getNodeValue();
465: }
466: }
467: else
468: {
469: if (prefix.equals(ctx.getLocalName()))
470: {
471: return ctx.getNodeValue();
472: }
473: }
474: }
475: }
476: }
477: return super.lookupNamespaceURI(prefix);
478: }
479:
480: public String getBaseURI()
481: {
482: if (attributes != null)
483: {
484: Node xmlBase =
485: attributes.getNamedItemNS(XMLConstants.XML_NS_URI, "base");
486: if (xmlBase != null)
487: {
488: return xmlBase.getNodeValue();
489: }
490: }
491: return super.getBaseURI();
492: }
493:
494: public TypeInfo getSchemaTypeInfo()
495: {
496:
497: DomDoctype doctype = (DomDoctype) owner.getDoctype();
498: if (doctype != null)
499: {
500: return doctype.getElementTypeInfo(getNodeName());
501: }
502:
503: return null;
504: }
505:
506: public void setIdAttribute(String name, boolean isId)
507: {
508: NamedNodeMap attrs = getAttributes();
509: Attr attr = (Attr) attrs.getNamedItem(name);
510: setIdAttributeNode(attr, isId);
511: }
512:
513: public void setIdAttributeNode(Attr attr, boolean isId)
514: {
515: if (readonly)
516: {
517: throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
518: }
519: if (attr == null || attr.getOwnerElement() != this)
520: {
521: throw new DomDOMException(DOMException.NOT_FOUND_ERR);
522: }
523: if (isId)
524: {
525: if (userIdAttrs == null)
526: {
527: userIdAttrs = new HashSet();
528: }
529: userIdAttrs.add(attr);
530: }
531: else if (userIdAttrs != null)
532: {
533: userIdAttrs.remove(attr);
534: if (userIdAttrs.isEmpty())
535: {
536: userIdAttrs = null;
537: }
538: }
539: }
540:
541: public void setIdAttributeNS(String namespaceURI, String localName,
542: boolean isId)
543: {
544: NamedNodeMap attrs = getAttributes();
545: Attr attr = (Attr) attrs.getNamedItemNS(namespaceURI, localName);
546: setIdAttributeNode(attr, isId);
547: }
548:
549: public boolean isEqualNode(Node arg)
550: {
551: if (!super.isEqualNode(arg))
552: return false;
553: getAttributes();
554: NamedNodeMap argAttrs = arg.getAttributes();
555: int len = argAttrs.getLength();
556: if (argAttrs == null || (len != attributes.length))
557: return false;
558: for (int i = 0; i < len; i++)
559: {
560: Node argCtx = argAttrs.item(i);
561:
562: if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI
563: .equals(argCtx.getNamespaceURI()))
564: continue;
565:
566: DomNode ctx = attributes.first;
567: for (; ctx != null; ctx = ctx.next)
568: {
569: if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI
570: .equals(ctx.getNamespaceURI()))
571: continue;
572: if (!ctx.isEqualNode(argCtx))
573: continue;
574: break;
575: }
576: if (ctx == null)
577: return false;
578: }
579: return true;
580: }
581:
582: }