1 /* 2 * Copyright (c) 2014, 2016, 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; 25 26 import java.io.ByteArrayInputStream; 27 import java.io.IOException; 28 import java.io.StringBufferInputStream; 29 import java.io.UnsupportedEncodingException; 30 31 import javax.xml.parsers.DocumentBuilder; 32 import javax.xml.parsers.DocumentBuilderFactory; 33 import javax.xml.parsers.ParserConfigurationException; 34 35 import org.testng.Assert; 36 import org.testng.annotations.BeforeMethod; 37 import org.testng.annotations.Listeners; 38 import org.testng.annotations.Test; 39 import org.w3c.dom.DOMImplementation; 40 import org.w3c.dom.Document; 41 import org.w3c.dom.ls.DOMImplementationLS; 42 import org.w3c.dom.ls.LSInput; 43 import org.w3c.dom.ls.LSParser; 44 import org.xml.sax.SAXException; 45 46 /* 47 * @bug 6355326 48 * @summary Test DOM implementation encoding. 49 */ 50 @Listeners({jaxp.library.BasePolicy.class}) 51 public class Bug6355326 { 52 53 DOMImplementationLS implLS = null; 54 String encodingXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><encodingXML/>"; 55 56 @BeforeMethod 57 protected void setUp() { 58 Document doc = null; 59 DocumentBuilder parser = null; 60 String xml1 = "<?xml version=\"1.0\"?><ROOT></ROOT>"; 61 try { 62 parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 63 } catch (ParserConfigurationException e) { 64 e.printStackTrace(); 65 } 66 StringBufferInputStream is = new StringBufferInputStream(xml1); 67 try { 68 doc = parser.parse(is); 69 } catch (SAXException e) { 70 e.printStackTrace(); 71 } catch (IOException e) { 72 e.printStackTrace(); 73 } 74 DOMImplementation impl = doc.getImplementation(); 75 implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0"); 76 } 77 78 @Test 79 public void testExternalEncoding() { 80 81 try { 82 LSInput src = null; 83 LSParser dp = null; 84 85 src = createLSInputEncoding(); 86 dp = createLSParser(); 87 88 src.setEncoding("UTF-16"); 89 Document doc = dp.parse(src); 90 Assert.assertTrue("encodingXML".equals(doc.getDocumentElement().getNodeName()), "XML document is not parsed correctly"); 91 92 } catch (Exception e) { 93 e.printStackTrace(); 94 Assert.fail("Exception occured: " + e.getMessage()); 95 } 96 } 97 98 private LSInput createLSInputEncoding() { 99 LSInput src = implLS.createLSInput(); 100 Assert.assertFalse(src == null, "Could not create LSInput from DOMImplementationLS"); 101 102 try { 103 src.setByteStream(new ByteArrayInputStream(encodingXML.getBytes("UTF-16"))); 104 } catch (UnsupportedEncodingException e) { 105 e.printStackTrace(); 106 Assert.fail("Exception occured: " + e.getMessage()); 107 } 108 return src; 109 } 110 111 private LSParser createLSParser() { 112 LSParser p = implLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, "http://www.w3.org/2001/XMLSchema"); 113 Assert.assertFalse(p == null, "Could not create Synchronous LSParser from DOMImplementationLS"); 114 return p; 115 } 116 }