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

Cloned attribute can't be added to another element

XMLWordPrintable

    • 1.1fcs
    • sparc
    • solaris_7



      Name: inR10064 Date: 11/04/2000


         When JAXP 1.1 method org.w3c.dom.Node.cloneNode(boolean deep) is used
      to clone an attribute of some element, it returns an attribute object
      which can't be added to another element by the method Element.setAttributeNode().
      DOMException is thrown with the INUSE_ATTRIBUTE_ERR error code (See test.java below).

        Definition of the method Element.setAttributeNode() in
      p. 1.2 "Fundamental Interfaces" of DOM Level 2 Core
      (http:/www.w3.org/TR/2000/PR-DOM-Level-2-Core-20000927/core.html#ID-745549614)
      says:

      " Exceptions
          DOMException
              ......
              INUSE_ATTRIBUTE_ERR: Raised if newAttr is already an attribute of
              another Element object. The DOM user must explicitly clone Attr nodes
              to re-use them in other elements. "

      This bug presents in builds jaxp-1.1ea-b8, jaxp-1.1fcs-b9 and jaxp-1.1ea2-b10 and
      affects the test in TCK JAXP 1.1

      api/com/sun/xml/tests/dom/ElementTest.html#setAttributeNode0009

      ------------------------------------test.java-----------------------------
      import org.w3c.dom.Document;
      import javax.xml.parsers.DocumentBuilderFactory;
      import javax.xml.parsers.DocumentBuilder;
      import org.w3c.dom.Element;
      import org.w3c.dom.Attr;
      import org.w3c.dom.DOMException;

      public class test {
          
          public static void main(String argv[]) {
              
              try {
                  DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance()
                                                    .newDocumentBuilder();

                  Document document = documentBuilder.newDocument();
                  
                  Attr attr = document.createAttribute("attr");
      attr.setValue("abc");
                  System.out.println("Attr implementation: " + attr.getClass().getName());
                  
      Element element = document.createElement("e1");
      element.setAttributeNode(attr);
                  
      Attr attr2 = (Attr)attr.cloneNode(true);
                  
      Element element2 = document.createElement("e2");
                  element2.setAttributeNode(attr2);
                  
                  System.out.println("Cloned attribute was added to second element.");
              } catch (DOMException e) {
                  if (e.code == DOMException.INUSE_ATTRIBUTE_ERR) {
                      System.out.println("Attribute is used by other element.");
                  }
              } catch (Exception e) {
                  System.out.println("Unexpected " + e + " was thrown");
              }
          }
      }
      ---------------------------------------------------------------------------
      ---------------------------------------------------------------------------
      % java -showversion -cp .:jaxp1.1/jaxp.jar:jaxp1.1/crimson.jar test
      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, interpreted mode)

      Attr implementation: org.apache.crimson.tree.AttributeNode
      Attribute is used by other element.

      % java -showversion -cp .:jaxp1.0.1/jaxp.jar:jaxp1.0.1/parser.jar test
      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, interpreted mode)

      Attr implementation: com.sun.xml.tree.AttributeNode
      Cloned attribute was added to second element.
      ---------------------------------------------------------------------------

      ======================================================================

            egoei Edwin Goei (Inactive)
            inevsunw Inev Inev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: