diff options
4 files changed, 279 insertions, 0 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 new file mode 100644 index 0000000000..ffabad5644 --- /dev/null +++ b/java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunction.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.policy.xml; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import javax.xml.namespace.NamespaceContext; +import javax.xml.namespace.QName; +import javax.xml.xpath.XPathFunction; +import javax.xml.xpath.XPathFunctionException; + +/** + * The SCA-defined XPath function + */ +public class PolicyXPathFunction implements XPathFunction { + 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"); + static final QName IntentRefs = new QName(PolicyConstants.SCA11_NS, "IntentRefs"); + static final QName URIRef = new QName(PolicyConstants.SCA11_NS, "URIRef"); + + static final Set<QName> functions = + new HashSet<QName>(Arrays.asList(InterfaceRef, OperationRef, MessageRef, IntentRefs, URIRef)); + + private NamespaceContext namespaceContext; + private final QName functionName; + + public PolicyXPathFunction(NamespaceContext namespaceContext, QName functionName) { + super(); + this.namespaceContext = namespaceContext; + this.functionName = functionName; + } + + public Object evaluate(List args) throws XPathFunctionException { + // FIXME: [rfeng] To be implemented + String arg = (String)args.get(0); + System.out.println(functionName + "(" + arg + ")"); + 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 new file mode 100644 index 0000000000..733b208de7 --- /dev/null +++ b/java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolver.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.policy.xml; + +import javax.xml.namespace.NamespaceContext; +import javax.xml.namespace.QName; +import javax.xml.xpath.XPathFunction; +import javax.xml.xpath.XPathFunctionResolver; + +/** + * A resolver that handles SCA-defined XPath functions + * + * Interface Related Functions + * <ul> + * <li>InterfaceRef( InterfaceName ) + * <li>OperationRef( InterfaceName/OperationName ) + * <li>MessageRef( InterfaceName/OperationName/MessageName ) + * </ul> + * + * Intent Related Functions + * <ul> + * <li>IntentRefs( IntentList ) + * </ul> + * + * URI Based Function + * <ul> + * <li>URIRef( URI ) + * </ul> + */ +public class PolicyXPathFunctionResolver implements XPathFunctionResolver { + private NamespaceContext namespaceContext; + + public PolicyXPathFunctionResolver(NamespaceContext namespaceContext) { + super(); + this.namespaceContext = namespaceContext; + } + + public XPathFunction resolveFunction(QName functionName, int arity) { + if (functionName == null) { + throw new NullPointerException("Function name is null"); + } + if (PolicyXPathFunction.functions.contains(functionName)) { + if (arity == 1) { + return new PolicyXPathFunction(namespaceContext, functionName); + } else { + return null; + } + } + return null; + } + +} 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 new file mode 100644 index 0000000000..fea1afb941 --- /dev/null +++ b/java/sca/modules/policy-xml/src/test/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolverTestCase.java @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.policy.xml; + +import java.util.Collections; +import java.util.Iterator; + +import javax.xml.namespace.NamespaceContext; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathFactory; + +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; + +/** + * + */ +public class PolicyXPathFunctionResolverTestCase { + private static XPathFactory factory; + + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + factory = XPathFactory.newInstance(); + } + + @Test + public void testXPath() throws Exception { + XPath xpath = factory.newXPath(); + xpath.setNamespaceContext(new NamespaceContextImpl()); + xpath.setXPathFunctionResolver(new PolicyXPathFunctionResolver(xpath.getNamespaceContext())); + InputSource xml = new InputSource(getClass().getResourceAsStream("Calculator.composite")); + XPathExpression exp = xpath.compile("//sca:composite//sca:component[sca:IntentRefs('sca:confidentiality')]"); + Object result = exp.evaluate(xml, XPathConstants.NODESET); + Assert.assertTrue(result instanceof NodeList); + NodeList nodes = (NodeList)result; + // Assert.assertEquals(1, nodes.getLength()); + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + private static class NamespaceContextImpl implements NamespaceContext { + + private static final String SCA11_NS = "http://docs.oasis-open.org/ns/opencsa/sca/200712"; + + public String getNamespaceURI(String prefix) { + if ("sca".equals(prefix)) { + return SCA11_NS; + } else { + return null; + } + } + + public String getPrefix(String namespaceURI) { + if (SCA11_NS.equals(namespaceURI)) { + return "sca"; + } + return null; + } + + public Iterator getPrefixes(String namespaceURI) { + if (SCA11_NS.equals(namespaceURI)) { + return Collections.singleton("sca").iterator(); + } + return null; + } + + } + +} diff --git a/java/sca/modules/policy-xml/src/test/resources/org/apache/tuscany/sca/policy/xml/Calculator.composite b/java/sca/modules/policy-xml/src/test/resources/org/apache/tuscany/sca/policy/xml/Calculator.composite new file mode 100644 index 0000000000..733a7333ba --- /dev/null +++ b/java/sca/modules/policy-xml/src/test/resources/org/apache/tuscany/sca/policy/xml/Calculator.composite @@ -0,0 +1,49 @@ +<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+-->
+<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200712"
+ targetNamespace="http://sample"
+ xmlns:sample="http://sample"
+ name="Calculator">
+
+ <component name="CalculatorServiceComponent" requires="sca:confidentiality">
+ <implementation.java class="calculator.CalculatorServiceImpl"/>
+ <reference name="addService" target="AddServiceComponent" />
+ <reference name="subtractService" target="SubtractServiceComponent" />
+ <reference name="multiplyService" target="MultiplyServiceComponent" />
+ <reference name="divideService" target="DivideServiceComponent" />
+ </component>
+
+ <component name="AddServiceComponent">
+ <implementation.java class="calculator.AddServiceImpl"/>
+ </component>
+
+ <component name="SubtractServiceComponent">
+ <implementation.java class="calculator.SubtractServiceImpl"/>
+ </component>
+
+ <component name="MultiplyServiceComponent">
+ <implementation.java class="calculator.MultiplyServiceImpl"/>
+ </component>
+
+ <component name="DivideServiceComponent">
+ <implementation.java class="calculator.DivideServiceImpl"/>
+ </component>
+
+</composite>
|