KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > data > xpath > GetVariableDataFunction


1 package org.jbpm.bpel.data.xpath;
2
3 import java.util.List JavaDoc;
4
5 import org.jaxen.*;
6 import org.jaxen.expr.LocationPath;
7 import org.jaxen.function.StringFunction;
8 import org.w3c.dom.Element JavaDoc;
9
10 import org.jbpm.bpel.data.exe.MessageVariableInstance;
11 import org.jbpm.bpel.data.exe.VariableInstance;
12
13 /**
14  * BPEL library function to extract arbitrary values from variables.
15  * <p><code><i>any</i> bpws:getVariableData(<i>string</i> variableName,
16  * <i>string</i> partName?, <i>string</i> locationPath?)</code></p>
17  * @see "WS-BPEL 2.0 &sect;14.1"
18  * @author Alejandro Guízar
19  * @version $Revision: 1.3 $ $Date: 2005/05/31 00:49:53 $
20  */

21 public class GetVariableDataFunction implements Function {
22   
23   public Object JavaDoc call(Context context, List JavaDoc args) throws FunctionCallException {
24     Object JavaDoc 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 JavaDoc evaluate(Object JavaDoc variableArg, Context context) throws FunctionCallException {
43     // convert argument to string
44
String JavaDoc variableName = StringFunction.evaluate(variableArg, context.getNavigator());
45     // lookup variable instance
46
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     // get the variable value
52
return instance.getValue();
53   }
54   
55   public static Object JavaDoc evaluate(Object JavaDoc variableArg, Object JavaDoc partArg, Context context)
56   throws FunctionCallException {
57     // convert arguments to strings
58
Navigator nav = context.getNavigator();
59     String JavaDoc variableName = StringFunction.evaluate(variableArg, nav);
60     String JavaDoc partName = StringFunction.evaluate(partArg, nav);
61     // lookup variable instance
62
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     // get the part value
71
return ((MessageVariableInstance) instance).getPart(partName);
72   }
73   
74   public static Object JavaDoc evaluate(Object JavaDoc variableArg, Object JavaDoc partArg, Object JavaDoc locationArg, Context context)
75   throws FunctionCallException {
76     // convert arguments to strings
77
Navigator nav = context.getNavigator();
78     String JavaDoc variableName = StringFunction.evaluate(variableArg, nav);
79     String JavaDoc partName = StringFunction.evaluate(partArg, nav);
80     String JavaDoc locationString = StringFunction.evaluate(locationArg, nav);
81     // lookup variable instance
82
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       // is the location path absolute?
94
if (locationString.charAt(0) != '/') {
95         throw new FunctionCallException("Query is not an absolute location path: " + locationString);
96       }
97       // make the location path relative
98
String JavaDoc relativeString;
99       if (locationString.charAt(1) != '/') {
100         relativeString = locationString.substring(1);
101       }
102       else {
103         // abbreviature for the descendant-or-self axis
104
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 JavaDoc 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