Author: sanka
Date: Sat Nov 19 09:11:19 2005
New Revision: 345654

URL: http://svn.apache.org/viewcvs?rev=345654&view=rev
Log:
Adding PolicyFactory and PolicyWriter classes which add support for policy 
serialization.

Added:
    
webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyFactory.java
    
webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyWriter.java
Modified:
    webservices/commons/trunk/policy/src/org/apache/policy/model/Assertion.java
    
webservices/commons/trunk/policy/src/org/apache/policy/model/CompositeAssertion.java
    
webservices/commons/trunk/policy/src/org/apache/policy/model/PrimitiveAssertion.java
    
webservices/commons/trunk/policy/src/org/apache/policy/parser/WSPConstants.java
    
webservices/commons/trunk/policy/src/org/apache/policy/parser/WSParserUtil.java
    
webservices/commons/trunk/policy/src/org/apache/policy/parser/WSPolicyParser.java

Modified: 
webservices/commons/trunk/policy/src/org/apache/policy/model/Assertion.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/policy/model/Assertion.java?rev=345654&r1=345653&r2=345654&view=diff
==============================================================================
--- webservices/commons/trunk/policy/src/org/apache/policy/model/Assertion.java 
(original)
+++ webservices/commons/trunk/policy/src/org/apache/policy/model/Assertion.java 
Sat Nov 19 09:11:19 2005
@@ -96,7 +96,7 @@
      * @throws UnsupportedOperationException if the merge is not meaningful

      */

     public Assertion merge(Assertion assertion, PolicyRegistry reg) throws 
UnsupportedOperationException;

-    

+        

     /**

      * 

      * @return


Modified: 
webservices/commons/trunk/policy/src/org/apache/policy/model/CompositeAssertion.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/policy/model/CompositeAssertion.java?rev=345654&r1=345653&r2=345654&view=diff
==============================================================================
--- 
webservices/commons/trunk/policy/src/org/apache/policy/model/CompositeAssertion.java
 (original)
+++ 
webservices/commons/trunk/policy/src/org/apache/policy/model/CompositeAssertion.java
 Sat Nov 19 09:11:19 2005
@@ -19,8 +19,6 @@
 import java.util.Iterator;

 import java.util.List;

 

-import org.apache.policy.util.PolicyRegistry;

-

 /**

  * CompositeAssertion abstract class implements few method which are common to

  * composite assertions. A composite assertion as some terms (if any) and 


Modified: 
webservices/commons/trunk/policy/src/org/apache/policy/model/PrimitiveAssertion.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/policy/model/PrimitiveAssertion.java?rev=345654&r1=345653&r2=345654&view=diff
==============================================================================
--- 
webservices/commons/trunk/policy/src/org/apache/policy/model/PrimitiveAssertion.java
 (original)
+++ 
webservices/commons/trunk/policy/src/org/apache/policy/model/PrimitiveAssertion.java
 Sat Nov 19 09:11:19 2005
@@ -349,4 +349,6 @@
         return ((AndCompositeAssertion) ((XorCompositeAssertion) 
policy.getTerms().get(0)).getTerms().get(0)).getTerms();

         

     }

+    

+    

 }


Modified: 
webservices/commons/trunk/policy/src/org/apache/policy/parser/WSPConstants.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/policy/parser/WSPConstants.java?rev=345654&r1=345653&r2=345654&view=diff
==============================================================================
--- 
webservices/commons/trunk/policy/src/org/apache/policy/parser/WSPConstants.java 
(original)
+++ 
webservices/commons/trunk/policy/src/org/apache/policy/parser/WSPConstants.java 
Sat Nov 19 09:11:19 2005
@@ -33,6 +33,9 @@
     public static final String WS_POLICY = "Policy";

     

     /** */

+    public static final String WS_POLICY_PREFIX = "wsp";

+    

+    /** */

     public static final String WS_POLICY_REFERENCE = "PolicyReference";

     

     /** */


Modified: 
webservices/commons/trunk/policy/src/org/apache/policy/parser/WSParserUtil.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/policy/parser/WSParserUtil.java?rev=345654&r1=345653&r2=345654&view=diff
==============================================================================
--- 
webservices/commons/trunk/policy/src/org/apache/policy/parser/WSParserUtil.java 
(original)
+++ 
webservices/commons/trunk/policy/src/org/apache/policy/parser/WSParserUtil.java 
Sat Nov 19 09:11:19 2005
@@ -40,14 +40,14 @@
             result.setStrValue(strValue);            
         }
         
-        Iterator childElements = element.getChildElements();
-        if (!(childElements.hasNext())) {
-            result.addTerm(element);
-            return result;
-        }
+//        Iterator childElements = element.getChildElements();
+//        if (!(childElements.hasNext())) {
+//            result.addTerm(element);
+//            return result;
+//        }
                 
         //CHECK ME
-        childElements = element.getChildElements();
+        Iterator childElements = element.getChildElements();
         while (childElements.hasNext()) {
             OMElement childElement = (OMElement) childElements.next();
             

Modified: 
webservices/commons/trunk/policy/src/org/apache/policy/parser/WSPolicyParser.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/policy/parser/WSPolicyParser.java?rev=345654&r1=345653&r2=345654&view=diff
==============================================================================
--- 
webservices/commons/trunk/policy/src/org/apache/policy/parser/WSPolicyParser.java
 (original)
+++ 
webservices/commons/trunk/policy/src/org/apache/policy/parser/WSPolicyParser.java
 Sat Nov 19 09:11:19 2005
@@ -31,7 +31,6 @@
 import org.apache.axis2.om.OMAttribute;

 import org.apache.axis2.om.OMElement;

 import org.apache.axis2.om.OMNode;

-import org.apache.axis2.om.OMText;

 import org.apache.axis2.om.OMXMLParserWrapper;

 import org.apache.axis2.om.impl.llom.factory.OMXMLBuilderFactory;

 import org.apache.policy.model.AndCompositeAssertion;

@@ -183,31 +182,8 @@
     public PrimitiveAssertion getPrimitiveAssertion(OMElement value) {

 //      QName qname = new QName(value.getNamespace().getName(), 
value.getLocalName());

 //      return new PrimitiveAssertion(qname, value);

-        QName qname = value.getQName();

-        

-        PrimitiveAssertion result = new PrimitiveAssertion(qname);

-        result.setAttributes(getAttributes(value));

-        

-        Iterator childElements = value.getChildElements();

-        if (!(childElements.hasNext())) {;

-            return result;

-        }

-        

-        childElements = value.getChildElements();

-        while (childElements.hasNext()) {

-            OMElement childElement = (OMElement) childElements.next();

-            

-            if 
(childElement.getNamespace().getName().equals(WSPConstants.WS_POLICY_NAMESPACE_URI)

-                    && 
childElement.getLocalName().equals(WSPConstants.WS_POLICY)) {

-                Policy policy = 
WSPolicyParser.getInstance().getPolicy(childElement);

-                result.addTerm(policy);

-            } else {

-                PrimitiveAssertion pa = 
WSParserUtil.getPrimitiveAssertion(childElement);

-                result.addTerm(pa);                

-            }

-        }

-        

-        return result;       

+       return WSParserUtil.getPrimitiveAssertion(value);

+          

     }

         

     public boolean isCompositeAssertion(OMElement value) {


Added: 
webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyFactory.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyFactory.java?rev=345654&view=auto
==============================================================================
--- 
webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyFactory.java 
(added)
+++ 
webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyFactory.java 
Sat Nov 19 09:11:19 2005
@@ -0,0 +1,38 @@
+/*

+ * Copyright 2001-2004 The Apache Software Foundation.

+ * 

+ * Licensed 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.

+ */

+

+package org.apache.policy.util;

+

+/**

+ * @author Sanka Samaranayake ([EMAIL PROTECTED])

+ */

+public class PolicyFactory {

+       private static PolicyFactory singleton = null;

+       

+       private PolicyFactory() {

+       }

+       

+       public static PolicyFactory getInstance() {

+               if (singleton == null) {

+                       singleton = new PolicyFactory();

+               }

+               return singleton;

+       }

+       

+       public PolicyWriter getPolicyWriter() {

+               return new PolicyWriter();

+       }

+}


Added: 
webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyWriter.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyWriter.java?rev=345654&view=auto
==============================================================================
--- 
webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyWriter.java 
(added)
+++ 
webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyWriter.java 
Sat Nov 19 09:11:19 2005
@@ -0,0 +1,175 @@
+/*

+ * Copyright 2001-2004 The Apache Software Foundation.

+ * 

+ * Licensed 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.

+ */

+

+package org.apache.policy.util;

+

+import java.io.OutputStream;

+import java.util.Hashtable;

+import java.util.Iterator;

+import java.util.List;

+

+import javax.xml.namespace.QName;

+import javax.xml.stream.XMLOutputFactory;

+import javax.xml.stream.XMLStreamException;

+import javax.xml.stream.XMLStreamWriter;

+

+import org.apache.policy.model.AndCompositeAssertion;

+import org.apache.policy.model.Assertion;

+import org.apache.policy.model.Policy;

+import org.apache.policy.model.PolicyReference;

+import org.apache.policy.model.PrimitiveAssertion;

+import org.apache.policy.model.XorCompositeAssertion;

+import org.apache.policy.parser.WSPConstants;

+

+/**

+ * @author Sanka Samaranayake ([EMAIL PROTECTED])

+ */

+public class PolicyWriter {

+       

+       PolicyWriter() {

+       }

+       

+       public void writePolicy(Policy policy, OutputStream output) {

+               XMLStreamWriter writer = null;

+               try {

+                       writer =

+                               
XMLOutputFactory.newInstance().createXMLStreamWriter(output);

+                       writePolicy(policy, writer);

+                       

+                       writer.flush();

+                       

+               } catch (XMLStreamException ex) {

+                       throw new RuntimeException(ex);

+               }

+       }

+       

+       private void writePolicy(Policy policy, XMLStreamWriter writer) throws 
XMLStreamException {

+               writer.writeStartElement(WSPConstants.WS_POLICY_PREFIX, 
WSPConstants.WS_POLICY, 

+                               WSPConstants.WSU_NAMESPACE_URI);

+               

+               Iterator iterator = policy.getTerms().iterator();

+               while (iterator.hasNext()) {

+                       Assertion term = (Assertion) iterator.next();

+                       writeAssertion(term, writer);

+               }

+               

+               writer.writeEndElement();

+       }

+       

+       private void writeAssertion(Assertion assertion,

+                       XMLStreamWriter writer) throws XMLStreamException {

+               if (assertion instanceof PrimitiveAssertion) {

+                       writePrimitiveAssertion((PrimitiveAssertion) assertion, 
writer);

+                       

+               } else if (assertion instanceof AndCompositeAssertion) {

+                       writeAndCompositeAssertion((AndCompositeAssertion) 
assertion, writer);

+                       

+               } else if (assertion instanceof XorCompositeAssertion) {

+                       writeXorCompositeAssertion((XorCompositeAssertion) 
assertion, writer);

+                       

+               } else if (assertion instanceof PolicyReference) {

+                       writePolicyReference((PolicyReference) assertion, 
writer);

+                       

+               } else if (assertion instanceof Policy) {

+                       writePolicy((Policy) assertion, writer);

+               } else {

+                       throw new RuntimeException("unknown element type");

+               }

+       }

+       

+       private void writeAndCompositeAssertion(AndCompositeAssertion 
assertion, 

+                       XMLStreamWriter writer) throws XMLStreamException {

+               writer.writeStartElement(WSPConstants.WS_POLICY_PREFIX, 

+                               WSPConstants.AND_COMPOSITE_ASSERTION, 
WSPConstants.WS_POLICY_NAMESPACE_URI);

+               

+               List terms = assertion.getTerms();

+               writeTerms(terms, writer);

+               

+               writer.writeEndElement();               

+       }

+       

+       private void writeXorCompositeAssertion(XorCompositeAssertion assertion,

+                       XMLStreamWriter writer) throws XMLStreamException {

+               writer.writeStartElement(WSPConstants.WS_POLICY_PREFIX, 

+                               WSPConstants.XOR_COMPOSITE_ASSERTION, 
WSPConstants.WS_POLICY_NAMESPACE_URI);

+               

+               List terms = assertion.getTerms();

+               writeTerms(terms, writer);

+               

+               writer.writeEndElement();               

+       }

+       

+       private void writePrimitiveAssertion(PrimitiveAssertion assertion,

+                       XMLStreamWriter writer) throws XMLStreamException {     

+               QName qname = assertion.getName();

+               

+               String prefix = qname.getPrefix();

+               if (prefix != null) {

+                       writer.writeStartElement(qname.getPrefix(), 
qname.getLocalPart(),

+                                       qname.getNamespaceURI());

+                       

+               } else {

+                       writer.writeStartElement(qname.getLocalPart(), 

+                                       qname.getNamespaceURI());

+               }

+               

+               Hashtable attributes = assertion.getAttributes();

+               writeAttributes(attributes, writer);            

+               

+               String text = (String) assertion.getStrValue();

+               if (text != null) {

+                       writer.writeCharacters(text);

+               }

+               

+               List terms = assertion.getTerms();

+               writeTerms(terms, writer);

+               

+               writer.writeEndElement();

+       }

+       

+       private void writePolicyReference(PolicyReference assertion, 

+                       XMLStreamWriter writer) throws XMLStreamException {

+       }

+       

+       private void writeTerms(List terms, 

+                       XMLStreamWriter writer) throws XMLStreamException {

+               

+               Iterator iterator = terms.iterator();

+               while (iterator.hasNext()) {

+                       Assertion assertion = (Assertion) iterator.next();

+                       writeAssertion(assertion, writer);

+               }

+       }       

+       

+       private void writeAttributes(Hashtable attributes, XMLStreamWriter 
writer) 

+                       throws XMLStreamException {

+               

+               Iterator iterator = attributes.keySet().iterator();

+               while (iterator.hasNext()) {

+                       QName qname = (QName) iterator.next();

+                       String value = (String) attributes.get(qname);

+                       

+                       String prefix = qname.getPrefix();

+                       if (prefix != null) {

+                               writer.writeAttribute(prefix, 
qname.getNamespaceURI(),

+                                               qname.getLocalPart(), value);

+                       } else {

+                               writer.writeAttribute(qname.getNamespaceURI(), 

+                                               qname.getLocalPart(), value);

+                       }

+               }

+       }

+}



Reply via email to