From 534f392233c4f651f62b1357130a973aa549db28 Mon Sep 17 00:00:00 2001 From: rfeng Date: Thu, 5 Mar 2009 22:09:33 +0000 Subject: 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 --- .../sca/policy/xml/PolicyXPathFunction.java | 44 ++++++++++++++++------ .../policy/xml/PolicyXPathFunctionResolver.java | 5 ++- .../xml/PolicyXPathFunctionResolverTestCase.java | 3 +- 3 files changed, 38 insertions(+), 14 deletions(-) (limited to 'java/sca') diff --git a/java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunction.java b/java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunction.java index f015fc90fb..1b6d64e4f7 100644 --- a/java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunction.java +++ b/java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunction.java @@ -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; } diff --git a/java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolver.java b/java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolver.java index 2d04a27cd7..ca44667903 100644 --- a/java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolver.java +++ b/java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolver.java @@ -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); diff --git a/java/sca/modules/policy-xml/src/test/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolverTestCase.java b/java/sca/modules/policy-xml/src/test/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolverTestCase.java index 7b647819b4..c9976a93b4 100644 --- a/java/sca/modules/policy-xml/src/test/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolverTestCase.java +++ b/java/sca/modules/policy-xml/src/test/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolverTestCase.java @@ -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; -- cgit v1.2.3