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.expr;
030:
031: import com.caucho.xml.NodeListImpl;
032: import com.caucho.xml.XmlUtil;
033: import com.caucho.xpath.Expr;
034: import com.caucho.xpath.ExprEnvironment;
035: import com.caucho.xpath.XPathException;
036: import com.caucho.xpath.pattern.*;
037:
038: import org.w3c.dom.Node;
039:
040: public class NodeSetExpr extends Expr {
041: private AbstractPattern _pattern;
042:
043: NodeSetExpr(AbstractPattern pattern) {
044: _pattern = pattern;
045: }
046:
047: /**
048: * Creates an expr, handling some special cases.
049: */
050: public static Expr create(AbstractPattern pattern) {
051: if (pattern instanceof NodeTypePattern
052: && pattern.getParent() instanceof FromSelf
053: && pattern.toString().equals("."))
054: return new ObjectExpr(SELF, ".");
055: else if (pattern instanceof FromContext
056: && ((FromContext) pattern).getCount() == 0
057: && pattern.getParent() == null)
058: return new ObjectExpr(SELF, ".");
059: else if (pattern instanceof NodePattern
060: && pattern.getParent() instanceof FromAttributes
061: && pattern.getParent().getParent() instanceof FromContext
062: && ((FromContext) pattern.getParent().getParent())
063: .getCount() == 0)
064: return new ObjectExpr(ATTRIBUTE, ((NodePattern) pattern)
065: .getNodeName());
066: else
067: return new NodeSetExpr(pattern);
068: }
069:
070: /**
071: * Returns the underlying pattern.
072: */
073: public AbstractPattern getPattern() {
074: return _pattern;
075: }
076:
077: /**
078: * NodeSetExprs prefer to be node sets.
079: */
080: public boolean isNodeSet() {
081: return true;
082: }
083:
084: /**
085: * Returns the value of the expression as a number.
086: *
087: * @param node the current node
088: * @param env the variable environment.
089: */
090: public double evalNumber(Node node, ExprEnvironment env)
091: throws XPathException {
092: Node value = _pattern.findAny(node, env);
093:
094: if (value == null)
095: return Double.NaN;
096:
097: String string = XmlUtil.textValue(value);
098:
099: return stringToNumber(string);
100: }
101:
102: /**
103: * Returns true if there are any patterns matching the pattern.
104: *
105: * @param node the current node
106: * @param env the variable environment.
107: */
108: public boolean evalBoolean(Node node, ExprEnvironment env)
109: throws XPathException {
110: return _pattern.findAny(node, env) != null;
111: }
112:
113: /**
114: * Returns the value of the node set expression as a string.
115: * The value is the text value of the first node.
116: *
117: * @param node the current node
118: * @param env the variable environment
119: *
120: * @return the combined text value of the node.
121: */
122: public String evalString(Node node, ExprEnvironment env)
123: throws XPathException {
124: Node value = _pattern.findAny(node, env);
125:
126: if (value == null)
127: return "";
128: else
129: return XmlUtil.textValue(value);
130: }
131:
132: /**
133: * Evaluate a node-set object, returning an ArrayList of the node set.
134: *
135: * @param node the current node
136: * @param env the variable environment
137: *
138: * @return an array list of the nodes
139: */
140: public Object evalObject(Node node, ExprEnvironment env)
141: throws XPathException {
142: NodeListImpl list = new NodeListImpl();
143:
144: NodeIterator iter = _pattern.select(node, env);
145:
146: Node value = null;
147: while ((value = iter.nextNode()) != null)
148: list.add(value);
149:
150: return list;
151: }
152:
153: /**
154: * Evaluate a node-set object, returning an iterator of the node set.
155: *
156: * @param node the current node
157: * @param env the variable environment
158: *
159: * @return an iterator of the nodes
160: */
161: public NodeIterator evalNodeSet(Node node, ExprEnvironment env)
162: throws XPathException {
163: return _pattern.select(node, env);
164: }
165:
166: /**
167: * Convert from an expression to a pattern.
168: */
169: protected AbstractPattern toNodeList() {
170: return _pattern;
171: }
172:
173: public boolean equals(Object b) {
174: if (!(b instanceof NodeSetExpr))
175: return false;
176:
177: return _pattern.equals(((NodeSetExpr) b)._pattern);
178: }
179:
180: public String toString() {
181: return _pattern.toString();
182: }
183: }
|