ws-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sa...@apache.org
Subject svn commit: r355068 - in /webservices/commons/trunk/policy/src/org/apache/ws/policy/model: AndCompositeAssertion.java Policy.java PolicyReference.java PrimitiveAssertion.java XorCompositeAssertion.java
Date Thu, 08 Dec 2005 10:56:25 GMT
Author: sanka
Date: Thu Dec  8 02:56:16 2005
New Revision: 355068

URL: http://svn.apache.org/viewcvs?rev=355068&view=rev
Log:
some refactoring in normalize(..) method in all core classes

Modified:
    webservices/commons/trunk/policy/src/org/apache/ws/policy/model/AndCompositeAssertion.java
    webservices/commons/trunk/policy/src/org/apache/ws/policy/model/Policy.java
    webservices/commons/trunk/policy/src/org/apache/ws/policy/model/PolicyReference.java
    webservices/commons/trunk/policy/src/org/apache/ws/policy/model/PrimitiveAssertion.java
    webservices/commons/trunk/policy/src/org/apache/ws/policy/model/XorCompositeAssertion.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=355068&r1=355067&r2=355068&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
Thu Dec  8 02:56:16 2005
@@ -229,17 +229,26 @@
         ArrayList andTerms = new ArrayList();
         ArrayList xorTerms = new ArrayList();
 
+        if (isEmpty()) {
+            AND.setNormalized(true);
+            return AND;
+        }
+
         Iterator terms = getTerms().iterator();
 
         while (terms.hasNext()) {
             Assertion term = (Assertion) terms.next();
-            term = term.normalize(reg);
+            term = (term instanceof Policy) ? term : term.normalize(reg);
 
             if (term instanceof Policy) {
-                xorTerms.add(((Policy) term).getTerms().get(0));
-
-            } else if (term instanceof XorCompositeAssertion) {
+                Assertion wrapper = new AndCompositeAssertion();
+                ((AndCompositeAssertion) wrapper).addTerms(((Policy) term)
+                        .getTerms());
+                term = wrapper.normalize(reg);
+            }
 
+            if (term instanceof XorCompositeAssertion) {
+                
                 if (((XorCompositeAssertion) term).isEmpty()) {
 
                     /*  */
@@ -248,17 +257,16 @@
                     return anXorTerm;
                 }
                 xorTerms.add(term);
+                break;
 
-            } else if (term instanceof AndCompositeAssertion) {
-
-                if (!((AndCompositeAssertion) term).isEmpty()) {
-                    AND.addTerms(((AndCompositeAssertion) term)
-                            .getTerms());
-                }
-
-            } else {
-                AND.addTerm(term);
             }
+            
+            if (term instanceof AndCompositeAssertion) {
+                AND.addTerms(((AndCompositeAssertion) term).getTerms());
+                break;
+            }
+            
+            AND.addTerm(term);
         }
 
         // processing child-XORCompositeAssertions
@@ -267,30 +275,29 @@
             XorCompositeAssertion xorTermA, xorTermB;
 
             for (int i = 0; i < xorTerms.size(); i++) {
-                
+
                 for (int j = i; j < xorTerms.size(); j++) {
-         
+
                     if (i != j) {
-                        xorTermA = (XorCompositeAssertion) xorTerms
-                                .get(i);
-                        xorTermB = (XorCompositeAssertion) xorTerms
-                                .get(j);
+                        xorTermA = (XorCompositeAssertion) xorTerms.get(i);
+                        xorTermB = (XorCompositeAssertion) xorTerms.get(j);
 
                         Iterator interatorA = xorTermA.getTerms().iterator();
 
                         CompositeAssertion andTermA;
                         Iterator iteratorB;
-                        
+
                         while (interatorA.hasNext()) {
                             andTermA = (CompositeAssertion) interatorA.next();
                             iteratorB = xorTermB.getTerms().iterator();
 
                             CompositeAssertion andTermB;
                             AndCompositeAssertion anAndTerm;
-                            
+
                             while (iteratorB.hasNext()) {
 
-                                andTermB = (CompositeAssertion) iteratorB.next();
+                                andTermB = (CompositeAssertion) iteratorB
+                                        .next();
                                 anAndTerm = new AndCompositeAssertion();
                                 anAndTerm.addTerms(andTermA.getTerms());
                                 anAndTerm.addTerms(andTermB.getTerms());
@@ -303,16 +310,15 @@
             }
 
         } else if (xorTerms.size() == 1) {
-            CompositeAssertion XORterm = (CompositeAssertion) xorTerms
-                    .get(0);
+            CompositeAssertion XORterm = (CompositeAssertion) xorTerms.get(0);
             XOR.addTerms(XORterm.getTerms());
         }
-        
+
         if (AND.isEmpty()) {
             XOR.setNormalized(true);
             return XOR;
         }
-        
+
         List primTerms = AND.getTerms();
         Iterator interator = XOR.getTerms().iterator();
 

Modified: webservices/commons/trunk/policy/src/org/apache/ws/policy/model/Policy.java
URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/ws/policy/model/Policy.java?rev=355068&r1=355067&r2=355068&view=diff
==============================================================================
--- webservices/commons/trunk/policy/src/org/apache/ws/policy/model/Policy.java (original)
+++ webservices/commons/trunk/policy/src/org/apache/ws/policy/model/Policy.java Thu Dec  8
02:56:16 2005
@@ -22,211 +22,196 @@
 
 import org.apache.ws.policy.util.PolicyRegistry;
 
+
 /**
- * Policy is the access point for policy framework. It the object model that 
+ * Policy is the access point for policy framework. It the object model that
  * represents a policy at runtime.
- *
+ *  
  */
 public class Policy extends AndCompositeAssertion implements Assertion {
     private String policyURI = null;
+
     private String xmlBase = null;
+
     private String id = null;
-    
+
     public Policy() {
         setNormalized(false);
     }
-    
+
     public Policy(String id) {
         this(null, id);
         setNormalized(false);
     }
-    
+
     public Policy(String xmlBase, String id) {
         this.xmlBase = xmlBase;
-        this.id = id;       
+        this.id = id;
         setNormalized(false);
     }
-    
+
     public void setBase(String xmlBase) {
         this.xmlBase = xmlBase;
     }
-    
+
     public String getBase() {
         return xmlBase;
     }
-    
+
     public void setId(String id) {
-        this.id = id;   
+        this.id = id;
     }
-    
+
     public String getId() {
         return id;
     }
-    
+
     public String getPolicyURI() {
         return (xmlBase != null) ? xmlBase + "#" + id : "#" + id;
     }
-    
+
     public Assertion normalize() {
         return normalize(null);
     }
-    
+
     public Assertion normalize(PolicyRegistry reg) {
-        if (getParent() == null) {
-            
-            String xmlBase = getBase();
-            String id      = getId();
-            Policy result = new Policy(xmlBase, id);
-            
-            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 PrimitiveAssertion) {
-                    resultantAndTerm.addTerm((Assertion) term);
-                
-                } else 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);
-                    }
-                    
-                } else if (term instanceof CompositeAssertion) {
-                    CompositeAssertion cterm = (CompositeAssertion) term;
+       
+
+        String xmlBase = getBase();
+        String id = getId();
+        Policy policy = new Policy(xmlBase, id);
+
+        AndCompositeAssertion AND = new AndCompositeAssertion();
+        XorCompositeAssertion XOR = new XorCompositeAssertion();
+
+        ArrayList childAndTermList = new ArrayList();
+        ArrayList childXorTermList = new ArrayList();
+
+        Iterator terms = getTerms().iterator();
+
+        while (terms.hasNext()) {
+            Assertion term = (Assertion) terms.next();
+            term = (term instanceof Policy) ? term.normalize(reg) : term;
+            
+            if (term instanceof Policy) {
+                Assertion wrapper = new AndCompositeAssertion();
+                ((AndCompositeAssertion) wrapper).addTerms(((Policy) term).getTerms());
+                term = wrapper.normalize(reg);                    
+                break;
+            }
+            if (term instanceof XorCompositeAssertion) {
+                if (((XorCompositeAssertion) term).isEmpty()) {
+                    XorCompositeAssertion emptyXor = new XorCompositeAssertion();
+                    emptyXor.setNormalized(true);
                     
-                    cterm =((cterm.isNormalized()) ? cterm  :(CompositeAssertion) cterm.normalize(reg));
+                    policy.addTerm(emptyXor);
+                    policy.setNormalized(true);
                     
-                    if (cterm instanceof AndCompositeAssertion) {
-                        childAndTermList.add(cterm);
-                    } else {
-                        childXorTermList.add(cterm);
-                    }
+                    return policy;
                 }
+                
+                childXorTermList.add(term);
+                break;
             }
             
-            // processing child-AndCompositeAssertion
-            if (! childAndTermList.isEmpty()) {
-                Iterator andTerms = childAndTermList.iterator();
+            if (term instanceof AndCompositeAssertion) {
                 
-                while (andTerms.hasNext()) {
-                    CompositeAssertion andTerm = (CompositeAssertion) andTerms.next();
-                    resultantAndTerm.addTerms(andTerm.getTerms());
-                }           
-            }       
+                if (((AndCompositeAssertion) term).isEmpty()) {
+                    AndCompositeAssertion emptyAnd = new AndCompositeAssertion();
+                    XOR.addTerm(emptyAnd);
                     
-            // processing child-XORCompositeAssertions
-            if (childXorTermList.size() > 1) {
+                } else {
+                    AND.addTerms(((AndCompositeAssertion) term).getTerms());
+                }
+                break;
+            }
+            AND.addTerm((Assertion) term);
+        }
+
+        // processing child-XORCompositeAssertions
+        if (childXorTermList.size() > 1) {
+
+            for (int i = 0; i < childXorTermList.size(); i++) {
+                
+                for (int j = i; j < childXorTermList.size(); j++) {
                 
-                outer : for (int i = 0; i < childXorTermList.size(); i++) {
-                    inner : for (int j = i; j < childXorTermList.size(); j++) {
-                        if (i != j) {
-                            XorCompositeAssertion xorTermA = (XorCompositeAssertion) childXorTermList.get(i);
-                            XorCompositeAssertion xorTermB = (XorCompositeAssertion) childXorTermList.get(j);
+                    if (i != j) {
+                        XorCompositeAssertion xorTermA = (XorCompositeAssertion) childXorTermList
+                                .get(i);
+                        XorCompositeAssertion xorTermB = (XorCompositeAssertion) childXorTermList
+                                .get(j);
+
+                        Iterator iterA = xorTermA.getTerms().iterator();
+
+                        while (iterA.hasNext()) {
+                            CompositeAssertion andTermA = (CompositeAssertion) iterA
+                                    .next();
                             
-                            // what if XORtermA or XORtermB is empty?
-                            if (xorTermA.isEmpty() || xorTermB.isEmpty()) {
-                                resultantXorTerm = new XorCompositeAssertion();
-                                break outer;
-                            }
-                            Iterator iterA = xorTermA.getTerms().iterator();
+                            Iterator iterB = xorTermB.getTerms().iterator();
                             
-                            while (iterA.hasNext()) {
-                                // must be an ANDterm
-                                CompositeAssertion andTermA = (CompositeAssertion) iterA.next();
-                                Iterator iterB = xorTermB.getTerms().iterator();
-                                while (iterB.hasNext()) {
-                                    // must be an ANDterm
-                                    CompositeAssertion andTermB = (CompositeAssertion) iterB.next();
-                                    AndCompositeAssertion andTerm = new AndCompositeAssertion();
-                                    andTerm.addTerms(andTermA.getTerms());
-                                    andTerm.addTerms(andTermB.getTerms());
-                                    resultantXorTerm.addTerm(andTerm);
-                                }
+                            while (iterB.hasNext()) {
+                                CompositeAssertion andTermB = (CompositeAssertion) iterB
+                                        .next();
+                                AndCompositeAssertion 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());
-            }
-                    
-            if (childXorTermList.isEmpty()) {
-                XorCompositeAssertion alters = new XorCompositeAssertion();
-                alters.addTerm(resultantAndTerm);
-                result.addTerm(alters);
-                result.setNormalized(true);
-                return result;
-            } 
-            
-            if (resultantXorTerm.isEmpty()) {
-                result.addTerm(resultantXorTerm);
-                result.setNormalized(true);
-                return result;
-            }
-            
-            //  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();
-                andTerm.addTerms(primTerms);
             }
-            result.addTerm(resultantXorTerm);
-            result.setNormalized(true);
-            return result;
-            
-        } else {
-            return super.normalize();
+
+        } else if (childXorTermList.size() == 1) {
+            CompositeAssertion xorTerm = (CompositeAssertion) childXorTermList
+                    .get(0);
+            XOR.addTerms(xorTerm.getTerms());
+        }
+
+        if (childXorTermList.isEmpty()) {
+            XorCompositeAssertion xor = new XorCompositeAssertion();
+            
+            xor.addTerm(AND);
+            policy.addTerm(xor);
+            return policy;
+        }
+
+        List primTerms = AND.getTerms();
+        Iterator andTerms = XOR.getTerms().iterator();
+
+        while (andTerms.hasNext()) {
+            CompositeAssertion anAndTerm = (CompositeAssertion) andTerms
+                    .next();
+            anAndTerm.addTerms(primTerms);
         }
-    }
-    
-    public Assertion intersect(Assertion assertion , PolicyRegistry reg) {
         
+        policy.addTerm(XOR);
+        policy.setNormalized(true);
+        return policy;
+
+    }
+
+    public Assertion intersect(Assertion assertion, PolicyRegistry reg) {
+
         Policy result = new Policy(getBase(), getId());
-        Policy normalizedMe = (Policy) ((isNormalized()) ? this : normalize(reg));
-                
-        XorCompositeAssertion alters = (XorCompositeAssertion) normalizedMe.getTerms().get(0);
-        
+        Policy normalizedMe = (Policy) ((isNormalized()) ? this
+                : normalize(reg));
+
+        XorCompositeAssertion alters = (XorCompositeAssertion) normalizedMe
+                .getTerms().get(0);
+
         if (assertion instanceof PrimitiveAssertion) {
             result.addTerm(alters.intersect(assertion, reg));
             return result;
-            
+
         } else {
             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 alters2 = (XorCompositeAssertion) target.getTerms().get(0);
+                XorCompositeAssertion alters2 = (XorCompositeAssertion) target
+                        .getTerms().get(0);
                 result.addTerm(alters.intersect(alters2));
                 return result;
             } else {
@@ -235,13 +220,15 @@
             }
         }
     }
-    
+
     public Assertion merge(Assertion assertion, PolicyRegistry reg) {
         Policy result = new Policy(getBase(), getId());
-        Policy normalizedMe = (Policy) ((isNormalized()) ? this : normalize(reg));
-        XorCompositeAssertion alters = (XorCompositeAssertion) normalizedMe.getTerms().get(0);
+        Policy normalizedMe = (Policy) ((isNormalized()) ? this
+                : normalize(reg));
+        XorCompositeAssertion alters = (XorCompositeAssertion) normalizedMe
+                .getTerms().get(0);
         Assertion test = alters.merge(assertion, reg);
-        
+
         result.addTerm(test);
         result.setNormalized(true);
         return result;

Modified: webservices/commons/trunk/policy/src/org/apache/ws/policy/model/PolicyReference.java
URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/ws/policy/model/PolicyReference.java?rev=355068&r1=355067&r2=355068&view=diff
==============================================================================
--- webservices/commons/trunk/policy/src/org/apache/ws/policy/model/PolicyReference.java (original)
+++ webservices/commons/trunk/policy/src/org/apache/ws/policy/model/PolicyReference.java Thu
Dec  8 02:56:16 2005
@@ -19,49 +19,58 @@
 import org.apache.ws.policy.util.PolicyRegistry;
 
 /**
- * PolicyReference class has implicit reference to a external policy. It acts 
- * as wrapper to external policies in the standard policy framework.
- *
+ * PolicyReference class has implicit reference to a external policy. It acts as
+ * wrapper to external policies in the standard policy framework.
+ *  
  */
 public class PolicyReference implements Assertion {
-    
+
     private String PolicyURIString = null;
+
     private Assertion parent = null;
-        
+
     public PolicyReference(String policyURIString) {
         this.PolicyURIString = policyURIString;
     }
-    
+
     public String getPolicyURIString() {
         return PolicyURIString;
     }
-    
+
     public Assertion normalize() {
         throw new UnsupportedOperationException();
     }
-    
+
     public Assertion normalize(PolicyRegistry reg) {
-        throw new UnsupportedOperationException();
+        if (reg == null) {
+            throw new RuntimeException("Cannot resolve : "
+                    + getPolicyURIString() + " .. PolicyRegistry is null");
+        }
+        Policy targetPolicy = reg.lookup(getPolicyURIString());
+        if (targetPolicy == null) {
+            throw new RuntimeException("error : " + getPolicyURIString()
+                    + " doesn't resolve to any known policy");
+        }
+
+        return targetPolicy.normalize(reg);
     }
-    
+
     public Assertion intersect(Assertion assertion) {
-        throw new UnsupportedOperationException("intersect is not defined for "
-                + "PolicyReference");
+        throw new UnsupportedOperationException();
     }
-    
+
     public Assertion intersect(Assertion assertion, PolicyRegistry reg)
             throws UnsupportedOperationException {
         throw new UnsupportedOperationException();
     }
-    
+
     public Assertion merge(Assertion assertion, PolicyRegistry reg)
             throws UnsupportedOperationException {
         throw new UnsupportedOperationException();
     }
-    
+
     public Assertion merge(Assertion assertion) {
-        throw new UnsupportedOperationException("merge is not supported  for " +
-                "PolicyReference");
+        throw new UnsupportedOperationException();
     }
 
     public boolean hasParent() {
@@ -75,11 +84,11 @@
     public void setParent(Assertion parent) {
         this.parent = parent;
     }
-    
+
     public boolean isNormalized() {
         throw new UnsupportedOperationException();
     }
-    
+
     public void setNormalized(boolean flag) {
         throw new UnsupportedOperationException();
     }

Modified: webservices/commons/trunk/policy/src/org/apache/ws/policy/model/PrimitiveAssertion.java
URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/ws/policy/model/PrimitiveAssertion.java?rev=355068&r1=355067&r2=355068&view=diff
==============================================================================
--- webservices/commons/trunk/policy/src/org/apache/ws/policy/model/PrimitiveAssertion.java
(original)
+++ webservices/commons/trunk/policy/src/org/apache/ws/policy/model/PrimitiveAssertion.java
Thu Dec  8 02:56:16 2005
@@ -38,9 +38,11 @@
     private List terms = new ArrayList();
     private Hashtable attributes = new Hashtable();
     private boolean flag = false;
+    private boolean isOptional = false;
     private String strValue = null;
     
     private Object value;
+    
     public PrimitiveAssertion(QName qname) {
         this.qname = qname;
     }
@@ -150,6 +152,25 @@
     }
         
     public Assertion normalize(PolicyRegistry reg) {
+        String isOptional;
+          
+        if (isOptional()) {
+            
+            XorCompositeAssertion xorCompositeAssertion = new XorCompositeAssertion();
+            AndCompositeAssertion andCompositeAssertion = new AndCompositeAssertion();
+                        
+            PrimitiveAssertion assertion = getSelfWithoutTerms();
+            assertion.removeAttribute(new QName(PolicyConstants.WS_POLICY_NAMESPACE_URI,
"Optional"));
+            assertion.setOptional(false);
+            assertion.setTerms(getTerms());
+            
+            andCompositeAssertion.addTerm(assertion);
+            xorCompositeAssertion.addTerm(andCompositeAssertion);
+            xorCompositeAssertion.addTerm(new AndCompositeAssertion());
+            
+            return xorCompositeAssertion.normalize(reg);
+        }
+        
         if (getTerms().isEmpty()) {
             PrimitiveAssertion primitiveAssertion = getSelfWithoutTerms();
             primitiveAssertion.setNormalized(true);
@@ -248,6 +269,7 @@
     private PrimitiveAssertion getSelfWithoutTerms() {
         PrimitiveAssertion self = new PrimitiveAssertion(getName());
         self.setAttributes(getAttributes());
+        self.setStrValue(getStrValue());
         return self;
     }
         
@@ -275,6 +297,10 @@
         return (String) attributes.get(qname);
     }
     
+    public void removeAttribute(QName qname) {
+        attributes.remove(qname);
+    }
+    
     public void setParent(Assertion parent) {
         this.owner = parent;
     }
@@ -312,6 +338,14 @@
         this.strValue = strValue;        
     }
     
+    public boolean isOptional() {
+        return isOptional;
+    }
+    
+    public void setOptional(boolean isOptional) {
+        this.isOptional = isOptional;
+    }
+    
     private Policy getSinglePolicy(List policyList, PolicyRegistry reg) {
         Policy result = null;
         Iterator iterator = policyList.iterator();
@@ -342,8 +376,6 @@
     
     private List getTerms(Policy policy) {
         return ((AndCompositeAssertion) ((XorCompositeAssertion) policy.getTerms().get(0)).getTerms().get(0)).getTerms();
-        
-    }
-    
+    }    
     
 }

Modified: webservices/commons/trunk/policy/src/org/apache/ws/policy/model/XorCompositeAssertion.java
URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/ws/policy/model/XorCompositeAssertion.java?rev=355068&r1=355067&r2=355068&view=diff
==============================================================================
--- webservices/commons/trunk/policy/src/org/apache/ws/policy/model/XorCompositeAssertion.java
(original)
+++ webservices/commons/trunk/policy/src/org/apache/ws/policy/model/XorCompositeAssertion.java
Thu Dec  8 02:56:16 2005
@@ -23,141 +23,150 @@
 /**
  * XORCompositeAssertion represents a bunch of policy alternatives. It requires
  * that exactly one of its terms (policy alternative) is statisfied.
- * 
+ *  
  */
-public class XorCompositeAssertion extends CompositeAssertion implements Assertion  {
-    
+public class XorCompositeAssertion extends CompositeAssertion implements
+        Assertion {
+
     public XorCompositeAssertion() {
     }
-    
+
     public void addTerm(Assertion assertion) {
-        if(!(isNormalized() && (assertion instanceof AndCompositeAssertion) 
-                && ((AndCompositeAssertion) assertion).isNormalized())) {
+        if (!(isNormalized() && (assertion instanceof AndCompositeAssertion) &&
((AndCompositeAssertion) assertion)
+                .isNormalized())) {
             setNormalized(false);
         }
         super.addTerm(assertion);
     }
-    
+
     public Assertion normalize(PolicyRegistry reg) {
-        XorCompositeAssertion xorLogic = new XorCompositeAssertion();
-        Iterator terms = getTerms().iterator();
+        XorCompositeAssertion XOR = new XorCompositeAssertion();
         
+        if (isEmpty()) {
+            XOR.setNormalized(true);
+            return XOR;
+        }
+        
+        Iterator terms = getTerms().iterator();
+       
         while (terms.hasNext()) {
             Assertion term = (Assertion) terms.next();
-            
-            if (term instanceof PrimitiveAssertion) { // just wrap it in an AND
-                                                      // logic and add 
-                AndCompositeAssertion wrapper = new AndCompositeAssertion();
-                wrapper.addTerm(term);
-                xorLogic.addTerm(wrapper);
-            } else 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()
+"> cannot be resolved");
-                } 
-                AndCompositeAssertion andTerm = new AndCompositeAssertion();
-                andTerm.addTerms(policy.getTerms());
-                Assertion normalizedPolicy = andTerm.normalize(reg);
-                if (normalizedPolicy instanceof XorCompositeAssertion) {
-                    xorLogic.addTerms(((XorCompositeAssertion) normalizedPolicy).getTerms());
-                } else {
-                    xorLogic.addTerm(normalizedPolicy);
-                }
-                
-            } else {
-                // must be a composite assertion
-                CompositeAssertion cterm = (CompositeAssertion) term;
-                cterm =((cterm.isNormalized()) ? cterm  :(CompositeAssertion) cterm.normalize(reg));
-                
-                if (cterm instanceof XorCompositeAssertion) {
-                    // just adds the child-terms to super
-                    xorLogic.addTerms(cterm.getTerms());
+            term = (term instanceof Policy) ? term : term.normalize(reg);
+
+            if (term instanceof Policy) {
+                Assertion wrapper = new AndCompositeAssertion();
+                ((AndCompositeAssertion) wrapper).addTerms(((Policy) term)
+                        .getTerms());
+                wrapper = wrapper.normalize(reg);
+
+                if (wrapper instanceof AndCompositeAssertion) {
+                    XOR.addTerm(wrapper);
+
                 } else {
-                    // must be an AndCompositeAssertion with primitives
-                    xorLogic.addTerm(cterm);
+                    XOR.addTerms(((XorCompositeAssertion) wrapper).getTerms());
                 }
+                break;
+            }
+
+            if (term instanceof PrimitiveAssertion) {
+                AndCompositeAssertion wrapper = new AndCompositeAssertion();
+                wrapper.addTerm(term);
+                XOR.addTerm(wrapper);
+                break;
+            }
+
+            if (term instanceof XorCompositeAssertion) {
+                XOR.addTerms(((XorCompositeAssertion) term).getTerms());
+                break;
+            }
+
+            if (term instanceof AndCompositeAssertion) {
+                XOR.addTerm(term);
             }
         }
-        xorLogic.setNormalized(true);
-        return xorLogic;
+
+        XOR.setNormalized(true);
+        return XOR;
     }
-    
+
     public Assertion intersect(Assertion assertion, PolicyRegistry reg) {
-        CompositeAssertion normalizedMe = (CompositeAssertion) ((isNormalized()) ? this :
normalize(reg));
-        
+        CompositeAssertion normalizedMe = (CompositeAssertion) ((isNormalized()) ? this
+                : normalize(reg));
+
         if (!(normalizedMe instanceof XorCompositeAssertion)) {
             return normalizedMe.intersect(assertion, reg);
         }
-        
+
         XorCompositeAssertion result = new XorCompositeAssertion();
-        
+
         if (assertion instanceof PrimitiveAssertion) {
-        
+
             Iterator iterator = normalizedMe.getTerms().iterator();
-            
+
             while (iterator.hasNext()) {
-                AndCompositeAssertion andTerm = (AndCompositeAssertion) iterator.next();
+                AndCompositeAssertion andTerm = (AndCompositeAssertion) iterator
+                        .next();
                 Assertion value = andTerm.intersect(assertion);
                 if (value instanceof AndCompositeAssertion) {
                     result.addTerm(value);
                 }
             }
-                            
+
         } else {
             CompositeAssertion target = (CompositeAssertion) assertion;
-            target = (CompositeAssertion) ((target.isNormalized()) ? target : target.normalize(reg));
-            
+            target = (CompositeAssertion) ((target.isNormalized()) ? target
+                    : target.normalize(reg));
+
             Iterator iterator = normalizedMe.getTerms().iterator();
             while (iterator.hasNext()) {
-                AndCompositeAssertion andTerm = (AndCompositeAssertion) iterator.next();
-        
+                AndCompositeAssertion andTerm = (AndCompositeAssertion) iterator
+                        .next();
+
                 if (target instanceof AndCompositeAssertion) {
                     Assertion value = andTerm.intersect(target);
-                    
+
                     if (value instanceof AndCompositeAssertion) {
                         result.addTerm(value);
-                    }               
-                    
+                    }
+
                 } else if (target instanceof XorCompositeAssertion) {
-                    
+
                     Iterator andTerms = target.getTerms().iterator();
-                                        
+
                     while (andTerms.hasNext()) {
-                        AndCompositeAssertion tAndTerm = (AndCompositeAssertion) andTerms.next();
+                        AndCompositeAssertion tAndTerm = (AndCompositeAssertion) andTerms
+                                .next();
                         Assertion value = andTerm.intersect(tAndTerm);
-                        
+
                         if (value instanceof AndCompositeAssertion) {
                             result.addTerm(value);
                         }
                     }
-                }           
-            }           
+                }
+            }
         }
-        
+
         return result;
     }
 
     public Assertion merge(Assertion assertion, PolicyRegistry reg) {
-        CompositeAssertion normalizedMe = (CompositeAssertion) ((isNormalized()) ? this :
normalize(reg));
-    
+        CompositeAssertion normalizedMe = (CompositeAssertion) ((isNormalized()) ? this
+                : normalize(reg));
+
         if (!(normalizedMe instanceof XorCompositeAssertion)) {
             return normalizedMe.merge(assertion, reg);
         }
-                
+
         if (assertion instanceof PrimitiveAssertion) {
             XorCompositeAssertion xorTerm = new XorCompositeAssertion();
-            
+
             Iterator iterator = normalizedMe.getTerms().iterator();
             if (iterator.hasNext()) {
                 do {
                     AndCompositeAssertion andTerm = new AndCompositeAssertion();
                     andTerm.addTerm(assertion);
-                    AndCompositeAssertion anAndTerm = (AndCompositeAssertion) iterator.next();
+                    AndCompositeAssertion anAndTerm = (AndCompositeAssertion) iterator
+                            .next();
                     andTerm.addTerms(anAndTerm.getTerms());
                     xorTerm.addTerm(andTerm);
                 } while (iterator.hasNext());
@@ -169,46 +178,52 @@
             xorTerm.setNormalized(true);
             return xorTerm;
         }
-        
+
         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();
             Iterator myAndTerms = normalizedMe.getTerms().iterator();
-            
+
             while (myAndTerms.hasNext()) {
-                AndCompositeAssertion myAndTerm = (AndCompositeAssertion) myAndTerms.next();
+                AndCompositeAssertion myAndTerm = (AndCompositeAssertion) myAndTerms
+                        .next();
                 while (hisAndTerms.hasNext()) {
-                    AndCompositeAssertion hisAndTerm = (AndCompositeAssertion) hisAndTerms.next();
+                    AndCompositeAssertion hisAndTerm = (AndCompositeAssertion) hisAndTerms
+                            .next();
                     xorTerm.addTerm(myAndTerm.merge(hisAndTerm));
                 }
             }
-            
+
             xorTerm.setNormalized(true);
             return xorTerm;
-            
+
         } else if (target instanceof AndCompositeAssertion) {
             XorCompositeAssertion xorTerm = new XorCompositeAssertion();
             Iterator myAndTerms = normalizedMe.getTerms().iterator();
-            
+
             while (myAndTerms.hasNext()) {
                 AndCompositeAssertion andTerm = new AndCompositeAssertion();
                 andTerm.addTerms(target.getTerms());
-                AndCompositeAssertion myAndTerm = (AndCompositeAssertion) myAndTerms.next();
+                AndCompositeAssertion myAndTerm = (AndCompositeAssertion) myAndTerms
+                        .next();
                 andTerm.addTerms(myAndTerm.getTerms());
-                xorTerm.addTerm(andTerm);               
+                xorTerm.addTerm(andTerm);
             }
-            
+
             xorTerm.setNormalized(true);
-            return xorTerm;         
+            return xorTerm;
         }
-        
-        throw new IllegalArgumentException("error : merge is not defined for" + target.getClass().getName());
-    }   
+
+        throw new IllegalArgumentException("error : merge is not defined for"
+                + target.getClass().getName());
+    }
 }



Mime
View raw message