You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ws.apache.org by sa...@apache.org on 2005/12/09 14:53:27 UTC

svn commit: r355489 - /webservices/commons/trunk/policy/src/org/apache/ws/policy/util/

Author: sanka
Date: Fri Dec  9 05:53:12 2005
New Revision: 355489

URL: http://svn.apache.org/viewcvs?rev=355489&view=rev
Log:
Adding PolicyReader and PolicyWriter interfaces.  Refactoring PolicyFactory

Added:
    webservices/commons/trunk/policy/src/org/apache/ws/policy/util/DOMPolicyReader.java
    webservices/commons/trunk/policy/src/org/apache/ws/policy/util/OMPolicyReader.java
    webservices/commons/trunk/policy/src/org/apache/ws/policy/util/SchemaRegistry.java
    webservices/commons/trunk/policy/src/org/apache/ws/policy/util/StAXPolicyWriter.java
Removed:
    webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyReaderDOM.java
Modified:
    webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyAttachmentUtil.java
    webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyFactory.java
    webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyReader.java
    webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyWriter.java

Added: webservices/commons/trunk/policy/src/org/apache/ws/policy/util/DOMPolicyReader.java
URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/ws/policy/util/DOMPolicyReader.java?rev=355489&view=auto
==============================================================================
--- webservices/commons/trunk/policy/src/org/apache/ws/policy/util/DOMPolicyReader.java (added)
+++ webservices/commons/trunk/policy/src/org/apache/ws/policy/util/DOMPolicyReader.java Fri Dec  9 05:53:12 2005
@@ -0,0 +1,217 @@
+/*
+ * 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.ws.policy.util;
+
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Hashtable;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import javax.xml.namespace.QName;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.NamedNodeMap;
+import org.xml.sax.SAXException;
+
+import org.apache.ws.policy.model.AndCompositeAssertion;
+import org.apache.ws.policy.model.Assertion;
+import org.apache.ws.policy.model.Policy;
+import org.apache.ws.policy.model.PolicyReference;
+import org.apache.ws.policy.model.PrimitiveAssertion;
+import org.apache.ws.policy.model.PolicyConstants;
+import org.apache.ws.policy.model.XorCompositeAssertion;
+
+/**
+ * @author Werner Dittmann (werner@apache.org)
+ * @author Sanka Samaranayake (sanka@apache.org)
+ */
+public class DOMPolicyReader implements PolicyReader {
+	DOMPolicyReader() {
+	}
+
+	public Policy readPolicy(InputStream in) {
+        try {
+			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+
+			dbf.setNamespaceAware(true);
+			dbf.setValidating(false);
+
+			DocumentBuilder db = dbf.newDocumentBuilder();
+			Document doc = db.parse(in);
+			Element element = doc.getDocumentElement();
+			return readPolicy(element);
+		} catch (ParserConfigurationException e) {
+			e.printStackTrace();
+			throw new RuntimeException("error : " + e.getMessage());
+		} catch (SAXException e) {
+			e.printStackTrace();
+			throw new RuntimeException("error : " + e.getMessage());
+		} catch (IOException e) {
+			e.printStackTrace();
+			throw new RuntimeException("error : " + e.getMessage());
+		}
+	}
+	
+	private Assertion readAssertion(Element element) {
+		String namespace = element.getNamespaceURI();
+		String localName = element.getLocalName();
+		
+		if (!(namespace.equals(PolicyConstants.WS_POLICY_NAMESPACE_URI))) {
+			return readPrimitiveAssertion(element);
+		}
+		
+		if (localName.equals(PolicyConstants.WS_POLICY)) {
+			return readPolicy(element);
+			
+		} else if (localName.equals(PolicyConstants.AND_COMPOSITE_ASSERTION)) {
+			return readAndComposite(element);
+			
+		} else if (localName.equals(PolicyConstants.XOR_COMPOSITE_ASSERTION)) {
+			return readXorComposite(element);
+			
+		} else if (localName.equals(PolicyConstants.WS_POLICY_REFERENCE)) {
+			return readPolicyReference(element);
+			
+		} else {
+			throw new RuntimeException("unknown element ..");
+		}		
+	}
+	
+	private Policy readPolicy(Element element) {
+		Policy policy = new Policy();
+        Attr attri;
+        attri = element.getAttributeNodeNS(PolicyConstants.WSU_NAMESPACE_URI, "Id");
+        if (attri != null) {
+            policy.setId(attri.getValue());
+        }
+
+    	attri = element.getAttributeNodeNS(PolicyConstants.XML_NAMESPACE_URI, "base");
+        if (attri != null) {
+            policy.setBase(attri.getValue());
+        }
+		
+		policy.addTerms(readTerms(element));
+		return policy;
+	}
+	
+	private AndCompositeAssertion readAndComposite(Element element) {
+		AndCompositeAssertion andCompositeAssertion = new AndCompositeAssertion();
+		andCompositeAssertion.addTerms(readTerms(element));
+		return andCompositeAssertion;
+	}	
+	
+	private XorCompositeAssertion readXorComposite(Element element) {
+		XorCompositeAssertion xorCompositeAssertion = new XorCompositeAssertion();
+		xorCompositeAssertion.addTerms(readTerms(element));
+		return xorCompositeAssertion;
+	}
+	
+	private PolicyReference readPolicyReference(Element element) {
+		Attr attribute = element.getAttributeNode("URI");
+		return new PolicyReference(attribute.getValue());
+	}
+	
+	private PrimitiveAssertion readPrimitiveAssertion(Element element) {
+		QName qname = new QName(element.getNamespaceURI(), element.getLocalName(), element.getPrefix());
+        PrimitiveAssertion result = new PrimitiveAssertion(qname);
+        
+        result.setAttributes(getAttributes(element));
+        String isOptional = result.getAttribute(new QName(
+                PolicyConstants.WS_POLICY_NAMESPACE_URI, "Optional"));
+        if (isOptional != null && Boolean.getBoolean(isOptional)) {
+            result.setOptional(true);
+        }        
+                
+        //CHECK ME
+		NodeList list = element.getChildNodes();
+		int length = list.getLength();
+
+		for (int i = 0; i < length; i++) {
+			Node node = list.item(i);
+			short nodeType = node.getNodeType();
+			
+			if (nodeType == Node.ELEMENT_NODE) {
+				Element childElement = (Element) node;
+				if (childElement.getNamespaceURI().equals(
+						PolicyConstants.WS_POLICY_NAMESPACE_URI)
+						&& childElement.getLocalName().equals(
+								PolicyConstants.WS_POLICY)) {
+					Policy policy = readPolicy(childElement);
+					result.addTerm(policy);
+
+				} else {
+					PrimitiveAssertion pa = readPrimitiveAssertion(childElement);
+					result.addTerm(pa);
+				}
+			}
+			else if (nodeType == Node.TEXT_NODE) {
+				String strValue = node.getNodeValue();
+		        
+		        if (strValue != null && strValue.length() != 0) {
+		        	result.setStrValue(strValue);            
+		        }
+			}
+        }        
+        return result;       
+    }
+	
+	private ArrayList readTerms(Element element) {
+		ArrayList terms = new ArrayList();
+		NodeList list = element.getChildNodes();
+		int length = list.getLength();
+
+		for (int i = 0; i < length; i++) {
+			Object obj = list.item(i);
+			
+			if (obj instanceof Element) {
+				Element e = (Element) obj;
+				terms.add(readAssertion(e));				
+			}
+		}
+		return terms;		
+	}
+	
+	private Hashtable getAttributes(Element element) {
+		Hashtable attributes = new Hashtable();
+		NamedNodeMap map = element.getAttributes();
+
+		int length = map.getLength();
+
+		for (int i = 0; i < length; i++) {
+			Attr attribute = (Attr) map.item(i);
+			String prefix = attribute.getPrefix();
+			QName qn = null;
+			if (prefix != null) {
+				qn = new QName(attribute.getNamespaceURI(), attribute.getLocalName(), prefix);
+			}
+			else {
+				qn = new QName(attribute.getNamespaceURI(), attribute.getLocalName());				
+			}
+			attributes.put(qn, attribute.getValue());
+		}
+		return attributes;
+	}
+}
\ No newline at end of file

Added: webservices/commons/trunk/policy/src/org/apache/ws/policy/util/OMPolicyReader.java
URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/ws/policy/util/OMPolicyReader.java?rev=355489&view=auto
==============================================================================
--- webservices/commons/trunk/policy/src/org/apache/ws/policy/util/OMPolicyReader.java (added)
+++ webservices/commons/trunk/policy/src/org/apache/ws/policy/util/OMPolicyReader.java Fri Dec  9 05:53:12 2005
@@ -0,0 +1,193 @@
+/*
+ * 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.ws.policy.util;
+
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Hashtable;
+import java.util.Iterator;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.axis2.om.OMAbstractFactory;
+import org.apache.axis2.om.OMAttribute;
+import org.apache.axis2.om.OMElement;
+import org.apache.axis2.om.OMXMLParserWrapper;
+import org.apache.axis2.om.impl.llom.factory.OMXMLBuilderFactory;
+import org.apache.ws.policy.model.AndCompositeAssertion;
+import org.apache.ws.policy.model.Assertion;
+import org.apache.ws.policy.model.Policy;
+import org.apache.ws.policy.model.PolicyReference;
+import org.apache.ws.policy.model.PrimitiveAssertion;
+import org.apache.ws.policy.model.PolicyConstants;
+import org.apache.ws.policy.model.XorCompositeAssertion;
+
+/**
+ * @author Sanka Samaranayake (sanka@apache.org)
+ */
+public class OMPolicyReader implements PolicyReader {
+    OMPolicyReader() {
+    }
+
+    public Policy readPolicy(InputStream in) {
+        try {
+            XMLStreamReader reader = XMLInputFactory.newInstance()
+                    .createXMLStreamReader(in);
+            OMXMLParserWrapper builder = OMXMLBuilderFactory
+                    .createStAXOMBuilder(OMAbstractFactory.getOMFactory(),
+                            reader);
+
+            OMElement element = builder.getDocumentElement();
+            return readPolicy(element);
+
+        } catch (XMLStreamException ex) {
+            throw new RuntimeException("error : " + ex.getMessage());
+        }
+    }
+
+    private Assertion readAssertion(OMElement element) {
+        String namespace = element.getNamespace().getName();
+        String localName = element.getLocalName();
+
+        if (!(namespace.equals(PolicyConstants.WS_POLICY_NAMESPACE_URI))) {
+            return readPrimitiveAssertion(element);
+        }
+
+        if (localName.equals(PolicyConstants.WS_POLICY)) {
+            return readPolicy(element);
+
+        } else if (localName.equals(PolicyConstants.AND_COMPOSITE_ASSERTION)) {
+            return readAndComposite(element);
+
+        } else if (localName.equals(PolicyConstants.XOR_COMPOSITE_ASSERTION)) {
+            return readXorComposite(element);
+
+        } else if (localName.equals(PolicyConstants.WS_POLICY_REFERENCE)) {
+            return readPolicyReference(element);
+
+        } else {
+            throw new RuntimeException("unknown element ..");
+        }
+    }
+
+    private Policy readPolicy(OMElement element) {
+        Policy policy = new Policy();
+
+        OMAttribute attri;
+        attri = element.getAttribute(new QName(
+                PolicyConstants.WSU_NAMESPACE_URI, "Id"));
+        if (attri != null) {
+            policy.setId(attri.getAttributeValue());
+        }
+        System.out.println("Searching base attribute");
+        attri = element.getAttribute(new QName(PolicyConstants.XML_NAMESPACE_URI, "base"));
+        if (attri != null) {
+        	System.out.println("found base attribute: " + attri.getAttributeValue());        	
+            policy.setBase(attri.getAttributeValue());
+        }
+
+        policy.addTerms(readTerms(element));
+        return policy;
+    }
+
+    private AndCompositeAssertion readAndComposite(OMElement element) {
+        AndCompositeAssertion andCompositeAssertion = new AndCompositeAssertion();
+        andCompositeAssertion.addTerms(readTerms(element));
+        return andCompositeAssertion;
+    }
+
+    private XorCompositeAssertion readXorComposite(OMElement element) {
+        XorCompositeAssertion xorCompositeAssertion = new XorCompositeAssertion();
+        xorCompositeAssertion.addTerms(readTerms(element));
+        return xorCompositeAssertion;
+    }
+
+    private PolicyReference readPolicyReference(OMElement element) {
+        OMAttribute attribute = element.getAttribute(new QName("URI"));
+        return new PolicyReference(attribute.getAttributeValue());
+    }
+
+    private PrimitiveAssertion readPrimitiveAssertion(OMElement element) {
+        QName qname = element.getQName();
+        PrimitiveAssertion result = new PrimitiveAssertion(qname);
+
+        result.setAttributes(getAttributes(element));
+
+        String isOptional = result.getAttribute(new QName(
+                PolicyConstants.WS_POLICY_NAMESPACE_URI, "Optional"));
+        if (isOptional != null && Boolean.getBoolean(isOptional)) {
+            result.setOptional(true);
+        }
+
+        // setting the text value ..
+        String strValue = element.getText();
+
+        if (strValue != null && strValue.length() != 0) {
+            result.setStrValue(strValue);
+        }
+
+        //CHECK ME
+        Iterator childElements = element.getChildElements();
+
+        while (childElements.hasNext()) {
+            OMElement childElement = (OMElement) childElements.next();
+
+            if (childElement.getNamespace().getName().equals(
+                    PolicyConstants.WS_POLICY_NAMESPACE_URI)
+                    && childElement.getLocalName().equals(
+                            PolicyConstants.WS_POLICY)) {
+                Policy policy = readPolicy(childElement);
+                result.addTerm(policy);
+
+            } else {
+                PrimitiveAssertion pa = readPrimitiveAssertion(childElement);
+                result.addTerm(pa);
+            }
+        }
+        return result;
+    }
+
+    private ArrayList readTerms(OMElement element) {
+        ArrayList terms = new ArrayList();
+        Iterator childElements = element.getChildren();
+
+        while (childElements.hasNext()) {
+            Object obj = childElements.next();
+
+            if (obj instanceof OMElement) {
+                OMElement e = (OMElement) obj;
+                terms.add(readAssertion(e));
+            }
+        }
+        return terms;
+    }
+
+    private Hashtable getAttributes(OMElement element) {
+        Hashtable attributes = new Hashtable();
+        Iterator iterator = element.getAllAttributes();
+
+        while (iterator.hasNext()) {
+            OMAttribute attribute = (OMAttribute) iterator.next();
+            attributes.put(attribute.getQName(), attribute.getAttributeValue());
+        }
+
+        return attributes;
+    }
+}
\ No newline at end of file

Modified: webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyAttachmentUtil.java
URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyAttachmentUtil.java?rev=355489&r1=355488&r2=355489&view=diff
==============================================================================
--- webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyAttachmentUtil.java (original)
+++ webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyAttachmentUtil.java Fri Dec  9 05:53:12 2005
@@ -84,7 +84,7 @@
             WSDLVersionWrapper build = WOMBuilderFactory.getBuilder(
                     WSDLConstants.WSDL_1_1).build(wsdlInputStream);
             wsdlDescription = build.getDescription();
-            
+
             populatePolicyRegistry();
             populateSchemaRegistry();
 
@@ -96,7 +96,7 @@
     public void setWSDLDescription(WSDLDescription wsdlDescription) {
         this.wsdlDescription = wsdlDescription;
         reg = new PolicyRegistry();
-        
+
         populatePolicyRegistry();
         populateSchemaRegistry();
     }
@@ -356,11 +356,11 @@
             QName fault) {
         throw new UnsupportedOperationException();
     }
-    
+
     public PolicyRegistry getPolicyRegistry() {
         return reg;
     }
-    
+
     public Element getSchemaElement(String uri) {
         return schemaRegistry.lookup(uri);
     }
@@ -484,7 +484,8 @@
 
     private Policy getPolicyFromElement(Element element) {
         InputStream policyInputStream = createInputStream(element);
-        PolicyReader reader = PolicyFactory.getInstance().getPolicyReader();
+        PolicyReader reader = PolicyFactory
+                .getPolicyReader(PolicyFactory.OM_POLICY_READER);
         return reader.readPolicy(policyInputStream);
     }
 
@@ -550,8 +551,8 @@
                 try {
                     URI policyURI = new URI(uriString);
                     URL policyURL = policyURI.toURL();
-                    PolicyReader reader = PolicyFactory.getInstance()
-                            .getPolicyReader();
+                    PolicyReader reader = PolicyFactory
+                            .getPolicyReader(PolicyFactory.OM_POLICY_READER);
                     Policy newPolicy = reader
                             .readPolicy(policyURL.openStream());
                     reg.register(uriString, newPolicy);
@@ -689,7 +690,8 @@
 
     private void registerPolicyElement(Element element) {
         InputStream elementInputStream = createInputStream(element);
-        PolicyReader reader = PolicyFactory.getInstance().getPolicyReader();
+        PolicyReader reader = PolicyFactory
+                .getPolicyReader(PolicyFactory.OM_POLICY_READER);
         Policy policy = reader.readPolicy(elementInputStream);
         reg.register(policy.getPolicyURI(), policy);
     }

Modified: webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyFactory.java
URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyFactory.java?rev=355489&r1=355488&r2=355489&view=diff
==============================================================================
--- webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyFactory.java (original)
+++ webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyFactory.java Fri Dec  9 05:53:12 2005
@@ -20,28 +20,25 @@
  * @author Sanka Samaranayake (sanka@apache.org)
  */
 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();
-	}
-	
-	public PolicyReader getPolicyReader() {
-		return new PolicyReader();
-	}
-	
-	public PolicyReaderDOM getPolicyReaderDOM() {
-		return new PolicyReaderDOM();
-	}
-
+    public static final int OM_POLICY_READER = 1;
+    public static final int StAX_POLICY_WRITER = 2;
+    
+    public static final int DOM_POLICY_READER = 3;
+    
+    
+    public static PolicyReader getPolicyReader(int type) {
+        switch (type) {
+        case DOM_POLICY_READER:
+            return new DOMPolicyReader();
+        case OM_POLICY_READER:
+            return new OMPolicyReader();
+        default: 
+            throw new IllegalArgumentException("Unknow PolicyReader type ..");
+        }
+    }
+    
+    public static PolicyWriter getPolicyWriter() {
+        return new StAXPolicyWriter();
+    }
+    
 }

Modified: webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyReader.java
URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyReader.java?rev=355489&r1=355488&r2=355489&view=diff
==============================================================================
--- webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyReader.java (original)
+++ webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyReader.java Fri Dec  9 05:53:12 2005
@@ -1,193 +1,28 @@
-/*
- * 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.ws.policy.util;
-
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.Hashtable;
-import java.util.Iterator;
-
-import javax.xml.namespace.QName;
-import javax.xml.stream.XMLInputFactory;
-import javax.xml.stream.XMLStreamException;
-import javax.xml.stream.XMLStreamReader;
-
-import org.apache.axis2.om.OMAbstractFactory;
-import org.apache.axis2.om.OMAttribute;
-import org.apache.axis2.om.OMElement;
-import org.apache.axis2.om.OMXMLParserWrapper;
-import org.apache.axis2.om.impl.llom.factory.OMXMLBuilderFactory;
-import org.apache.ws.policy.model.AndCompositeAssertion;
-import org.apache.ws.policy.model.Assertion;
-import org.apache.ws.policy.model.Policy;
-import org.apache.ws.policy.model.PolicyReference;
-import org.apache.ws.policy.model.PrimitiveAssertion;
-import org.apache.ws.policy.model.PolicyConstants;
-import org.apache.ws.policy.model.XorCompositeAssertion;
-
-/**
- * @author Sanka Samaranayake (sanka@apache.org)
- */
-public class PolicyReader {
-    PolicyReader() {
-    }
-
-    public Policy readPolicy(InputStream in) {
-        try {
-            XMLStreamReader reader = XMLInputFactory.newInstance()
-                    .createXMLStreamReader(in);
-            OMXMLParserWrapper builder = OMXMLBuilderFactory
-                    .createStAXOMBuilder(OMAbstractFactory.getOMFactory(),
-                            reader);
-
-            OMElement element = builder.getDocumentElement();
-            return readPolicy(element);
-
-        } catch (XMLStreamException ex) {
-            throw new RuntimeException("error : " + ex.getMessage());
-        }
-    }
-
-    private Assertion readAssertion(OMElement element) {
-        String namespace = element.getNamespace().getName();
-        String localName = element.getLocalName();
-
-        if (!(namespace.equals(PolicyConstants.WS_POLICY_NAMESPACE_URI))) {
-            return readPrimitiveAssertion(element);
-        }
-
-        if (localName.equals(PolicyConstants.WS_POLICY)) {
-            return readPolicy(element);
-
-        } else if (localName.equals(PolicyConstants.AND_COMPOSITE_ASSERTION)) {
-            return readAndComposite(element);
-
-        } else if (localName.equals(PolicyConstants.XOR_COMPOSITE_ASSERTION)) {
-            return readXorComposite(element);
-
-        } else if (localName.equals(PolicyConstants.WS_POLICY_REFERENCE)) {
-            return readPolicyReference(element);
-
-        } else {
-            throw new RuntimeException("unknown element ..");
-        }
-    }
-
-    private Policy readPolicy(OMElement element) {
-        Policy policy = new Policy();
-
-        OMAttribute attri;
-        attri = element.getAttribute(new QName(
-                PolicyConstants.WSU_NAMESPACE_URI, "Id"));
-        if (attri != null) {
-            policy.setId(attri.getAttributeValue());
-        }
-        System.out.println("Searching base attribute");
-        attri = element.getAttribute(new QName(PolicyConstants.XML_NAMESPACE_URI, "base"));
-        if (attri != null) {
-        	System.out.println("found base attribute: " + attri.getAttributeValue());        	
-            policy.setBase(attri.getAttributeValue());
-        }
-
-        policy.addTerms(readTerms(element));
-        return policy;
-    }
-
-    private AndCompositeAssertion readAndComposite(OMElement element) {
-        AndCompositeAssertion andCompositeAssertion = new AndCompositeAssertion();
-        andCompositeAssertion.addTerms(readTerms(element));
-        return andCompositeAssertion;
-    }
-
-    private XorCompositeAssertion readXorComposite(OMElement element) {
-        XorCompositeAssertion xorCompositeAssertion = new XorCompositeAssertion();
-        xorCompositeAssertion.addTerms(readTerms(element));
-        return xorCompositeAssertion;
-    }
-
-    private PolicyReference readPolicyReference(OMElement element) {
-        OMAttribute attribute = element.getAttribute(new QName("URI"));
-        return new PolicyReference(attribute.getAttributeValue());
-    }
-
-    private PrimitiveAssertion readPrimitiveAssertion(OMElement element) {
-        QName qname = element.getQName();
-        PrimitiveAssertion result = new PrimitiveAssertion(qname);
-
-        result.setAttributes(getAttributes(element));
-
-        String isOptional = result.getAttribute(new QName(
-                PolicyConstants.WS_POLICY_NAMESPACE_URI, "Optional"));
-        if (isOptional != null && Boolean.getBoolean(isOptional)) {
-            result.setOptional(true);
-        }
-
-        // setting the text value ..
-        String strValue = element.getText();
-
-        if (strValue != null && strValue.length() != 0) {
-            result.setStrValue(strValue);
-        }
-
-        //CHECK ME
-        Iterator childElements = element.getChildElements();
-
-        while (childElements.hasNext()) {
-            OMElement childElement = (OMElement) childElements.next();
-
-            if (childElement.getNamespace().getName().equals(
-                    PolicyConstants.WS_POLICY_NAMESPACE_URI)
-                    && childElement.getLocalName().equals(
-                            PolicyConstants.WS_POLICY)) {
-                Policy policy = readPolicy(childElement);
-                result.addTerm(policy);
-
-            } else {
-                PrimitiveAssertion pa = readPrimitiveAssertion(childElement);
-                result.addTerm(pa);
-            }
-        }
-        return result;
-    }
-
-    private ArrayList readTerms(OMElement element) {
-        ArrayList terms = new ArrayList();
-        Iterator childElements = element.getChildren();
-
-        while (childElements.hasNext()) {
-            Object obj = childElements.next();
-
-            if (obj instanceof OMElement) {
-                OMElement e = (OMElement) obj;
-                terms.add(readAssertion(e));
-            }
-        }
-        return terms;
-    }
-
-    private Hashtable getAttributes(OMElement element) {
-        Hashtable attributes = new Hashtable();
-        Iterator iterator = element.getAllAttributes();
-
-        while (iterator.hasNext()) {
-            OMAttribute attribute = (OMAttribute) iterator.next();
-            attributes.put(attribute.getQName(), attribute.getAttributeValue());
-        }
-
-        return attributes;
-    }
-}
\ No newline at end of file
+/*
+ * Copyright 2004,2005 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.ws.policy.util;
+
+import java.io.InputStream;
+
+import org.apache.ws.policy.model.Policy;
+
+/**
+ * @author Sanka Samaranayake (sanka@apache.org)
+ */
+public interface PolicyReader {
+    public Policy readPolicy(InputStream inputStream) throws RuntimeException;
+}

Modified: webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyWriter.java
URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyWriter.java?rev=355489&r1=355488&r2=355489&view=diff
==============================================================================
--- webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyWriter.java (original)
+++ webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyWriter.java Fri Dec  9 05:53:12 2005
@@ -1,194 +1,28 @@
-/*
- * 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.ws.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.ws.policy.model.AndCompositeAssertion;
-import org.apache.ws.policy.model.Assertion;
-import org.apache.ws.policy.model.Policy;
-import org.apache.ws.policy.model.PolicyConstants;
-import org.apache.ws.policy.model.PolicyReference;
-import org.apache.ws.policy.model.PrimitiveAssertion;
-import org.apache.ws.policy.model.XorCompositeAssertion;
-
-/**
- * @author Sanka Samaranayake (sanka@apache.org)
- */
-public class PolicyWriter {
-
-    private int num = 1;
-
-    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(PolicyConstants.WS_POLICY_PREFIX,
-                PolicyConstants.WS_POLICY);
-        writer.writeNamespace(PolicyConstants.WS_POLICY_PREFIX,
-                PolicyConstants.WS_POLICY_NAMESPACE_URI);
-
-        if (policy.getId() != null) {
-            writer.writeAttribute("wsu", PolicyConstants.WSU_NAMESPACE_URI,
-                    "Id", policy.getId());
-        }
-
-        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(PolicyConstants.WS_POLICY_PREFIX,
-                PolicyConstants.AND_COMPOSITE_ASSERTION,
-                PolicyConstants.WS_POLICY_NAMESPACE_URI);
-
-        List terms = assertion.getTerms();
-        writeTerms(terms, writer);
-
-        writer.writeEndElement();
-    }
-
-    private void writeXorCompositeAssertion(XorCompositeAssertion assertion,
-            XMLStreamWriter writer) throws XMLStreamException {
-        writer.writeStartElement(PolicyConstants.WS_POLICY_PREFIX,
-                PolicyConstants.XOR_COMPOSITE_ASSERTION,
-                PolicyConstants.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());
-            writer.writeNamespace(qname.getPrefix(), qname.getNamespaceURI());
-
-        } else {
-            writer.writeStartElement(qname.getLocalPart(), qname
-                    .getNamespaceURI());
-            writer.writeNamespace(generateNamespace(), 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);
-            }
-        }
-    }
-
-    private String generateNamespace() {
-        return "ns" + num++;
-    }
-}
+/*
+ * Copyright 2004,2005 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.ws.policy.util;
+
+import java.io.OutputStream;
+
+import org.apache.ws.policy.model.Policy;
+
+/**
+ * @author Sanka Samaranayake (sanka@apache.org)
+ */
+public interface PolicyWriter {
+    public void writePolicy(Policy policy, OutputStream outputStream);
+}

Added: webservices/commons/trunk/policy/src/org/apache/ws/policy/util/SchemaRegistry.java
URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/ws/policy/util/SchemaRegistry.java?rev=355489&view=auto
==============================================================================
--- webservices/commons/trunk/policy/src/org/apache/ws/policy/util/SchemaRegistry.java (added)
+++ webservices/commons/trunk/policy/src/org/apache/ws/policy/util/SchemaRegistry.java Fri Dec  9 05:53:12 2005
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2004,2005 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.ws.policy.util;
+
+import java.util.HashMap;
+
+import org.w3c.dom.Element;
+
+/**
+ * @author Sanka Samaranayake (sanka@apache.org)
+ */
+public class SchemaRegistry {
+    private HashMap reg = new HashMap();
+    
+    Element lookup(String uri) {
+        return (Element) reg.get(uri);
+    }
+    
+    public void register(String uri, Element schemaElement) {
+        reg.put(uri, schemaElement);
+    }
+    
+    public void unregister(String uri) {
+        reg.remove(uri);        
+    }
+}

Added: webservices/commons/trunk/policy/src/org/apache/ws/policy/util/StAXPolicyWriter.java
URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/ws/policy/util/StAXPolicyWriter.java?rev=355489&view=auto
==============================================================================
--- webservices/commons/trunk/policy/src/org/apache/ws/policy/util/StAXPolicyWriter.java (added)
+++ webservices/commons/trunk/policy/src/org/apache/ws/policy/util/StAXPolicyWriter.java Fri Dec  9 05:53:12 2005
@@ -0,0 +1,194 @@
+/*
+ * 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.ws.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.ws.policy.model.AndCompositeAssertion;
+import org.apache.ws.policy.model.Assertion;
+import org.apache.ws.policy.model.Policy;
+import org.apache.ws.policy.model.PolicyConstants;
+import org.apache.ws.policy.model.PolicyReference;
+import org.apache.ws.policy.model.PrimitiveAssertion;
+import org.apache.ws.policy.model.XorCompositeAssertion;
+
+/**
+ * @author Sanka Samaranayake (sanka@apache.org)
+ */
+public class StAXPolicyWriter implements PolicyWriter {
+
+    private int num = 1;
+
+    StAXPolicyWriter() {
+    }
+
+    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(PolicyConstants.WS_POLICY_PREFIX,
+                PolicyConstants.WS_POLICY);
+        writer.writeNamespace(PolicyConstants.WS_POLICY_PREFIX,
+                PolicyConstants.WS_POLICY_NAMESPACE_URI);
+
+        if (policy.getId() != null) {
+            writer.writeAttribute("wsu", PolicyConstants.WSU_NAMESPACE_URI,
+                    "Id", policy.getId());
+        }
+
+        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(PolicyConstants.WS_POLICY_PREFIX,
+                PolicyConstants.AND_COMPOSITE_ASSERTION,
+                PolicyConstants.WS_POLICY_NAMESPACE_URI);
+
+        List terms = assertion.getTerms();
+        writeTerms(terms, writer);
+
+        writer.writeEndElement();
+    }
+
+    private void writeXorCompositeAssertion(XorCompositeAssertion assertion,
+            XMLStreamWriter writer) throws XMLStreamException {
+        writer.writeStartElement(PolicyConstants.WS_POLICY_PREFIX,
+                PolicyConstants.XOR_COMPOSITE_ASSERTION,
+                PolicyConstants.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());
+            writer.writeNamespace(qname.getPrefix(), qname.getNamespaceURI());
+
+        } else {
+            writer.writeStartElement(qname.getLocalPart(), qname
+                    .getNamespaceURI());
+            writer.writeNamespace(generateNamespace(), 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);
+            }
+        }
+    }
+
+    private String generateNamespace() {
+        return "ns" + num++;
+    }
+}