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.Node;
036:
037: /**
038: * Matches the current node.
039: */
040: public class CurrentPattern extends Axis {
041: public CurrentPattern() {
042: super (null);
043: }
044:
045: /**
046: * Matches the current node
047: *
048: * @param node the starting node
049: * @param env the xpath environment
050: *
051: * @return true if the node is the current node.
052: */
053: public boolean match(Node node, ExprEnvironment env) {
054: return (node == env.getCurrentNode());
055: }
056:
057: /**
058: * Returns true if the pattern selects a single node
059: */
060: boolean isSingleSelect() {
061: return true;
062: }
063:
064: /**
065: * Creates a new node iterator.
066: *
067: * @param node the starting node
068: * @param env the xpath environment
069: * @param match the axis match pattern
070: *
071: * @return the node iterator
072: */
073: public NodeIterator createNodeIterator(Node node,
074: ExprEnvironment env, AbstractPattern match)
075: throws XPathException {
076: Node current = env.getCurrentNode();
077:
078: if (match == null || match.match(current, env))
079: return new SingleNodeIterator(env, current);
080: else
081: return null;
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 env.getCurrentNode();
093: }
094:
095: /**
096: * Returns the next node in the selection order.
097: *
098: * @param node the current node
099: * @param last the last node
100: *
101: * @return the next node
102: */
103: public Node nextNode(Node node, Node last) {
104: return null;
105: }
106:
107: /**
108: * There is only a single node in the current
109: */
110: public int position(Node node, Env env, AbstractPattern pattern) {
111: return 1;
112: }
113:
114: /**
115: * There is only a single node in the current
116: */
117: public int count(Node node, Env env, Node context) {
118: return 1;
119: }
120:
121: public String toString() {
122: return "current()";
123: }
124: }
|