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