< prev index next >

test/javax/xml/jaxp/functional/javax/xml/transform/ptests/SAXTFactoryTest012.java

Print this page




   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.FileInputStream;
  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.TransformerConfigurationException;
  31 import javax.xml.transform.TransformerFactory;
  32 import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
  33 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
  34 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  35 import javax.xml.transform.sax.SAXSource;
  36 import javax.xml.transform.sax.SAXTransformerFactory;

  37 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  38 import static jaxp.library.JAXPTestUtilities.failCleanup;
  39 import static jaxp.library.JAXPTestUtilities.failUnexpected;
  40 import static org.testng.Assert.assertTrue;
  41 import org.testng.annotations.Test;
  42 import org.xml.sax.InputSource;
  43 import org.xml.sax.SAXException;
  44 import org.xml.sax.XMLFilter;
  45 import org.xml.sax.XMLReader;
  46 import org.xml.sax.helpers.XMLReaderFactory;
  47 
  48 /**
  49  * Test XMLFilter parse InputSource along with customized ContentHandler by
  50  * using SAX parser as it's reader.
  51  */
  52 public class SAXTFactoryTest012 {
  53     /**
  54      * Unit test for contentHandler setter/getter.







  55      */
  56     @Test
  57     public void testcase01() {

  58         String outputFile = CLASS_DIR + "saxtf012.out";
  59         String goldFile = GOLDEN_DIR + "saxtf012GF.out";
  60         String xsltFile = XML_DIR + "cities.xsl";
  61         String xmlFile = XML_DIR + "cities.xml";
  62         try {
  63             // The transformer will use a SAX parser as it's reader.
  64             XMLReader reader = XMLReaderFactory.createXMLReader();
  65 
  66             InputSource is = new InputSource(new FileInputStream(xsltFile));
  67             SAXSource saxSource = new SAXSource();
  68             saxSource.setInputSource(is);
  69 
  70             SAXTransformerFactory saxTFactory = (SAXTransformerFactory)TransformerFactory.newInstance();
  71             XMLFilter filter = saxTFactory.newXMLFilter(saxSource);
  72 
  73             filter.setParent(reader);
  74             filter.setContentHandler(new MyContentHandler(outputFile));
  75 
  76             // Now, when you call transformer.parse, it will set itself as
  77             // the content handler for the parser object (it's "parent"), and
  78             // will then call the parse method on the parser.
  79             filter.parse(new InputSource(xmlFile));
  80             assertTrue(compareWithGold(goldFile, outputFile));
  81         } catch (SAXException | IOException | TransformerConfigurationException ex) {
  82             failUnexpected(ex);
  83         } finally {
  84             try {
  85                 Path outputPath = Paths.get(outputFile);
  86                 if(Files.exists(outputPath))
  87                     Files.delete(outputPath);
  88             } catch (IOException ex) {
  89                 failCleanup(ex, outputFile);
  90             }
  91         }
  92     }
  93 }


   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.FileInputStream;
  26 import java.io.IOException;



  27 import javax.xml.transform.TransformerConfigurationException;
  28 import javax.xml.transform.TransformerFactory;
  29 import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
  30 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
  31 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  32 import javax.xml.transform.sax.SAXSource;
  33 import javax.xml.transform.sax.SAXTransformerFactory;
  34 import jaxp.library.JAXPFileBaseTest;
  35 import static jaxp.library.JAXPTestUtilities.compareWithGold;


  36 import static org.testng.Assert.assertTrue;
  37 import org.testng.annotations.Test;
  38 import org.xml.sax.InputSource;
  39 import org.xml.sax.SAXException;
  40 import org.xml.sax.XMLFilter;
  41 import org.xml.sax.XMLReader;
  42 import org.xml.sax.helpers.XMLReaderFactory;
  43 
  44 /**
  45  * Test XMLFilter parse InputSource along with customized ContentHandler by
  46  * using SAX parser as it's reader.
  47  */
  48 public class SAXTFactoryTest012 extends JAXPFileBaseTest {
  49     /**
  50      * Unit test for contentHandler setter/getter.
  51      * 
  52      * @throws SAXException If any parse errors occur.
  53      * @throws IOException if the file exists but is a directory rather than
  54      *         a regular file, does not exist but cannot be created, or cannot 
  55      *         be opened for any other reason.
  56      * @throws TransformerConfigurationException If for some reason the
  57      *         TransformerHandler can not be created.
  58      */
  59     @Test
  60     public void testcase01() throws SAXException, IOException, 
  61             TransformerConfigurationException {
  62         String outputFile = CLASS_DIR + "saxtf012.out";
  63         String goldFile = GOLDEN_DIR + "saxtf012GF.out";
  64         String xsltFile = XML_DIR + "cities.xsl";
  65         String xmlFile = XML_DIR + "cities.xml";

  66         // The transformer will use a SAX parser as it's reader.
  67         XMLReader reader = XMLReaderFactory.createXMLReader();
  68 
  69         InputSource is = new InputSource(new FileInputStream(xsltFile));
  70         SAXSource saxSource = new SAXSource();
  71         saxSource.setInputSource(is);
  72 
  73         SAXTransformerFactory saxTFactory = (SAXTransformerFactory)TransformerFactory.newInstance();
  74         XMLFilter filter = saxTFactory.newXMLFilter(saxSource);
  75 
  76         filter.setParent(reader);
  77         filter.setContentHandler(new MyContentHandler(outputFile));
  78 
  79         // Now, when you call transformer.parse, it will set itself as
  80         // the content handler for the parser object (it's "parent"), and
  81         // will then call the parse method on the parser.
  82         filter.parse(new InputSource(xmlFile));
  83         assertTrue(compareWithGold(goldFile, outputFile));











  84     }
  85 }
< prev index next >