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.xml.XmlUtil;
032: import com.caucho.xpath.Env;
033: import com.caucho.xpath.ExprEnvironment;
034: import com.caucho.xpath.XPathException;
035:
036: import org.w3c.dom.Node;
037:
038: /**
039: * Implements the ancestor:: axis.
040: */
041: public class FromAncestors extends Axis {
042: private boolean _self;
043:
044: public FromAncestors(AbstractPattern parent, boolean self) {
045: super (parent);
046: _self = self;
047:
048: if (parent == null)
049: throw new RuntimeException();
050: }
051:
052: /**
053: * Matches if a descendant matches the parent pattern.
054: *
055: * @param node the current node
056: * @param env the variable environment
057: *
058: * @return true if the pattern matches
059: */
060: public boolean match(Node node, ExprEnvironment env)
061: throws XPathException {
062: if (node == null)
063: return false;
064:
065: Node lastNode = lastDescendantNode(node);
066:
067: if (!_self)
068: node = XmlUtil.getNext(node);
069:
070: for (; node != null && node != lastNode; node = XmlUtil
071: .getNext(node)) {
072: if (_parent.match(node, env))
073: return true;
074: }
075:
076: return false;
077: }
078:
079: public boolean isAscending() {
080: return false;
081: }
082:
083: /**
084: * Returns the first node in the selection order.
085: *
086: * @param node the current node
087: *
088: * @return the first node
089: */
090: public Node firstNode(Node node, ExprEnvironment env) {
091: if (_self)
092: return node;
093: else
094: return node.getParentNode();
095: }
096:
097: /**
098: * Returns the next node in the selection order.
099: *
100: * @param node the current node
101: * @param last the last node
102: *
103: * @return the next node
104: */
105: public Node nextNode(Node node, Node last) {
106: return (node == null) ? null : node.getParentNode();
107: }
108:
109: /**
110: * The ancestor position is the number of matching nodes between it
111: * and an axis-context.
112: */
113: public int position(Node node, Env env, AbstractPattern pattern)
114: throws XPathException {
115: int index = env.getPositionIndex();
116:
117: Node lastNode = lastDescendantNode(node);
118:
119: Node axis = _self ? node : XmlUtil.getNext(node);
120:
121: for (; index >= 0; index--) {
122: for (; axis != lastNode && axis != null; axis = XmlUtil
123: .getNext(axis)) {
124: if (_parent.match(axis, env))
125: break;
126: }
127:
128: if (index > 0)
129: axis = XmlUtil.getNext(axis);
130: }
131:
132: if (axis == lastNode)
133: return 0;
134:
135: Node next = XmlUtil.getNext(axis);
136: for (; next != lastNode; next = XmlUtil.getNext(next)) {
137: if (_parent.match(next, env)) {
138: env.setMorePositions(true);
139: break;
140: }
141: }
142:
143: Node a1 = axis;
144:
145: if (!_self && axis != null)
146: axis = axis.getParentNode();
147:
148: int count = 0;
149: for (; axis != null; axis = axis.getParentNode()) {
150: if (pattern.match(axis, env))
151: count++;
152:
153: if (node == axis)
154: break;
155: }
156:
157: return count;
158: }
159:
160: /**
161: * Returns the last node in the selection order.
162: *
163: * @param node the current node
164: *
165: * @return the last node
166: */
167: private Node lastDescendantNode(Node node) {
168: Node last = node;
169:
170: for (; last != null && last.getNextSibling() == null; last = last
171: .getParentNode()) {
172: }
173:
174: return last != null ? last.getNextSibling() : null;
175: }
176:
177: /**
178: * counts the number of matching ancestors from the axis context
179: */
180: public int count(Node node, Env env, AbstractPattern pattern) {
181: throw new RuntimeException();
182: }
183:
184: public String toString() {
185: return getPrefix()
186: + (_self ? "ancestor-or-self::" : "ancestor::");
187: }
188: }
|