001: package net.xoetrope.xml.jaxp;
002:
003: import java.util.Enumeration;
004: import java.util.Vector;
005:
006: import org.w3c.dom.Element;
007: import org.w3c.dom.NamedNodeMap;
008: import org.w3c.dom.NodeList;
009: import net.xoetrope.xml.XmlElement;
010: import org.w3c.dom.Node;
011:
012: /**
013: * <p>A customization of the XmlElement to interface with Xerces</p>
014: * <p>Copyright (c) Xoetrope Ltd., 1998-2003<br>
015: * License: see license.txt
016: * @version $Revision: 1.11 $
017: */
018: public class JaxpXmlElement implements XmlElement {
019: protected Element element;
020: protected Vector children;
021:
022: public JaxpXmlElement() {
023: }
024:
025: public JaxpXmlElement(Element e) {
026: element = e;
027: }
028:
029: public JaxpXmlElement(String name) {
030: // ElementImpl ei = new ElementImpl();
031: // element = new Element( name );
032: throw new java.lang.UnsupportedOperationException(
033: "Method XercesXmlElement(String) not yet implemented.");
034: }
035:
036: public XmlElement elementAt(int i) {
037: if (children == null)
038: getChildren();
039: Object obj = children.elementAt(i);
040: if (obj instanceof Element)
041: return new JaxpXmlElement((Element) obj);
042: else if (obj instanceof JaxpXmlElement)
043: return (JaxpXmlElement) obj;
044:
045: return null;
046: }
047:
048: public String getAttribute(String name) {
049: return element.getAttribute(name);
050: }
051:
052: public Vector getChildren() {
053: return getChildren("*");
054: }
055:
056: /**
057: * Get the children of this element
058: * @param path
059: * @return a vector of child elements
060: */
061: public Vector getChildren(String path) {
062: children = new Vector();
063: NodeList childElements = element.getChildNodes();
064: int numChildren = childElements.getLength();
065: for (int i = 0; i < numChildren; i++) {
066: Object obj = childElements.item(i);
067: if (obj instanceof Element) {
068: Element e = (Element) obj;
069: if ((path == null) || (path.charAt(0) == '*')
070: || (path.equals(e.getNodeName())))
071: children.addElement(new JaxpXmlElement(e));
072: }
073: }
074:
075: return children;
076: }
077:
078: public String getName() {
079: return element.getTagName();
080: }
081:
082: public void setName(String newName) {
083: // element.setName( newName );
084: throw new java.lang.UnsupportedOperationException(
085: "Method setName( String newName ) not yet implemented.");
086: }
087:
088: public String getContent() {
089: return element.getNodeValue();
090: }
091:
092: public Enumeration enumerateAttributeNames() {
093: Vector attribs = new Vector();
094: NamedNodeMap nnm = element.getAttributes();
095: int numAttributes = nnm.getLength();
096: for (int i = 0; i < numAttributes; i++)
097: attribs.add(nnm.item(i).getNodeName());
098:
099: return attribs.elements();
100: }
101:
102: public void setAttribute(String name, String value) {
103: element.setAttribute(name, value);
104: }
105:
106: public void addChild(XmlElement child) {
107: if (child instanceof JaxpXmlElement)
108: element.appendChild((Element) child.getImplementation());
109: }
110:
111: public XmlElement createElement(String name) {
112: Element eleChild = element.getOwnerDocument().createElement(
113: name);
114: element.appendChild(eleChild);
115: return new JaxpXmlElement(eleChild);
116: }
117:
118: public XmlElement getFirstChildNamed(String name) {
119: Element ele = (Element) element.getElementsByTagName(name)
120: .item(0);
121:
122: return ele == null ? null : new JaxpXmlElement(ele);
123: }
124:
125: public String getNamespace() {
126: return element.getNamespaceURI();
127: }
128:
129: public int getAttributeCount() {
130: return element.getAttributes().getLength();
131: }
132:
133: public String getAttributeNamespace(String name) {
134: return element.getAttributeNode(name).getOwnerElement()
135: .getNamespaceURI();
136: }
137:
138: /**
139: * Returns an instance of the implementing NanoXml element (XMLElement)
140: * @return
141: */
142: public Object getImplementation() {
143: return element;
144: }
145:
146: public Element getElement() {
147: return element;
148: }
149: }
|