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

noreply at shibboleth.net noreply at shibboleth.net
Tue Jan 31 12:09:17 GMT 2012


Author: rdw
Date: Tue Jan 31 12:09:17 2012
New Revision: 225

URL: http://svn.shibboleth.net/view/utilities?rev=225&view=rev
Log:
Add extra tests to appendNamespaceDeclaration so as to explicitly detect violations on DOM's weird restrictions on namespaces and thus avoid the implementation's idiotic behavior in the face of misbehavior.

Also clean up typos and reformat.

Modified:
    java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/xml/NamespaceSupport.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=225&r1=224&r2=225&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 Tue Jan 31 12:09:17 2012
@@ -43,24 +43,35 @@
      * 
      * @param element the element to add the attribute to
      * @param namespaceURI the URI of the namespace
-     * @param prefix the prefix for the namespace
-     */
-    public static void
-            appendNamespaceDeclaration(@Nonnull final Element element, @Nullable final String namespaceURI, @Nullable final String prefix) {
+     * @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,
+            @Nullable final String prefix) {
         Assert.isNotNull(element, "Element may not be null");
 
         final String nsURI = StringSupport.trimOrNull(namespaceURI);
         final String nsPrefix = StringSupport.trimOrNull(prefix);
 
-        // This results in xmlns="" being emitted, which seems wrong.
+        // This results in xmlns="" being omitted, which seems wrong.
         if (nsURI == null && nsPrefix == null) {
             return;
         }
 
         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");
+            }
             attributeName = XmlConstants.XMLNS_PREFIX;
         } else {
+            if (nsPrefix.equals(element.getPrefix())) {
+                throw new DOMException(DOMException.INVALID_ACCESS_ERR,
+                        "Cannot replace an element's default namespace");
+            }
             attributeName = XmlConstants.XMLNS_PREFIX + ":" + nsPrefix;
         }
 
@@ -76,20 +87,20 @@
 
     /**
      * 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.
+     * from the {@link Node#lookupNamespaceURI(java.lang.String)} in that it only returns 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 declaration attribute.
      * 
      * @param startingElement the starting element
-     * @param stopingElement the ancestor of the starting element that serves as the upper-bound, inclusive, for the
+     * @param stoppingElement the ancestor of the starting element that serves as the upper-bound, inclusive, for the
      *            search
-     * @param prefix the prefix to look up
+     * @param prefix the prefix to look up. If null then the default namespace is returned.
      * 
      * @return the namespace URI for the given prefer or null
      */
-    @Nullable public static String lookupNamespaceURI(@Nonnull final Element startingElement, @Nullable final Element stopingElement,
-            final String prefix) {
+    @Nullable public static String lookupNamespaceURI(@Nonnull final Element startingElement,
+            @Nullable final Element stoppingElement, @Nonnull final String prefix) {
         Assert.isNotNull(startingElement, "Starting element may not be null");
 
         // This code is a modified version of the lookup code within Xerces
@@ -103,11 +114,11 @@
                 value = attr.getNodeValue();

[... 150 lines stripped ...]


More information about the commits mailing list