From 87e1281790214047900aaaf8f2f55c8635965ebe Mon Sep 17 00:00:00 2001 From: bdaniel Date: Mon, 26 Jul 2010 04:30:03 +0000 Subject: TUSCANY-3630 Add xpath processing of externalAttachment elements in builder git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@979153 13f79535-47bb-0310-9956-ffa450edef68 --- .../builder/impl/PolicyAttachmentBuilderImpl.java | 143 ++++++++++++--------- 1 file changed, 83 insertions(+), 60 deletions(-) (limited to 'sca-java-2.x/trunk/modules/builder') diff --git a/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentBuilderImpl.java b/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentBuilderImpl.java index e9a49c8842..f911d5a5e4 100644 --- a/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentBuilderImpl.java +++ b/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentBuilderImpl.java @@ -21,7 +21,6 @@ package org.apache.tuscany.sca.builder.impl; import java.io.IOException; import java.io.StringWriter; -import java.util.Set; import javax.xml.namespace.QName; import javax.xml.stream.XMLStreamException; @@ -49,6 +48,7 @@ import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtens import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.definitions.Definitions; import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.policy.ExternalAttachment; import org.apache.tuscany.sca.policy.PolicySet; import org.apache.tuscany.sca.policy.PolicySubject; import org.w3c.dom.Document; @@ -109,7 +109,7 @@ public class PolicyAttachmentBuilderImpl implements CompositeBuilder { monitor.pushContext("Composite: " + composite.getName().toString()); try { - if (definitions == null || definitions.getPolicySets().isEmpty()) { + if (definitions == null || (definitions.getPolicySets().isEmpty() && definitions.getExternalAttachments().isEmpty()) ) { return composite; } // Recursively apply the xpath against the composites referenced by @@ -122,67 +122,31 @@ public class PolicyAttachmentBuilderImpl implements CompositeBuilder { } } } + Document document = null; for (PolicySet ps : definitions.getPolicySets()) { - // First calculate the applicable nodes - Set applicableNodes = null; - /* - XPathExpression appliesTo = ps.getAppliesToXPathExpression(); - if (appliesTo != null) { - applicableNodes = new HashSet(); - NodeList nodes = (NodeList)appliesTo.evaluate(document, XPathConstants.NODESET); - for (int i = 0; i < nodes.getLength(); i++) { - applicableNodes.add(nodes.item(i)); - } - } - */ - XPathExpression exp = ps.getAttachToXPathExpression(); - if (exp != null) { - if (document == null) { - document = saveAsDOM(composite); - } - NodeList nodes = (NodeList)exp.evaluate(document, XPathConstants.NODESET); - for (int i = 0; i < nodes.getLength(); i++) { - Node node = nodes.item(i); - - // POL_40002 - you can't attach a policy to a property node - // or one of it's children - // walk backwards up the node tree looking for an element called property - // and raise an error if we find one - Node testNode = node; - while (testNode != null){ - if ((node.getNodeType() == Node.ELEMENT_NODE) && - (node.getLocalName().equals("property"))){ - Monitor.error(monitor, - this, - BUILDER_VALIDATION_BUNDLE, - "PolicyAttachedToProperty", - ps.getName().toString()); - break; - } - testNode = testNode.getParentNode(); - } - - if (applicableNodes == null || applicableNodes.contains(node)) { - // The node can be a component, service, reference or binding - String index = getStructuralURI(node); - PolicySubject subject = lookup(composite, index); - if (subject != null) { - subject.getPolicySets().add(ps); - } else { - // raise a warning that the XPath node didn't match a node in the - // models - Monitor.warning(monitor, - this, - BUILDER_VALIDATION_BUNDLE, - "PolicyDOMModelMissmatch", - ps.getName().toString(), - index); - } - } - } - } + XPathExpression exp = ps.getAttachToXPathExpression(); + if ( exp != null ) { + if ( document == null ) { + document = saveAsDOM(composite); + } + NodeList nodes = (NodeList) exp.evaluate(document, XPathConstants.NODESET); + attachPolicySetToNodes(composite, monitor, nodes, ps); + } + } + + for ( ExternalAttachment ea : definitions.getExternalAttachments() ) { + XPathExpression exp = ea.getAttachToXPathExpression(); + if ( exp != null ) { + if ( document == null ) { + document = saveAsDOM(composite); + } + NodeList nodes = (NodeList) exp.evaluate(document, XPathConstants.NODESET); + for ( PolicySet ps : ea.getPolicySets() ) { + attachPolicySetToNodes(composite, monitor, nodes, ps); + } + } } return composite; @@ -191,6 +155,61 @@ public class PolicyAttachmentBuilderImpl implements CompositeBuilder { } } + private void attachPolicySetToNodes(Composite composite, + Monitor monitor, NodeList nodes, PolicySet ps) { + + for (int i = 0; i < nodes.getLength(); i++) { + Node node = nodes.item(i); + + if ( isAttachedToProperty(node) ) { + Monitor.error(monitor, + this, + BUILDER_VALIDATION_BUNDLE, + "PolicyAttachedToProperty", + ps.getName().toString()); + } + + + // The node can be a component, service, reference or binding + String index = getStructuralURI(node); + PolicySubject subject = lookup(composite, index); + if (subject != null) { + subject.getPolicySets().add(ps); + } else { + // raise a warning that the XPath node didn't match a node in the + // models + Monitor.warning(monitor, + this, + BUILDER_VALIDATION_BUNDLE, + "PolicyDOMModelMissmatch", + ps.getName().toString(), + index); + } + + } + } + + /** + * POL_40002 - you can't attach a policy to a property node + * or one of it's children. walk backwards up the node tree + * looking for an element called property and raise an error + * if we find one + * @param node + * @return + */ + private boolean isAttachedToProperty(Node node) { + + Node testNode = node; + while (testNode != null){ + if ((node.getNodeType() == Node.ELEMENT_NODE) && + (node.getLocalName().equals("property"))){ + return true; + } + testNode = testNode.getParentNode(); + } + return false; + } + protected Document saveAsDOM(Composite composite) throws XMLStreamException, ContributionWriteException, IOException, SAXException { // First write the composite into a DOM document so that we can apply the xpath @@ -228,6 +247,8 @@ public class PolicyAttachmentBuilderImpl implements CompositeBuilder { String uri = component.getAttributeNS(null, "uri"); String reference = ((Element)node).getAttributeNS(null, "name"); return uri + "#reference(" + reference + ")"; + } else if ( new QName(Base.SCA11_NS, "composite").equals(name)) { + return ""; } else { String localName = node.getLocalName(); if (localName.startsWith("binding.")) { @@ -259,6 +280,8 @@ public class PolicyAttachmentBuilderImpl implements CompositeBuilder { protected PolicySubject lookup(Composite composite, String structuralURI) { if (structuralURI == null) { return null; + } else if ( structuralURI.equals("")) { + return composite; } int index = structuralURI.indexOf('#'); String componentURI = structuralURI; -- cgit v1.2.3