For POL_4028 test that a policy isn't being attached to a property element or one of its children.

git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@883409 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
slaws 2009-11-23 16:58:53 +00:00
parent cc4acbfe1d
commit 9c9214b9d8
2 changed files with 105 additions and 41 deletions

View file

@ -109,55 +109,90 @@ public class PolicyAttachmentBuilderImpl implements CompositeBuilder {
* @throws Exception
*/
private Composite applyXPath(Composite composite, Definitions definitions, Monitor monitor) throws Exception {
if (definitions == null || definitions.getPolicySets().isEmpty()) {
return composite;
}
// Recursively apply the xpath against the composites referenced by <implementation.composite>
for (Component component : composite.getComponents()) {
Implementation impl = component.getImplementation();
if (impl instanceof Composite) {
Composite patched = applyXPath((Composite)impl, definitions, monitor);
if (patched != impl) {
component.setImplementation(patched);
}
}
}
Document document = null;
for (PolicySet ps : definitions.getPolicySets()) {
// First calculate the applicable nodes
Set<Node> applicableNodes = null;
/*
XPathExpression appliesTo = ps.getAppliesToXPathExpression();
if (appliesTo != null) {
applicableNodes = new HashSet<Node>();
NodeList nodes = (NodeList)appliesTo.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
applicableNodes.add(nodes.item(i));
monitor.pushContext("Composite: " + composite.getName().toString());
try {
if (definitions == null || definitions.getPolicySets().isEmpty()) {
return composite;
}
// Recursively apply the xpath against the composites referenced by <implementation.composite>
for (Component component : composite.getComponents()) {
Implementation impl = component.getImplementation();
if (impl instanceof Composite) {
Composite patched = applyXPath((Composite)impl, definitions, monitor);
if (patched != impl) {
component.setImplementation(patched);
}
}
}
*/
XPathExpression exp = ps.getAttachToXPathExpression();
if (exp != null) {
if (document == null) {
document = saveAsDOM(composite);
Document document = null;
for (PolicySet ps : definitions.getPolicySets()) {
// First calculate the applicable nodes
Set<Node> applicableNodes = null;
/*
XPathExpression appliesTo = ps.getAppliesToXPathExpression();
if (appliesTo != null) {
applicableNodes = new HashSet<Node>();
NodeList nodes = (NodeList)appliesTo.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
applicableNodes.add(nodes.item(i));
}
}
NodeList nodes = (NodeList)exp.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
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);
*/
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,
"org.apache.tuscany.sca.builder.builder-validation-messages",
"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,
"org.apache.tuscany.sca.builder.builder-validation-messages",
"PolicyDOMModelMissmatch",
ps.getName().toString(),
index);
}
}
}
}
}
}
return composite;
return composite;
} finally {
monitor.popContext();
}
}
private Document saveAsDOM(Composite composite) throws XMLStreamException, ContributionWriteException, IOException,
@ -170,6 +205,10 @@ public class PolicyAttachmentBuilderImpl implements CompositeBuilder {
writer.close();
Document document = domHelper.load(sw.toString());
// Debugging
//System.out.println("<!-- DOM to which XPath will be applies is -->\n" + sw.toString());
return document;
}

View file

@ -0,0 +1,25 @@
#
#
# 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.
#
#
# there are a whole stack of builder related messages that
# are still yet to be moved here from assembly-validation-messages.properties
PolicyAttachedToProperty = [POL40002] The policy {0} has been attached to a property or one of its children. This is not allowed.
PolicyDOMModelMissmatch = The DOM node which has been found as a result of evaluating the XPath attachment of policy {0} cannot be mapped back to an element in the SCA model. The structural URI of the node is {1}