Prototype a way to access the context node for sca XPath funcitons

git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@750627 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
rfeng 2009-03-05 22:09:33 +00:00
parent cf46fb5463
commit 534f392233
3 changed files with 38 additions and 14 deletions
java/sca/modules/policy-xml/src
main/java/org/apache/tuscany/sca/policy/xml
test/java/org/apache/tuscany/sca/policy/xml

View file

@ -23,16 +23,23 @@ import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
import javax.xml.xpath.XPathFunction;
import javax.xml.xpath.XPathFunctionException;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* The SCA-defined XPath function
*/
public class PolicyXPathFunction implements XPathFunction {
private static Logger logger = Logger.getLogger(PolicyXPathFunction.class.getName());
static final QName InterfaceRef = new QName(PolicyConstants.SCA11_NS, "InterfaceRef");
static final QName OperationRef = new QName(PolicyConstants.SCA11_NS, "OperationRef");
static final QName MessageRef = new QName(PolicyConstants.SCA11_NS, "MessageRef");
@ -51,12 +58,25 @@ public class PolicyXPathFunction implements XPathFunction {
this.functionName = functionName;
}
private Node getContextNode(List args) {
if (args.size() >= 2) {
NodeList nodeList = (NodeList)args.get(1);
if (nodeList.getLength() > 0) {
return nodeList.item(0);
}
}
return null;
}
public Object evaluate(List args) throws XPathFunctionException {
System.out.println(functionName + "(" + args + ")");
// FIXME: [rfeng] To be implemented
if (logger.isLoggable(Level.FINE)) {
logger.fine(functionName + "(" + args + ")");
}
String arg = (String)args.get(0);
Node node = getContextNode(args);
if (InterfaceRef.equals(functionName)) {
return evaluateInterface(arg);
return evaluateInterface(arg, node);
} else if (OperationRef.equals(functionName)) {
String[] params = arg.split("/");
if (params.length != 2) {
@ -64,7 +84,7 @@ public class PolicyXPathFunction implements XPathFunction {
}
String interfaceName = params[0];
String operationName = params[1];
return evaluateOperation(interfaceName, operationName);
return evaluateOperation(interfaceName, operationName, node);
} else if (MessageRef.equals(functionName)) {
String[] params = arg.split("/");
if (params.length != 3) {
@ -73,34 +93,34 @@ public class PolicyXPathFunction implements XPathFunction {
String interfaceName = params[0];
String operationName = params[1];
String messageName = params[2];
return evaluateMessage(interfaceName, operationName, messageName);
return evaluateMessage(interfaceName, operationName, messageName, node);
} else if (URIRef.equals(functionName)) {
return evaluateURI(arg);
return evaluateURI(arg, node);
} else if (IntentRefs.equals(functionName)) {
String[] intents = arg.split("(\\s)+");
return evaluateIntents(intents);
return evaluateIntents(intents, node);
} else {
return Boolean.FALSE;
}
}
private Boolean evaluateInterface(String interfaceName) {
private Boolean evaluateInterface(String interfaceName, Node node) {
return Boolean.FALSE;
}
private Boolean evaluateOperation(String interfaceName, String operationName) {
private Boolean evaluateOperation(String interfaceName, String operationName, Node node) {
return Boolean.FALSE;
}
private Boolean evaluateMessage(String interfaceName, String operationName, String messageName) {
private Boolean evaluateMessage(String interfaceName, String operationName, String messageName, Node node) {
return Boolean.FALSE;
}
private Boolean evaluateURI(String uri) {
private Boolean evaluateURI(String uri, Node node) {
return Boolean.FALSE;
}
private Boolean evaluateIntents(String[] intents) {
private Boolean evaluateIntents(String[] intents, Node node) {
return Boolean.FALSE;
}

View file

@ -57,7 +57,10 @@ public class PolicyXPathFunctionResolver implements XPathFunctionResolver {
throw new NullPointerException("Function name is null");
}
if (PolicyXPathFunction.functions.contains(functionName)) {
if (arity == 1) {
if (arity >= 1) {
// We are relaxing the arity here so that we can pass in the context node
// by modifying the original xpath so that sca functions take self::node()
// as the 2nd argument
return new PolicyXPathFunction(namespaceContext, functionName);
} else {
throw new IllegalArgumentException("Invalid number of arguments: " + arity);

View file

@ -55,7 +55,8 @@ public class PolicyXPathFunctionResolverTestCase {
@Test
public void testIntentsRef() throws Exception {
InputSource xml = new InputSource(getClass().getResourceAsStream("Calculator.composite"));
XPathExpression exp = xpath.compile("//sca:composite/sca:component[sca:IntentRefs('sca:confidentiality')]");
// Test the rewrite of xpath so that the self:node() is passed into the SCA function
XPathExpression exp = xpath.compile("//sca:composite/sca:component[sca:IntentRefs('sca:confidentiality', self::node())]");
Object result = exp.evaluate(xml, XPathConstants.NODESET);
Assert.assertTrue(result instanceof NodeList);
NodeList nodes = (NodeList)result;