The following small test case crashes with crimson (cvs snapshot): import org.w3c.dom.*; import org.apache.crimson.tree.*; public class BugB { // CJO 02/2002 public static void main(String[] arg) { // directly create XmlDocument just for the prupose // of having a smaller test... XmlDocument doc = new XmlDocument(); Element elt = doc.createElementNS(null, "foo"); doc.appendChild(elt); elt.setAttributeNS(null, "bar", "bar_value"); // this line should not return the previous // element has the namespace is not the same // but it should not crash either... elt.getAttributeNS("myNS", "bar"); } }
The problem is caused by the fact that the code does not expect someone mixing namespaces with non-namespaces. if the requested namespace is not null, the code silently expects all namespaces of the node attributes to be not null, and sends them messages without null - checks ... i will upload a small patch here that will fix the problem in this place. the code is not-so-efficient afterwards because it always sends an "equals" to the namespace-strings, while mostly an "==" should give the right anwer. i guess to keep the code efficient one should write two loops in the affected method; one for null and one for non-null arguments, to avoid repetetive tests for null / not null within the loop. (i have made no performance measurements; i think the performance loss if my patch is neglegible ... but who knows ...) by the way: is passing "null" argument as namespaceURI legal anyway? if one changes the test case from: elt.setAttributeNS(null, "bar", "bar_value"); to elt.setAttribute( "bar", "bar_value"); no exception happens. If "null" namespaces are not allowed (the w3c DOM-spec says nothing about it, because it is defined in IDL, wich does not case about Java "null"; but seems reasonable to me, that namespaces should not be null), this bug report is invalid and the patch obsolete. Instead one should throw an exception in "setAttributeNS" instead (which happens already now, if the attribute name contains namespace definition; change: elt.setAttributeNS(null, "bar", "bar_value"); to: elt.setAttributeNS(null, "foo:bar", "bar_value"); to see the exception.
Created attachment 1523 [details] patch allowing "null"-namespaceURIs in Element.getAttributeNS
FYI, a <null> namespace is perfectly conformant to W3C DOM. So this have to be fixed. From DOM Level 2 recommendation: "Applications must use the value null as the namespaceURI parameter for methods if they wish to have no namespace."
Er, Christophe is right ... I should have read the spec before make statements about it :(