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