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.SAXParseException;
  45 import org.xml.sax.XMLReader;
  46 import org.xml.sax.helpers.XMLFilterImpl;
  47 
  48 /**
  49  * Set parent of XMLFilter to XMLReader. Parsing on XML file will invoke XMLFilter
  50  * to write to output file. Test verifies output is same as the golden file.
  51  */
  52 @Test
  53 @Listeners({jaxp.library.FilePolicy.class})
  54 public class XMLFilterCBTest {
  55     /**
  56      * Test XMLFilter working with XML reader.
  57      *
  58      * @throws Exception If any errors occur.
  59      */
  60     public void testXMLFilterCB() throws Exception {
  61         String outputFile = USER_DIR + "XMLFilter.out";
  62         String goldFile = GOLDEN_DIR + "XMLFilterGF.out";
  63         String xmlFile = XML_DIR + "namespace1.xml";
  64 
  65         try (FileInputStream fis = new FileInputStream(xmlFile);
  66                 MyXMLFilter myXmlFilter = new MyXMLFilter(outputFile)){
  67             SAXParserFactory spf = SAXParserFactory.newInstance();
  68             spf.setNamespaceAware(true);
  69             XMLReader xmlReader = spf.newSAXParser().getXMLReader();
  70             myXmlFilter.setParent(xmlReader);
  71             myXmlFilter.parse(new InputSource(fis));
  72         }
  73         // Need close the output file before we compare it with golden file.
  74         assertTrue(compareWithGold(goldFile, outputFile));
  75     }
  76 }
  77 
  78 /**
  79  * Writer XMLFiler which write all tags to output file when event happens.
  80  */
  81 class MyXMLFilter extends XMLFilterImpl implements AutoCloseable {
  82     /**
  83      * FileWriter to write string to output file.
  84      */
  85     private final BufferedWriter bWriter;
  86 
  87     /**
  88      * Initiate FileWriter for output file.
  89      * @param outputFileName output file name.
  90      * @throws SAXException if open file failed.
  91      */
  92     MyXMLFilter(String outputFileName) throws SAXException {
  93         try {
  94             bWriter = new BufferedWriter(new FileWriter(outputFileName));
  95         } catch (IOException ex) {
  96             throw new SAXException(ex);
  97         }
  98     }
  99 
 100     /**
 101      * Write characters tag along with content of characters when meet
 102      * characters event.
 103      * @throws IOException error happen when writing file.
 104      */
 105     @Override
 106     public void characters(char[] ch, int start, int length) throws SAXException {
 107         String s = new String(ch, start, length);
 108         println("characters...\n" + s);
 109     }
 110 
 111     /**
 112      * Write endDocument tag then flush the content and close the file when meet
 113      * endDocument event.
 114      * @throws IOException error happen when writing file or closing file.
 115      */
 116     @Override
 117     public void endDocument() throws SAXException {
 118         try {
 119             println("endDocument...");
 120             bWriter.flush();
 121             bWriter.close();
 122         } catch (IOException ex) {
 123             throw new SAXException(ex);
 124         }
 125     }
 126 
 127     /**
 128      * Write endElement tag with namespaceURI, localName, qName to the file when
 129      * meet endElement event.
 130      * @throws IOException error happen when writing file.
 131      */
 132     @Override
 133     public void endElement(String namespaceURI,String localName,String qName)
 134             throws SAXException{
 135         println("endElement...\n" + "namespaceURI: " + namespaceURI +
 136                 " localName: "+ localName + " qName: " + qName);
 137     }
 138 
 139     /**
 140      * Write endPrefixMapping tag along with prefix to the file when meet
 141      * endPrefixMapping event.
 142      * @throws IOException error happen when writing file.
 143      */
 144     @Override
 145     public void endPrefixMapping(String prefix) throws SAXException {
 146         println("endPrefixmapping .." + prefix);
 147     }
 148 
 149     /**
 150      * Write error tag along with exception to the file when meet recoverable
 151      * error event.
 152      * @throws IOException error happen when writing file.
 153      */
 154     @Override
 155     public void error(SAXParseException e) throws SAXException {
 156         println("error: " + e.getMessage());
 157     }
 158 
 159     /**
 160      * Write fatalError tag along with exception to the file when meet
 161      * unrecoverable error event.
 162      * @throws IOException error happen when writing file.
 163      */
 164     @Override
 165     public void fatalError(SAXParseException e) throws SAXException {
 166         println("fatal error: ");
 167     }
 168 
 169     /**
 170      * Write warning tag along with exception to the file when meet warning event.
 171      * @throws IOException error happen when writing file.
 172      */
 173     @Override
 174     public void warning(SAXParseException e) throws SAXException {
 175         println("warning : ");
 176     }
 177 
 178     /**
 179      * Write ignorableWhitespace tag along with white spaces when meet
 180      * ignorableWhitespace event.
 181      * @throws IOException error happen when writing file.
 182      */
 183     @Override
 184     public void ignorableWhitespace(char[] ch, int start, int length)
 185             throws SAXException {
 186         String s = new String(ch, start, length);
 187         println("ignorableWhitespace...\n" + s +
 188                 " ignorable white space string length: " + s.length());
 189     }
 190 
 191     /**
 192      * Write processingInstruction tag along with target name and target data
 193      * when meet processingInstruction event.
 194      * @throws IOException error happen when writing file.
 195      */
 196     @Override
 197     public void processingInstruction(String target, String data)
 198             throws SAXException {
 199         println("processingInstruction...target:" + target +
 200                         " data: " + data);
 201     }
 202 
 203     /**
 204      * Write setDocumentLocator tag when meet setDocumentLocator event.
 205      */
 206     @Override
 207     public void setDocumentLocator(Locator locator) {
 208         try {
 209             println("setDocumentLocator...");
 210         } catch (SAXException ex) {
 211             System.err.println(ex);
 212         }
 213     }
 214 
 215     /**
 216      * Write skippedEntity tag along with entity name when meet skippedEntity
 217      * event.
 218      * @throws IOException error happen when writing file.
 219      */
 220     @Override
 221     public void skippedEntity(String name) throws SAXException {
 222         println("skippedEntity...\n" + "name: " + name);
 223     }
 224 
 225     /**
 226      * Write startDocument tag when meet startDocument event.
 227      * @throws IOException error happen when writing file.
 228      */
 229     @Override
 230     public void startDocument() throws SAXException {
 231         println("startDocument...");
 232     }
 233 
 234     /**
 235      * Write startElement tag along with namespaceURI, localName, qName, number
 236      * of attributes and line number when meet startElement event.
 237      * @throws IOException error happen when writing file.
 238      */
 239     @Override
 240     public void startElement(String namespaceURI, String localName,
 241                     String qName, Attributes atts) throws SAXException {
 242         println("startElement...\n" + "namespaceURI: " +  namespaceURI +
 243                         " localName: " + localName +  " qName: " + qName +
 244                         " Number of Attributes: " + atts.getLength());
 245     }
 246 
 247     /**
 248      * Write startPrefixMapping tag along with prefix and uri when meet
 249      * startPrefixMapping event.
 250      * @throws IOException error happen when writing file.
 251      */
 252     @Override
 253     public void startPrefixMapping(String prefix, String uri) throws SAXException {
 254         println("startPrefixMapping...\n" + "prefix: "
 255                                 + prefix + " uri: " + uri);
 256     }
 257 
 258     /**
 259      * Write outString to file.
 260      * @param outString String to be written to File
 261      * @throws SAXException if write file failed
 262      */
 263     private void println(String outString) throws SAXException {
 264         try {
 265             bWriter.write( outString, 0, outString.length());
 266             bWriter.newLine();
 267         } catch (IOException ex) {
 268             throw new SAXException(ex);
 269         }
 270     }
 271 
 272     /**
 273      * Close writer handler.
 274      * @throws IOException if any I/O error when close writer handler.
 275      */
 276     @Override
 277     public void close() throws IOException {
 278         if (bWriter != null)
 279             bWriter.close();
 280     }
 281 }