You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xindice-dev@xml.apache.org by Terry Rosenbaum <Te...@amicas.com> on 2002/09/09 18:12:56 UTC

NPE while removeAttribute or removeAttributeNS (patches included)

Referring to latest xindice embedded version from CVS as of Sun Sep  8 
11 AM EDT.

NPE while doing ElementImpl.removeAttribute(String name) if named 
attribute not found
and NPE while doing ElementImpl.removeAttributeNS(String namespaceURI, 
String localName)
under various conditions. The errata for DOM 2 specification clarifies 
that if the attribute being
removed is not found, the call should have no effect (certainly not an 
NPE). Also, removeAttributeNS
could NPE under various conditions (attributes of an element have null 
namespace).

Reference:

see: http://www.w3.org/2000/11/DOM-Level-2-errata#core-19

    core-19. 2001-08-31. [clarification]. Element.removeAttribute.
        The following sentence is missing in the description:
        If the attribute does not have a specified or default value,
        calling this method has no effect. (ditto for removeAttributeNS)

Patches to fix NPE from removeAttribute(String name) and 
removeAttributeNS(String namespaceURI, String localName):

---------------------------------------------------- 8< 
----------------------------------------------------
diff -aur 
xml-xindice/java/src/org/apache/xindice/xml/dom/ElementImpl.java 
patched-xml-xindice/java/src/org/apache/xindice/xml/dom/ElementImpl.java
--- xml-xindice/java/src/org/apache/xindice/xml/dom/ElementImpl.java   
 Sat Aug  3 11:49:08 2002
+++ 
patched-xml-xindice/java/src/org/apache/xindice/xml/dom/ElementImpl.java   
 Sun Sep  8 11:31:56 2002
@@ -202,7 +202,9 @@
 
    /**
     * Removes an attribute by name. If the removed attribute has a default
-    * value it is immediately replaced.
+    * value it is immediately replaced. If the named attribute does not 
exist,
+    * this method has no effect (no exception is thrown).
+    *
     * @param name The name of the attribute to remove.
     * @exception DOMException
     *   NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
@@ -210,8 +212,17 @@
    public void removeAttribute(String name) throws DOMException {
       checkReadOnly();
       AttrImpl attr = (AttrImpl)attributes.removeNamedItem(name);
-      ((AttrImpl)attr).setParentNode((NodeImpl)this.getOwnerDocument());
-      setDirty();
+      // do not cause an exception if the attribute does not exist --
+      // see: http://www.w3.org/2000/11/DOM-Level-2-errata#core-19
+      // core-19. 2001-08-31. [clarification]. Element.removeAttribute.
+      // The following sentence is missing in the description:
+      // If the attribute does not have a specified or default value,
+      // calling this method has no effect. (ditto for removeAttributeNS)
+      if(attr != null)
+      {   
+          
((AttrImpl)attr).setParentNode((NodeImpl)this.getOwnerDocument());
+          setDirty();
+      }
    }
 
    /**
@@ -390,7 +401,8 @@
      *  Removes an attribute by local name and namespace URI. If the 
removed
      * attribute has a default value it is immediately replaced. The
      * replacing attribute has the same namespace URI and local name, 
as well
-     * as the original prefix.
+     * as the original prefix. If the named attribute is not present, 
this method
+     * has no effect (per clarification in DOM2 errata).
      * <br> HTML-only DOM implementations do not need to implement this 
method.
      * @param namespaceURI  The  namespace URI of the attribute to remove.
      * @param localName  The  local name of the attribute to remove.
diff -aur 
xml-xindice/java/src/org/apache/xindice/xml/dom/NamedNodeMapImpl.java 
patched-xml-xindice/java/src/org/apache/xindice/xml/dom/NamedNodeMapImpl.java
--- 
xml-xindice/java/src/org/apache/xindice/xml/dom/NamedNodeMapImpl.java   
 Thu Dec  6 14:34:00 2001
+++ 
patched-xml-xindice/java/src/org/apache/xindice/xml/dom/NamedNodeMapImpl.java   
 Sun Sep  8 11:31:56 2002
@@ -164,10 +164,18 @@
       Node node;
       for ( int i = 0; enum.hasNext(); i++ ) {
          node = (Node)enum.next();
-         if ( node.getNamespaceURI().equals(namespaceURI)
-           && node.getLocalName().equals(localName) ) {
-            return removeNamedItem(node.getNodeName());
+         if(namespaceURI != null) {
+             if(namespaceURI.equals(node.getNamespaceURI()) &&
+                localName.equals(node.getLocalName())) {
+                return removeNamedItem(node.getNodeName());
+            }
          }
+         else {
+             if(node.getNamespaceURI() == null &&
+                localName.equals(node.getLocalName())) {
+                return removeNamedItem(node.getNodeName());
+            }
+         }
       }
       return null;
    }
---------------------------------------------------- 8< 
----------------------------------------------------

Could someone with commit power please review these patches, and if 
acceptable check them in?

Thanks.

-Terry Rosenbaum


Re: NPE while removeAttribute or removeAttributeNS (patches included)

Posted by Terry Rosenbaum <Te...@amicas.com>.
Fernando asked:

 > Terry, have you figured out why we have our own dom implementation? 
What's
 > the reason we can't use XercesDOM? ( since you seem to have looked at
 > this code more than me ).

I wondered that myself. It certainly is detrimental -- we have to 
maintain our own
DOM and, one would hope that someone else's DOM (e.g. xerces) would be
superior. But, I am relatively new to Xindice (about a month) and have not
figured out the answer to that question. I will look into it and see if 
I can come up
with an answer.

 > As an aside, my personal preference is to send them as attachments, 
it's just much easier
 > for be to save an attachment to a file to patch, than to copy/paste, 
etc etc..

Ok. No problem. Wiil do in the future.

-Terry

Fernando Padilla wrote:

>Alright, this one is a little bit more complicated.  As an aside, my
>personal preference is to send them as attachments, it's just much easier
>for be to save an attachment to a file to patch, than to copy/paste, etc
>etc..
>
>So at the moment, I looked over ElementImpl.removeAttribute, 
>ElementImpl.removeAttributeNS.  They are now both checking to see if the 
>removed attribute was null, and removeAttributeNS is properly setting the 
>removed attribute's parentNode.  I'll take a look at the NamedNodeMapImpl 
>in a second.
>
>Terry, have you figured out why we have our own dom implementation? What's 
>the reason we can't use XercesDOM?  ( since you seem to have looked at 
>this code more than me ).
>
>Fernando
>
>
>
>On Mon, 9 Sep 2002, Terry Rosenbaum wrote:
>
>  
>
>>Referring to latest xindice embedded version from CVS as of Sun Sep  8 
>>11 AM EDT.
>>
>>NPE while doing ElementImpl.removeAttribute(String name) if named 
>>attribute not found
>>and NPE while doing ElementImpl.removeAttributeNS(String namespaceURI, 
>>String localName)
>>under various conditions. The errata for DOM 2 specification clarifies 
>>that if the attribute being
>>removed is not found, the call should have no effect (certainly not an 
>>NPE). Also, removeAttributeNS
>>could NPE under various conditions (attributes of an element have null 
>>namespace).
>>
>>Reference:
>>
>>see: http://www.w3.org/2000/11/DOM-Level-2-errata#core-19
>>
>>    core-19. 2001-08-31. [clarification]. Element.removeAttribute.
>>        The following sentence is missing in the description:
>>        If the attribute does not have a specified or default value,
>>        calling this method has no effect. (ditto for removeAttributeNS)
>>    
>>
>
>
>
>  
>



Re: NPE while removeAttribute or removeAttributeNS (patches included)

Posted by Fernando Padilla <fe...@interdimensions.com>.

Alright, this one is a little bit more complicated.  As an aside, my
personal preference is to send them as attachments, it's just much easier
for be to save an attachment to a file to patch, than to copy/paste, etc
etc..

So at the moment, I looked over ElementImpl.removeAttribute, 
ElementImpl.removeAttributeNS.  They are now both checking to see if the 
removed attribute was null, and removeAttributeNS is properly setting the 
removed attribute's parentNode.  I'll take a look at the NamedNodeMapImpl 
in a second.

Terry, have you figured out why we have our own dom implementation? What's 
the reason we can't use XercesDOM?  ( since you seem to have looked at 
this code more than me ).

Fernando



On Mon, 9 Sep 2002, Terry Rosenbaum wrote:

> Referring to latest xindice embedded version from CVS as of Sun Sep  8 
> 11 AM EDT.
> 
> NPE while doing ElementImpl.removeAttribute(String name) if named 
> attribute not found
> and NPE while doing ElementImpl.removeAttributeNS(String namespaceURI, 
> String localName)
> under various conditions. The errata for DOM 2 specification clarifies 
> that if the attribute being
> removed is not found, the call should have no effect (certainly not an 
> NPE). Also, removeAttributeNS
> could NPE under various conditions (attributes of an element have null 
> namespace).
> 
> Reference:
> 
> see: http://www.w3.org/2000/11/DOM-Level-2-errata#core-19
> 
>     core-19. 2001-08-31. [clarification]. Element.removeAttribute.
>         The following sentence is missing in the description:
>         If the attribute does not have a specified or default value,
>         calling this method has no effect. (ditto for removeAttributeNS)