ws-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From veit...@apache.org
Subject svn commit: r1379724 - in /webservices/commons/trunk/modules/axiom/modules: axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/ axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/attr/ axiom-dom-testsuite/src/main/java/org/apache/axiom/ts...
Date Sat, 01 Sep 2012 08:58:04 GMT
Author: veithen
Date: Sat Sep  1 08:58:04 2012
New Revision: 1379724

URL: http://svn.apache.org/viewvc?rev=1379724&view=rev
Log:
AXIOM-423: Applied Suresh Attanayake's patch to implement the lookupNamespaceURI method.

Added:
    webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/attr/TestLookpNamespaceURI.java
  (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/element/TestLookupNamespaceURI.java
  (with props)
Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/DOMTestSuiteBuilder.java
    webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/NodeImpl.java

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/DOMTestSuiteBuilder.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/DOMTestSuiteBuilder.java?rev=1379724&r1=1379723&r2=1379724&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/DOMTestSuiteBuilder.java
(original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/DOMTestSuiteBuilder.java
Sat Sep  1 08:58:04 2012
@@ -40,6 +40,7 @@ public class DOMTestSuiteBuilder extends
         addTest(new org.apache.axiom.ts.dom.attr.TestGetValueWithMultipleChildren(dbf));
         addTest(new org.apache.axiom.ts.dom.attr.TestSetPrefixNotNullWithNamespace(dbf));
         addTest(new org.apache.axiom.ts.dom.attr.TestSetPrefixNotNullWithoutNamespace(dbf));
+        addTest(new org.apache.axiom.ts.dom.attr.TestLookpNamespaceURI(dbf));
         addTest(new org.apache.axiom.ts.dom.builder.TestParseURI(dbf));
         addTest(new org.apache.axiom.ts.dom.builder.TestWhitespaceAroundDocumentElement(dbf));
         addTest(new org.apache.axiom.ts.dom.document.TestAdoptNode(dbf));
@@ -121,6 +122,7 @@ public class DOMTestSuiteBuilder extends
         addTest(new org.apache.axiom.ts.dom.element.TestSetPrefixNotNullWithoutNamespace(dbf));
         addTest(new org.apache.axiom.ts.dom.element.TestSetPrefixNull(dbf));
         addTest(new org.apache.axiom.ts.dom.element.TestSetTextContent(dbf));
+        addTest(new org.apache.axiom.ts.dom.element.TestLookupNamespaceURI(dbf));
         addTest(new org.apache.axiom.ts.dom.element.attributes.TestSetNamedItemNSWrongDocument(dbf));
         addTest(new org.apache.axiom.ts.dom.element.attributes.TestSetNamedItemWrongDocument(dbf));
         addTest(new org.apache.axiom.ts.dom.text.TestAppendData(dbf));

Added: webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/attr/TestLookpNamespaceURI.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/attr/TestLookpNamespaceURI.java?rev=1379724&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/attr/TestLookpNamespaceURI.java
(added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/attr/TestLookpNamespaceURI.java
Sat Sep  1 08:58:04 2012
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.axiom.ts.dom.attr;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import org.apache.axiom.ts.dom.DOMTestCase;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+
+public class TestLookpNamespaceURI extends DOMTestCase {
+
+    public TestLookpNamespaceURI(DocumentBuilderFactory dbf) {
+        super(dbf);
+    }
+
+    protected void runTest() throws Throwable {
+
+        String namespace = "http://apache/axiom/dom/ns";
+        String defaultNamespace = "http://www.w3.org/2000/xmlns/";
+        String prefix = "ns";
+        String defaultPrefix = "xmlns";
+
+        // <ns:Element xmlns:ns="http://apache/axiom/dom/ns">
+        Document doc = dbf.newDocumentBuilder().newDocument();
+        Element ele = doc.createElement("Element");
+        ele.setAttributeNS(defaultNamespace, defaultPrefix+":"+prefix, namespace);
+
+        NamedNodeMap attributes = ele.getAttributes();
+        Attr attr = (Attr) attributes.item(0);
+        
+        assertEquals("Incorrect namespace returned for the attribute", namespace,
+                attr.lookupNamespaceURI("ns"));
+    }
+
+}
\ No newline at end of file

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/attr/TestLookpNamespaceURI.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/element/TestLookupNamespaceURI.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/element/TestLookupNamespaceURI.java?rev=1379724&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/element/TestLookupNamespaceURI.java
(added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/element/TestLookupNamespaceURI.java
Sat Sep  1 08:58:04 2012
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.axiom.ts.dom.element;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import org.apache.axiom.ts.dom.DOMTestCase;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+public class TestLookupNamespaceURI extends DOMTestCase {
+
+    public TestLookupNamespaceURI(DocumentBuilderFactory dbf) {
+        super(dbf);
+    }
+
+    protected void runTest() throws Throwable {
+
+        String ns1 = "http://apache/axiom/dom/ns1";
+        String element1Name = "ele1";
+        String ns2 = "http://apache/axiom/dom/ns2";
+        String element2Name = "ns2:ele2";
+        String pref2 = "ns2";
+        String element3Name = "ele3";
+        String nsParent = "http://apache/axiom/dom/parent";
+        String parentName = "p:parent";
+        String prefParent = "p";
+
+        Document doc = dbf.newDocumentBuilder().newDocument();
+        Element parent = doc.createElementNS(nsParent, parentName);
+        // default namespace, no prefix
+        Element element1 = doc.createElementNS(ns1, element1Name);
+        parent.appendChild(element1);
+        // non default namespace, with prefix
+        Element element2 = doc.createElementNS(ns2, element2Name);
+        parent.appendChild(element2);
+        // parent has the prefix
+        Element element3 = doc.createElement(element3Name);
+        parent.appendChild(element3);
+
+        assertEquals("Incorrect default namespace returned for the element", ns1,
+                element1.lookupNamespaceURI(null));
+        assertEquals("Incorrect namespace returned for the element", ns2,
+                element2.lookupNamespaceURI(pref2));
+        assertEquals("Incorrect namespace returned for the given prefix", nsParent,
+                element3.lookupNamespaceURI(prefParent));
+    }
+
+}
\ No newline at end of file

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/element/TestLookupNamespaceURI.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/NodeImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/NodeImpl.java?rev=1379724&r1=1379723&r2=1379724&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/NodeImpl.java
(original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/NodeImpl.java
Sat Sep  1 08:58:04 2012
@@ -37,6 +37,7 @@ import org.w3c.dom.Document;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
 import org.w3c.dom.UserDataHandler;
+import org.w3c.dom.Attr;
 
 import java.io.OutputStream;
 import java.io.Writer;
@@ -246,9 +247,82 @@ public abstract class NodeImpl implement
         throw new UnsupportedOperationException("TODO");
     }
 
-    public String lookupNamespaceURI(String prefix) {
-        // TODO TODO
-        throw new UnsupportedOperationException("TODO");
+    /**
+     * Returns the namespace for a given prefix <br/>
+     * The prefix can be of an Attribute's or an Element's.
+     */
+    public String lookupNamespaceURI(String specifiedPrefix) {
+        short type = this.getNodeType();
+        switch (type) {
+        case Node.ELEMENT_NODE: {
+
+            String namespace = this.getNamespaceURI();
+            String prefix = this.getPrefix();
+            // looking in the element
+            if (namespace != null) {
+                if (prefix == null && specifiedPrefix == null) {
+                    // looking for default namespace
+                    return namespace;
+                } else if (prefix != null && prefix.equals(specifiedPrefix)) {
+                    // non default namespace
+                    return namespace;
+                }
+            }
+            // looking in attributes
+            if (this.hasAttributes()) {
+                NamedNodeMap map = this.getAttributes();
+                int length = map.getLength();
+                for (int i = 0; i < length; i++) {
+                    Node attr = map.item(i);
+                    String attrPrefix = attr.getPrefix();
+                    String value = attr.getNodeValue();
+                    namespace = attr.getNamespaceURI();
+                    if (namespace != null && namespace.equals("http://www.w3.org/2000/xmlns/"))
{
+                        if (specifiedPrefix == null && attr.getNodeName().equals("xmlns"))
{
+                            return value.length() > 0 ? value : null;
+                        } else if (attrPrefix != null && attrPrefix.equals("xmlns")
+                                && attr.getLocalName().equals(specifiedPrefix)) {
+                            return value.length() > 0 ? value : null;
+                        }
+                    }
+                }
+            }
+            // looking in ancestor
+            NodeImpl ancestor = (NodeImpl) getElementAncestor(this);
+            if (ancestor != null) {
+                return ancestor.lookupNamespaceURI(specifiedPrefix);
+            }
+
+            return null;
+
+        }
+        case Node.DOCUMENT_NODE: {
+            return ((NodeImpl) ((Document) this).getDocumentElement())
+                    .lookupNamespaceURI(specifiedPrefix);
+        }
+        case Node.ENTITY_NODE:
+        case Node.NOTATION_NODE:
+        case Node.DOCUMENT_FRAGMENT_NODE:
+        case Node.DOCUMENT_TYPE_NODE:
+            // type is unknown
+            return null;
+        case Node.ATTRIBUTE_NODE: {
+            NodeImpl ownerNode = (NodeImpl) ((Attr) this).getOwnerElement();
+            if (ownerNode.getNodeType() == Node.ELEMENT_NODE) {
+                return ownerNode.lookupNamespaceURI(specifiedPrefix);
+
+            }
+            return null;
+        }
+        default: {
+            NodeImpl ancestor = (NodeImpl) getElementAncestor(this);
+            if (ancestor != null) {
+                return ancestor.lookupNamespaceURI(specifiedPrefix);
+            }
+            return null;
+        }
+
+        }
     }
 
     /**
@@ -860,4 +934,17 @@ public abstract class NodeImpl implement
     }
 
     abstract NodeImpl clone(OMCloneOptions options, ParentNode targetParent, boolean deep,
boolean namespaceRepairing);
+
+    public Node getElementAncestor(Node currentNode) {
+        Node parent = currentNode.getParentNode();
+        while (parent != null) {
+            short type = parent.getNodeType();
+            if (type == Node.ELEMENT_NODE) {
+                return parent;
+            }
+            parent = parent.getParentNode();
+        }
+        return null;
+    }
+
 }



Mime
View raw message