summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/trunk/modules
diff options
context:
space:
mode:
authorbdaniel <bdaniel@13f79535-47bb-0310-9956-ffa450edef68>2010-06-21 20:01:29 +0000
committerbdaniel <bdaniel@13f79535-47bb-0310-9956-ffa450edef68>2010-06-21 20:01:29 +0000
commit8d3b584002e231806537a6d21bc0b107acac325f (patch)
treea53cd6bc3d901f3464841212ee8df99753b11f5c /sca-java-2.x/trunk/modules
parentfd14187eb09d5d8402ea156ea437a4644453a31b (diff)
Add evaluation for XPath IntentRef function
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@956679 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/modules')
-rw-r--r--sca-java-2.x/trunk/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicySetProcessor.java4
-rw-r--r--sca-java-2.x/trunk/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunction.java67
2 files changed, 69 insertions, 2 deletions
diff --git a/sca-java-2.x/trunk/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicySetProcessor.java b/sca-java-2.x/trunk/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicySetProcessor.java
index 6309cc5403..454f382575 100644
--- a/sca-java-2.x/trunk/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicySetProcessor.java
+++ b/sca-java-2.x/trunk/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicySetProcessor.java
@@ -162,7 +162,9 @@ public class PolicySetProcessor extends BaseStAXArtifactProcessor implements StA
try {
XPath path = xpathHelper.newXPath();
NamespaceContext nsContext = xpathHelper.getNamespaceContext(attachTo, reader.getNamespaceContext());
- path.setXPathFunctionResolver(new PolicyXPathFunctionResolver(nsContext));
+ path.setXPathFunctionResolver(new PolicyXPathFunctionResolver(nsContext));
+
+ attachTo = PolicyXPathFunction.normalize(attachTo);
XPathExpression expression = xpathHelper.compile(path, nsContext, attachTo);
policySet.setAttachTo(attachTo);
policySet.setAttachToXPathExpression(expression);
diff --git a/sca-java-2.x/trunk/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunction.java b/sca-java-2.x/trunk/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunction.java
index 10f142c67b..aa27c41e5c 100644
--- a/sca-java-2.x/trunk/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunction.java
+++ b/sca-java-2.x/trunk/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunction.java
@@ -33,6 +33,7 @@ import javax.xml.namespace.QName;
import javax.xml.xpath.XPathFunction;
import javax.xml.xpath.XPathFunctionException;
+import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@@ -123,15 +124,79 @@ public class PolicyXPathFunction implements XPathFunction {
}
private Boolean evaluateIntents(String[] intents, Node node) {
+ if ( node == null )
+ return false;
+ if ( node.getAttributes() != null ) {
+ for ( int i=0; i < node.getAttributes().getLength(); i++) {
+ Node attr = node.getAttributes().item(i);
+
+ if ( "requires".equalsIgnoreCase(attr.getNodeName())) {
+
+ for ( int j = 0; j < intents.length; j++ ) {
+ // Check negative intents
+ if ( intents[j].startsWith("!")) {
+ if ( matchIntent(intents[j].substring(1), attr, node.getNamespaceURI()))
+ return Boolean.FALSE;
+ } else if ( !matchIntent(intents[j], attr, node.getNamespaceURI())){
+ return Boolean.FALSE;
+ }
+ }
+ return Boolean.TRUE;
+ }
+
+ }
+ }
+
return Boolean.FALSE;
}
- private static Pattern FUNCTION;
+ private boolean matchIntent(String intent, Node node, String namespaceURI) {
+ String[] requires = node.getNodeValue().split("(\\s)+");
+ QName intentName = getStringAsQName(intent);
+
+
+ for ( int i=0; i < requires.length; i++ ) {
+ QName nodeName = null;
+ int idx = requires[i].indexOf(':');
+
+ // No prefix specified
+ if ( idx == -1 ) {
+ nodeName = new QName(namespaceURI, requires[i]);
+ } else {
+ String prefix = requires[i].substring(0, idx);
+ String name = requires[i].substring(idx + 1);
+ String ns = node.lookupNamespaceURI(prefix);
+ nodeName = new QName(ns, name, prefix);
+ }
+ if ( intentName.equals(nodeName))
+ return true;
+ }
+ return false;
+ }
+
+
+ private QName getStringAsQName(String intent) {
+ int idx = intent.indexOf(':');
+ if (idx == -1)
+ return new QName(intent);
+
+ String prefix = intent.substring(0, idx);
+ intent = intent.substring(idx + 1);
+
+ return new QName(namespaceContext.getNamespaceURI(prefix), intent, prefix);
+
+ }
+
+
+ private static Pattern FUNCTION;
static {
String functionPattern = "(URIRef|InterfaceRef|OperationRef|MessageRef|IntentRefs)\\s*\\((.*)\\)";
FUNCTION = Pattern.compile(functionPattern);
}
+ /** Adds the node as an argument to the XPath function.
+ * Required in order to have access to the NodeList within the function
+ */
public static String normalize(String attachTo) {
Matcher matcher = FUNCTION.matcher(attachTo);
boolean result = matcher.find();