1 61 62 package org.jaxen.function; 63 64 import java.util.ArrayList ; 65 import java.util.Collections ; 66 import java.util.Iterator ; 67 import java.util.List ; 68 import java.util.StringTokenizer ; 69 70 import org.jaxen.Context; 71 import org.jaxen.Function; 72 import org.jaxen.FunctionCallException; 73 import org.jaxen.Navigator; 74 75 90 public class IdFunction implements Function 91 { 92 93 96 public IdFunction() {} 97 98 111 public Object call(Context context, List args) throws FunctionCallException 112 { 113 if ( args.size() == 1 ) { 114 return evaluate( context.getNodeSet(), 115 args.get(0), context.getNavigator() ); 116 } 117 118 throw new FunctionCallException( "id() requires one argument" ); 119 } 120 121 133 public static List evaluate(List contextNodes, Object arg, Navigator nav) 134 { 135 if (contextNodes.size() == 0) return Collections.EMPTY_LIST; 136 137 List nodes = new ArrayList (); 138 139 Object contextNode = contextNodes.get(0); 140 141 if (arg instanceof List ) { 142 Iterator iter = ((List )arg).iterator(); 143 while (iter.hasNext()) { 144 String id = StringFunction.evaluate(iter.next(), nav); 145 nodes.addAll( evaluate( contextNodes, id, nav ) ); 146 } 147 } 148 else { 149 String ids = StringFunction.evaluate(arg, nav); 150 StringTokenizer tok = new StringTokenizer (ids, " \t\n\r"); 151 while (tok.hasMoreTokens()) { 152 String id = tok.nextToken(); 153 Object node = nav.getElementById(contextNode, id); 154 if (node != null) { 155 nodes.add(node); 156 } 157 } 158 } 159 return nodes; 160 } 161 162 } 163 164 | Popular Tags |