1 61 62 package org.jaxen.function; 63 64 import java.util.Iterator ; 65 import java.util.List ; 66 67 import org.jaxen.Context; 68 import org.jaxen.Function; 69 import org.jaxen.FunctionCallException; 70 import org.jaxen.Navigator; 71 72 141 public class NumberFunction implements Function 142 { 143 144 private final static Double NaN = new Double ( Double.NaN ); 145 146 147 150 public NumberFunction() {} 151 152 166 public Object call(Context context, List args) throws FunctionCallException 167 { 168 if (args.size() == 1) 169 { 170 return evaluate( args.get(0), context.getNavigator() ); 171 } 172 else if (args.size() == 0) 173 { 174 return evaluate( context.getNodeSet(), context.getNavigator() ); 175 } 176 177 throw new FunctionCallException( "number() takes at most one argument." ); 178 } 179 180 189 public static Double evaluate(Object obj, Navigator nav) 190 { 191 if( obj instanceof Double ) 192 { 193 return (Double ) obj; 194 } 195 else if ( obj instanceof String ) 196 { 197 String str = (String ) obj; 198 try 199 { 200 Double doubleValue = new Double ( str ); 201 return doubleValue; 202 } 203 catch (NumberFormatException e) 204 { 205 return NaN; 206 } 207 } 208 else if ( obj instanceof List || obj instanceof Iterator ) 209 { 210 return evaluate( StringFunction.evaluate( obj, nav ), nav ); 211 } 212 else if ( nav.isElement( obj ) || nav.isAttribute( obj ) ) 213 { 214 return evaluate( StringFunction.evaluate( obj, nav ), nav ); 215 } 216 else if ( obj instanceof Boolean ) 217 { 218 if ( obj == Boolean.TRUE ) 219 { 220 return new Double ( 1 ); 221 } 222 else 223 { 224 return new Double ( 0 ); 225 } 226 } 227 return NaN; 228 } 229 230 237 public static boolean isNaN( double val ) 238 { 239 return Double.isNaN(val); 240 } 241 242 249 public static boolean isNaN( Double val ) 250 { 251 return val.equals( NaN ); 252 } 253 254 } 255 | Popular Tags |