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.File;
  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.SAXParser;
  34 import javax.xml.parsers.SAXParserFactory;
  35 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  36 import static jaxp.library.JAXPTestUtilities.failCleanup;
  37 import static jaxp.library.JAXPTestUtilities.failUnexpected;
  38 import static org.testng.Assert.assertTrue;
  39 import org.testng.annotations.Test;
  40 import org.xml.sax.Attributes;
  41 import org.xml.sax.Locator;
  42 import org.xml.sax.SAXException;
  43 import org.xml.sax.SAXParseException;
  44 import org.xml.sax.helpers.DefaultHandler;
  45 import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR;
  46 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
  47 import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
  48 
  49 /**
  50  * XMLReader parse XML with default handler that transverses XML and 
  51  * print all visited node. Test verifies output is same as the golden file.
  52  */
  53 public class DefaultHandlerTest {
  54     /**
  55      * Test default handler that transverses XML and  print all visited node.
  56      */
  57     @Test
  58     public void testDefaultHandler() {
  59         String outputFile = CLASS_DIR + "DefaultHandler.out";
  60         String goldFile = GOLDEN_DIR + "DefaultHandlerGF.out";
  61         String xmlFile = XML_DIR + "namespace1.xml";
  62 
  63         try {
  64             SAXParserFactory spf = SAXParserFactory.newInstance();
  65             spf.setNamespaceAware(true);
  66             SAXParser saxparser = spf.newSAXParser();
  67 
  68             MyDefaultHandler handler = new MyDefaultHandler(outputFile);
  69             File file = new File(xmlFile);
  70             String Absolutepath = file.getAbsolutePath();
  71             String newAbsolutePath = Absolutepath;
  72             if (File.separatorChar == '\\')
  73                     newAbsolutePath = Absolutepath.replace('\\', '/');
  74             String uri = "file:///" + newAbsolutePath;
  75             saxparser.parse(uri, handler);
  76         } catch (IOException | ParserConfigurationException | SAXException ex) {
  77             failUnexpected(ex);
  78         }
  79         // Need close the output file before we compare it with golden file.
  80         try {
  81             assertTrue(compareWithGold(goldFile, outputFile));
  82         } catch (IOException ex) {
  83             failUnexpected(ex);
  84         } finally {
  85             try {
  86                 Path outputPath = Paths.get(outputFile);
  87                 if(Files.exists(outputPath))
  88                     Files.delete(outputPath);
  89             } catch (IOException ex) {
  90                 failCleanup(ex, outputFile);
  91             }
  92         }
  93     }
  94 }
  95 
  96 class MyDefaultHandler extends DefaultHandler {
  97     /**
  98      * Prefix to every exception.
  99      */
 100     private final static String WRITE_ERROR = "bWrite error";
 101 
 102     /**
 103      * FileWriter to write string to output file.
 104      */
 105     private final BufferedWriter bWriter;
 106     
 107     /**
 108      * Initiate FileWriter when construct a MyContentHandler.
 109      * @param outputFileName output file name.
 110      * @throws SAXException creation of FileWriter failed.
 111      */
 112     MyDefaultHandler(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         println("characters...\n" + new String(ch, start, length));
 128     }
 129 
 130     /**
 131      * Write endDocument tag then flush the content and close the file when meet
 132      * endDocument event.
 133      * @throws IOException error happen when writing file or closing file.
 134      */
 135     @Override
 136     public void endDocument() throws SAXException {
 137         try {
 138             println("endDocument...");
 139             bWriter.flush();
 140             bWriter.close();
 141         } catch (IOException ex) {
 142             throw new SAXException(WRITE_ERROR, ex);
 143         }
 144     }
 145 
 146     /**
 147      * Write endElement tag with namespaceURI, localName, qName to the file when
 148      * meet endElement event.
 149      * @throws IOException error happen when writing file.
 150      */
 151     @Override
 152     public void endElement(String namespaceURI,String localName,String qName) throws SAXException{
 153         println("endElement...\n" + "namespaceURI: " + namespaceURI +
 154                 " localName: "+ localName + " qName: " + qName);
 155     }
 156 
 157     /**
 158      * Write endPrefixMapping tag along with prefix to the file when meet 
 159      * endPrefixMapping event.
 160      * @throws IOException error happen when writing file.
 161      */
 162     @Override
 163     public void endPrefixMapping(String prefix) throws SAXException {
 164         println("endPrefixmapping .." + prefix);
 165     }
 166 
 167     /**
 168      * Write error tag along with exception to the file when meet recoverable 
 169      * error event.
 170      * @throws IOException error happen when writing file.
 171      */
 172     @Override
 173     public void error(SAXParseException e) throws SAXException {
 174         println("error: " + e.getMessage());
 175     }
 176 
 177     /**
 178      * Write fatalError tag along with exception to the file when meet 
 179      * unrecoverable error event.
 180      * @throws IOException error happen when writing file.
 181      */
 182     @Override
 183     public void fatalError(SAXParseException e) throws SAXException {
 184         println("fatal error: ");
 185     }
 186 
 187     /**
 188      * Write warning tag along with exception to the file when meet warning event.
 189      * @throws IOException error happen when writing file.
 190      */
 191     @Override
 192     public void warning(SAXParseException e) throws SAXException {
 193         println("warning : ");
 194     }
 195 
 196     /**
 197      * Write ignorableWhitespace tag along with white spaces when meet 
 198      * ignorableWhitespace event.
 199      * @throws IOException error happen when writing file.
 200      */
 201     @Override
 202     public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
 203         String s = new String(ch, start, length);
 204         println("ignorableWhitespace...\n" + s +
 205                 " ignorable white space string length: " + s.length());
 206     }
 207 
 208     /**
 209      * Write processingInstruction tag along with target name and target data
 210      * when meet processingInstruction event.
 211      * @throws IOException error happen when writing file.
 212      */
 213     @Override
 214     public void processingInstruction(String target, String data) throws SAXException {
 215         println("processingInstruction...target:" + target +
 216                         " data: " + data);
 217     }
 218 
 219     @Override
 220     public void setDocumentLocator(Locator locator) {
 221         try {
 222             println("setDocumentLocator...");
 223         } catch (SAXException ex) {
 224             System.err.println(WRITE_ERROR + ex);
 225         }
 226     }
 227 
 228     /**
 229      * Write skippedEntity tag along with entity name when meet skippedEntity
 230      * event.
 231      * @throws IOException error happen when writing file.
 232      */
 233     @Override
 234     public void skippedEntity(String name) throws SAXException {
 235         println("skippedEntity...\n" + "name: " + name);
 236     }
 237 
 238     /**
 239      * Write startDocument tag when meet startDocument event.
 240      * @throws IOException error happen when writing file.
 241      */
 242     @Override
 243     public void startDocument() throws SAXException {
 244         println("startDocument...");
 245     }
 246 
 247     /**
 248      * Write startElement tag along with namespaceURI, localName, qName, number
 249      * of attributes and line number when meet startElement event.
 250      * @throws IOException error happen when writing file.
 251      */
 252     @Override
 253     public void startElement(String namespaceURI, String localName,
 254                                         String qName, Attributes atts) throws SAXException {
 255         println("startElement...\n" + "namespaceURI: " +  namespaceURI +
 256                         " localName: " + localName +  " qName: " + qName +
 257                         " Number of Attributes: " + atts.getLength());
 258     }
 259 
 260     /**
 261      * Write startPrefixMapping tag along with prefix and uri when meet 
 262      * startPrefixMapping event.
 263      * @throws IOException error happen when writing file.
 264      */
 265     @Override
 266     public void startPrefixMapping(String prefix, String uri) throws SAXException {
 267         println("startPrefixMapping...\n" + "prefix: " + prefix + " uri: " + uri);
 268     }
 269     
 270     /**
 271      * Write outString to file.
 272      * @param outString String to be written to File
 273      * @throws SAXException if write file failed 
 274      */
 275     private void println(String outString) throws SAXException {
 276         try {
 277             bWriter.write( outString, 0, outString.length());
 278             bWriter.newLine();
 279         } catch (IOException ex) {
 280             throw new SAXException(WRITE_ERROR, ex);
 281         }   
 282     }
 283 }