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.XmlUtil;
032: import com.caucho.xpath.Expr;
033: import com.caucho.xpath.ExprEnvironment;
034: import com.caucho.xpath.XPathException;
035: import com.caucho.xpath.pattern.*;
036:
037: import org.w3c.dom.Element;
038: import org.w3c.dom.Node;
039:
040: import java.util.ArrayList;
041:
042: public class ObjectExpr extends Expr {
043: private int _code;
044: private String _name;
045: private Expr _left;
046: private Expr _right;
047: private Expr _third;
048:
049: public ObjectExpr(int code, ArrayList args) {
050: _code = code;
051:
052: if (args != null && args.size() > 0)
053: _left = (Expr) args.get(0);
054: if (args != null && args.size() > 1)
055: _right = (Expr) args.get(1);
056: if (args != null && args.size() > 2)
057: _third = (Expr) args.get(2);
058:
059: if (_right == null || _third == null)
060: throw new NullPointerException();
061: }
062:
063: public ObjectExpr(int code, String name) {
064: _code = code;
065: _name = name;
066: }
067:
068: /**
069: * Returns true if the expression evaluates to a node-set.
070: */
071: public boolean isNodeSet() {
072: return _code == SELF;
073: }
074:
075: /**
076: * Returns true if the expression evaluates to a node-set.
077: */
078: public boolean isString() {
079: return _code == ATTRIBUTE;
080: }
081:
082: /**
083: * Evaluates the expression as a boolean.
084: *
085: * @param node current node
086: * @param env the environment
087: *
088: * @return the boolean value
089: */
090: public boolean evalBoolean(Node node, ExprEnvironment env)
091: throws XPathException {
092: switch (_code) {
093: case IF:
094: if (_left.evalBoolean(node, env))
095: return _right.evalBoolean(node, env);
096: else
097: return _third.evalBoolean(node, env);
098:
099: case ATTRIBUTE:
100: if (node instanceof Element)
101: return !((Element) node).getAttribute(_name).equals("");
102: else
103: return false;
104:
105: case SELF:
106: return true;
107:
108: default:
109: return toBoolean(evalObject(node, env));
110: }
111: }
112:
113: /**
114: * Evaluates the expression as number.
115: *
116: * @param node current node
117: * @param env the environment
118: */
119: public double evalNumber(Node node, ExprEnvironment env)
120: throws XPathException {
121: switch (_code) {
122: case IF:
123: if (_left.evalBoolean(node, env))
124: return _right.evalNumber(node, env);
125: else
126: return _third.evalNumber(node, env);
127:
128: case ATTRIBUTE:
129: if (node instanceof Element)
130: return toDouble(((Element) node).getAttribute(_name));
131: else
132: return Double.NaN;
133:
134: case SELF:
135: return toDouble(XmlUtil.textValue(node));
136:
137: default:
138: return toDouble(evalObject(node, env));
139: }
140: }
141:
142: /**
143: * Evaluates the expression as string.
144: *
145: * @param node current node
146: * @param env the environment
147: *
148: * @return the string representation
149: */
150: public String evalString(Node node, ExprEnvironment env)
151: throws XPathException {
152: switch (_code) {
153: case IF:
154: if (_left.evalBoolean(node, env))
155: return _right.evalString(node, env);
156: else
157: return _third.evalString(node, env);
158:
159: case ATTRIBUTE:
160: if (node instanceof Element)
161: return ((Element) node).getAttribute(_name);
162: else
163: return "";
164:
165: case SELF:
166: return XmlUtil.textValue(node);
167:
168: default:
169: return toString(evalObject(node, env));
170: }
171: }
172:
173: /**
174: * Evaluates the expression as an object.
175: *
176: * @param node current node
177: * @param env the environment
178: *
179: * @return the object representation
180: */
181: public Object evalObject(Node node, ExprEnvironment env)
182: throws XPathException {
183: switch (_code) {
184: case IF:
185: if (_left.evalBoolean(node, env))
186: return _right.evalObject(node, env);
187: else
188: return _third.evalObject(node, env);
189:
190: case SELF:
191: return node;
192:
193: case ATTRIBUTE:
194: if (node instanceof Element)
195: return ((Element) node).getAttributeNode(_name);
196: else
197: return null;
198:
199: default:
200: return null;
201: }
202: }
203:
204: /**
205: * Evaluates the expression as a node set.
206: *
207: * @param node current node
208: * @param env the variable environment
209: */
210: public NodeIterator evalNodeSet(Node node, ExprEnvironment env)
211: throws XPathException {
212: switch (_code) {
213: case IF:
214: if (_left.evalBoolean(node, env))
215: return _right.evalNodeSet(node, env);
216: else
217: return _third.evalNodeSet(node, env);
218:
219: case SELF:
220: return new SingleNodeIterator(env, node);
221:
222: case ATTRIBUTE:
223: if (node instanceof Element)
224: return new SingleNodeIterator(env, ((Element) node)
225: .getAttributeNode(_name));
226: else
227: return new SingleNodeIterator(env, null);
228:
229: default:
230: return null;
231: }
232: }
233:
234: /**
235: * Convert from an expression to a pattern.
236: */
237: protected AbstractPattern toNodeList() {
238: switch (_code) {
239: case SELF:
240: return NodeTypePattern.create(new FromSelf(null),
241: NodeTypePattern.ANY);
242:
243: case ATTRIBUTE:
244: return new NodePattern(new FromAttributes(null), _name,
245: Node.ATTRIBUTE_NODE);
246:
247: default:
248: return super .toNodeList();
249: }
250: }
251:
252: public String toString() {
253: switch (_code) {
254: case IF:
255: return "if(" + _left + "," + _right + "," + _third + ")";
256:
257: case SELF:
258: return ".";
259:
260: case ATTRIBUTE:
261: return "@" + _name;
262:
263: default:
264: return super.toString();
265: }
266: }
267: }
|