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
This commit is contained in:
parent
86163b4ee4
commit
536352a545
5 changed files with 91 additions and 29 deletions
|
@ -19,9 +19,10 @@
|
|||
|
||||
package org.apache.tuscany.sca.common.xml.xpath;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.HashSet;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.xml.XMLConstants;
|
||||
import javax.xml.namespace.NamespaceContext;
|
||||
|
@ -100,27 +101,50 @@ public class XPathHelper {
|
|||
* @return A collection of prefixes
|
||||
*/
|
||||
private Collection<String> getPrefixes(String expression) {
|
||||
List<String> prefixes = new ArrayList<String>();
|
||||
prefixes.add("");
|
||||
String[] segments = expression.split(":");
|
||||
for (int i = 0; i < segments.length - 1; i++) {
|
||||
String prefix = segments[i];
|
||||
if(prefix.length()<1) {
|
||||
continue;
|
||||
}
|
||||
int j = prefix.length() -1;
|
||||
for (; j >= 0; j--) {
|
||||
if (XMLCharHelper.isNCName(prefix.charAt(j))) {
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
// j is before the first char of the prefix
|
||||
if (j != (prefix.length() - 1) && XMLCharHelper.isNCNameStart(prefix.charAt(j + 1))) {
|
||||
prefixes.add(prefix.substring(j + 1));
|
||||
Collection<String> prefixes = new HashSet<String>();
|
||||
Pattern pattern = Pattern.compile("([^:]+):([^:]+)");
|
||||
Matcher matcher = pattern.matcher(expression);
|
||||
while (matcher.find()) {
|
||||
String prefix = extractNCName(matcher.group(1), true);
|
||||
String local = extractNCName(matcher.group(2), false);
|
||||
if (prefix != null && local != null) {
|
||||
prefixes.add(prefix);
|
||||
}
|
||||
}
|
||||
return prefixes;
|
||||
}
|
||||
|
||||
private String extractNCName(String str, boolean reverse) {
|
||||
if (str.length() < 1) {
|
||||
return null;
|
||||
}
|
||||
if (!reverse) {
|
||||
if (!XMLCharHelper.isNCNameStart(str.charAt(0))) {
|
||||
return null;
|
||||
}
|
||||
int i = 0, j = str.length();
|
||||
// Find the last non-NCName char
|
||||
for (; i < j; i++) {
|
||||
if (!XMLCharHelper.isNCName(str.charAt(i))) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return str.substring(0, i);
|
||||
} else {
|
||||
int j = str.length() - 1;
|
||||
// Find the first non-NCName char
|
||||
for (; j >= 0; j--) {
|
||||
if (!XMLCharHelper.isNCName(str.charAt(j))) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// j is before the first char of the prefix
|
||||
if (j != (str.length() - 1) && XMLCharHelper.isNCNameStart(str.charAt(j + 1))) {
|
||||
return str.substring(j + 1);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -52,7 +52,7 @@ public class XPathHelperTestCase {
|
|||
+ "</r:root>";
|
||||
|
||||
private static String XPATH =
|
||||
"<policySet attachTo=\"//c:child1[@name='child1']\" xmlns:c=\"http://child1\" xmlns=\"http://p\">" + "<child xmlns:c=\"http://c2\"/></policySet>";
|
||||
"<policySet attachTo=\"//c:child1[@name='child1']/self::node()\" xmlns:c=\"http://child1\" xmlns=\"http://p\">" + "<child xmlns:c=\"http://c2\"/></policySet>";
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
|
|
|
@ -33,6 +33,7 @@ import org.apache.tuscany.sca.assembly.Base;
|
|||
import org.apache.tuscany.sca.assembly.Composite;
|
||||
import org.apache.tuscany.sca.assembly.builder.BuilderExtensionPoint;
|
||||
import org.apache.tuscany.sca.assembly.builder.CompositeBuilder;
|
||||
import org.apache.tuscany.sca.assembly.builder.impl.CompositeBindingURIBuilderImpl;
|
||||
import org.apache.tuscany.sca.assembly.builder.impl.CompositeCloneBuilderImpl;
|
||||
import org.apache.tuscany.sca.assembly.builder.impl.CompositeIncludeBuilderImpl;
|
||||
import org.apache.tuscany.sca.contribution.processor.ContributionReadException;
|
||||
|
@ -126,6 +127,7 @@ public class PolicyAttachmentTestCase {
|
|||
|
||||
CompositeBuilder includeBuilder = new CompositeIncludeBuilderImpl();
|
||||
CompositeBuilder cloneBuilder = new CompositeCloneBuilderImpl();
|
||||
CompositeBuilder uriBuilder = new CompositeBindingURIBuilderImpl(extensionPoints);
|
||||
|
||||
/*
|
||||
CompositeBuilder includeBuilder =
|
||||
|
@ -136,6 +138,7 @@ public class PolicyAttachmentTestCase {
|
|||
|
||||
includeBuilder.build(domainComposite, definitions, monitor);
|
||||
cloneBuilder.build(domainComposite, definitions, monitor);
|
||||
uriBuilder.build(domainComposite, definitions, monitor);
|
||||
|
||||
PolicyAttachmentBuilderImpl builder = new PolicyAttachmentBuilderImpl(extensionPoints);
|
||||
builder.build(domainComposite, definitions, monitor);
|
||||
|
|
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue