1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   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 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      */
 112     public MyContentHandler(String outputFileName) throws SAXException {
 113         try {
 114             bWriter = new BufferedWriter(new FileWriter(outputFileName));
 115         } catch (IOException ex) {
 116             throw new SAXException(ex);
 117         }
 118     }
 119 
 120     /**
 121      * Write characters tag along with content of characters when meet
 122      * characters event.
 123      * @throws IOException error happen when writing file.
 124      */
 125     @Override
 126     public void characters(char[] ch, int start, int length) throws SAXException {
 127         String s = new String(ch, start, length);
 128         println("characters...\n" + s);
 129     }
 130 
 131     /**
 132      * Write endDocument tag then flush the content and close the file when meet
 133      * endDocument event.
 134      * @throws IOException error happen when writing file or closing file.
 135      */
 136     @Override
 137     public void endDocument() throws SAXException {
 138         try {
 139             println("endDocument...");
 140             bWriter.flush();
 141             bWriter.close();
 142         } catch (IOException ex) {
 143             throw new SAXException(WRITE_ERROR, ex);
 144         }
 145     }
 146 
 147     /**
 148      * Write endElement tag with namespaceURI, localName, qName to the file when
 149      * meet endElement event.
 150      * @throws IOException error happen when writing file.
 151      */
 152     @Override
 153     public void endElement(String namespaceURI,String localName,String qName) throws SAXException{
 154         println("endElement...\n" + "namespaceURI: " + namespaceURI +
 155                 " localName: "+ localName + " qName: " + qName);
 156     }
 157 
 158     /**
 159      * Write endPrefixMapping tag along with prefix to the file when meet 
 160      * endPrefixMapping event.
 161      * @throws IOException error happen when writing file.
 162      */
 163     @Override
 164     public void endPrefixMapping(String prefix) throws SAXException {
 165         println("endPrefixMapping...\n" + "prefix: " + prefix);
 166     }
 167 
 168     /**
 169      * Write ignorableWhitespace tag along with white spaces when meet 
 170      * ignorableWhitespace event.
 171      * @throws IOException error happen when writing file.
 172      */
 173     @Override
 174     public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
 175         String s = new String(ch, start, length);
 176         println("ignorableWhitespace...\n" + s +
 177                 " ignorable white space string length: " + s.length());
 178     }
 179 
 180     /**
 181      * Write processingInstruction tag along with target name and target data
 182      * when meet processingInstruction event.
 183      * @throws IOException error happen when writing file.
 184      */
 185     @Override
 186     public void processingInstruction(String target, String data) throws SAXException {
 187         println("processingInstruction...target:" + target +
 188                 " data: " + data);
 189     }
 190 
 191     /**
 192      * Write setDocumentLocator tag when meet setDocumentLocator event. 
 193      */
 194     @Override
 195     public void setDocumentLocator(Locator locator) {
 196         try {
 197             this.locator = locator;
 198             println("setDocumentLocator...");
 199         } catch (SAXException ex) {
 200             System.err.println(WRITE_ERROR + ex);
 201         }
 202     }
 203 
 204     /**
 205      * Write skippedEntity tag along with entity name when meet skippedEntity
 206      * event.
 207      * @throws IOException error happen when writing file.
 208      */
 209     @Override
 210     public void skippedEntity(String name) throws SAXException {
 211         println("skippedEntity...\n" + "name: " + name);
 212     }
 213 
 214     /**
 215      * Write startDocument tag when meet startDocument event.
 216      * @throws IOException error happen when writing file.
 217      */
 218     @Override
 219     public void startDocument() throws SAXException {
 220         println("startDocument...");
 221     }
 222 
 223     /**
 224      * Write startElement tag along with namespaceURI, localName, qName, number
 225      * of attributes and line number when meet startElement event.
 226      * @throws IOException error happen when writing file.
 227      */
 228     @Override
 229     public void startElement(String namespaceURI, String localName,
 230                         String qName, Attributes atts) throws SAXException {
 231         println("startElement...\n" + "namespaceURI: " +  namespaceURI +
 232                 " localName: " + localName +  " qName: " + qName +
 233                 " Number of Attributes: " + atts.getLength() +
 234                 " Line# " + locator.getLineNumber());
 235     }
 236 
 237     /**
 238      * Write startPrefixMapping tag along with prefix and uri when meet 
 239      * startPrefixMapping event.
 240      * @throws IOException error happen when writing file.
 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 }