1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 // Common Qetest / Xalan testing imports 19 import org.apache.qetest.Datalet; 20 import org.apache.qetest.Logger; 21 import org.apache.qetest.TestletImpl; 22 23 import org.xml.sax.InputSource; 24 import org.xml.sax.SAXException; 25 import org.xml.sax.XMLReader; 26 27 import javax.xml.parsers.ParserConfigurationException; 28 import javax.xml.parsers.SAXParserFactory; 29 import javax.xml.transform.TransformerConfigurationException; 30 import javax.xml.transform.TransformerException; 31 import javax.xml.transform.TransformerFactory; 32 import javax.xml.transform.sax.SAXTransformerFactory; 33 import javax.xml.transform.sax.TemplatesHandler; 34 import javax.xml.transform.stream.StreamResult; 35 import javax.xml.transform.stream.StreamSource; 36 import java.io.FileNotFoundException; 37 import java.io.FileOutputStream; 38 import java.io.IOException; 39 import java.util.Properties; 40 41 42 /** 43 * Testlet for reproducing Bugzilla reported bugs. 44 * 45 * @author [email protected] 46 * @author [email protected] 47 */ 48 public class Bugzilla6312 extends TestletImpl 49 { 50 // Initialize our classname for TestletImpl's main() method - must be updated! 51 static { thisClassName = "Bugzilla6312"; } 52 53 /** 54 * Write Minimal code to reproduce your Bugzilla bug report. 55 * Many Bugzilla tests won't bother with a datalet; they'll 56 * just have the data to reproduce the bug encoded by default. 57 * @param d (optional) Datalet to use as data point for the test. 58 */ execute(Datalet d)59 public void execute(Datalet d) 60 { 61 // Use logger.logMsg(...) instead of System.out.println(...) 62 logger.logMsg(Logger.STATUSMSG, "Reproducing Bugzilla#6312"); 63 64 // Set the TransformerFactory system property to generate and use a translet. 65 // Note: To make this sample more flexible, load properties from a properties file. 66 // The setting for the Xalan Transformer is "org.apache.xalan.processor.TransformerFactoryImpl" 67 String key = "javax.xml.transform.TransformerFactory"; 68 String value = "org.apache.xalan.xsltc.trax.TransformerFactoryImpl"; 69 Properties props = System.getProperties(); 70 props.put(key, value); 71 System.setProperties(props); 72 73 String xslInURI = "Bugzilla6312.xsl"; 74 String xmlInURI = "Bugzilla6312.xml"; 75 String htmlOutURI = "Bugzilla6312.output"; 76 try 77 { 78 // Instantiate the TransformerFactory, and use it along with a SteamSource 79 // XSL stylesheet to create a Transformer. 80 SAXTransformerFactory tFactory = (SAXTransformerFactory)TransformerFactory.newInstance(); 81 82 //Transformer transformer = tFactory.newTransformer(new StreamSource(xslInURI)); 83 TemplatesHandler templatesHandler = tFactory.newTemplatesHandler(); 84 SAXParserFactory sFactory = SAXParserFactory.newInstance(); 85 sFactory.setNamespaceAware(true); 86 XMLReader reader = sFactory.newSAXParser().getXMLReader(); 87 reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); 88 reader.setContentHandler(templatesHandler); 89 reader.parse(new InputSource(xslInURI)); 90 91 // Perform the transformation from a StreamSource to a StreamResult; 92 templatesHandler.getTemplates().newTransformer().transform(new StreamSource(xmlInURI), 93 new StreamResult(new FileOutputStream(htmlOutURI))); 94 95 logger.logMsg(logger.STATUSMSG, "Successfully created " + htmlOutURI); 96 logger.checkPass("Crash test: didn't throw exception (note: output file contents not validated"); 97 } 98 catch (Throwable t) 99 { 100 logger.logThrowable(logger.ERRORMSG, t, "Unexpected exception"); 101 logger.checkErr("Unexpected exception: " + t.toString()); 102 } 103 104 } 105 106 /** 107 * <a href="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=6312"> 108 * Link to Bugzilla report</a> 109 * @return Problems with using JAXPTransletOneTransformation if we use XMLReader/TemplatesHandler. 110 */ getDescription()111 public String getDescription() 112 { 113 return "Problems with using JAXPTransletOneTransformation if we use XMLReader/TemplatesHandler"; 114 } 115 116 } // end of class Bugzilla6312 117 118