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

org.w3c.dom.Node.setNodeValue(String) does not throw expected DOMException

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 6
    • xml
    • 1.4
    • generic
    • windows_xp
    • Verified

        The problem can be reproducible using the latest jdk 6.

        According to the spec for org.w3c.dom.Node.setNodeValue(String):
        "
        void setNodeValue(String nodeValue)
                          throws DOMException
            Throws:
                DOMException - NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly and if it is not defined to be null.
        "

        In the following code example, the test calls setNodeValue(String) on a read-only Entity node. The test expects the method to throw DOMException.NO_MODIFICATION_ALLOWED_ERR, but on jdk 6, it doesn't throw any exception.
        ===============================================================
        import org.w3c.dom.*;
        import javax.xml.parsers.DocumentBuilder;
        import javax.xml.parsers.DocumentBuilderFactory;
        import javax.xml.parsers.ParserConfigurationException;
        import org.xml.sax.SAXException;
        import java.io.IOException;
        import java.io.ByteArrayInputStream;
        import java.io.InputStreamReader;
        import org.xml.sax.InputSource;
        import java.io.StringReader;

        public class MyDomTest1 {

            public static void main(String[] args) {
            String data =
                    "<?xml version=\"1.0\" ?>"
                    + "<!DOCTYPE root ["
                    + "<!ELEMENT root ANY>"
                    + "<!ENTITY ent \"foo\">"
                    + "<!NOTATION not PUBLIC \"http://xxx.xxx.xx/x.txt\">"
                    + "]>"
                    + "<root>"
                    + "</root>";
            
            Document document = null;
            try {
                DocumentBuilderFactory docBF = DocumentBuilderFactory.newInstance();
                docBF.setNamespaceAware(true);
                DocBuilderWrapper docBuilder = new DocBuilderWrapper(docBF.newDocumentBuilder());
                document = docBuilder.parse(data);
            } catch (ParserConfigurationException e) {
                //return Status.failed(e.toString());
            } catch (IOException e) {
                //return Status.failed(e.toString());
            } catch (SAXException e) {
               // return Status.failed(e.toString());
            }

            Entity anEntity = (Entity)document.getDoctype().getEntities().item(0);
            try {
                anEntity.setNodeValue("someValue"); //on jdk 6, not even throwing exception
                
                System.out.println("Should throw DOMException: NO_MODIFICATION_ALLOWED_ERR ");
            } catch(DOMException e) {
                if (e.code == DOMException.NO_MODIFICATION_ALLOWED_ERR) {
                    System.out.println("OK");
                } else {
                    System.out.println("should throw DOMException.NO_MODIFICATION_ALLOWED_ERR (7). The error returned is " + e.code );
                }
            }
            }
        }

        class DocBuilderWrapper {

             private DocumentBuilder docBuilder;
             private final String ENCODING = "UTF-8";

             /**
              * Creates a new <code>DocBuilderWrapper</code> with the default
              * <code>DocumentBuilder</code> object.
              *
              * @throws ParserConfigurationException if a DocumentBuilder cannot be create
              */
             public DocBuilderWrapper() throws ParserConfigurationException {
                 this.docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
             }

             /**
              * Creates a new <code>DocBuilderWrapper</code> with the specified
              * <code>DocumentBuilder</code> object.
              *
              * @param docBuilder the <code>DocumentBuilder</code> object, it can not be null
              * @throws IllegalArgumentException If the docBuilder is null
              */
             public DocBuilderWrapper(DocumentBuilder docBuilder) {
                 setDocumentBuilder(docBuilder);
             }

             /**
              * Gets the <code>DocumentBuilder</code> object of this wrapper.
              *
              * @return the wrapped <code>DocumentBuilder</code> object
              */
             public DocumentBuilder getDocumentBuilder() {
                 return docBuilder;
             }

             /**
              * Sets the <code>DocumentBuilder</code> object to wrap.
              *
              * @param docBuilder the DocumentBuilder object, it can not be null
              * @throws IllegalArgumentException If the docBuilder is null
              */
             public void setDocumentBuilder(DocumentBuilder docBuilder) {
                 if (docBuilder == null) {
                     new IllegalArgumentException("DocumentBuilder cannot be null");
                 }
                 
                 this.docBuilder = docBuilder;
             }

             /**
              * Parses the content of the given String as an XML document.
              *
              * @param xmlData <code>String</code> containing the content to be parsed, it cannot be null.
              * @return A new <code>Document</code> object.
              * @throws IllegalArgumentException If the xmlData is null
              */
             public Document parse(String xmlData) throws IOException, SAXException {
                 if (xmlData == null) {
                     new IllegalArgumentException("String cannot be null");
                 }
                 
                 ByteArrayInputStream bis = new ByteArrayInputStream(xmlData.getBytes(ENCODING));
                 InputStreamReader isr = new InputStreamReader(bis, ENCODING);
                 InputSource source = new InputSource(isr);
                 return docBuilder.parse(source);
             }
         }
        ---------------------------------------------------------------------

              joehw Joe Wang
              hji Huafang Ji (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: