001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xpath.pattern;
030:
031: import com.caucho.xpath.ExprEnvironment;
032: import com.caucho.xpath.XPathException;
033:
034: import org.w3c.dom.Element;
035: import org.w3c.dom.NamedNodeMap;
036: import org.w3c.dom.Node;
037:
038: /**
039: * Uses the axis to select new nodes.
040: */
041: public class AttributeListIterator extends NodeIterator {
042: protected NodeIterator _parentIter;
043:
044: protected NamedNodeMap _attributeMap;
045: protected Node _node;
046: protected int _index;
047: protected AbstractPattern _match;
048:
049: protected AttributeListIterator(ExprEnvironment env) {
050: super (env);
051: }
052:
053: /**
054: * Creates the new AxisIterator.
055: *
056: * @param parentIter the parent iterator
057: * @param env the variable environment
058: * @param match the node matching pattern
059: */
060: public AttributeListIterator(NodeIterator parentIter,
061: ExprEnvironment env, AbstractPattern match)
062: throws XPathException {
063: super (env);
064:
065: _parentIter = parentIter;
066: _match = match;
067:
068: _node = findFirstMatchingNode();
069: }
070:
071: /**
072: * True if there's more data.
073: */
074: public boolean hasNext() {
075: return _node != null;
076: }
077:
078: /**
079: * Returns the next selected node.
080: */
081: public Node nextNode() throws XPathException {
082: Node node = _node;
083:
084: _node = findFirstMatchingNode();
085:
086: return node;
087: }
088:
089: /**
090: * Finds the next matching node.
091: */
092: private Node findFirstMatchingNode() throws XPathException {
093: Node node = null;
094:
095: while (true) {
096: Node parent;
097:
098: if (node != null
099: && (_match == null || _match.match(node, _env))) {
100: _position++;
101: return node;
102: }
103:
104: if (_attributeMap != null
105: && _index < _attributeMap.getLength())
106: node = _attributeMap.item(_index++);
107: else if (_parentIter == null
108: || (parent = _parentIter.nextNode()) == null)
109: return null;
110: else if (parent instanceof Element) {
111: _position = 0;
112: _size = 0;
113: _index = 0;
114: _attributeMap = ((Element) parent).getAttributes();
115: }
116: }
117: }
118:
119: /**
120: * Returns the number of nodes in the context list.
121: */
122: public int getContextSize() {
123: if (_attributeMap == null)
124: return 0;
125: else
126: return _attributeMap.getLength();
127: }
128:
129: public Object clone() {
130: AttributeListIterator iter = new AttributeListIterator(_env);
131:
132: iter.copy(this );
133:
134: if (_parentIter != null)
135: iter._parentIter = (NodeIterator) _parentIter.clone();
136: iter._node = _node;
137: iter._index = _index;
138: iter._attributeMap = _attributeMap;
139: iter._match = _match;
140:
141: return iter;
142: }
143:
144: public String toString() {
145: return "AttributeListIterator[" + _match + "]";
146: }
147: }
|