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 parsers.xinclude; 25 26 import static java.lang.System.lineSeparator; 27 import static org.testng.Assert.assertEquals; 28 29 import java.io.File; 30 import java.io.StringWriter; 31 32 import javax.xml.parsers.DocumentBuilder; 33 import javax.xml.parsers.DocumentBuilderFactory; 34 import javax.xml.transform.OutputKeys; 35 import javax.xml.transform.Transformer; 36 import javax.xml.transform.TransformerFactory; 37 import javax.xml.transform.dom.DOMSource; 38 import javax.xml.transform.stream.StreamResult; 39 40 import org.testng.annotations.Listeners; 41 import org.testng.annotations.Test; 42 import org.w3c.dom.Document; 43 import org.w3c.dom.NodeList; 44 45 /* 46 * @bug 6794483 8080908 47 * @summary Test JAXP parser can resolve the included content properly if the 48 * included xml contains an empty tag that ends with "/>", refer to XERCESJ-1134. 49 */ 50 @Listeners({jaxp.library.FilePolicy.class}) 51 public class Bug6794483Test { 52 53 @Test 54 public final void test() throws Exception { 55 Document doc = parseXmlFile(getClass().getResource("test1.xml").getPath()); 56 57 // check node4 58 NodeList nodeList = doc.getElementsByTagName("node4"); 59 assertEquals(nodeList.getLength(), 1); 60 assertEquals(nodeList.item(0).getTextContent(), "Node4 Value", "The data of node4 is missed in parsing: " + lineSeparator() + printXmlDoc(doc)); 61 62 // check node6 63 nodeList = doc.getElementsByTagName("node6"); 64 assertEquals(nodeList.getLength(), 1); 65 assertEquals(nodeList.item(0).getTextContent(), "Node6 Value", "The data of node6 is missed in parsing: " + lineSeparator() + printXmlDoc(doc)); 66 } 67 68 public String printXmlDoc(Document doc) throws Exception { 69 StringWriter sw = new StringWriter(); 70 StreamResult result = new StreamResult(sw); 71 72 TransformerFactory transformerFact = TransformerFactory.newInstance(); 73 transformerFact.setAttribute("indent-number", new Integer(4)); 74 Transformer transformer; 75 76 transformer = transformerFact.newTransformer(); 77 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 78 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); 79 transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml"); 80 81 transformer.transform(new DOMSource(doc), result); 82 return sw.toString(); 83 } 84 85 public Document parseXmlFile(String fileName) throws Exception { 86 System.out.println("Parsing XML file... " + fileName); 87 DocumentBuilder docBuilder = null; 88 Document doc = null; 89 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 90 docBuilderFactory.setCoalescing(true); 91 docBuilderFactory.setXIncludeAware(true); 92 System.out.println("Include: " + docBuilderFactory.isXIncludeAware()); 93 docBuilderFactory.setNamespaceAware(true); 94 docBuilderFactory.setExpandEntityReferences(true); 95 96 docBuilder = docBuilderFactory.newDocumentBuilder(); 97 98 File sourceFile = new File(fileName); 99 doc = docBuilder.parse(sourceFile); 100 101 System.out.println("XML file parsed"); 102 return doc; 103 104 } 105 }