1 /* 2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package dom.ls; 25 26 import javax.xml.parsers.DocumentBuilderFactory; 27 import javax.xml.parsers.ParserConfigurationException; 28 29 import org.testng.Assert; 30 import org.testng.annotations.Test; 31 import org.w3c.dom.DOMConfiguration; 32 import org.w3c.dom.DOMError; 33 import org.w3c.dom.DOMErrorHandler; 34 import org.w3c.dom.DOMException; 35 import org.w3c.dom.DOMImplementation; 36 import org.w3c.dom.ls.DOMImplementationLS; 37 import org.w3c.dom.ls.LSInput; 38 import org.w3c.dom.ls.LSParser; 39 import org.w3c.dom.ls.LSResourceResolver; 40 41 /* 42 * @summary Test LSParser's DOMConfiguration for supported properties. 43 */ 44 public class LSParserTest { 45 46 @Test 47 public void testDOMConfiguration() { 48 49 final DOMErrorHandler handler = new DOMErrorHandler() { 50 public boolean handleError(final DOMError error) { 51 return false; 52 } 53 }; 54 55 final LSResourceResolver resolver = new LSResourceResolver() { 56 public LSInput resolveResource(final String type, final String namespaceURI, final String publicId, final String systemId, final String baseURI) { 57 return null; 58 } 59 }; 60 61 final Object[][] values = { 62 // parameter, value 63 { "canonical-form", Boolean.FALSE }, { "cdata-sections", Boolean.FALSE }, { "cdata-sections", Boolean.TRUE }, 64 { "check-character-normalization", Boolean.FALSE }, { "comments", Boolean.FALSE }, { "comments", Boolean.TRUE }, 65 { "datatype-normalization", Boolean.FALSE }, { "entities", Boolean.FALSE }, { "entities", Boolean.TRUE }, { "error-handler", handler }, 66 { "infoset", Boolean.TRUE }, { "namespaces", Boolean.TRUE }, { "namespace-declarations", Boolean.TRUE }, 67 { "namespace-declarations", Boolean.FALSE }, { "normalize-characters", Boolean.FALSE }, { "split-cdata-sections", Boolean.TRUE }, 68 { "split-cdata-sections", Boolean.FALSE }, { "validate", Boolean.FALSE }, { "validate-if-schema", Boolean.FALSE }, 69 { "well-formed", Boolean.TRUE }, { "element-content-whitespace", Boolean.TRUE }, 70 71 { "charset-overrides-xml-encoding", Boolean.TRUE }, { "charset-overrides-xml-encoding", Boolean.FALSE }, { "disallow-doctype", Boolean.FALSE }, 72 { "ignore-unknown-character-denormalizations", Boolean.TRUE }, { "resource-resolver", resolver }, { "resource-resolver", null }, 73 { "supported-media-types-only", Boolean.FALSE }, }; 74 75 DOMImplementation domImpl = null; 76 try { 77 domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation(); 78 } catch (ParserConfigurationException parserConfigurationException) { 79 parserConfigurationException.printStackTrace(); 80 Assert.fail(parserConfigurationException.toString()); 81 } 82 83 DOMImplementationLS lsImpl = (DOMImplementationLS) domImpl.getFeature("LS", "3.0"); 84 85 LSParser lsParser = lsImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); 86 87 DOMConfiguration config = lsParser.getDomConfig(); 88 89 for (int i = values.length; --i >= 0;) { 90 Object val = values[i][1]; 91 String param = (String) values[i][0]; 92 try { 93 config.setParameter(param, val); 94 Object returned = config.getParameter(param); 95 Assert.assertEquals(val, returned, "'" + param + "' is set to " + returned + ", but expected " + val); 96 System.out.println("set '" + param + "'" + " to '" + val + "'" + " and returned '" + returned + "'"); 97 } catch (DOMException e) { 98 String settings = "setting '" + param + "' to " + val; 99 System.err.println(settings); 100 e.printStackTrace(); 101 Assert.fail(settings + ", " + e.toString()); 102 } 103 } 104 } 105 }