1 /*
   2  * Copyright (c) 2003, 2016, 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 static jaxp.library.JAXPTestUtilities.USER_DIR;
  26 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  27 import static org.testng.Assert.assertTrue;
  28 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
  29 import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
  30 
  31 import java.io.BufferedWriter;
  32 import java.io.FileInputStream;
  33 import java.io.FileWriter;
  34 import java.io.IOException;
  35 
  36 import javax.xml.parsers.SAXParserFactory;
  37 
  38 import org.testng.annotations.Listeners;
  39 import org.testng.annotations.Test;
  40 import org.xml.sax.Attributes;
  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 
  47 /**
  48  * Class registers a content event handler to XMLReader. Content event handler
  49  * transverses XML and print all visited node  when XMLreader parses XML. Test
  50  * verifies output is same as the golden file.
  51  */
  52 @Listeners({jaxp.library.FilePolicy.class})
  53 public class ContentHandlerTest {
  54     /**
  55      * Content event handler visit all nodes to print to output file.
  56      *
  57      * @throws Exception If any errors occur.
  58      */
  59     @Test
  60     public void testcase01() throws Exception {
  61         String outputFile = USER_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                 MyContentHandler cHandler = new MyContentHandler(outputFile)) {
  67             SAXParserFactory spf = SAXParserFactory.newInstance();
  68             spf.setNamespaceAware(true);
  69             XMLReader xmlReader = spf.newSAXParser().getXMLReader();
  70             xmlReader.setContentHandler(cHandler);
  71             xmlReader.parse(new InputSource(instream));
  72         }
  73         assertTrue(compareWithGold(goldFile, outputFile));
  74     }
  75 }
  76 
  77 /**
  78  * A content write out handler.
  79  */
  80 class MyContentHandler extends XMLFilterImpl implements AutoCloseable {
  81     /**
  82      * Prefix to every exception.
  83      */
  84     private final static String WRITE_ERROR = "bWriter error";
  85 
  86     /**
  87      * FileWriter to write string to output file.
  88      */
  89     private final BufferedWriter bWriter;
  90 
  91     /**
  92      * Default document locator.
  93      */
  94     private Locator locator;
  95 
  96     /**
  97      * Initiate FileWriter when construct a MyContentHandler.
  98      * @param outputFileName output file name.
  99      * @throws SAXException creation of FileWriter failed.
 100      */
 101     public MyContentHandler(String outputFileName) throws SAXException {
 102         try {
 103             bWriter = new BufferedWriter(new FileWriter(outputFileName));
 104         } catch (IOException ex) {
 105             throw new SAXException(ex);
 106         }
 107     }
 108 
 109     /**
 110      * Write characters tag along with content of characters when meet
 111      * characters event.
 112      * @throws IOException error happen when writing file.
 113      */
 114     @Override
 115     public void characters(char[] ch, int start, int length) throws SAXException {
 116         String s = new String(ch, start, length);
 117         println("characters...\n" + s);
 118     }
 119 
 120     /**
 121      * Write endDocument tag then flush the content and close the file when meet
 122      * endDocument event.
 123      * @throws IOException error happen when writing file or closing file.
 124      */
 125     @Override
 126     public void endDocument() throws SAXException {
 127         try {
 128             println("endDocument...");
 129             bWriter.flush();
 130             bWriter.close();
 131         } catch (IOException ex) {
 132             throw new SAXException(WRITE_ERROR, ex);
 133         }
 134     }
 135 
 136     /**
 137      * Write endElement tag with namespaceURI, localName, qName to the file when
 138      * meet endElement event.
 139      * @throws IOException error happen when writing file.
 140      */
 141     @Override
 142     public void endElement(String namespaceURI,String localName,String qName) throws SAXException{
 143         println("endElement...\n" + "namespaceURI: " + namespaceURI +
 144                 " localName: "+ localName + " qName: " + qName);
 145     }
 146 
 147     /**
 148      * Write endPrefixMapping tag along with prefix to the file when meet
 149      * endPrefixMapping event.
 150      * @throws IOException error happen when writing file.
 151      */
 152     @Override
 153     public void endPrefixMapping(String prefix) throws SAXException {
 154         println("endPrefixMapping...\n" + "prefix: " + prefix);
 155     }
 156 
 157     /**
 158      * Write ignorableWhitespace tag along with white spaces when meet
 159      * ignorableWhitespace event.
 160      * @throws IOException error happen when writing file.
 161      */
 162     @Override
 163     public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
 164         String s = new String(ch, start, length);
 165         println("ignorableWhitespace...\n" + s +
 166                 " ignorable white space string length: " + s.length());
 167     }
 168 
 169     /**
 170      * Write processingInstruction tag along with target name and target data
 171      * when meet processingInstruction event.
 172      * @throws IOException error happen when writing file.
 173      */
 174     @Override
 175     public void processingInstruction(String target, String data) throws SAXException {
 176         println("processingInstruction...target:" + target +
 177                 " data: " + data);
 178     }
 179 
 180     /**
 181      * Write setDocumentLocator tag when meet setDocumentLocator event.
 182      */
 183     @Override
 184     public void setDocumentLocator(Locator locator) {
 185         try {
 186             this.locator = locator;
 187             println("setDocumentLocator...");
 188         } catch (SAXException ex) {
 189             System.err.println(WRITE_ERROR + ex);
 190         }
 191     }
 192 
 193     /**
 194      * Write skippedEntity tag along with entity name when meet skippedEntity
 195      * event.
 196      * @throws IOException error happen when writing file.
 197      */
 198     @Override
 199     public void skippedEntity(String name) throws SAXException {
 200         println("skippedEntity...\n" + "name: " + name);
 201     }
 202 
 203     /**
 204      * Write startDocument tag when meet startDocument event.
 205      * @throws IOException error happen when writing file.
 206      */
 207     @Override
 208     public void startDocument() throws SAXException {
 209         println("startDocument...");
 210     }
 211 
 212     /**
 213      * Write startElement tag along with namespaceURI, localName, qName, number
 214      * of attributes and line number when meet startElement event.
 215      * @throws IOException error happen when writing file.
 216      */
 217     @Override
 218     public void startElement(String namespaceURI, String localName,
 219                         String qName, Attributes atts) throws SAXException {
 220         println("startElement...\n" + "namespaceURI: " +  namespaceURI +
 221                 " localName: " + localName +  " qName: " + qName +
 222                 " Number of Attributes: " + atts.getLength() +
 223                 " Line# " + locator.getLineNumber());
 224     }
 225 
 226     /**
 227      * Write startPrefixMapping tag along with prefix and uri when meet
 228      * startPrefixMapping event.
 229      * @throws IOException error happen when writing file.
 230      */
 231     @Override
 232     public void startPrefixMapping(String prefix, String uri) throws SAXException {
 233         println("startPrefixMapping...\n" + "prefix: " + prefix +
 234                 " uri: " + uri);
 235     }
 236 
 237     /**
 238      * Write outString to file.
 239      * @param outString String to be written to File
 240      * @throws SAXException if write file failed
 241      */
 242     private void println(String outString) throws SAXException {
 243         try {
 244             bWriter.write( outString, 0, outString.length());
 245             bWriter.newLine();
 246         } catch (IOException ex) {
 247             throw new SAXException(WRITE_ERROR, ex);
 248         }
 249     }
 250 
 251     /**
 252      * Close the writer if it's initiated.
 253      * @throws IOException if any IO error when close buffered writer.
 254      */
 255     @Override
 256     public void close() throws IOException {
 257         if (bWriter != null)
 258             bWriter.close();
 259     }
 260 }