tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rf...@apache.org
Subject svn commit: r747275 - in /tuscany/java/sca/modules/policy-xml/src: main/java/org/apache/tuscany/sca/policy/xml/ test/java/org/apache/tuscany/sca/policy/xml/ test/resources/org/apache/tuscany/sca/policy/xml/
Date Tue, 24 Feb 2009 04:51:49 GMT
Author: rfeng
Date: Tue Feb 24 04:51:49 2009
New Revision: 747275

URL: http://svn.apache.org/viewvc?rev=747275&view=rev
Log:
Start to implement the @attachTo XPath functions

Added:
    tuscany/java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunction.java
  (with props)
    tuscany/java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolver.java
  (with props)
    tuscany/java/sca/modules/policy-xml/src/test/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolverTestCase.java
  (with props)
    tuscany/java/sca/modules/policy-xml/src/test/resources/org/apache/tuscany/sca/policy/xml/Calculator.composite

Added: tuscany/java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunction.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunction.java?rev=747275&view=auto
==============================================================================
--- tuscany/java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunction.java
(added)
+++ tuscany/java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunction.java
Tue Feb 24 04:51:49 2009
@@ -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;
+    }
+
+}

Propchange: tuscany/java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunction.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tuscany/java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunction.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: tuscany/java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolver.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolver.java?rev=747275&view=auto
==============================================================================
--- tuscany/java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolver.java
(added)
+++ tuscany/java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolver.java
Tue Feb 24 04:51:49 2009
@@ -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;
+    }
+
+}

Propchange: tuscany/java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolver.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tuscany/java/sca/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolver.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: tuscany/java/sca/modules/policy-xml/src/test/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolverTestCase.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/policy-xml/src/test/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolverTestCase.java?rev=747275&view=auto
==============================================================================
--- tuscany/java/sca/modules/policy-xml/src/test/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolverTestCase.java
(added)
+++ tuscany/java/sca/modules/policy-xml/src/test/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolverTestCase.java
Tue Feb 24 04:51:49 2009
@@ -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;
+        }
+
+    }
+
+}

Propchange: tuscany/java/sca/modules/policy-xml/src/test/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolverTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tuscany/java/sca/modules/policy-xml/src/test/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolverTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: tuscany/java/sca/modules/policy-xml/src/test/resources/org/apache/tuscany/sca/policy/xml/Calculator.composite
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/policy-xml/src/test/resources/org/apache/tuscany/sca/policy/xml/Calculator.composite?rev=747275&view=auto
==============================================================================
--- tuscany/java/sca/modules/policy-xml/src/test/resources/org/apache/tuscany/sca/policy/xml/Calculator.composite
(added)
+++ tuscany/java/sca/modules/policy-xml/src/test/resources/org/apache/tuscany/sca/policy/xml/Calculator.composite
Tue Feb 24 04:51:49 2009
@@ -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>



Mime
View raw message