001: package net.sf.saxon.dom;
002:
003: import net.sf.saxon.om.Axis;
004: import net.sf.saxon.om.AxisIterator;
005: import net.sf.saxon.om.NodeInfo;
006: import org.w3c.dom.DOMException;
007: import org.w3c.dom.NamedNodeMap;
008: import org.w3c.dom.Node;
009:
010: /**
011: * Implementation of DOM NamedNodeMap used to represent the attributes of an element, for use when
012: * Saxon element and attribute nodes are accessed using the DOM API.
013: */
014:
015: class DOMAttributeMap implements NamedNodeMap {
016:
017: private NodeInfo parent;
018:
019: /**
020: * Construct an AttributeMap for a given element node
021: */
022:
023: public DOMAttributeMap(NodeInfo parent) {
024: this .parent = parent;
025: }
026:
027: /**
028: * Get named attribute (DOM NamedNodeMap method)
029: */
030:
031: public Node getNamedItem(String name) {
032: AxisIterator atts = parent.iterateAxis(Axis.ATTRIBUTE);
033: while (true) {
034: NodeInfo att = (NodeInfo) atts.next();
035: if (att == null) {
036: return null;
037: }
038: if (name.equals(att.getDisplayName())) {
039: return NodeOverNodeInfo.wrap(att);
040: }
041: }
042: }
043:
044: /**
045: * Get n'th attribute (DOM NamedNodeMap method).
046: * Namespace declarations are not retrieved.
047: */
048:
049: public Node item(int index) {
050: if (index < 0) {
051: return null;
052: }
053: int length = 0;
054: AxisIterator atts = parent.iterateAxis(Axis.ATTRIBUTE);
055: while (true) {
056: NodeInfo att = (NodeInfo) atts.next();
057: if (att == null) {
058: return null;
059: }
060: if (length == index) {
061: return NodeOverNodeInfo.wrap(att);
062: }
063: length++;
064: }
065: }
066:
067: /**
068: * Get number of attributes (DOM NamedNodeMap method).
069: */
070:
071: public int getLength() {
072: int length = 0;
073: AxisIterator atts = parent.iterateAxis(Axis.ATTRIBUTE);
074: while (atts.next() != null) {
075: length++;
076: }
077: return length;
078: }
079:
080: /**
081: * Get named attribute (DOM NamedNodeMap method)
082: */
083:
084: public Node getNamedItemNS(String uri, String localName) {
085: if (uri == null)
086: uri = "";
087: AxisIterator atts = parent.iterateAxis(Axis.ATTRIBUTE);
088: while (true) {
089: NodeInfo att = (NodeInfo) atts.next();
090: if (att == null) {
091: return null;
092: }
093: if (uri.equals(att.getURI())
094: && localName.equals(att.getLocalPart())) {
095: return NodeOverNodeInfo.wrap(att);
096: }
097: }
098: }
099:
100: /**
101: * Set named attribute (DOM NamedNodeMap method: always fails)
102: */
103:
104: public Node setNamedItem(Node arg) throws DOMException {
105: NodeOverNodeInfo.disallowUpdate();
106: return null;
107: }
108:
109: /**
110: * Remove named attribute (DOM NamedNodeMap method: always fails)
111: */
112:
113: public Node removeNamedItem(String name) throws DOMException {
114: NodeOverNodeInfo.disallowUpdate();
115: return null;
116: }
117:
118: /**
119: * Set named attribute (DOM NamedNodeMap method: always fails)
120: */
121:
122: public Node setNamedItemNS(Node arg) throws DOMException {
123: NodeOverNodeInfo.disallowUpdate();
124: return null;
125: }
126:
127: /**
128: * Remove named attribute (DOM NamedNodeMap method: always fails)
129: */
130:
131: public Node removeNamedItemNS(String uri, String localName)
132: throws DOMException {
133: NodeOverNodeInfo.disallowUpdate();
134: return null;
135: }
136:
137: }
138:
139: //
140: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
141: // you may not use this file except in compliance with the License. You may obtain a copy of the
142: // License at http://www.mozilla.org/MPL/
143: //
144: // Software distributed under the License is distributed on an "AS IS" basis,
145: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
146: // See the License for the specific language governing rights and limitations under the License.
147: //
148: // The Original Code is: all this file.
149: //
150: // The Initial Developer of the Original Code is Michael H. Kay.
151: //
152: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
153: //
154: // Contributor(s): none.
155: //
|