Author: sanka
Date: Tue May  2 03:18:16 2006
New Revision: 398890

URL: http://svn.apache.org/viewcvs?rev=398890&view=rev
Log:
Minor fix in DOMPolicyReader ,thanks for pointing this out Mike

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

Modified: 
webservices/commons/trunk/modules/policy/src/org/apache/ws/policy/util/DOMPolicyReader.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/policy/src/org/apache/ws/policy/util/DOMPolicyReader.java?rev=398890&r1=398889&r2=398890&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/policy/src/org/apache/ws/policy/util/DOMPolicyReader.java
 (original)
+++ 
webservices/commons/trunk/modules/policy/src/org/apache/ws/policy/util/DOMPolicyReader.java
 Tue May  2 03:18:16 2006
@@ -48,175 +48,186 @@
  * create a policy object. It uses DOM as it underlying mechanism to XML.
  */
 public class DOMPolicyReader implements PolicyReader {
-       public static final String XMLNS_NS_URI = 
"http://www.w3.org/2000/xmlns/";;
+    public static final String XMLNS_NS_URI = "http://www.w3.org/2000/xmlns/";;
 
-       DOMPolicyReader() {
-       }
+    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 ..");
-               }
-       }
-
-       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");
-               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());
-               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();
-
-               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;
-       }
+    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 ..");
+        }
+    }
+
+    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");
+        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");
+    
+        if (attribute == null) {
+            attribute = element.getAttributeNodeNS(element.getNamespaceURI(),
+                    element.getLocalName());
+        }
+
+        if (attribute == null) {
+            throw new IllegalArgumentException(
+                    "PolicyReference element has no URI attribute");
+        }
+
+        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();
+
+        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