001: package net.sf.saxon.tree;
002:
003: import net.sf.saxon.om.*;
004: import net.sf.saxon.pattern.NameTest;
005: import net.sf.saxon.pattern.NodeTest;
006: import net.sf.saxon.type.Type;
007:
008: /**
009: * AttributeEnumeration is an enumeration of all the attribute nodes of an Element.
010: */
011:
012: final class AttributeEnumeration extends AxisIteratorImpl implements
013: LookaheadIterator {
014:
015: private ElementImpl element;
016: private NodeTest nodeTest;
017: private NodeInfo next;
018: private int index;
019: private int length;
020:
021: /**
022: * Constructor
023: * @param node: the element whose attributes are required. This may be any type of node,
024: * but if it is not an element the enumeration will be empty
025: * @param nodeTest: condition to be applied to the names of the attributes selected
026: */
027:
028: public AttributeEnumeration(NodeImpl node, NodeTest nodeTest) {
029:
030: this .nodeTest = nodeTest;
031:
032: if (node.getNodeKind() == Type.ELEMENT) {
033: element = (ElementImpl) node;
034: AttributeCollection attlist = element.getAttributeList();
035: index = 0;
036:
037: if (nodeTest instanceof NameTest) {
038: NameTest test = (NameTest) nodeTest;
039: index = attlist.getIndexByFingerprint(test
040: .getFingerprint());
041:
042: if (index < 0) {
043: next = null;
044: } else {
045: next = new AttributeImpl(element, index);
046: index = 0;
047: length = 0; // force iteration to select one node only
048: }
049:
050: } else {
051: index = 0;
052: length = attlist.getLength();
053: advance();
054: }
055: } else { // if it's not an element, or if we're not looking for attributes,
056: // then there's nothing to find
057: next = null;
058: index = 0;
059: length = 0;
060: }
061: }
062:
063: /**
064: * Test if there are mode nodes still to come.
065: * ("elements" is used here in the sense of the Java enumeration class, not in the XML sense)
066: */
067:
068: public boolean hasNext() {
069: return next != null;
070: }
071:
072: /**
073: * Get the next node in the iteration, or null if there are no more.
074: */
075:
076: public Item next() {
077: if (next == null) {
078: current = null;
079: position = -1;
080: return null;
081: } else {
082: current = next;
083: position++;
084: advance();
085: return current;
086: }
087: }
088:
089: /**
090: * Move to the next node in the enumeration.
091: */
092:
093: private void advance() {
094: do {
095: if (index < length) {
096: next = new AttributeImpl(element, index);
097: index++;
098: } else {
099: next = null;
100: return;
101: }
102: } while (!nodeTest.matches(next));
103: }
104:
105: /**
106: * Get another enumeration of the same nodes
107: */
108:
109: public SequenceIterator getAnother() {
110: return new AttributeEnumeration(element, nodeTest);
111: }
112:
113: /**
114: * Get properties of this iterator, as a bit-significant integer.
115: *
116: * @return the properties of this iterator. This will be some combination of
117: * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
118: * and {@link LOOKAHEAD}. It is always
119: * acceptable to return the value zero, indicating that there are no known special properties.
120: * It is acceptable for the properties of the iterator to change depending on its state.
121: */
122:
123: public int getProperties() {
124: return LOOKAHEAD;
125: }
126: }
127:
128: //
129: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
130: // you may not use this file except in compliance with the License. You may obtain a copy of the
131: // License at http://www.mozilla.org/MPL/
132: //
133: // Software distributed under the License is distributed on an "AS IS" basis,
134: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
135: // See the License for the specific language governing rights and limitations under the License.
136: //
137: // The Original Code is: all this file.
138: //
139: // The Initial Developer of the Original Code is Michael H. Kay.
140: //
141: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
142: //
143: // Contributor(s): none.
144: //
|