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 newTransformerhandler() method which takes DOMSource as argument can 57 * be set to XMLReader. 58 */ 59 public class SAXTFactoryTest003 { 60 /** 61 * Unit test for newTransformerhandler(Source). DcoumentBuilderFactory is 62 * namespace awareness, DocumentBuilder parse xslt file as DOMSource. 63 */ 64 @Test 65 public void testcase01() { 66 String outputFile = CLASS_DIR + "saxtf003.out"; 67 String goldFile = GOLDEN_DIR + "saxtf003GF.out"; 68 String xsltFile = XML_DIR + "cities.xsl"; 69 String xmlFile = XML_DIR + "cities.xml"; 70 71 try (FileOutputStream fos = new FileOutputStream(outputFile)) { 72 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 73 dbf.setNamespaceAware(true); 74 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 75 Document document = docBuilder.parse(new File(xsltFile)); 76 Node node = (Node)document; 77 DOMSource domSource= new DOMSource(node); 78 79 XMLReader reader = XMLReaderFactory.createXMLReader(); 80 SAXTransformerFactory saxTFactory 81 = (SAXTransformerFactory)TransformerFactory.newInstance(); 82 TransformerHandler handler = 83 saxTFactory.newTransformerHandler(domSource); 84 Result result = new StreamResult(fos); 85 handler.setResult(result); 86 reader.setContentHandler(handler); 87 reader.parse(xmlFile); 88 assertTrue(compareWithGold(goldFile, outputFile)); 89 } catch (TransformerConfigurationException | ParserConfigurationException 90 | SAXException | IOException ex) { 91 failUnexpected(ex); 92 } finally { 93 try { 94 Path outputPath = Paths.get(outputFile); 95 if(Files.exists(outputPath)) 96 Files.delete(outputPath); 97 } catch (IOException ex) { 98 failCleanup(ex, outputFile); 99 } 100 } 101 } 102 } | 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 static jaxp.library.JAXPTestUtilities.compareWithGold; 43 import static org.testng.Assert.assertTrue; 44 import org.testng.annotations.Test; 45 import org.w3c.dom.Document; 46 import org.w3c.dom.Node; 47 import org.xml.sax.SAXException; 48 import org.xml.sax.XMLReader; 49 import org.xml.sax.helpers.XMLReaderFactory; 50 51 /** 52 * Test newTransformerhandler() method which takes DOMSource as argument can 53 * be set to XMLReader. 54 */ 55 public class SAXTFactoryTest003 extends JAXPFileBaseTest { 56 /** 57 * Unit test for newTransformerhandler(Source). DcoumentBuilderFactory is 58 * namespace awareness, DocumentBuilder parse xslt file as DOMSource. 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 + "saxtf003.out"; 73 String goldFile = GOLDEN_DIR + "saxtf003GF.out"; 74 String xsltFile = XML_DIR + "cities.xsl"; 75 String xmlFile = XML_DIR + "cities.xml"; 76 77 try (FileOutputStream fos = new FileOutputStream(outputFile)) { 78 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 79 dbf.setNamespaceAware(true); 80 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 81 Document document = docBuilder.parse(new File(xsltFile)); 82 Node node = (Node)document; 83 DOMSource domSource= new DOMSource(node); 84 85 XMLReader reader = XMLReaderFactory.createXMLReader(); 86 SAXTransformerFactory saxTFactory 87 = (SAXTransformerFactory)TransformerFactory.newInstance(); 88 TransformerHandler handler = 89 saxTFactory.newTransformerHandler(domSource); 90 Result result = new StreamResult(fos); 91 handler.setResult(result); 92 reader.setContentHandler(handler); 93 reader.parse(xmlFile); 94 } 95 assertTrue(compareWithGold(goldFile, outputFile)); 96 } 97 } |