[utilities COMMIT] in /java-support/trunk/src: main/java/net/shibboleth/utilities/java/support/xml/NamespaceSupport.j...

noreply at shibboleth.net noreply at shibboleth.net
Thu Feb 2 08:38:38 GMT 2012


Author: rdw
Date: Thu Feb  2 08:38:38 2012
New Revision: 227

URL: http://svn.shibboleth.net/view/utilities?rev=227&view=rev
Log:
(Finally) Tests and general bug fixes for NameSupport.
Minor tidy up of BasicParserPoolTest

Added:
    java-support/trunk/src/test/java/net/shibboleth/utilities/java/support/xml/NamespaceSupportTest.java   (with props)
    java-support/trunk/src/test/resources/data/net/shibboleth/utilities/java/support/xml/namespaceSupportTest.xml   (with props)
    java-support/trunk/src/test/resources/data/net/shibboleth/utilities/java/support/xml/qnameSupportTest.xml   (with props)
Modified:
    java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/xml/NamespaceSupport.java
    java-support/trunk/src/test/java/net/shibboleth/utilities/java/support/xml/BasicParserPoolTest.java

Modified: java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/xml/NamespaceSupport.java
URL: http://svn.shibboleth.net/view/utilities/java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/xml/NamespaceSupport.java?rev=227&r1=226&r2=227&view=diff
==============================================================================
--- java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/xml/NamespaceSupport.java (original)
+++ java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/xml/NamespaceSupport.java Thu Feb  2 08:38:38 2012
@@ -42,42 +42,40 @@
      * Adds a namespace declaration (xmlns:) attribute to the given element.
      * 
      * @param element the element to add the attribute to
-     * @param namespaceURI the URI of the namespace
+     * @param namespaceURI the URI of the namespace.  Cannot be null or empty (semantics would be undefined)
      * @param prefix the prefix for the namespace. If this is null this is the default namespace being added.
      */
-    public static void appendNamespaceDeclaration(@Nonnull final Element element, @Nullable final String namespaceURI,
+    public static void appendNamespaceDeclaration(@Nonnull final Element element, @Nonnull final String namespaceURI,
             @Nullable final String prefix) {
         Assert.isNotNull(element, "Element may not be null");
 
         final String nsURI = StringSupport.trimOrNull(namespaceURI);
         final String nsPrefix = StringSupport.trimOrNull(prefix);
+        
+        Assert.isNotNull(nsURI, "namespace may not be null or empty");
 
         String attributeName;
         if (nsPrefix == null) {
             if (null == element.getPrefix()) {
-                //
-                // We cannot change this so complain.
-                //
-                throw new DOMException(DOMException.INVALID_ACCESS_ERR, 
-                        "Cannot replace an element's default namespace");
+                
+                if (!namespaceURI.equals(element.getNamespaceURI())) {
+                    //
+                    // We cannot change this so complain.
+                    //
+                    throw new DOMException(DOMException.INVALID_ACCESS_ERR, 
+                            "Cannot replace an element's default namespace");
+                }
             }
             attributeName = XmlConstants.XMLNS_PREFIX;
         } else {
-            if (nsPrefix.equals(element.getPrefix())) {
+            if (nsPrefix.equals(element.getPrefix()) && !namespaceURI.equals(element.getNamespaceURI())) {
                 throw new DOMException(DOMException.INVALID_ACCESS_ERR,
                         "Cannot replace an element's default namespace");
             }
             attributeName = XmlConstants.XMLNS_PREFIX + ":" + nsPrefix;
         }
 
-        String attributeValue;
-        if (nsURI == null) {
-            attributeValue = "";
-        } else {
-            attributeValue = nsURI;
-        }
-
-        element.setAttributeNS(XmlConstants.XMLNS_NS, attributeName, attributeValue);
+        element.setAttributeNS(XmlConstants.XMLNS_NS, attributeName, nsURI);
     }
 
     /**
@@ -132,23 +130,6 @@
     }
 
     /**
-     * Looks up the namespace URI associated with the given prefix starting at the given element. This method differs
-     * from the {@link Node#lookupNamespaceURI(java.lang.String)} in that it only those namespaces declared by an xmlns
-     * attribute are inspected. The Node method also checks the namespace a particular node was created in by way of a
-     * call like {@link org.w3c.dom.Document#createElementNS(java.lang.String, java.lang.String)} even if the resulting
-     * element doesn't have an namespace delcaration attribute.
-     * 
-     * @param startingElement the starting element
-     * @param prefix the prefix to look up
-     * 
-     * @return the namespace URI for the given prefix
-     */
-    @Nullable public static String lookupNamespaceURI(@Nonnull final Element startingElement,
-            @Nullable final String prefix) {
-        return lookupNamespaceURI(startingElement, null, prefix);
-    }
-
-    /**

[... 46 lines stripped ...]


More information about the commits mailing list