Code example for DOMConfiguration

Methods: canSetParameter, getParameter, setParameter

0
        assertUnsupported("element-content-whitespace", false); 
        assertSupported("element-content-whitespace", true); 
    } 
 
    public void testEntities() { 
        assertEquals(true, domConfiguration.getParameter("entities"));
        assertSupported("entities", false); 
        assertSupported("entities", true); 
    } 
 
    public void testErrorHandler() { 
        assertEquals(null, domConfiguration.getParameter("error-handler"));
        assertSupported("error-handler", null); 
        assertSupported("error-handler", new DOMErrorHandler() {
            public boolean handleError(DOMError error) {
                return true; 
            } 
        }); 
    } 
 
    public void testInfoset() { 
        assertEquals(false, domConfiguration.getParameter("infoset"));
        assertSupported("infoset", false); 
        assertSupported("infoset", true); 
    } 
 
    public void testSettingInfosetUpdatesImplied() { 
        // first clear those other parameters 
        for (String name : infosetImpliesFalse) {
            if (domConfiguration.canSetParameter(name, true)) {
                domConfiguration.setParameter(name, true);
            } 
        } 
        for (String name : infosetImpliesTrue) {
            if (domConfiguration.canSetParameter(name, false)) {
                domConfiguration.setParameter(name, false);
            } 
        } 
 
        // set infoset 
        domConfiguration.setParameter("infoset", true);
 
        // now the parameters should all match what infoset implies 
        for (String name : infosetImpliesFalse) {
            assertEquals(false, domConfiguration.getParameter(name));
        } 
        for (String name : infosetImpliesTrue) {
            assertEquals(true, domConfiguration.getParameter(name));
        } 
    } 
 
    public void testSettingImpliedUpdatesInfoset() { 
        for (String name : infosetImpliesFalse) {
            domConfiguration.setParameter("infoset", true);
            if (domConfiguration.canSetParameter(name, true)) {
                domConfiguration.setParameter(name, true);
                assertEquals(false, domConfiguration.getParameter("infoset"));
            } 
        } 
 
        for (String name : infosetImpliesTrue) {
            domConfiguration.setParameter("infoset", true);
            if (domConfiguration.canSetParameter(name, false)) {
                domConfiguration.setParameter(name, false);
                assertEquals(false, domConfiguration.getParameter("infoset"));
            } 
        } 
    } 
 
    public void testNamespaces() { 
        assertEquals(true, domConfiguration.getParameter("namespaces"));
        assertSupported("namespaces", false); 
        assertSupported("namespaces", true); 
    } 
 
    public void testNamespaceDeclarations() { 
        assertEquals(true, domConfiguration.getParameter("namespace-declarations"));
        assertUnsupported("namespace-declarations", false); // supported in RI 6 
        assertSupported("namespace-declarations", true); 
    } 
 
    public void testNormalizeCharacters() { 
        assertEquals(false, domConfiguration.getParameter("normalize-characters"));
        assertSupported("normalize-characters", false); 
        assertUnsupported("normalize-characters", true); 
    } 
 
    public void testSchemaLocation() { 
        assertEquals(null, domConfiguration.getParameter("schema-location"));
        assertSupported("schema-location", "http://foo"); 
        assertSupported("schema-location", null); 
    } 
 
    /** 
     * This fails under the RI because setParameter() succeeds even though 
     * canSetParameter() returns false. 
     */ 
    public void testSchemaTypeDtd() { 
        assertUnsupported("schema-type", "http://www.w3.org/TR/REC-xml"); // supported in RI v6 
    } 
 
    public void testSchemaTypeXmlSchema() { 
        assertEquals(null, domConfiguration.getParameter("schema-type"));
        assertSupported("schema-type", null); 
        assertSupported("schema-type", "http://www.w3.org/2001/XMLSchema"); 
    } 
 
    public void testSplitCdataSections() { 
        assertEquals(true, domConfiguration.getParameter("split-cdata-sections"));
        assertSupported("split-cdata-sections", false); 
        assertSupported("split-cdata-sections", true); 
    } 
 
    public void testValidate() { 
        assertEquals(false, domConfiguration.getParameter("validate"));
        assertSupported("validate", false); 
        assertSupported("validate", true); 
    } 
 
    public void testValidateIfSchema() { 
        assertEquals(false, domConfiguration.getParameter("validate-if-schema"));
        assertSupported("validate-if-schema", false); 
        assertUnsupported("validate-if-schema", true); 
    } 
 
    public void testWellFormed() { 
        assertEquals(true, domConfiguration.getParameter("well-formed"));
        assertSupported("well-formed", false); 
        assertSupported("well-formed", true); 
    } 
 
    public void testMissingParameter() { 
        assertFalse(domConfiguration.canSetParameter("foo", true));
        try { 
            domConfiguration.getParameter("foo");
            fail();
        } catch (DOMException e) {
        } 
        try { 
            domConfiguration.setParameter("foo", true);
            fail();
        } catch (DOMException e) {
        } 
    } 
 
    public void testNullKey() { 
        try { 
            domConfiguration.canSetParameter(null, true);
            fail();
        } catch (NullPointerException e) {
        } 
        try { 
            domConfiguration.getParameter(null);
            fail();
        } catch (NullPointerException e) {
        } 
        try { 
            domConfiguration.setParameter(null, true);
            fail();
        } catch (NullPointerException e) {
        } 
    } 
 
    public void testNullValue() { 
        String message = "This implementation's canSetParameter() disagrees"
                + " with its setParameter()"; 
        try { 
            domConfiguration.setParameter("well-formed", null);
            fail(message);
        } catch (DOMException e) {
        } 
        assertEquals(message, false, domConfiguration.canSetParameter("well-formed", null));
    } 
 
    public void testTypeMismatch() { 
        assertEquals(false, domConfiguration.canSetParameter("well-formed", "true"));
        try { 
            domConfiguration.setParameter("well-formed", "true");
            fail();
        } catch (DOMException e) {
        } 
 
        assertEquals(false, domConfiguration.canSetParameter("well-formed", new Object()));
        try { 
            domConfiguration.setParameter("well-formed", new Object());
            fail();
        } catch (DOMException e) {
        } 
    } 
 
    private void assertUnsupported(String name, Object value) {
        String message = "This implementation's setParameter() supports an unexpected value: "
                + name + "=" + value;
        assertFalse(message, domConfiguration.canSetParameter(name, value));
        try { 
            domConfiguration.setParameter(name, value);
            fail(message);
        } catch (DOMException e) {
            assertEquals(DOMException.NOT_SUPPORTED_ERR, e.code);
        } 
        try { 
            domConfiguration.setParameter(name.toUpperCase(), value);
            fail(message);
        } catch (DOMException e) {
            assertEquals(DOMException.NOT_SUPPORTED_ERR, e.code);
        } 
        assertFalse(value.equals(domConfiguration.getParameter(name)));
    } 
 
    private void assertSupported(String name, Object value) {
        String message = "This implementation's canSetParameter() disagrees"
                + " with its setParameter() for " + name + "=" + value;
        try { 
            domConfiguration.setParameter(name, value);
        } catch (DOMException e) {
            if (domConfiguration.canSetParameter(name, value)) {
                fail(message);
            } else { 
                fail("This implementation's setParameter() doesn't support: "
                        + name + "=" + value);
            } 
        } 
        assertTrue(message, domConfiguration.canSetParameter(name.toUpperCase(), value));
        assertTrue(message, domConfiguration.canSetParameter(name, value));
        assertEquals(value, domConfiguration.getParameter(name));
        domConfiguration.setParameter(name.toUpperCase(), value);
        assertEquals(value, domConfiguration.getParameter(name.toUpperCase()));
    } 
 
    public void testCdataSectionsNotHonoredByNodeNormalize() throws Exception { 
        String xml = "<foo>ABC<![CDATA[DEF]]>GHI</foo>";
        parse(xml);