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.util.CharBuffer;
032: import com.caucho.xpath.Expr;
033: import com.caucho.xpath.ExprEnvironment;
034: import com.caucho.xpath.XPathException;
035: import com.caucho.xpath.pattern.NodeArrayListIterator;
036: import com.caucho.xpath.pattern.NodeIterator;
037: import com.caucho.xpath.pattern.NodeListIterator;
038: import com.caucho.xpath.pattern.SingleNodeIterator;
039:
040: import org.w3c.dom.Node;
041: import org.w3c.dom.NodeList;
042:
043: import java.util.ArrayList;
044:
045: public abstract class Var {
046: /**
047: * Returns the value as a boolean.
048: */
049: boolean getBoolean() throws XPathException {
050: return Expr.toBoolean(getObject());
051: }
052:
053: /**
054: * Returns the value as a double.
055: */
056: double getDouble() throws XPathException {
057: Object o = getObject();
058: return Expr.toDouble(getObject());
059: }
060:
061: /**
062: * Returns the value as a string.
063: */
064: String getString() throws XPathException {
065: return Expr.toString(getObject());
066: }
067:
068: /**
069: * Returns the value as a string.
070: */
071: void getString(CharBuffer cb) throws XPathException {
072: cb.append(getString());
073: }
074:
075: /**
076: * Returns the value as a node set.
077: */
078: NodeIterator getNodeSet(ExprEnvironment env) throws XPathException {
079: Object obj = getObject();
080:
081: if (obj instanceof Node)
082: return new SingleNodeIterator(env, (Node) obj);
083: else if (obj instanceof NodeList)
084: return new NodeListIterator(env, (NodeList) obj);
085: else if (obj instanceof ArrayList)
086: return new NodeArrayListIterator(env, (ArrayList) obj);
087: else
088: return new SingleNodeIterator(env, null);
089: }
090:
091: /**
092: * Returns the value as an object.
093: */
094: abstract Object getObject();
095:
096: /**
097: * Frees the var.
098: */
099: public void free() {
100: }
101: }
|