1 package org.jbpm.bpel.data.xpath; 2 3 import java.util.List ; 4 5 import org.jaxen.*; 6 import org.jaxen.expr.LocationPath; 7 import org.jaxen.function.StringFunction; 8 import org.w3c.dom.Element ; 9 10 import org.jbpm.bpel.data.exe.MessageVariableInstance; 11 import org.jbpm.bpel.data.exe.VariableInstance; 12 13 21 public class GetVariableDataFunction implements Function { 22 23 public Object call(Context context, List args) throws FunctionCallException { 24 Object value; 25 26 switch (args.size()) { 27 case 1: 28 value = evaluate(args.get(0), context); 29 break; 30 case 2: 31 value = evaluate(args.get(0), args.get(1), context); 32 break; 33 case 3: 34 value = evaluate(args.get(0), args.get(1), args.get(2), context); 35 break; 36 default: 37 throw new FunctionCallException("getVariableData() requires one to three arguments"); 38 } 39 return value; 40 } 41 42 public static Object evaluate(Object variableArg, Context context) throws FunctionCallException { 43 String variableName = StringFunction.evaluate(variableArg, context.getNavigator()); 45 ScopeVariables variables = (ScopeVariables) context.getContextSupport().getVariableContext(); 47 VariableInstance instance = variables.getVariable(variableName); 48 if (instance == null) { 49 throw new FunctionCallException("variable not found: " + variableName); 50 } 51 return instance.getValue(); 53 } 54 55 public static Object evaluate(Object variableArg, Object partArg, Context context) 56 throws FunctionCallException { 57 Navigator nav = context.getNavigator(); 59 String variableName = StringFunction.evaluate(variableArg, nav); 60 String partName = StringFunction.evaluate(partArg, nav); 61 ScopeVariables variables = (ScopeVariables) context.getContextSupport().getVariableContext(); 63 VariableInstance instance = variables.getVariable(variableName); 64 if (instance == null) { 65 throw new FunctionCallException("variable not found: " + variableName); 66 } 67 if (!(instance instanceof MessageVariableInstance)) { 68 throw new FunctionCallException("variable does not have a message type: " + variableName); 69 } 70 return ((MessageVariableInstance) instance).getPart(partName); 72 } 73 74 public static Object evaluate(Object variableArg, Object partArg, Object locationArg, Context context) 75 throws FunctionCallException { 76 Navigator nav = context.getNavigator(); 78 String variableName = StringFunction.evaluate(variableArg, nav); 79 String partName = StringFunction.evaluate(partArg, nav); 80 String locationString = StringFunction.evaluate(locationArg, nav); 81 ContextSupport support = context.getContextSupport(); 83 ScopeVariables variables = (ScopeVariables) support.getVariableContext(); 84 VariableInstance instance = variables.getVariable(variableName); 85 if (instance == null) { 86 throw new FunctionCallException("variable not found: " + variableName); 87 } 88 if (!(instance instanceof MessageVariableInstance)) { 89 throw new FunctionCallException("variable does not have a message type: " + variableName); 90 } 91 try { 92 locationString = locationString.trim(); 93 if (locationString.charAt(0) != '/') { 95 throw new FunctionCallException("Query is not an absolute location path: " + locationString); 96 } 97 String relativeString; 99 if (locationString.charAt(1) != '/') { 100 relativeString = locationString.substring(1); 101 } 102 else { 103 relativeString = '.' + locationString; 105 } 106 RelativePath relativePath = new RelativePath(relativeString); 107 if (!(relativePath.getRootExpr() instanceof LocationPath)) { 108 throw new FunctionCallException("Query is not an absolute location path: " + locationString); 109 } 110 relativePath.setNamespaceContext(support.getNamespaceContext()); 111 Element part = ((MessageVariableInstance) instance).getPart(partName); 112 return relativePath.evaluate(part.getParentNode()); 113 } 114 catch (JaxenException e) { 115 throw new FunctionCallException("selection failure (internal error)", e); 116 } 117 } 118 } 119 | Popular Tags |