ws-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sa...@apache.org
Subject svn commit: r354982 - /webservices/commons/trunk/policy/src/org/apache/ws/policy/model/AndCompositeAssertion.java
Date Thu, 08 Dec 2005 04:54:15 GMT
Author: sanka
Date: Wed Dec  7 20:54:03 2005
New Revision: 354982

URL: http://svn.apache.org/viewcvs?rev=354982&view=rev
Log:
Some refactoring in AndCompositeAssertion class

Modified:
    webservices/commons/trunk/policy/src/org/apache/ws/policy/model/AndCompositeAssertion.java

Modified: webservices/commons/trunk/policy/src/org/apache/ws/policy/model/AndCompositeAssertion.java
URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/ws/policy/model/AndCompositeAssertion.java?rev=354982&r1=354981&r2=354982&view=diff
==============================================================================
--- webservices/commons/trunk/policy/src/org/apache/ws/policy/model/AndCompositeAssertion.java
(original)
+++ webservices/commons/trunk/policy/src/org/apache/ws/policy/model/AndCompositeAssertion.java
Wed Dec  7 20:54:03 2005
@@ -25,17 +25,20 @@
 import org.apache.ws.policy.util.PolicyRegistry;
 
 /**
- * AndCompositeAssertion represents either policy or a single policy 
+ * AndCompositeAssertion represents either policy or a single policy
  * alternative. It requires that all its terms are satisfied.
  */
-public class AndCompositeAssertion extends CompositeAssertion implements Assertion {
-    
+public class AndCompositeAssertion extends CompositeAssertion implements
+        Assertion {
+
     public AndCompositeAssertion() {
     }
-        
+
     /**
      * Adds an Assertion to its terms list
-     * @param assertion Assertion to be added
+     * 
+     * @param assertion
+     *           Assertion to be added
      */
     public void addTerm(Assertion assertion) {
         if (!(isNormalized() && (assertion instanceof PrimitiveAssertion))) {
@@ -43,31 +46,35 @@
         }
         super.addTerm(assertion);
     }
-    
+
     /**
-     * Returns the intersection of self and argument against a 
-     * specified Policy Registry.
-     *  
-     * @param assertion  the assertion to intersect with self
-     * @param reg a sepcified policy registry
-     * @return assertion the assertion which is equivalent to 
-     *                   intersection between self and the argument
+     * Returns the intersection of self and argument against a specified Policy
+     * Registry.
+     * 
+     * @param assertion
+     *           the assertion to intersect with self
+     * @param reg
+     *           a sepcified policy registry
+     * @return assertion the assertion which is equivalent to intersection
+     *         between self and the argument
      */
     public Assertion intersect(Assertion assertion, PolicyRegistry reg) {
-        
-        CompositeAssertion normalizedMe = (CompositeAssertion) ((isNormalized()) ? this :
normalize(reg));
-        
+
+        CompositeAssertion normalizedMe = (CompositeAssertion) ((isNormalized()) ? this
+                : normalize(reg));
+
         if (!(normalizedMe instanceof AndCompositeAssertion)) {
             return normalizedMe.intersect(assertion, reg);
         }
-                
+
         if (assertion instanceof PrimitiveAssertion) {
             QName qname = ((PrimitiveAssertion) assertion).getName();
             Iterator iterator = getTerms().iterator();
             boolean isMatch = false;
-            
+
             while (iterator.hasNext()) {
-                PrimitiveAssertion primTerm = (PrimitiveAssertion) iterator.next();
+                PrimitiveAssertion primTerm = (PrimitiveAssertion) iterator
+                        .next();
                 if (primTerm.getName().equals(qname)) {
                     isMatch = true;
                     break;
@@ -75,20 +82,23 @@
             }
             return (isMatch) ? normalizedMe : new XorCompositeAssertion();
         }
-        
+
         CompositeAssertion target = (CompositeAssertion) assertion;
-        target = (CompositeAssertion) ((target.isNormalized()) ? target : target.normalize(reg));
-        
+        target = (CompositeAssertion) ((target.isNormalized()) ? target
+                : target.normalize(reg));
+
         if (target instanceof Policy) {
-            XorCompositeAssertion alters = (XorCompositeAssertion) target.getTerms().get(0);
+            XorCompositeAssertion alters = (XorCompositeAssertion) target
+                    .getTerms().get(0);
             return normalizedMe.intersect(alters);
-            
+
         } else if (target instanceof XorCompositeAssertion) {
             XorCompositeAssertion result = new XorCompositeAssertion();
             Iterator iterator = target.getTerms().iterator();
-            
+
             while (iterator.hasNext()) {
-                AndCompositeAssertion andTerm = (AndCompositeAssertion) iterator.next();
+                AndCompositeAssertion andTerm = (AndCompositeAssertion) iterator
+                        .next();
                 Assertion value = normalizedMe.intersect(andTerm);
                 if (value instanceof AndCompositeAssertion) {
                     result.addTerm(value);
@@ -96,70 +106,73 @@
             }
             return result;
         }
-        
+
         if (normalizedMe.isEmpty()) {
             return target;
         }
         if (target.isEmpty()) {
             return normalizedMe;
         }
-                
-        List primTermsA = ((size() > target.size()) ? normalizedMe.getTerms() : target.getTerms());
-        List primTermsB = ((size() > target.size()) ? target.getTerms() : normalizedMe.getTerms());
+
+        List primTermsA = ((size() > target.size()) ? normalizedMe.getTerms()
+                : target.getTerms());
+        List primTermsB = ((size() > target.size()) ? target.getTerms()
+                : normalizedMe.getTerms());
 
         boolean isMatch = true;
         PrimitiveAssertion primTermA, primTermB;
         QName qnameA, qnameB;
-        
+
         for (int i = 0; i < primTermsA.size(); i++) {
             primTermA = (PrimitiveAssertion) primTermsA.get(i);
             qnameA = primTermA.getName();
             boolean flag = false;
-            
+
             for (int j = 0; j < primTermsB.size(); j++) {
                 primTermB = (PrimitiveAssertion) primTermsB.get(j);
                 qnameB = primTermB.getName();
                 if (qnameA.equals(qnameB)) {
                     flag = true;
                     break;
-                }                       
+                }
             }
             if (!flag) {
                 isMatch = false;
                 break;
-            }                   
+            }
         }
-        
+
         if (isMatch) { // vocabulary matches
             AndCompositeAssertion result = new AndCompositeAssertion();
             result.addTerms(primTermsA);
             result.addTerms(primTermsB);
             return result;
         }
-        
+
         return new XorCompositeAssertion(); // no behaviour is admisible
     }
-    
-    
-    
+
     /**
-     * Returns an assertion which is equivalent to merge of self and the 
-     * argument. 
+     * Returns an assertion which is equivalent to merge of self and the
+     * argument.
      * 
-     * @param assertion the assertion to be merged with
-     * @param reg the policy registry which the is used resolve external policy
-     *        references
-     * @return assertion the resultant assertion which is equivalent to merge 
-     *         of self and argument
+     * @param assertion
+     *           the assertion to be merged with
+     * @param reg
+     *           the policy registry which the is used resolve external policy
+     *           references
+     * @return assertion the resultant assertion which is equivalent to merge of
+     *         self and argument
      */
     public Assertion merge(Assertion assertion, PolicyRegistry reg) {
-        
-        CompositeAssertion normalizedMe = (CompositeAssertion) ((isNormalized()) ? this :
normalize(reg));
-        
+
+        CompositeAssertion normalizedMe = (CompositeAssertion) ((isNormalized()) ? this
+                : normalize(reg));
+
         if (!(normalizedMe instanceof AndCompositeAssertion)) {
             return normalizedMe.merge(assertion, reg);
         }
-        
+
         if (assertion instanceof PrimitiveAssertion) {
             AndCompositeAssertion andTerm = new AndCompositeAssertion();
             andTerm.addTerm(assertion);
@@ -169,174 +182,146 @@
         }
 
         CompositeAssertion target = (CompositeAssertion) assertion;
-        target = (CompositeAssertion) ((target.isNormalized()) ? target : target.normalize(reg));
-        
+        target = (CompositeAssertion) ((target.isNormalized()) ? target
+                : target.normalize(reg));
+
         if (target instanceof Policy) {
-            XorCompositeAssertion xorTerm = (XorCompositeAssertion) target.getTerms().get(0);
+            XorCompositeAssertion xorTerm = (XorCompositeAssertion) target
+                    .getTerms().get(0);
             return normalizedMe.merge(xorTerm);
-            
+
         } else if (target instanceof XorCompositeAssertion) {
             XorCompositeAssertion xorTerm = new XorCompositeAssertion();
-            
+
             Iterator hisAndTerms = target.getTerms().iterator();
             while (hisAndTerms.hasNext()) {
-                AndCompositeAssertion hisAndTerm = (AndCompositeAssertion) hisAndTerms.next();
-                xorTerm.addTerm(normalizedMe.merge(hisAndTerm));                
+                AndCompositeAssertion hisAndTerm = (AndCompositeAssertion) hisAndTerms
+                        .next();
+                xorTerm.addTerm(normalizedMe.merge(hisAndTerm));
             }
             xorTerm.setNormalized(true);
             return xorTerm;
-            
-        }  else if (target instanceof AndCompositeAssertion) {
+
+        } else if (target instanceof AndCompositeAssertion) {
             AndCompositeAssertion andTerm = new AndCompositeAssertion();
             andTerm.addTerms(normalizedMe.getTerms());
             andTerm.addTerms(target.getTerms());
             andTerm.setNormalized(true);
-            return andTerm;         
+            return andTerm;
         }
-        
-        throw new IllegalArgumentException("error : merge is not defined for" 
+
+        throw new IllegalArgumentException("error : merge is not defined for"
                 + assertion.getClass().getName());
     }
-    
+
     /**
-     * Returns an Assertion which is normalized using a specified 
-     * policy registry.
+     * Returns an Assertion which is normalized using a specified policy
+     * registry.
      * 
-     * @param reg the policy registry used to resolve policy 
-     *            references
-     * @return an Assertion which is the normalized form of
-     *         self 
+     * @param reg
+     *           the policy registry used to resolve policy references
+     * @return an Assertion which is the normalized form of self
      */
     public Assertion normalize(PolicyRegistry reg) {
-        AndCompositeAssertion resultantAndTerm = new AndCompositeAssertion();
-        XorCompositeAssertion resultantXorTerm = new XorCompositeAssertion();
-        
-        ArrayList childAndTermList = new ArrayList();
-        ArrayList childXorTermList = new ArrayList();
-        
-        Iterator myTerms = getTerms().iterator();
-        
-        while (myTerms.hasNext()) {
-            Object term = myTerms.next();
-            
-            if (term instanceof PolicyReference) {
-                if (reg == null) {
-                    throw new RuntimeException("PolicyCache is not defined");
-                }
-                PolicyReference policyRef = (PolicyReference) term;
-                Policy policy = reg.lookup(policyRef.getPolicyURIString());
-                if (policy == null) {
-                    throw new RuntimeException("PolicyReference<" + policyRef.getPolicyURIString()
+ "can not be resolved");
-                }
-                
-                AndCompositeAssertion andTerm = new AndCompositeAssertion();
-                andTerm.addTerms(policy.getTerms());
-                Assertion normalizedPolicyRef = andTerm.normalize(reg);
-                
-                if (normalizedPolicyRef instanceof AndCompositeAssertion) {
-                    childAndTermList.add(normalizedPolicyRef);
-                } else {
-                    childXorTermList.add(normalizedPolicyRef);
+        AndCompositeAssertion AND = new AndCompositeAssertion();
+        XorCompositeAssertion XOR = new XorCompositeAssertion();
+
+        ArrayList andTerms = new ArrayList();
+        ArrayList xorTerms = new ArrayList();
+
+        Iterator terms = getTerms().iterator();
+
+        while (terms.hasNext()) {
+            Assertion term = (Assertion) terms.next();
+            term = term.normalize(reg);
+
+            if (term instanceof Policy) {
+                xorTerms.add(((Policy) term).getTerms().get(0));
+
+            } else if (term instanceof XorCompositeAssertion) {
+
+                if (((XorCompositeAssertion) term).isEmpty()) {
+
+                    /*  */
+                    XorCompositeAssertion anXorTerm = new XorCompositeAssertion();
+                    anXorTerm.setNormalized(true);
+                    return anXorTerm;
                 }
-                
-                 
-            } else if (term instanceof PrimitiveAssertion) {
-                resultantAndTerm.addTerm((Assertion) term);
-            
-            } else if (term instanceof CompositeAssertion) {
-                CompositeAssertion cterm = (CompositeAssertion) term;
-                
-                cterm =((cterm.isNormalized()) ? cterm  :(CompositeAssertion) cterm.normalize(reg));
-                
-                if (cterm instanceof AndCompositeAssertion) {
-                    childAndTermList.add(cterm);
-                } else {
-                    childXorTermList.add(cterm);
+                xorTerms.add(term);
+
+            } else if (term instanceof AndCompositeAssertion) {
+
+                if (!((AndCompositeAssertion) term).isEmpty()) {
+                    AND.addTerms(((AndCompositeAssertion) term)
+                            .getTerms());
                 }
+
+            } else {
+                AND.addTerm(term);
             }
         }
-        
-        // processing child-AndCompositeAssertions
-        
-        if (! childAndTermList.isEmpty()) {
-            Iterator ANDterms = childAndTermList.iterator();
-            
-            while (ANDterms.hasNext()) {
-                CompositeAssertion ANDterm = (CompositeAssertion) ANDterms.next();
-                resultantAndTerm.addTerms(ANDterm.getTerms());
-            }           
-        }       
-                
+
         // processing child-XORCompositeAssertions
-        if (childXorTermList.size() > 1) {
-            
-            outer : for (int i = 0; i < childXorTermList.size(); i++) {
-                inner : for (int j = i; j < childXorTermList.size(); j++) {
+        if (xorTerms.size() > 1) {
+
+            XorCompositeAssertion xorTermA, xorTermB;
+
+            for (int i = 0; i < xorTerms.size(); i++) {
+                
+                for (int j = i; j < xorTerms.size(); j++) {
+         
                     if (i != j) {
-                        XorCompositeAssertion xorTermA = (XorCompositeAssertion) childXorTermList.get(i);
-                        XorCompositeAssertion xorTermB = (XorCompositeAssertion) childXorTermList.get(j);
-                        
-                        /*
-                         * if xorTermA or xorTermB is empty then the result should be an

-                         * a policy with zero alternatives
-                         */
-                        
-                        if (xorTermA.isEmpty() || xorTermB.isEmpty()) {
-                            resultantXorTerm = new XorCompositeAssertion();
-                            break outer;
-                        }
+                        xorTermA = (XorCompositeAssertion) xorTerms
+                                .get(i);
+                        xorTermB = (XorCompositeAssertion) xorTerms
+                                .get(j);
+
                         Iterator interatorA = xorTermA.getTerms().iterator();
+
+                        CompositeAssertion andTermA;
+                        Iterator iteratorB;
                         
                         while (interatorA.hasNext()) {
-                            CompositeAssertion andTermA = (CompositeAssertion) interatorA.next();
-                            Iterator iteratorB = xorTermB.getTerms().iterator();
+                            andTermA = (CompositeAssertion) interatorA.next();
+                            iteratorB = xorTermB.getTerms().iterator();
+
+                            CompositeAssertion andTermB;
+                            AndCompositeAssertion anAndTerm;
                             
                             while (iteratorB.hasNext()) {
-                            
-                                CompositeAssertion andTermB = (CompositeAssertion) iteratorB.next();
-                                AndCompositeAssertion andTerm = new AndCompositeAssertion();
-                                andTerm.addTerms(andTermA.getTerms());
-                                andTerm.addTerms(andTermB.getTerms());
-                                resultantXorTerm.addTerm(andTerm);
+
+                                andTermB = (CompositeAssertion) iteratorB.next();
+                                anAndTerm = new AndCompositeAssertion();
+                                anAndTerm.addTerms(andTermA.getTerms());
+                                anAndTerm.addTerms(andTermB.getTerms());
+                                XOR.addTerm(anAndTerm);
                             }
                         }
-                        
+
                     }
                 }
             }
-        
-        } else if (childXorTermList.size() == 1) {
-            CompositeAssertion XORterm = (CompositeAssertion) childXorTermList.get(0);
-            resultantXorTerm.addTerms(XORterm.getTerms());
+
+        } else if (xorTerms.size() == 1) {
+            CompositeAssertion XORterm = (CompositeAssertion) xorTerms
+                    .get(0);
+            XOR.addTerms(XORterm.getTerms());
         }
         
-        if (childXorTermList.isEmpty()) {
-    
-            resultantAndTerm.setNormalized(true);
-            return resultantAndTerm;
-        } 
-        
-        if (resultantXorTerm.isEmpty()) {
-            if (resultantAndTerm.isEmpty()) {
-                resultantAndTerm.setNormalized(true);
-                return resultantAndTerm;
-            }
-            resultantXorTerm.setNormalized(true);
-            return resultantXorTerm;
+        if (AND.isEmpty()) {
+            XOR.setNormalized(true);
+            return XOR;
         }
         
-        //  get list of primitive assertions form result (AndCompositeAssertion)
-        List primTerms = resultantAndTerm.getTerms();
-        
-        // these terms should be AndCompositeAssertions
-        Iterator andTerms = resultantXorTerm.getTerms().iterator();
-        
-        while (andTerms.hasNext()) {
-            CompositeAssertion andTerm = (CompositeAssertion) andTerms.next();
+        List primTerms = AND.getTerms();
+        Iterator interator = XOR.getTerms().iterator();
+
+        while (interator.hasNext()) {
+            CompositeAssertion andTerm = (CompositeAssertion) interator.next();
             andTerm.addTerms(primTerms);
         }
-        
-        resultantXorTerm.setNormalized(true);
-        return resultantXorTerm;
+
+        XOR.setNormalized(true);
+        return XOR;
     }
 }



Mime
View raw message