1 /*
   2  * Copyright (c) 2014, 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 javax.xml.parsers.xinclude;
  25 
  26 import java.io.File;
  27 import java.io.IOException;
  28 import java.io.StringWriter;
  29 
  30 import javax.xml.parsers.DocumentBuilder;
  31 import javax.xml.parsers.DocumentBuilderFactory;
  32 import javax.xml.parsers.ParserConfigurationException;
  33 import javax.xml.transform.OutputKeys;
  34 import javax.xml.transform.Transformer;
  35 import javax.xml.transform.TransformerConfigurationException;
  36 import javax.xml.transform.TransformerException;
  37 import javax.xml.transform.TransformerFactory;
  38 import javax.xml.transform.dom.DOMSource;
  39 import javax.xml.transform.stream.StreamResult;
  40 
  41 import org.testng.Assert;
  42 import org.testng.annotations.Test;
  43 import org.w3c.dom.Document;
  44 import org.xml.sax.SAXException;
  45 
  46 /*
  47  * @bug 6794483
  48  * @summary Test JAXP parser can parse xml file using <xi:include> to include another xml, which has an empty element.
  49  */
  50 public class Bug6794483Test {
  51 
  52     @Test
  53     public final void test() {
  54         String xml = getClass().getResource("test1.xml").getPath();
  55         Document doc = parseXmlFile(xml);
  56 
  57         StringWriter sw = new StringWriter();
  58         StreamResult result = new StreamResult(sw);
  59 
  60         TransformerFactory transformerFact = TransformerFactory.newInstance();
  61         transformerFact.setAttribute("indent-number", new Integer(4));
  62         Transformer transformer;
  63 
  64         try {
  65             transformer = transformerFact.newTransformer();
  66             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  67             transformer.setOutputProperty(OutputKeys.METHOD, "xml");
  68             transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");
  69 
  70             // "true" indicate Append content If file exist in system
  71             transformer.transform(new DOMSource(doc), result);
  72             System.out.println("test" + sw);
  73 
  74         } catch (TransformerConfigurationException ex) {
  75             ex.printStackTrace();
  76             Assert.fail("unexpected TransformerConfigurationException");
  77         } catch (TransformerException ex) {
  78             ex.printStackTrace();
  79             Assert.fail("unexpected TransformerException");
  80         }
  81 
  82     }
  83 
  84     public Document parseXmlFile(String fileName) {
  85         System.out.println("Parsing XML file... " + fileName);
  86         DocumentBuilder docBuilder = null;
  87         Document doc = null;
  88         DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
  89         docBuilderFactory.setCoalescing(true);
  90         docBuilderFactory.setXIncludeAware(true);
  91         System.out.println("Include: " + docBuilderFactory.isXIncludeAware());
  92         docBuilderFactory.setNamespaceAware(true);
  93         docBuilderFactory.setExpandEntityReferences(true);
  94 
  95         try {
  96             docBuilder = docBuilderFactory.newDocumentBuilder();
  97         } catch (ParserConfigurationException e) {
  98             e.printStackTrace();
  99         }
 100 
 101         File sourceFile = new File(fileName);
 102         try {
 103             doc = docBuilder.parse(sourceFile);
 104         } catch (SAXException e) {
 105             e.printStackTrace();
 106         } catch (IOException e) {
 107             e.printStackTrace();
 108         }
 109 
 110         System.out.println("XML file parsed");
 111         return doc;
 112 
 113     }
 114 }