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

Method Node.replaceChild() throws ClassCastException

XMLWordPrintable

    • b46
    • generic
    • generic
    • Verified

      Name: erR10175 Date: 03/11/2004



           The method Node.replaceChild(Node newChild, Node oldChild) throws
      java.lang.ClassCastException when the oldChild parameter is a DocumentType node.

      The method is specified to throw DOMException, but the ClassCastException
      is not an instance of that. So it is a bug.

      In the test (see below) HIERARCHY_REQUEST_ERR is expected according to the
      last case of following wording:
      "
      Throws:
      DOMException - HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does
      not allow children of the type of the newChild node, or if the node to put in is
      one of this node's ancestors or this node itself, or if this node is of type
      Document and the result of the replacement operation would add a second DocumentType
      or Element on the Document node.
      "

      This bug affects new test in JCK 1.5 (not integrated yet)
          api/org_w3c/dom/Node/index.html#ReplaceChild[ReplaceChild010]

      The bug is found in jdk1.5.0/beta/b41.

      To reproduce the bug compile and run test.java as shown in the log below:
      ------------------------------------------ test.java
      import java.io.StringReader;
      import javax.xml.parsers.DocumentBuilderFactory;
      import org.w3c.dom.DOMException;
      import org.w3c.dom.Document;
      import org.w3c.dom.DocumentType;
      import org.w3c.dom.Element;
      import org.xml.sax.InputSource;

      class test {

           static final String docText = "<?xml version=\"1.0\" ?>"
                       + "<!DOCTYPE root ["
                       + "ELEMENT root ANY"
                       + "ENTITY ent \"foo\""
                       + "NOTATION not PUBLIC \"http://test/test.txt\""
                       + "]>"
                       + "<root/>";

           public static void main(String argv[]) throws Exception {
               Document document = null;
               InputSource inSource = new InputSource(new StringReader(docText));
               document = DocumentBuilderFactory.newInstance()
                        .newDocumentBuilder().parse(inSource);

               Element node = document.createElement("ANewElement");
               DocumentType docType = document.getDoctype();

               try {
                   document.replaceChild(node, docType);

                   System.out.println("Failed: "
                                + "replaceChild does not throw a DOMException");
               } catch (DOMException e) {
                   if (e.code == DOMException.HIERARCHY_REQUEST_ERR
                    || e.code == DOMException.NOT_SUPPORTED_ERR) {
                       System.out.println("Passed.");
                   } else {
                       System.out.println("Failed: "
                                        + "Unexpected error code " + e.code);
                   }
               }
           }
      }
      ----------------------------------------------------

      ------------------------------------------------ log
      $javac test.java && java -cp . -showversion test
      java version "1.5.0-beta2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b41)
      Java HotSpot(TM) Client VM (build 1.5.0-beta2-b41, mixed mode)

      Exception in thread "main" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.ElementImpl
               at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.replaceChild(CoreDocumentImpl.java:485)
               at test.main(test.java:29)
      ----------------------------------------------------

      ======================================================================
      ###@###.### 2004-03-15

      Name: erR10175 Date: 04/11/2004


      The test.java should be corrected to define docText variable as follows:
      ------------------------------------------------------------------------
           static final String docText = "<?xml version=\"1.0\" ?>"
                       + "<!DOCTYPE root ["
                       + "<!ELEMENT root ANY>"
                       + "<!ENTITY ent \"foo\">"
                       + "<!NOTATION not PUBLIC \"http://test/test.txt\">"
                       + "]>"
                       + "<root/>";
      ------------------------------------------------------------------------

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

            vkorcl Venugopal K (Inactive)
            reysunw Rey Rey (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: