KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jbpm.bpel.data.xpath;
2
3 import java.util.List JavaDoc;
4
5 import javax.xml.namespace.QName JavaDoc;
6
7 import org.jaxen.*;
8 import org.jaxen.function.StringFunction;
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 global property values from variables.
15  * <p><code><i>any</i> bpws:getVariableData(<i>string</i> variableName,
16  * <i>string</i> propertyName?)</code></p>
17  * @see "WS-BPEL 2.0 &sect;9.1, 14.1"
18  * @author Alejandro Guízar
19  * @version $Revision: 1.2 $ $Date: 2005/05/31 00:49:52 $
20  */

21 public class GetVariablePropertyFunction implements Function {
22   
23   public Object JavaDoc call(Context context, List JavaDoc args) throws FunctionCallException {
24     if (args.size() != 2) {
25       throw new FunctionCallException("getVariableProperty() requires two arguments");
26     }
27     return evaluate(args.get(0), args.get(1), context);
28   }
29   
30   public static Object JavaDoc evaluate(Object JavaDoc variableArg, Object JavaDoc propertyArg, Context context)
31   throws FunctionCallException {
32     // convert arguments to strings
33
Navigator nav = context.getNavigator();
34     String JavaDoc variableName = StringFunction.evaluate(variableArg, nav);
35     String JavaDoc propertyName = StringFunction.evaluate(propertyArg, nav);
36     // resolve property name
37
ContextSupport sup = context.getContextSupport();
38     NamespaceContext nsContext = sup.getNamespaceContext();
39     QName JavaDoc propertyQName = toQName(propertyName, nsContext);
40     // lookup variable instance
41
ScopeVariables variables = (ScopeVariables) sup.getVariableContext();
42     VariableInstance instance = variables.getVariable(variableName);
43     if (instance == null) {
44       throw new FunctionCallException("variable not found: " + variableName);
45     }
46     if (!(instance instanceof MessageVariableInstance)) {
47       throw new FunctionCallException("variable does not have a message type: " + variableName);
48     }
49     // get property value
50
return ((MessageVariableInstance) instance).getProperty(propertyQName);
51   }
52
53   public static QName JavaDoc toQName(String JavaDoc prefixedName, NamespaceContext namespaceContext) {
54     QName JavaDoc qName;
55     int colonIndex = prefixedName.indexOf(':');
56     if (colonIndex == -1) {
57       qName = new QName JavaDoc(prefixedName);
58     }
59     else {
60       String JavaDoc name = prefixedName.substring(colonIndex + 1);
61       String JavaDoc prefix = prefixedName.substring(0, colonIndex);
62       String JavaDoc uri = namespaceContext.translateNamespacePrefixToUri(prefix);
63       qName = new QName JavaDoc(uri, name, prefix);
64     }
65     return qName;
66   }
67 }
68
Popular Tags