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: public class FromParent extends Axis {
037: public FromParent(AbstractPattern parent) {
038: super (parent);
039:
040: if (parent == null)
041: throw new RuntimeException();
042: }
043:
044: /**
045: * Matches if a child matches the parent pattern.
046: *
047: * @param node the current node
048: * @param env the variable environment
049: *
050: * @return true if the pattern matches
051: */
052: public boolean match(Node node, ExprEnvironment env)
053: throws XPathException {
054: if (node == null)
055: return false;
056:
057: if (node.getNodeType() != node.ELEMENT_NODE
058: && node.getNodeType() != node.DOCUMENT_NODE)
059: return false;
060:
061: for (node = node.getFirstChild(); node != null; node = node
062: .getNextSibling()) {
063: if (_parent.match(node, env))
064: return true;
065: }
066:
067: return false;
068: }
069:
070: /**
071: * The parent is strictly ascending if there's only one possible selection.
072: */
073: public boolean isStrictlyAscending() {
074: return isSingleSelect();
075: }
076:
077: /**
078: * Returns true if the pattern selects a single node
079: */
080: boolean isSingleSelect() {
081: return _parent == null ? true : _parent.isSingleSelect();
082: }
083:
084: /**
085: * Returns the first node in the selection order.
086: *
087: * @param node the current node
088: *
089: * @return the first node
090: */
091: public Node firstNode(Node node, ExprEnvironment env) {
092: return node.getParentNode();
093: }
094:
095: /**
096: * Returns the next node in the selection order.
097: *
098: * @param node the current node
099: * @param lastNode the last node
100: *
101: * @return the next node
102: */
103: public Node nextNode(Node node, Node lastNode) {
104: return null;
105: }
106:
107: public String toString() {
108: return getPrefix() + "parent::";
109: }
110: }
|