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.xml.XmlChar;
033: import com.caucho.xpath.Expr;
034: import com.caucho.xpath.ExprEnvironment;
035: import com.caucho.xpath.XPathException;
036:
037: import org.w3c.dom.Node;
038:
039: /**
040: * Implements the builtin XPath string expressions.
041: */
042: abstract public class AbstractStringExpr extends Expr {
043: /**
044: * The StringExpr returns a string value.
045: */
046: public boolean isString() {
047: return true;
048: }
049:
050: /**
051: * Evaluates the expression as an string.
052: *
053: * @param node the current node
054: * @param env the variable environment.
055: *
056: * @return the string representation of the expression.
057: */
058: abstract public String evalString(Node node, ExprEnvironment env)
059: throws XPathException;
060:
061: /**
062: * Evaluate the expression as a boolean, i.e. evaluate it as a string
063: * and then convert it to a boolean.
064: *
065: * @param node the current node
066: * @param env the variable environment.
067: *
068: * @return the boolean representation of the expression.
069: */
070: public boolean evalBoolean(Node node, ExprEnvironment env)
071: throws XPathException {
072: String string = evalString(node, env);
073:
074: return string != null && string.length() > 0;
075: }
076:
077: /**
078: * Evaluate the expression as a double, i.e. evaluate it as a string
079: * and then convert it to a double.
080: *
081: * @param node the current node
082: * @param env the variable environment.
083: *
084: * @return the numeric representation of the expression.
085: */
086: public double evalNumber(Node node, ExprEnvironment env)
087: throws XPathException {
088: return stringToNumber(evalString(node, env));
089: }
090:
091: /**
092: * Evaluate the expression as an object, i.e. return the string value.
093: *
094: * @param node the current node
095: * @param env the variable environment.
096: *
097: * @return the boolean representation of the expression.
098: */
099: public Object evalObject(Node node, ExprEnvironment env)
100: throws XPathException {
101: return evalString(node, env);
102: }
103:
104: /**
105: * Normalize the string, converting all whitespace to a space and
106: * eliminating consecutive spaces.
107: */
108: protected static String normalize(String string) {
109: CharBuffer result = new CharBuffer();
110:
111: int i = 0;
112: int len = string.length();
113: for (; i < len && XmlChar.isWhitespace(string.charAt(i)); i++) {
114: }
115:
116: boolean lastIsWhitespace = false;
117: for (; i < len; i++) {
118: if (XmlChar.isWhitespace(string.charAt(i))) {
119: lastIsWhitespace = true;
120: } else if (lastIsWhitespace) {
121: result.append(' ');
122: result.append(string.charAt(i));
123: lastIsWhitespace = false;
124: } else
125: result.append(string.charAt(i));
126: }
127:
128: return result.toString();
129: }
130: }
|