1 61 62 package org.jaxen.function.ext; 63 64 import java.util.Collections ; 65 import java.util.List ; 66 67 import org.jaxen.Context; 68 import org.jaxen.ContextSupport; 69 import org.jaxen.Function; 70 import org.jaxen.FunctionCallException; 71 import org.jaxen.Navigator; 72 import org.jaxen.XPath; 73 import org.jaxen.function.StringFunction; 74 75 80 public class EvaluateFunction implements Function 81 { 82 public Object call( Context context, List args ) 83 throws FunctionCallException 84 { 85 if ( args.size() == 1 ) { 86 return evaluate( context, args.get(0)); 87 } 88 89 throw new FunctionCallException( "evaluate() requires one argument" ); 90 } 91 92 public static List evaluate (Context context, Object arg) 93 throws FunctionCallException 94 { 95 List contextNodes = context.getNodeSet(); 96 97 if (contextNodes.size() == 0) 98 return Collections.EMPTY_LIST; 99 100 Navigator nav = context.getNavigator(); 101 102 String xpathString; 103 if ( arg instanceof String ) 104 xpathString = (String )arg; 105 else 106 xpathString = StringFunction.evaluate(arg, nav); 107 108 try { 109 XPath xpath = nav.parseXPath(xpathString); 110 ContextSupport support = context.getContextSupport(); 111 xpath.setVariableContext( support.getVariableContext() ); 112 xpath.setFunctionContext( support.getFunctionContext() ); 113 xpath.setNamespaceContext( support.getNamespaceContext() ); 114 return xpath.selectNodes( context.duplicate() ); 115 } 116 catch ( org.jaxen.saxpath.SAXPathException e ) { 117 throw new FunctionCallException(e.toString()); 118 } 119 } 120 } 121 122 | Popular Tags |