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