Author: veithen
Date: Tue Oct 25 21:05:15 2011
New Revision: 1188910

URL: http://svn.apache.org/viewvc?rev=1188910&view=rev
Log:
* AXIOM-388: Added an OMElement#getNamespaceContext method.
* Fixed an issue in OMElement#findNamespace related to namespace prefixes that 
are redeclared.

Added:
    
webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/LiveNamespaceContext.java
   (with props)
    
webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/OMElementImplUtil.java
   (with props)
    
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestFindNamespaceByNamespaceURIMasked.java
   (with props)
    
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestGetNamespaceContext.java
   (with props)
    
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/resources/org/apache/axiom/ts/om/element/namespacecontext.xml
   (with props)
Modified:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMElement.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/namespace/MapBasedNamespaceContext.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/OMTestSuiteBuilder.java

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMElement.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMElement.java?rev=1188910&r1=1188909&r2=1188910&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMElement.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMElement.java
 Tue Oct 25 21:05:15 2011
@@ -19,6 +19,7 @@
 
 package org.apache.axiom.om;
 
+import javax.xml.namespace.NamespaceContext;
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamException;
@@ -239,6 +240,33 @@ public interface OMElement extends OMNod
     Iterator getNamespacesInScope();
     
     /**
+     * Get the namespace context of this element, as determined by the 
namespace declarations
+     * present on this element and its ancestors.
+     * <p>
+     * The method supports two different {@link NamespaceContext} 
implementation variants:
+     * <ul>
+     * <li>A "live" variant that keeps a reference to the element and that 
performs lookups by
+     * accessing the object model. This means that any change in the object 
model will automatically
+     * be reflected by the {@link NamespaceContext}.
+     * <li>A "detached" variant that stores a snapshot of the namespace 
context and that doesn't
+     * have any reference to the object model.
+     * </ul>
+     * <p>
+     * Typically, creating a live {@link NamespaceContext} is cheaper, but the 
lookup performance of
+     * a detached {@link NamespaceContext} is better. The detached variant 
should always be used if
+     * the reference to the {@link NamespaceContext} is kept longer than the 
object model itself,
+     * because in this case a live {@link NamespaceContext} would prevent the 
object model from
+     * being garbage collected.
+     * 
+     * @param detached
+     *            <code>true</code> if the method should return a detached 
implementation,
+     *            <code>false</code> if the method should return a live object
+     * @return The namespace context for this element. Note that the caller 
must not make any
+     *         assumption about the actual implementation class returned by 
this method.
+     */
+    NamespaceContext getNamespaceContext(boolean detached);
+    
+    /**
      * Returns a list of OMAttributes.
      * <p/>
      * <p>Note that the iterator returned by this function will be invalidated 
by any

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/namespace/MapBasedNamespaceContext.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/namespace/MapBasedNamespaceContext.java?rev=1188910&r1=1188909&r2=1188910&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/namespace/MapBasedNamespaceContext.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/namespace/MapBasedNamespaceContext.java
 Tue Oct 25 21:05:15 2011
@@ -31,8 +31,14 @@ import javax.xml.XMLConstants;
  * Namespace context implementation that stores namespace bindings in a {@link 
Map}.
  */
 public class MapBasedNamespaceContext extends AbstractNamespaceContext {
-    protected Map namespaces;
+    private final Map namespaces;
 
+    /**
+     * Constructor.
+     * 
+     * @param map
+     *            a map containing the (prefix, namespace URI) entries
+     */
     public MapBasedNamespaceContext(Map map) {
         namespaces = map;
     }

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/LiveNamespaceContext.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/LiveNamespaceContext.java?rev=1188910&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/LiveNamespaceContext.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/LiveNamespaceContext.java
 Tue Oct 25 21:05:15 2011
@@ -0,0 +1,58 @@
+/*
+ * 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.om.impl.common;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.xml.XMLConstants;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.util.namespace.AbstractNamespaceContext;
+
+class LiveNamespaceContext extends AbstractNamespaceContext {
+    private final OMElement element;
+
+    public LiveNamespaceContext(OMElement element) {
+        this.element = element;
+    }
+
+    protected String doGetNamespaceURI(String prefix) {
+        OMNamespace ns = element.findNamespaceURI(prefix);
+        return ns == null ? XMLConstants.NULL_NS_URI : ns.getNamespaceURI();
+    }
+
+    protected String doGetPrefix(String namespaceURI) {
+        OMNamespace ns = element.findNamespace(namespaceURI, null);
+        return ns == null ? null : ns.getPrefix();
+    }
+
+    protected Iterator doGetPrefixes(String namespaceURI) {
+        List prefixes = new ArrayList();
+        for (Iterator it = element.getNamespacesInScope(); it.hasNext(); ) {
+            OMNamespace ns = (OMNamespace)it.next();
+            if (ns.getNamespaceURI().equals(namespaceURI)) {
+                prefixes.add(ns.getPrefix());
+            }
+        }
+        return prefixes.iterator();
+    }
+}

Propchange: 
webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/LiveNamespaceContext.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/OMElementImplUtil.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/OMElementImplUtil.java?rev=1188910&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/OMElementImplUtil.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/OMElementImplUtil.java
 Tue Oct 25 21:05:15 2011
@@ -0,0 +1,50 @@
+/*
+ * 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.om.impl.common;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.xml.namespace.NamespaceContext;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.util.namespace.MapBasedNamespaceContext;
+
+/**
+ * Utility class with default implementations for some of the methods defined 
by the
+ * {@link OMElement} interface.
+ */
+public class OMElementImplUtil {
+    private OMElementImplUtil() {}
+    
+    public static NamespaceContext getNamespaceContext(OMElement element, 
boolean detached) {
+        if (detached) {
+            Map namespaces = new HashMap();
+            for (Iterator it = element.getNamespacesInScope(); it.hasNext(); ) 
{
+                OMNamespace ns = (OMNamespace)it.next();
+                namespaces.put(ns.getPrefix(), ns.getNamespaceURI());
+            }
+            return new MapBasedNamespaceContext(namespaces);
+        } else {
+            return new LiveNamespaceContext(element);
+        }
+    }
+}

Propchange: 
webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/OMElementImplUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java?rev=1188910&r1=1188909&r2=1188910&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java
 Tue Oct 25 21:05:15 2011
@@ -31,6 +31,7 @@ import org.apache.axiom.om.OMXMLParserWr
 import org.apache.axiom.om.impl.common.NamespaceIterator;
 import org.apache.axiom.om.impl.common.OMChildElementIterator;
 import org.apache.axiom.om.impl.common.OMDescendantsIterator;
+import org.apache.axiom.om.impl.common.OMElementImplUtil;
 import org.apache.axiom.om.impl.common.OMNamespaceImpl;
 import org.apache.axiom.om.impl.dom.factory.OMDOMFactory;
 import org.apache.axiom.om.impl.traverse.OMQNameFilterIterator;
@@ -47,6 +48,7 @@ import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 import org.w3c.dom.TypeInfo;
 
+import javax.xml.namespace.NamespaceContext;
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamException;
@@ -826,6 +828,10 @@ public class ElementImpl extends ParentN
             if (parentNode instanceof OMElement) {
                 namespace = ((ElementImpl) parentNode).findNamespace(uri,
                                                                      prefix);
+                // If the prefix has been redeclared, then ignore the binding 
found on the ancestors
+                if (prefix == null && namespace != null && 
findDeclaredNamespace(null, namespace.getPrefix()) != null) {
+                    namespace = null;
+                }
             }
         }
 
@@ -1153,6 +1159,10 @@ public class ElementImpl extends ParentN
         return new NamespaceIterator(this);
     }
 
+    public NamespaceContext getNamespaceContext(boolean detached) {
+        return OMElementImplUtil.getNamespaceContext(this, detached);
+    }
+
     /** @see org.apache.axiom.om.OMElement#getAllAttributes() */
     public Iterator getAllAttributes() {
         if (attributes == null) {

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java?rev=1188910&r1=1188909&r2=1188910&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java
 Tue Oct 25 21:05:15 2011
@@ -40,6 +40,7 @@ import org.apache.axiom.om.impl.common.O
 import org.apache.axiom.om.impl.common.OMChildrenNamespaceIterator;
 import org.apache.axiom.om.impl.common.OMChildrenQNameIterator;
 import org.apache.axiom.om.impl.common.OMDescendantsIterator;
+import org.apache.axiom.om.impl.common.OMElementImplUtil;
 import org.apache.axiom.om.impl.common.OMNamespaceImpl;
 import org.apache.axiom.om.impl.jaxp.OMSource;
 import org.apache.axiom.om.impl.llom.factory.OMLinkedListImplFactory;
@@ -50,6 +51,7 @@ import org.apache.axiom.om.util.StAXUtil
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+import javax.xml.namespace.NamespaceContext;
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamException;
@@ -460,6 +462,10 @@ public class OMElementImpl extends OMNod
             //element should be enough.
             if (parent instanceof OMElement) {
                 namespace = ((OMElementImpl) parent).findNamespace(uri, 
prefix);
+                // If the prefix has been redeclared, then ignore the binding 
found on the ancestors
+                if (prefix == null && namespace != null && 
findDeclaredNamespace(null, namespace.getPrefix()) != null) {
+                    namespace = null;
+                }
             }
         }
 
@@ -555,6 +561,10 @@ public class OMElementImpl extends OMNod
         return new NamespaceIterator(this);
     }
 
+    public NamespaceContext getNamespaceContext(boolean detached) {
+        return OMElementImplUtil.getNamespaceContext(this, detached);
+    }
+
     /**
      * Returns a List of OMAttributes.
      *

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java?rev=1188910&r1=1188909&r2=1188910&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
 Tue Oct 25 21:05:15 2011
@@ -37,6 +37,7 @@ import org.apache.axiom.om.util.StAXUtil
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+import javax.xml.namespace.NamespaceContext;
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamException;
@@ -407,6 +408,11 @@ public class OMSourcedElementImpl extend
         return super.getNamespacesInScope();
     }
 
+    public NamespaceContext getNamespaceContext(boolean detached) {
+        forceExpand();
+        return super.getNamespaceContext(detached);
+    }
+
     /* (non-Javadoc)
      * @see org.apache.axiom.om.OMElement#getAllAttributes()
      */

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/OMTestSuiteBuilder.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/OMTestSuiteBuilder.java?rev=1188910&r1=1188909&r2=1188910&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/OMTestSuiteBuilder.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/OMTestSuiteBuilder.java
 Tue Oct 25 21:05:15 2011
@@ -119,6 +119,7 @@ public class OMTestSuiteBuilder extends 
         addTest(new 
org.apache.axiom.ts.om.element.TestDigestWithNamespace(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.element.TestDigestWithoutNamespace(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.element.TestFindNamespaceURIWithPrefixUndeclaring(metaFactory));
+        addTest(new 
org.apache.axiom.ts.om.element.TestFindNamespaceByNamespaceURIMasked(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.element.TestFindNamespaceByPrefix(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.element.TestGetAllAttributes1(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.element.TestGetAllAttributes2(metaFactory));
@@ -145,6 +146,8 @@ public class OMTestSuiteBuilder extends 
         addTest(new 
org.apache.axiom.ts.om.element.TestGetDescendants(metaFactory, false));
         addTest(new 
org.apache.axiom.ts.om.element.TestGetFirstChildWithName(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.element.TestGetFirstChildWithNameOnIncompleteElement(metaFactory));
+        addTest(new 
org.apache.axiom.ts.om.element.TestGetNamespaceContext(metaFactory, false));
+        addTest(new 
org.apache.axiom.ts.om.element.TestGetNamespaceContext(metaFactory, true));
         addTest(new 
org.apache.axiom.ts.om.element.TestGetNamespacesInScope(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.element.TestGetNamespacesInScopeWithDefaultNamespace(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.element.TestGetNamespacesInScopeWithMaskedDefaultNamespace(metaFactory));

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestFindNamespaceByNamespaceURIMasked.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestFindNamespaceByNamespaceURIMasked.java?rev=1188910&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestFindNamespaceByNamespaceURIMasked.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestFindNamespaceByNamespaceURIMasked.java
 Tue Oct 25 21:05:15 2011
@@ -0,0 +1,42 @@
+/*
+ * 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.om.element;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.util.AXIOMUtil;
+import org.apache.axiom.ts.AxiomTestCase;
+
+/**
+ * Tests that {@link OMElement#findNamespace(String, String)} returns 
<code>null</code> if the
+ * matching namespace declaration is masked by another namespace declaration, 
i.e. if the
+ * corresponding prefix is redeclared.
+ */
+public class TestFindNamespaceByNamespaceURIMasked extends AxiomTestCase {
+    public TestFindNamespaceByNamespaceURIMasked(OMMetaFactory metaFactory) {
+        super(metaFactory);
+    }
+
+    protected void runTest() throws Throwable {
+        OMElement root =
+                AXIOMUtil.stringToOM(metaFactory.getOMFactory(), "<root 
xmlns:p='urn:ns1'><child xmlns:p='urn:ns2'/></a>");
+        assertNull(root.getFirstElement().findNamespace("urn:ns1", null));
+        root.close(false);
+    }
+}

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

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestGetNamespaceContext.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestGetNamespaceContext.java?rev=1188910&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestGetNamespaceContext.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestGetNamespaceContext.java
 Tue Oct 25 21:05:15 2011
@@ -0,0 +1,80 @@
+/*
+ * 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.om.element;
+
+import java.io.InputStream;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import javax.xml.namespace.NamespaceContext;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.OMXMLBuilderFactory;
+import org.apache.axiom.ts.AxiomTestCase;
+
+/**
+ * Tests {@link OMElement#getNamespaceContext(boolean)}.
+ */
+public class TestGetNamespaceContext extends AxiomTestCase {
+    private final boolean detached;
+    
+    public TestGetNamespaceContext(OMMetaFactory metaFactory, boolean 
detached) {
+        super(metaFactory);
+        this.detached = detached;
+        addTestProperty("detached", Boolean.toString(detached));
+    }
+
+    protected void runTest() throws Throwable {
+        InputStream in = 
TestGetNamespaceContext.class.getResourceAsStream("namespacecontext.xml");
+        OMElement root = 
OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), 
in).getDocumentElement();
+        OMElement inner = root.getFirstElement().getFirstElement();
+        NamespaceContext context = inner.getNamespaceContext(detached);
+        assertEquals("urn:test2", context.getNamespaceURI("p"));
+        assertEquals("urn:test3", context.getNamespaceURI("q"));
+        assertEquals("urn:test3", context.getNamespaceURI("r"));
+        assertEquals("urn:test4", context.getNamespaceURI(""));
+        assertEquals("", context.getNamespaceURI("unbound"));
+        
+        assertNull(context.getPrefix("urn:test1"));
+        assertEquals("p", context.getPrefix("urn:test2"));
+        String prefix = context.getPrefix("urn:test3");
+        assertTrue(prefix.equals("q") || prefix.equals("r"));
+        assertEquals("", context.getPrefix("urn:test4"));
+        assertNull(context.getPrefix("unbound"));
+        
+        Iterator it = context.getPrefixes("urn:test1");
+        assertFalse(it.hasNext());
+        
+        it = context.getPrefixes("urn:test2");
+        assertTrue(it.hasNext());
+        assertEquals("p", it.next());
+        assertFalse(it.hasNext());
+
+        it = context.getPrefixes("urn:test3");
+        Set prefixes = new HashSet();
+        while (it.hasNext()) {
+            prefixes.add(it.next());
+        }
+        assertEquals(2, prefixes.size());
+        assertTrue(prefixes.contains("q"));
+        assertTrue(prefixes.contains("r"));
+    }
+}

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

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/resources/org/apache/axiom/ts/om/element/namespacecontext.xml
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/resources/org/apache/axiom/ts/om/element/namespacecontext.xml?rev=1188910&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/resources/org/apache/axiom/ts/om/element/namespacecontext.xml
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/resources/org/apache/axiom/ts/om/element/namespacecontext.xml
 Tue Oct 25 21:05:15 2011
@@ -0,0 +1,6 @@
+<?xml version="1.0"?>
+<a xmlns:p="urn:test1" xmlns:q="urn:test3">
+    <a xmlns:p="urn:test2" xmlns="urn:test4">
+        <a xmlns:r="urn:test3"/>
+    </a>
+</a>
\ No newline at end of file

Propchange: 
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/resources/org/apache/axiom/ts/om/element/namespacecontext.xml
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to