1 61 62 63 package org.jaxen.function; 64 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 116 public class BooleanFunction implements Function 117 { 118 119 120 123 public BooleanFunction() {} 124 125 137 public Object call(Context context, 138 List args) throws FunctionCallException 139 { 140 if ( args.size() == 1 ) 141 { 142 return evaluate( args.get(0), context.getNavigator() ); 143 } 144 145 throw new FunctionCallException("boolean() requires one argument"); 146 } 147 148 165 public static Boolean evaluate(Object obj, Navigator nav) 166 { 167 if ( obj instanceof List ) 168 { 169 List list = (List ) obj; 170 171 if (list.size() == 0) 173 { 174 return Boolean.FALSE; 175 } 176 177 obj = list.get(0); 179 } 180 181 184 if ( obj instanceof Boolean ) 186 { 187 return (Boolean ) obj; 188 } 189 else if ( obj instanceof Number ) 191 { 192 double d = ((Number ) obj).doubleValue(); 193 if ( d == 0 || Double.isNaN(d) ) 194 { 195 return Boolean.FALSE; 196 } 197 return Boolean.TRUE; 198 } 199 else if ( obj instanceof String ) 201 { 202 return ( ((String )obj).length() > 0 203 ? Boolean.TRUE 204 : Boolean.FALSE ); 205 } 206 else 207 { 208 return ( obj != null ) ? Boolean.TRUE : Boolean.FALSE; 211 } 212 213 } 214 } 215 | Popular Tags |