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