< prev index next >

test/javax/xml/jaxp/functional/org/xml/sax/ptests/ContentHandlerTest.java

Print this page




   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 org.xml.sax.ptests;
  24 
  25 import java.io.BufferedWriter;
  26 import java.io.FileInputStream;
  27 import java.io.FileWriter;
  28 import java.io.IOException;
  29 import java.nio.file.Files;
  30 import java.nio.file.Path;
  31 import java.nio.file.Paths;
  32 import javax.xml.parsers.ParserConfigurationException;
  33 import javax.xml.parsers.SAXParserFactory;

  34 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  35 import static jaxp.library.JAXPTestUtilities.failCleanup;
  36 import static jaxp.library.JAXPTestUtilities.failUnexpected;
  37 import static org.testng.Assert.assertTrue;
  38 import org.testng.annotations.Test;
  39 import org.xml.sax.Attributes;
  40 import org.xml.sax.ContentHandler;
  41 import org.xml.sax.InputSource;
  42 import org.xml.sax.Locator;
  43 import org.xml.sax.SAXException;
  44 import org.xml.sax.XMLReader;
  45 import org.xml.sax.helpers.XMLFilterImpl;
  46 import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR;
  47 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
  48 import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
  49 
  50 /**
  51  * Class registers a content event handler to XMLReader. Content event handler
  52  * transverses XML and print all visited node  when XMLreader parses XML. Test
  53  * verifies output is same as the golden file.
  54  */
  55 public class ContentHandlerTest {
  56     /**
  57      * Content event handler visit all nodes to print to output file.







  58      */
  59     @Test
  60     public void testcase01() {
  61         String outputFile = CLASS_DIR + "Content.out";
  62         String goldFile = GOLDEN_DIR + "ContentGF.out";
  63         String xmlFile = XML_DIR + "namespace1.xml";
  64 
  65         try(FileInputStream instream = new FileInputStream(xmlFile)) {

  66             SAXParserFactory spf = SAXParserFactory.newInstance();
  67             spf.setNamespaceAware(true);
  68             XMLReader xmlReader = spf.newSAXParser().getXMLReader();
  69             ContentHandler cHandler = new MyContentHandler(outputFile);
  70             xmlReader.setContentHandler(cHandler);
  71             InputSource is = new InputSource(instream);
  72             xmlReader.parse(is);
  73             assertTrue(compareWithGold(goldFile, outputFile));
  74         } catch( IOException | SAXException | ParserConfigurationException ex) {
  75             failUnexpected(ex);
  76         } finally {
  77             try {
  78                 Path outputPath = Paths.get(outputFile);
  79                 if(Files.exists(outputPath))
  80                     Files.delete(outputPath);
  81             } catch (IOException ex) {
  82                 failCleanup(ex, outputFile);
  83             }
  84         }

  85     }
  86 }
  87 
  88 /**
  89  * A content write out handler.
  90  */
  91 class MyContentHandler extends XMLFilterImpl {
  92     /**
  93      * Prefix to every exception.
  94      */
  95     private final static String WRITE_ERROR = "bWriter error";
  96 
  97     /**
  98      * FileWriter to write string to output file.
  99      */
 100     private final BufferedWriter bWriter;
 101 
 102     /**
 103      * Default document locator.
 104      */
 105     private Locator locator;
 106 
 107     /**
 108      * Initiate FileWriter when construct a MyContentHandler.
 109      * @param outputFileName output file name.
 110      * @throws SAXException creation of FileWriter failed.
 111      */


 241      */
 242     @Override
 243     public void startPrefixMapping(String prefix, String uri) throws SAXException {
 244         println("startPrefixMapping...\n" + "prefix: " + prefix +
 245                 " uri: " + uri);
 246     }
 247 
 248     /**
 249      * Write outString to file.
 250      * @param outString String to be written to File
 251      * @throws SAXException if write file failed
 252      */
 253     private void println(String outString) throws SAXException {
 254         try {
 255             bWriter.write( outString, 0, outString.length());
 256             bWriter.newLine();
 257         } catch (IOException ex) {
 258             throw new SAXException(WRITE_ERROR, ex);
 259         }
 260     }










 261 }


   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 org.xml.sax.ptests;
  24 
  25 import java.io.BufferedWriter;
  26 import java.io.FileInputStream;
  27 import java.io.FileWriter;
  28 import java.io.IOException;



  29 import javax.xml.parsers.ParserConfigurationException;
  30 import javax.xml.parsers.SAXParserFactory;
  31 import jaxp.library.JAXPFileBaseTest;
  32 import static jaxp.library.JAXPTestUtilities.compareWithGold;


  33 import static org.testng.Assert.assertTrue;
  34 import org.testng.annotations.Test;
  35 import org.xml.sax.Attributes;

  36 import org.xml.sax.InputSource;
  37 import org.xml.sax.Locator;
  38 import org.xml.sax.SAXException;
  39 import org.xml.sax.XMLReader;
  40 import org.xml.sax.helpers.XMLFilterImpl;
  41 import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR;
  42 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
  43 import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
  44 
  45 /**
  46  * Class registers a content event handler to XMLReader. Content event handler
  47  * transverses XML and print all visited node  when XMLreader parses XML. Test
  48  * verifies output is same as the golden file.
  49  */
  50 public class ContentHandlerTest extends JAXPFileBaseTest {
  51     /**
  52      * Content event handler visit all nodes to print to output file.
  53      * 
  54      * @throws SAXException If any parse errors occur.
  55      * @throws IOException if the file exists but is a directory rather than
  56      *         a regular file, does not exist but cannot be created, or cannot 
  57      *         be opened for any other reason.
  58      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
  59      *         created which satisfies the configuration requested.
  60      */
  61     @Test
  62     public void testcase01() throws IOException, SAXException, ParserConfigurationException {
  63         String outputFile = CLASS_DIR + "Content.out";
  64         String goldFile = GOLDEN_DIR + "ContentGF.out";
  65         String xmlFile = XML_DIR + "namespace1.xml";
  66 
  67         try(FileInputStream instream = new FileInputStream(xmlFile);
  68                 MyContentHandler cHandler = new MyContentHandler(outputFile)) {
  69             SAXParserFactory spf = SAXParserFactory.newInstance();
  70             spf.setNamespaceAware(true);
  71             XMLReader xmlReader = spf.newSAXParser().getXMLReader();

  72             xmlReader.setContentHandler(cHandler);
  73             xmlReader.parse(new InputSource(instream));












  74         }
  75         assertTrue(compareWithGold(goldFile, outputFile));
  76     }
  77 }
  78 
  79 /**
  80  * A content write out handler.
  81  */
  82 class MyContentHandler extends XMLFilterImpl implements AutoCloseable {
  83     /**
  84      * Prefix to every exception.
  85      */
  86     private final static String WRITE_ERROR = "bWriter error";
  87 
  88     /**
  89      * FileWriter to write string to output file.
  90      */
  91     private final BufferedWriter bWriter;
  92 
  93     /**
  94      * Default document locator.
  95      */
  96     private Locator locator;
  97 
  98     /**
  99      * Initiate FileWriter when construct a MyContentHandler.
 100      * @param outputFileName output file name.
 101      * @throws SAXException creation of FileWriter failed.
 102      */


 232      */
 233     @Override
 234     public void startPrefixMapping(String prefix, String uri) throws SAXException {
 235         println("startPrefixMapping...\n" + "prefix: " + prefix +
 236                 " uri: " + uri);
 237     }
 238 
 239     /**
 240      * Write outString to file.
 241      * @param outString String to be written to File
 242      * @throws SAXException if write file failed
 243      */
 244     private void println(String outString) throws SAXException {
 245         try {
 246             bWriter.write( outString, 0, outString.length());
 247             bWriter.newLine();
 248         } catch (IOException ex) {
 249             throw new SAXException(WRITE_ERROR, ex);
 250         }
 251     }
 252 
 253     /**
 254      * Close the writer if it's initiated.
 255      * @throws IOException if any IO error when close buffered writer.
 256      */
 257     @Override
 258     public void close() throws IOException {
 259         if (bWriter != null)
 260             bWriter.close();
 261     }
 262 }
< prev index next >