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.transform.ptests; 25 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.transform.Result; 32 import javax.xml.transform.TransformerConfigurationException; 33 import javax.xml.transform.TransformerFactory; 34 import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR; 35 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR; 36 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; 37 import javax.xml.transform.sax.SAXTransformerFactory; 38 import javax.xml.transform.sax.TransformerHandler; 39 import javax.xml.transform.stream.StreamResult; 40 import javax.xml.transform.stream.StreamSource; 41 import static jaxp.library.JAXPTestUtilities.compareWithGold; 42 import static jaxp.library.JAXPTestUtilities.failCleanup; 43 import static jaxp.library.JAXPTestUtilities.failUnexpected; 44 import static org.testng.Assert.assertTrue; 45 import org.testng.annotations.Test; 46 import org.xml.sax.SAXException; 47 import org.xml.sax.XMLReader; 48 import org.xml.sax.helpers.XMLReaderFactory; 49 50 /** 51 * Test newTransformerhandler() method which takes StreamSource as argument can 52 * be set to XMLReader. 53 */ 54 public class SAXTFactoryTest001 { 55 /** 56 * SAXTFactory.newTransformerhandler() method which takes SAXSource as 57 * argument can be set to XMLReader. SAXSource has input XML file as its 58 * input source. XMLReader has a transformer handler which write out the 59 * result to output file. Test verifies output file is same as golden file. 60 */ 61 @Test 62 public void testcase01() { 63 String outputFile = CLASS_DIR + "saxtf001.out"; 64 String goldFile = GOLDEN_DIR + "saxtf001GF.out"; 65 String xsltFile = XML_DIR + "cities.xsl"; 66 String xmlFile = XML_DIR + "cities.xml"; 67 68 try (FileOutputStream fos = new FileOutputStream(outputFile)) { 69 XMLReader reader = XMLReaderFactory.createXMLReader(); 70 SAXTransformerFactory saxTFactory 71 = (SAXTransformerFactory) TransformerFactory.newInstance(); 72 TransformerHandler handler = saxTFactory.newTransformerHandler( 73 new StreamSource(xsltFile)); 74 Result result = new StreamResult(fos); 75 handler.setResult(result); 76 reader.setContentHandler(handler); 77 reader.parse(xmlFile); 78 assertTrue(compareWithGold(goldFile, outputFile)); 79 } catch (SAXException | TransformerConfigurationException | IOException ex) { 80 failUnexpected(ex); 81 } finally { 82 try { 83 Path outputPath = Paths.get(outputFile); 84 if(Files.exists(outputPath)) 85 Files.delete(outputPath); 86 } catch (IOException ex) { 87 failCleanup(ex, outputFile); 88 } 89 } 90 } 91 } | 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.transform.ptests; 25 26 import java.io.FileOutputStream; 27 import java.io.IOException; 28 import javax.xml.transform.Result; 29 import javax.xml.transform.TransformerConfigurationException; 30 import javax.xml.transform.TransformerFactory; 31 import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR; 32 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR; 33 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; 34 import javax.xml.transform.sax.SAXTransformerFactory; 35 import javax.xml.transform.sax.TransformerHandler; 36 import javax.xml.transform.stream.StreamResult; 37 import javax.xml.transform.stream.StreamSource; 38 import jaxp.library.JAXPFileBaseTest; 39 import static jaxp.library.JAXPTestUtilities.compareWithGold; 40 import static org.testng.Assert.assertTrue; 41 import org.testng.annotations.Test; 42 import org.xml.sax.SAXException; 43 import org.xml.sax.XMLReader; 44 import org.xml.sax.helpers.XMLReaderFactory; 45 46 /** 47 * Test newTransformerhandler() method which takes StreamSource as argument can 48 * be set to XMLReader. 49 */ 50 public class SAXTFactoryTest001 extends JAXPFileBaseTest { 51 /** 52 * SAXTFactory.newTransformerhandler() method which takes SAXSource as 53 * argument can be set to XMLReader. SAXSource has input XML file as its 54 * input source. XMLReader has a transformer handler which write out the 55 * result to output file. Test verifies output file is same as golden file. 56 * 57 * @throws SAXException If any parse errors occur. 58 * @throws IOException if the file exists but is a directory rather than 59 * a regular file, does not exist but cannot be created, or cannot 60 * be opened for any other reason. 61 * @throws TransformerConfigurationException If for some reason the 62 * TransformerHandler can not be created. 63 */ 64 @Test 65 public void testcase01() throws IOException, SAXException, 66 TransformerConfigurationException { 67 String outputFile = CLASS_DIR + "saxtf001.out"; 68 String goldFile = GOLDEN_DIR + "saxtf001GF.out"; 69 String xsltFile = XML_DIR + "cities.xsl"; 70 String xmlFile = XML_DIR + "cities.xml"; 71 72 try (FileOutputStream fos = new FileOutputStream(outputFile)) { 73 XMLReader reader = XMLReaderFactory.createXMLReader(); 74 SAXTransformerFactory saxTFactory 75 = (SAXTransformerFactory) TransformerFactory.newInstance(); 76 TransformerHandler handler = saxTFactory.newTransformerHandler( 77 new StreamSource(xsltFile)); 78 Result result = new StreamResult(fos); 79 handler.setResult(result); 80 reader.setContentHandler(handler); 81 reader.parse(xmlFile); 82 } 83 assertTrue(compareWithGold(goldFile, outputFile)); 84 } 85 } |