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 88 public class SubstringBeforeFunction implements Function 89 { 90 91 92 95 public SubstringBeforeFunction() {} 96 97 98 113 public Object call(Context context, 114 List args) throws FunctionCallException 115 { 116 if (args.size() == 2) 117 { 118 return evaluate( args.get(0), 119 args.get(1), 120 context.getNavigator() ); 121 } 122 123 throw new FunctionCallException( "substring-before() requires two arguments." ); 124 } 125 126 140 public static String evaluate(Object strArg, 141 Object matchArg, 142 Navigator nav) 143 { 144 String str = StringFunction.evaluate( strArg, 145 nav ); 146 147 String match = StringFunction.evaluate( matchArg, 148 nav ); 149 150 int loc = str.indexOf(match); 151 152 if ( loc < 0 ) 153 { 154 return ""; 155 } 156 157 return str.substring(0, loc); 158 159 } 160 } 161 | Popular Tags |