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.Node;
035:
036: import java.util.logging.Level;
037:
038: /**
039: * Selects namespace nodes.
040: */
041: public class NamespaceIterator extends NodeIterator {
042: protected NodeIterator _parentIter;
043: protected AbstractPattern _match;
044:
045: protected NamespaceNode _node;
046: protected NamespaceNode _next;
047:
048: protected NamespaceIterator(ExprEnvironment env) {
049: super (env);
050: }
051:
052: /**
053: * Creates the new NamespaceIterator.
054: *
055: * @param node the initial node
056: * @param parentIter the parent iterator
057: * @param env the variable environment
058: * @param match the node matching pattern
059: */
060: public NamespaceIterator(Node node, NodeIterator parentIter,
061: ExprEnvironment env, AbstractPattern match)
062: throws XPathException {
063: super (env);
064:
065: _parentIter = parentIter;
066: _match = match;
067:
068: if (parentIter == null)
069: _node = NamespaceNode.create(node);
070:
071: _node = findFirstMatchingNode();
072: _next = _node;
073: }
074:
075: /**
076: * True if there's more data.
077: */
078: public boolean hasNext() {
079: if (_next == null) {
080: try {
081: _next = (NamespaceNode) nextNode();
082: } catch (XPathException e) {
083: log.log(Level.FINE, e.toString(), e);
084: }
085: }
086:
087: return _next != null;
088: }
089:
090: /**
091: * Returns the next selected node.
092: */
093: public Node nextNode() throws XPathException {
094: if (_next != null) {
095: _node = _next;
096: _next = null;
097:
098: return _node;
099: }
100:
101: if (_node != null) {
102: _node = (NamespaceNode) _node.getNextSibling();
103: _node = findFirstMatchingNode();
104: }
105:
106: _next = null;
107:
108: return _node;
109: }
110:
111: /**
112: * Returns the next selected node.
113: */
114: public SelectedNode nextSelectedNode() throws XPathException {
115: Node node = nextNode();
116:
117: return node == null ? null : new SelectedAttribute(node);
118: }
119:
120: /**
121: * Finds the next matching node.
122: */
123: private NamespaceNode findFirstMatchingNode() throws XPathException {
124: while (true) {
125: Node parentNode;
126:
127: if (_node != null) {
128: if (_match == null || _match.match(_node, _env)) {
129: _position++;
130: return _node;
131: } else {
132: _node = (NamespaceNode) _node.getNextSibling();
133: continue;
134: }
135: }
136:
137: else if (_parentIter == null
138: || (parentNode = _parentIter.nextNode()) == null)
139: return null;
140: else {
141: _position = 0;
142: _size = 0;
143: _node = NamespaceNode.create(parentNode);
144: }
145: }
146: }
147:
148: /**
149: * Returns the number of nodes in the context list.
150: */
151: public int getContextSize() {
152: return _position;
153: }
154:
155: public Object clone() {
156: return null;
157: }
158:
159: public String toString() {
160: return "NamespaceIterator[" + _match + "]";
161: }
162: }
|