KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xpath > expr > AbstractStringExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xpath.expr;
30
31 import com.caucho.util.CharBuffer;
32 import com.caucho.xml.XmlChar;
33 import com.caucho.xpath.Expr;
34 import com.caucho.xpath.ExprEnvironment;
35 import com.caucho.xpath.XPathException;
36
37 import org.w3c.dom.Node JavaDoc;
38
39 /**
40  * Implements the builtin XPath string expressions.
41  */

42 abstract public class AbstractStringExpr extends Expr {
43   /**
44    * The StringExpr returns a string value.
45    */

46   public boolean isString()
47   {
48     return true;
49   }
50
51   /**
52    * Evaluates the expression as an string.
53    *
54    * @param node the current node
55    * @param env the variable environment.
56    *
57    * @return the string representation of the expression.
58    */

59   abstract public String JavaDoc evalString(Node JavaDoc node, ExprEnvironment env)
60     throws XPathException;
61
62   /**
63    * Evaluate the expression as a boolean, i.e. evaluate it as a string
64    * and then convert it to a boolean.
65    *
66    * @param node the current node
67    * @param env the variable environment.
68    *
69    * @return the boolean representation of the expression.
70    */

71   public boolean evalBoolean(Node JavaDoc node, ExprEnvironment env)
72     throws XPathException
73   {
74     String JavaDoc string = evalString(node, env);
75
76     return string != null && string.length() > 0;
77   }
78
79   /**
80    * Evaluate the expression as a double, i.e. evaluate it as a string
81    * and then convert it to a double.
82    *
83    * @param node the current node
84    * @param env the variable environment.
85    *
86    * @return the numeric representation of the expression.
87    */

88   public double evalNumber(Node JavaDoc node, ExprEnvironment env)
89     throws XPathException
90   {
91     return stringToNumber(evalString(node, env));
92   }
93
94   /**
95    * Evaluate the expression as an object, i.e. return the string value.
96    *
97    * @param node the current node
98    * @param env the variable environment.
99    *
100    * @return the boolean representation of the expression.
101    */

102   public Object JavaDoc evalObject(Node JavaDoc node, ExprEnvironment env)
103     throws XPathException
104   {
105     return evalString(node, env);
106   }
107
108   /**
109    * Normalize the string, converting all whitespace to a space and
110    * eliminating consecutive spaces.
111    */

112   protected static String JavaDoc normalize(String JavaDoc string)
113   {
114     CharBuffer result = new CharBuffer();
115
116     int i = 0;
117     int len = string.length();
118     for (; i < len && XmlChar.isWhitespace(string.charAt(i)); i++) {
119     }
120
121     boolean lastIsWhitespace = false;
122     for (; i < len; i++) {
123       if (XmlChar.isWhitespace(string.charAt(i))) {
124     lastIsWhitespace = true;
125       }
126       else if (lastIsWhitespace) {
127     result.append(' ');
128     result.append(string.charAt(i));
129     lastIsWhitespace = false;
130       }
131       else
132     result.append(string.charAt(i));
133     }
134
135     return result.toString();
136   }
137 }
138
Popular Tags