You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by lr...@apache.org on 2008/07/08 19:13:15 UTC

svn commit: r674885 - /tuscany/java/sca/modules/policy/src/main/java/org/apache/tuscany/sca/policy/util/PolicyComputationUtils.java

Author: lresende
Date: Tue Jul  8 10:13:15 2008
New Revision: 674885

URL: http://svn.apache.org/viewvc?rev=674885&view=rev
Log:
TUSCANY-2464 - Applying Greg's patch

Modified:
    tuscany/java/sca/modules/policy/src/main/java/org/apache/tuscany/sca/policy/util/PolicyComputationUtils.java

Modified: tuscany/java/sca/modules/policy/src/main/java/org/apache/tuscany/sca/policy/util/PolicyComputationUtils.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/policy/src/main/java/org/apache/tuscany/sca/policy/util/PolicyComputationUtils.java?rev=674885&r1=674884&r2=674885&view=diff
==============================================================================
--- tuscany/java/sca/modules/policy/src/main/java/org/apache/tuscany/sca/policy/util/PolicyComputationUtils.java (original)
+++ tuscany/java/sca/modules/policy/src/main/java/org/apache/tuscany/sca/policy/util/PolicyComputationUtils.java Tue Jul  8 10:13:15 2008
@@ -231,15 +231,13 @@
     private static byte[] addApplicablePolicySets(Document doc, Collection<PolicySet> policySets)
         throws XPathExpressionException, TransformerConfigurationException, TransformerException {
         
-        int prefixCount = 1;
-
         for (PolicySet policySet : policySets) {
             if (policySet.getAppliesTo() != null) {
-                addApplicablePolicySets(policySet, doc, prefixCount);
+                addApplicablePolicySets(policySet, doc);
             }
 
             if (policySet.getAlwaysAppliesTo() != null) {
-                addAlwaysApplicablePolicySets(policySet, doc, prefixCount);
+                addAlwaysApplicablePolicySets(policySet, doc);
             }
         }
 
@@ -265,8 +263,7 @@
     }
 
     private static void addAlwaysApplicablePolicySets(PolicySet policySet,
-                                                      Document doc,
-                                                      int prefixCount) throws XPathExpressionException {
+                                                      Document doc) throws XPathExpressionException {
         XPathExpression expression = policySet.getAlwaysAppliesToXPathExpression();
         NodeList result = (NodeList)expression.evaluate(doc, XPathConstants.NODESET);
 
@@ -275,14 +272,11 @@
                 Node aResultNode = result.item(counter);
 
                 String alwaysApplicablePolicySets = null;
-                String policySetPrefix = POLICYSET_PREFIX + prefixCount++;
-                String policySetsAttrPrefix = "sca";
 
-                policySetPrefix =
-                    declareNamespace((Element)aResultNode, policySetPrefix, policySet.getName()
-                        .getNamespaceURI());
-                policySetsAttrPrefix =
-                    declareNamespace((Element)aResultNode, policySetsAttrPrefix, SCA10_NS);
+                String policySetPrefix =
+                    declareNamespace((Element)aResultNode, policySet.getName().getNamespaceURI());
+                String policySetsAttrPrefix =
+                    declareNamespace((Element)aResultNode, SCA10_NS);
                 if (aResultNode.getAttributes().getNamedItem(POLICY_SETS_ATTR) != null) {
                     alwaysApplicablePolicySets =
                         aResultNode.getAttributes().getNamedItem(POLICY_SETS_ATTR).getNodeValue();
@@ -305,8 +299,7 @@
     }
 
     private static void addApplicablePolicySets(PolicySet policySet,
-                                         Document doc,
-                                         int prefixCount) throws XPathExpressionException {
+                                         Document doc) throws XPathExpressionException {
         XPathExpression expression = policySet.getAppliesToXPathExpression();
         NodeList result = (NodeList)expression.evaluate(doc, XPathConstants.NODESET);
 
@@ -315,15 +308,11 @@
                 Node aResultNode = result.item(counter);
 
                 String applicablePolicySets = null;
-                String policySetPrefix = POLICYSET_PREFIX + prefixCount++;
-                String appPolicyAttrPrefix = APPLICABLE_POLICYSET_ATTR_PREFIX;
 
-                policySetPrefix =
-                    declareNamespace((Element)aResultNode, policySetPrefix, policySet.getName()
-                        .getNamespaceURI());
-                appPolicyAttrPrefix =
+                String policySetPrefix =
+                    declareNamespace((Element)aResultNode, policySet.getName().getNamespaceURI());
+                String appPolicyAttrPrefix =
                     declareNamespace((Element)aResultNode,
-                                     appPolicyAttrPrefix,
                                      APPLICABLE_POLICYSET_ATTR_NS);
                 if (aResultNode.getAttributes().getNamedItemNS(APPLICABLE_POLICYSET_ATTR_NS,
                                                                APPLICABLE_POLICYSET_ATTR) != null) {
@@ -385,20 +374,12 @@
 
     }
     
-    private static String declareNamespace(Element element, String prefix, String ns) {
+    private static String declareNamespace(Element element, String ns) {
         if (ns == null) {
             ns = "";
         }
-        if (prefix == null) {
-            prefix = "";
-        }
-        String qname = null;
-        if ("".equals(prefix)) {
-            qname = "xmlns";
-        } else {
-            qname = "xmlns:" + prefix;
-        }
         Node node = element;
+        String prefix = "";
         boolean declared = false;
         while (node != null && node.getNodeType() == Node.ELEMENT_NODE) {
             if ( node.lookupPrefix(ns) != null ) {
@@ -419,6 +400,14 @@
             }
         }
         if (!declared) {
+            // Find an available prefix
+            for (int i=1; ; i++) {
+                prefix = POLICYSET_PREFIX + i;
+                if (element.lookupNamespaceURI(prefix) == null) {
+                    break;
+                }
+            }
+            String qname = "xmlns:" + prefix;
             org.w3c.dom.Attr attr = element.getOwnerDocument().createAttributeNS(XMLNS_ATTRIBUTE_NS_URI, qname);
             attr.setValue(ns);
             element.setAttributeNodeNS(attr);