Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8264845

org.w3c.dom.Element.setAttributeNS() unsets attribute not specified in the call

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      A call to org.w3c.dom.Element.setAttributeNS(aNamespace, "aName","aValue") unsets an existing attribute not named "aName".

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the sample

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      This output:
      Variant 0 using name "location" tested OK.
      Variant 1 using name "location" tested OK
      Variant 2 using name "location" tested OK
      Variant 0 using name "ns0:location" tested OK
      Variant 1 using name "ns0:location" tested OK
      Variant 2 using name "ns0:location" tested OK
      Variant 0 using name "info:location" tested OK.
      Variant 1 using name "info:location" tested OK
      Variant 2 using name "info:location" tested OK
      ACTUAL -
      This output:
      Variant 0 using name "location" failed.
      Variant 1 using name "location" tested OK
      Variant 2 using name "location" tested OK
      Variant 0 using name "ns0:location" tested OK
      Variant 1 using name "ns0:location" tested OK
      Variant 2 using name "ns0:location" tested OK
      Variant 0 using name "info:location" failed.
      Variant 1 using name "info:location" tested OK
      Variant 2 using name "info:location" tested OK

      ---------- BEGIN SOURCE ----------
      import javax.xml.parsers.DocumentBuilderFactory;
      import javax.xml.parsers.DocumentBuilder;
      import org.w3c.dom.DOMImplementation;
      import org.w3c.dom.Document;
      import org.w3c.dom.Element;
      import org.w3c.dom.NodeList;
      import org.xml.sax.InputSource;
      import java.io.FileInputStream;
      import java.io.InputStream;
      import java.io.StringReader;
      import java.net.URL;
      /**
       * Calling calling org.w3c.dom.Element.setAttributeNS(ANamespace,"aLocalNameOtherThanName",aValue) can have the side effect of removing the "name" attribute from that node.
       * Test show show that the behavior can depend on:
       * - the number of attributes (removing any one of the 5 attributes will no longer reproduce the issue)
       * - the alphabetic order of the attributes (changing the name of the first from "a" to "w" or of the last from "o" to "n" will no longer reproduce the issue)
       */
      public class DomBug
      {
          static final String NS="http://www.foo.com/LOCATION";
          public static void main(String[] args) throws Exception
          {
              test(0,"location"); //fails
              test(1,"location"); //OK
              test(2,"location"); //OK

              test(0,"ns0:location"); //OK
              test(1,"ns0:location"); //OK
              test(2,"ns0:location"); //OK

              test(0,"info:location"); //fails
              test(1,"info:location"); //OK
              test(2,"info:location"); //OK
          }
          static void test(int variant,String qName) throws Exception
          {
              Element e=getElement(variant);

              assert e.hasAttribute("name");
              e.setAttributeNS(NS,qName,"y");
              if(!e.hasAttribute("name"))
                  System.err.println("Variant "+variant+" using name \""+qName+"\" failed.");
              else
                  System.out.println("Variant "+variant+" using name \""+qName+"\" tested OK");
          }
          static Element getElement(int variant) throws Exception
          {
              DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance();
              documentBuilderFactory.setNamespaceAware(true);
              DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder();
              Document document=documentBuilder.parse(new InputSource(new StringReader(
              "<?xml version='1.0'?>\n"+
              "<other:Root xmlns:other='http://www.foo.com/OTHER&#39; xmlns='http://www.foo.com/DEFAULT&#39; xmlns:ns0='http://www.foo.com/LOCATION&#39;>\n"+
              (variant==0?
              " <ANODE a='a' b='b' c='c' name='AName' ns0:location='x' o='o'/>\n"
               :variant==1?
              " <ANODE w='w' b='b' c='c' name='AName' ns0:location='x' o='o'/>\n"
               :
              " <ANODE a='a' b='b' c='c' name='AName' ns0:location='x' n='n'/>\n"
              )+
              "</other:Root>"
              )));
              return (Element)document.getElementsByTagName("ANODE").item(0);
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      In the call to org.w3c.dom.Element.setAttributeNS() use the same prefix in the qualified name that is specified in the original document.

            tongwan Andrew Wang
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: