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