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