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 package javax.xml.transform.ptests; 24 25 import java.io.File; 26 import java.io.FileOutputStream; 27 import java.io.IOException; 28 import java.nio.file.Files; 29 import java.nio.file.Path; 30 import java.nio.file.Paths; 31 import javax.xml.parsers.DocumentBuilder; 32 import javax.xml.parsers.DocumentBuilderFactory; 33 import javax.xml.parsers.ParserConfigurationException; 34 import javax.xml.transform.Result; 35 import javax.xml.transform.TransformerConfigurationException; 36 import javax.xml.transform.TransformerFactory; 37 import javax.xml.transform.dom.DOMSource; 38 import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR; 39 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR; 40 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; 41 import javax.xml.transform.sax.SAXTransformerFactory; 42 import javax.xml.transform.sax.TransformerHandler; 43 import javax.xml.transform.stream.StreamResult; 44 import static jaxp.library.JAXPTestUtilities.compareWithGold; 45 import static jaxp.library.JAXPTestUtilities.failCleanup; 46 import static jaxp.library.JAXPTestUtilities.failUnexpected; 47 import static org.testng.Assert.assertTrue; 48 import org.testng.annotations.Test; 49 import org.w3c.dom.Document; 50 import org.w3c.dom.Node; 51 import org.xml.sax.SAXException; 52 import org.xml.sax.XMLReader; 53 import org.xml.sax.helpers.XMLReaderFactory; 54 55 /** 56 * Test SAXSource API when relative URI is used in xsl file and SystemId was set 57 */ 58 public class SAXTFactoryTest005 { 59 /** 60 * Unit test for XMLReader parsing when relative URI is used in xsl file and 61 * SystemId was set. 62 */ 63 @Test 64 public void testcase01() { 65 String outputFile = CLASS_DIR + "saxtf005.out"; 66 String goldFile = GOLDEN_DIR + "saxtf005GF.out"; 67 String xsltFile = XML_DIR + "citiesinclude.xsl"; 68 String xmlFile = XML_DIR + "cities.xml"; 69 70 try (FileOutputStream fos = new FileOutputStream(outputFile)) { 71 XMLReader reader = XMLReaderFactory.createXMLReader(); 72 SAXTransformerFactory saxTFactory 73 = (SAXTransformerFactory)TransformerFactory.newInstance(); 74 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 75 dbf.setNamespaceAware(true); 76 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 77 Document document = docBuilder.parse(new File(xsltFile)); 78 Node node = (Node)document; 79 DOMSource domSource= new DOMSource(node); 80 81 domSource.setSystemId("file:///" + XML_DIR); 82 83 TransformerHandler handler = 84 saxTFactory.newTransformerHandler(domSource); 85 Result result = new StreamResult(fos); 86 87 handler.setResult(result); 88 reader.setContentHandler(handler); 89 reader.parse(xmlFile); 90 assertTrue(compareWithGold(goldFile, outputFile)); 91 } catch (TransformerConfigurationException | ParserConfigurationException 92 | SAXException | IOException ex) { 93 failUnexpected(ex); 94 } finally { 95 try { 96 Path outputPath = Paths.get(outputFile); 97 if(Files.exists(outputPath)) 98 Files.delete(outputPath); 99 } catch (IOException ex) { 100 failCleanup(ex, outputFile); 101 } 102 } 103 } 104 } | 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 package javax.xml.transform.ptests; 24 25 import java.io.File; 26 import java.io.FileOutputStream; 27 import java.io.IOException; 28 import javax.xml.parsers.DocumentBuilder; 29 import javax.xml.parsers.DocumentBuilderFactory; 30 import javax.xml.parsers.ParserConfigurationException; 31 import javax.xml.transform.Result; 32 import javax.xml.transform.TransformerConfigurationException; 33 import javax.xml.transform.TransformerFactory; 34 import javax.xml.transform.dom.DOMSource; 35 import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR; 36 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR; 37 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; 38 import javax.xml.transform.sax.SAXTransformerFactory; 39 import javax.xml.transform.sax.TransformerHandler; 40 import javax.xml.transform.stream.StreamResult; 41 import jaxp.library.JAXPFileBaseTest; 42 import jaxp.library.JAXPFileBaseTest; 43 import static jaxp.library.JAXPTestUtilities.compareWithGold; 44 import static org.testng.Assert.assertTrue; 45 import org.testng.annotations.Test; 46 import org.w3c.dom.Document; 47 import org.w3c.dom.Node; 48 import org.xml.sax.SAXException; 49 import org.xml.sax.XMLReader; 50 import org.xml.sax.helpers.XMLReaderFactory; 51 52 /** 53 * Test SAXSource API when relative URI is used in xsl file and SystemId was set 54 */ 55 public class SAXTFactoryTest005 extends JAXPFileBaseTest { 56 /** 57 * Unit test for XMLReader parsing when relative URI is used in xsl file and 58 * SystemId was set. 59 * 60 * @throws SAXException If any parse errors occur. 61 * @throws IOException if the file exists but is a directory rather than 62 * a regular file, does not exist but cannot be created, or cannot 63 * be opened for any other reason. 64 * @throws TransformerConfigurationException If for some reason the 65 * TransformerHandler can not be created. 66 * @throws ParserConfigurationException if a DocumentBuilder cannot be 67 * created which satisfies the configuration requested. 68 */ 69 @Test 70 public void testcase01() throws IOException, SAXException, 71 TransformerConfigurationException, ParserConfigurationException { 72 String outputFile = CLASS_DIR + "saxtf005.out"; 73 String goldFile = GOLDEN_DIR + "saxtf005GF.out"; 74 String xsltFile = XML_DIR + "citiesinclude.xsl"; 75 String xmlFile = XML_DIR + "cities.xml"; 76 77 try (FileOutputStream fos = new FileOutputStream(outputFile)) { 78 XMLReader reader = XMLReaderFactory.createXMLReader(); 79 SAXTransformerFactory saxTFactory 80 = (SAXTransformerFactory)TransformerFactory.newInstance(); 81 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 82 dbf.setNamespaceAware(true); 83 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 84 Document document = docBuilder.parse(new File(xsltFile)); 85 Node node = (Node)document; 86 DOMSource domSource= new DOMSource(node); 87 88 domSource.setSystemId("file:///" + XML_DIR); 89 90 TransformerHandler handler = 91 saxTFactory.newTransformerHandler(domSource); 92 Result result = new StreamResult(fos); 93 94 handler.setResult(result); 95 reader.setContentHandler(handler); 96 reader.parse(xmlFile); 97 } 98 assertTrue(compareWithGold(goldFile, outputFile)); 99 } 100 } |