Author: ajith
Date: Wed Mar  1 21:12:46 2006
New Revision: 382301

URL: http://svn.apache.org/viewcvs?rev=382301&view=rev
Log:
Made a slight imporvement to the DOM policy reader

Modified:
    
webservices/commons/modules/policy/src/org/apache/ws/policy/util/DOMPolicyReader.java

Modified: 
webservices/commons/modules/policy/src/org/apache/ws/policy/util/DOMPolicyReader.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/modules/policy/src/org/apache/ws/policy/util/DOMPolicyReader.java?rev=382301&r1=382300&r2=382301&view=diff
==============================================================================
--- 
webservices/commons/modules/policy/src/org/apache/ws/policy/util/DOMPolicyReader.java
 (original)
+++ 
webservices/commons/modules/policy/src/org/apache/ws/policy/util/DOMPolicyReader.java
 Wed Mar  1 21:12:46 2006
@@ -49,167 +49,171 @@
  * @author Sanka Samaranayake ([EMAIL PROTECTED])
  */
 public class DOMPolicyReader implements PolicyReader {
-       DOMPolicyReader() {
-       }
+    public static final String XMLNS_NS_URI = "http://www.w3.org/2000/xmlns/";;
 
-       public Policy readPolicy(InputStream in) {
+    DOMPolicyReader() {
+    }
+
+    public Policy readPolicy(InputStream in) {
         try {
-                       DocumentBuilderFactory dbf = 
DocumentBuilderFactory.newInstance();
+            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);
+        }
 
-                       dbf.setNamespaceAware(true);
-                       dbf.setValidating(false);
+        if (localName.equals(PolicyConstants.WS_POLICY)) {
+            return readPolicy(element);
 
-                       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 ..");
-               }               
-       }
-       
-       public Policy readPolicy(Element element) {
-               Policy policy = new Policy();
+        } 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 ..");
+        }
+    }
+
+    public 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");
+        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;
-       }
-       
-       public 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());
+
+        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;
+    }
+
+    public 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"));
         result.setOptional(new Boolean(isOptional).booleanValue());
-                        
+
         //CHECK ME
-               NodeList list = element.getChildNodes();
-               int length = list.getLength();
+        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;
-       }
+        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);
+            if (!XMLNS_NS_URI.equals(attribute.getNamespaceURI())){
+                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;
+    }
 }


Reply via email to