< prev index next >

test/javax/xml/jaxp/functional/javax/xml/transform/ptests/SAXTFactoryTest011.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.File;
  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.parsers.DocumentBuilder;
  31 import javax.xml.parsers.DocumentBuilderFactory;
  32 import javax.xml.parsers.ParserConfigurationException;
  33 import javax.xml.transform.TransformerConfigurationException;
  34 import javax.xml.transform.TransformerFactory;
  35 import javax.xml.transform.dom.DOMSource;
  36 import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
  37 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
  38 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  39 import javax.xml.transform.sax.SAXTransformerFactory;

  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.testng.annotations.Test;
  45 import org.w3c.dom.Document;
  46 import org.w3c.dom.Node;
  47 import org.xml.sax.InputSource;
  48 import org.xml.sax.SAXException;
  49 import org.xml.sax.XMLFilter;
  50 import org.xml.sax.XMLReader;
  51 import org.xml.sax.helpers.XMLReaderFactory;
  52 
  53 /**
  54  * Test XMLFilter parse InputSource along with customized ContentHandler by
  55  * using SAX parser as it's reader.
  56  */
  57 public class SAXTFactoryTest011 {
  58     /**
  59      * Unit test for contentHandler setter/getter with parent.









  60      */
  61     @Test
  62     public void testcase01() {

  63         String outputFile = CLASS_DIR + "saxtf011.out";
  64         String goldFile = GOLDEN_DIR + "saxtf011GF.out";
  65         String xsltFile = XML_DIR + "cities.xsl";
  66         String xmlFile = XML_DIR + "cities.xml";
  67 
  68         try {
  69             // The transformer will use a SAX parser as it's reader.
  70             XMLReader reader = XMLReaderFactory.createXMLReader();
  71             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  72             dbf.setNamespaceAware(true);
  73             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
  74             Document document = docBuilder.parse(new File(xsltFile));
  75             Node node = (Node)document;
  76             DOMSource domSource= new DOMSource(node);
  77 
  78             SAXTransformerFactory saxTFactory
  79                     = (SAXTransformerFactory)TransformerFactory.newInstance();
  80             XMLFilter filter = saxTFactory.newXMLFilter(domSource);
  81 
  82             filter.setParent(reader);
  83             filter.setContentHandler(new MyContentHandler(outputFile));
  84 
  85             // Now, when you call transformer.parse, it will set itself as
  86             // the content handler for the parser object (it's "parent"), and
  87             // will then call the parse method on the parser.
  88             filter.parse(new InputSource(xmlFile));
  89             assertTrue(compareWithGold(goldFile, outputFile));
  90         } catch (SAXException | IOException | TransformerConfigurationException
  91                 | ParserConfigurationException ex) {
  92             failUnexpected(ex);
  93         } finally {
  94             try {
  95                 Path outputPath = Paths.get(outputFile);
  96                 if(Files.exists(outputPath))
  97                     Files.delete(outputPath);
  98             } catch (IOException ex) {
  99                 failCleanup(ex, outputFile);
 100             }
 101         }
 102     }
 103 }


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



  27 import javax.xml.parsers.DocumentBuilder;
  28 import javax.xml.parsers.DocumentBuilderFactory;
  29 import javax.xml.parsers.ParserConfigurationException;
  30 import javax.xml.transform.TransformerConfigurationException;
  31 import javax.xml.transform.TransformerFactory;
  32 import javax.xml.transform.dom.DOMSource;
  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 jaxp.library.JAXPFileBaseTest;
  38 import static jaxp.library.JAXPTestUtilities.compareWithGold;


  39 import static org.testng.Assert.assertTrue;
  40 import org.testng.annotations.Test;
  41 import org.w3c.dom.Document;
  42 import org.w3c.dom.Node;
  43 import org.xml.sax.InputSource;
  44 import org.xml.sax.SAXException;
  45 import org.xml.sax.XMLFilter;
  46 import org.xml.sax.XMLReader;
  47 import org.xml.sax.helpers.XMLReaderFactory;
  48 
  49 /**
  50  * Test XMLFilter parse InputSource along with customized ContentHandler by
  51  * using SAX parser as it's reader.
  52  */
  53 public class SAXTFactoryTest011 extends JAXPFileBaseTest {
  54     /**
  55      * Unit test for contentHandler setter/getter with parent.
  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      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
  64      *         created which satisfies the configuration requested.
  65      */
  66     @Test
  67     public void testcase01() throws SAXException, ParserConfigurationException, 
  68             IOException, TransformerConfigurationException {
  69         String outputFile = CLASS_DIR + "saxtf011.out";
  70         String goldFile = GOLDEN_DIR + "saxtf011GF.out";
  71         String xsltFile = XML_DIR + "cities.xsl";
  72         String xmlFile = XML_DIR + "cities.xml";


  73         // The transformer will use a SAX parser as it's reader.
  74         XMLReader reader = XMLReaderFactory.createXMLReader();
  75         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  76         dbf.setNamespaceAware(true);
  77         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
  78         Document document = docBuilder.parse(new File(xsltFile));
  79         Node node = (Node)document;
  80         DOMSource domSource= new DOMSource(node);
  81 
  82         SAXTransformerFactory saxTFactory
  83                 = (SAXTransformerFactory)TransformerFactory.newInstance();
  84         XMLFilter filter = saxTFactory.newXMLFilter(domSource);
  85 
  86         filter.setParent(reader);
  87         filter.setContentHandler(new MyContentHandler(outputFile));
  88 
  89         // Now, when you call transformer.parse, it will set itself as
  90         // the content handler for the parser object (it's "parent"), and
  91         // will then call the parse method on the parser.
  92         filter.parse(new InputSource(xmlFile));
  93         assertTrue(compareWithGold(goldFile, outputFile));












  94     }
  95 }
< prev index next >