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.Env;
032: import com.caucho.xpath.ExprEnvironment;
033: import com.caucho.xpath.XPathException;
034:
035: import org.w3c.dom.Document;
036: import org.w3c.dom.Node;
037:
038: /**
039: * Represents any of the axis patterns, i.e. those patterns that
040: * select many nodes from a single node.
041: */
042: abstract class Axis extends AbstractPattern {
043: Axis(AbstractPattern parent) {
044: super (parent);
045: }
046:
047: /**
048: * Calculates the position of the node in its context.
049: *
050: * @param node the current node
051: * @param env the variable environment
052: * @param pattern the position pattern
053: *
054: * @return the node's position.
055: */
056: public int position(Node node, Env env, AbstractPattern pattern)
057: throws XPathException {
058: return 1;
059: }
060:
061: /**
062: * Counts the nodes within the axis matching the pattern.
063: *
064: * @param node the current node
065: * @param env the variable environment
066: * @param pattern the position pattern
067: *
068: * @return the count of the node list.
069: */
070: public int count(Node node, Env env, AbstractPattern pattern)
071: throws XPathException {
072: return 1;
073: }
074:
075: /**
076: * Creates a new node iterator.
077: *
078: * @param node the starting node
079: * @param env the variable environment
080: * @param match the axis match pattern
081: *
082: * @return the node iterator
083: */
084: public NodeIterator createNodeIterator(Node node,
085: ExprEnvironment env, AbstractPattern match)
086: throws XPathException {
087: if (_parent == null)
088: return new AxisIterator(null, this , node, env, match);
089: else if (_parent instanceof FromRoot) {
090: if (node instanceof Document)
091: return new AxisIterator(null, this , node, env, match);
092: else if (node != null)
093: return new AxisIterator(null, this , node
094: .getOwnerDocument(), env, match);
095: }
096:
097: NodeIterator parentIter;
098: parentIter = _parent.createNodeIterator(node, env, _parent
099: .copyPosition());
100:
101: return new AxisIterator(parentIter, this , null, env, match);
102: }
103:
104: /**
105: * Returns the node itself for the axis.
106: */
107: public AbstractPattern copyAxis() {
108: return this ;
109: }
110:
111: /**
112: * Returns null since the axis isn't part of the position pattern.
113: */
114: public AbstractPattern copyPosition() {
115: return null;
116: }
117:
118: /**
119: * Returns true if the pattern selects a single node
120: */
121: boolean isSingleSelect() {
122: return false;
123: }
124:
125: /**
126: * Returns true if the pattern is strictly ascending.
127: */
128: public boolean isStrictlyAscending() {
129: return isSingleLevel();
130: }
131:
132: /**
133: * Returns true if the pattern's selector returns unique nodes.
134: */
135: public boolean isUnique() {
136: return isStrictlyAscending();
137: }
138: }
|