1 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 ; 38 39 42 abstract public class AbstractStringExpr extends Expr { 43 46 public boolean isString() 47 { 48 return true; 49 } 50 51 59 abstract public String evalString(Node node, ExprEnvironment env) 60 throws XPathException; 61 62 71 public boolean evalBoolean(Node node, ExprEnvironment env) 72 throws XPathException 73 { 74 String string = evalString(node, env); 75 76 return string != null && string.length() > 0; 77 } 78 79 88 public double evalNumber(Node node, ExprEnvironment env) 89 throws XPathException 90 { 91 return stringToNumber(evalString(node, env)); 92 } 93 94 102 public Object evalObject(Node node, ExprEnvironment env) 103 throws XPathException 104 { 105 return evalString(node, env); 106 } 107 108 112 protected static String normalize(String 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 |