summaryrefslogtreecommitdiffstats
path: root/java/sca/modules/policy-xml
diff options
context:
space:
mode:
authorrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2009-09-22 21:40:50 +0000
committerrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2009-09-22 21:40:50 +0000
commit536352a54507e044006bc0234f50d7a089dba746 (patch)
tree2278e8c90420e1aecf29cf21ef0778a63742b013 /java/sca/modules/policy-xml
parent86163b4ee4d80014e063e01add2f69b77cdf63a0 (diff)
Apply the @attachTo to SCA domain info set for external policy attachment
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@817852 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules/policy-xml')
-rw-r--r--java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunction.java37
-rw-r--r--java/sca/modules/policy-xml/src/test/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolverTestCase.java12
2 files changed, 42 insertions, 7 deletions
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 1b6d64e4f7..10f142c67b 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
@@ -25,6 +25,8 @@ import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
@@ -39,7 +41,7 @@ import org.w3c.dom.NodeList;
*/
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");
@@ -67,12 +69,12 @@ public class PolicyXPathFunction implements XPathFunction {
}
return null;
}
-
+
public Object evaluate(List args) throws XPathFunctionException {
if (logger.isLoggable(Level.FINE)) {
logger.fine(functionName + "(" + args + ")");
}
-
+
String arg = (String)args.get(0);
Node node = getContextNode(args);
if (InterfaceRef.equals(functionName)) {
@@ -124,4 +126,33 @@ public class PolicyXPathFunction implements XPathFunction {
return Boolean.FALSE;
}
+ private static Pattern FUNCTION;
+ static {
+ String functionPattern = "(URIRef|InterfaceRef|OperationRef|MessageRef|IntentRefs)\\s*\\((.*)\\)";
+ FUNCTION = Pattern.compile(functionPattern);
+ }
+
+ public static String normalize(String attachTo) {
+ Matcher matcher = FUNCTION.matcher(attachTo);
+ boolean result = matcher.find();
+ if (result) {
+ StringBuffer sb = new StringBuffer();
+ do {
+ String function = matcher.group(1);
+ String args = matcher.group(2);
+ String replacement = null;
+ if (args.trim().length() > 0) {
+ replacement = function + "(" + args + "," + "self::node())";
+ } else {
+ replacement = function + "(self::node())";
+ }
+ matcher.appendReplacement(sb, replacement);
+ result = matcher.find();
+ } while (result);
+ matcher.appendTail(sb);
+ return sb.toString();
+ }
+ return attachTo;
+ }
+
}
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 e954f1ebe4..f0bd5d8bde 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
@@ -19,6 +19,8 @@
package org.apache.tuscany.sca.policy.xml;
+import static org.apache.tuscany.sca.policy.xml.PolicyXPathFunction.normalize;
+
import java.util.Collections;
import java.util.Iterator;
@@ -55,8 +57,10 @@ public class PolicyXPathFunctionResolverTestCase {
@Test
public void testIntentsRef() throws Exception {
InputSource xml = new InputSource(getClass().getResourceAsStream("Calculator.composite"));
+ String str = "//sca:composite/sca:component[sca:IntentRefs('sca:confidentiality')]";
+ str = normalize(str);
// 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())]");
+ XPathExpression exp = xpath.compile(str);
Object result = exp.evaluate(xml, XPathConstants.NODESET);
Assert.assertTrue(result instanceof NodeList);
NodeList nodes = (NodeList)result;
@@ -66,7 +70,7 @@ public class PolicyXPathFunctionResolverTestCase {
@Test
public void testURIRef() throws Exception {
InputSource xml = new InputSource(getClass().getResourceAsStream("Calculator.composite"));
- XPathExpression exp = xpath.compile("sca:composite/sca:component[sca:URIRef('AddServiceComponent')]");
+ XPathExpression exp = xpath.compile(normalize("sca:composite/sca:component[sca:URIRef('AddServiceComponent')]"));
Object result = exp.evaluate(xml, XPathConstants.NODESET);
Assert.assertTrue(result instanceof NodeList);
NodeList nodes = (NodeList)result;
@@ -76,7 +80,7 @@ public class PolicyXPathFunctionResolverTestCase {
@Test
public void testInterfaceRef() throws Exception {
InputSource xml = new InputSource(getClass().getResourceAsStream("Calculator.composite"));
- XPathExpression exp = xpath.compile("//sca:composite/sca:component/sca:service[sca:InterfaceRef('AddService')]");
+ XPathExpression exp = xpath.compile(normalize("//sca:composite/sca:component/sca:service[sca:InterfaceRef('AddService')]"));
Object result = exp.evaluate(xml, XPathConstants.NODESET);
Assert.assertTrue(result instanceof NodeList);
NodeList nodes = (NodeList)result;
@@ -86,7 +90,7 @@ public class PolicyXPathFunctionResolverTestCase {
@Test
public void testOperationRef() throws Exception {
InputSource xml = new InputSource(getClass().getResourceAsStream("Calculator.composite"));
- XPathExpression exp = xpath.compile("//sca:composite/sca:component/sca:reference[sca:OperationRef('AddService/add')]");
+ XPathExpression exp = xpath.compile(normalize("//sca:composite/sca:component/sca:reference[sca:OperationRef('AddService/add')]"));
Object result = exp.evaluate(xml, XPathConstants.NODESET);
Assert.assertTrue(result instanceof NodeList);
NodeList nodes = (NodeList)result;