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.testng.annotations.Test; 35 import org.xml.sax.Attributes; 36 import org.xml.sax.InputSource; 37 import org.xml.sax.Locator; 38 import org.xml.sax.SAXException; 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 * Class registers a content event handler to XMLReader. Content event handler 47 * transverses XML and print all visited node when XMLreader parses XML. Test 48 * verifies output is same as the golden file. 49 */ 50 public class ContentHandlerTest extends JAXPFileBaseTest { 51 /** 52 * Content event handler visit all nodes to print to output file. 53 * 54 * @throws SAXException If any parse errors occur. 55 * @throws IOException if the file exists but is a directory rather than 56 * a regular file, does not exist but cannot be created, or cannot 57 * be opened for any other reason. 58 * @throws ParserConfigurationException if a DocumentBuilder cannot be 59 * created which satisfies the configuration requested. 60 */ 61 @Test 62 public void testcase01() throws IOException, SAXException, ParserConfigurationException { 63 String outputFile = CLASS_DIR + "Content.out"; 64 String goldFile = GOLDEN_DIR + "ContentGF.out"; 65 String xmlFile = XML_DIR + "namespace1.xml"; 66 67 try(FileInputStream instream = new FileInputStream(xmlFile); 68 MyContentHandler cHandler = new MyContentHandler(outputFile)) { 69 SAXParserFactory spf = SAXParserFactory.newInstance(); 70 spf.setNamespaceAware(true); 71 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 72 xmlReader.setContentHandler(cHandler); 73 xmlReader.parse(new InputSource(instream)); 74 } 75 assertTrue(compareWithGold(goldFile, outputFile)); 76 } 77 } 78 79 /** 80 * A content write out handler. 81 */ 82 class MyContentHandler extends XMLFilterImpl implements AutoCloseable { 83 /** 84 * Prefix to every exception. 85 */ 86 private final static String WRITE_ERROR = "bWriter error"; 87 88 /** 89 * FileWriter to write string to output file. 90 */ 91 private final BufferedWriter bWriter; 92 93 /** 94 * Default document locator. 95 */ 96 private Locator locator; 97 98 /** 99 * Initiate FileWriter when construct a MyContentHandler. 100 * @param outputFileName output file name. 101 * @throws SAXException creation of FileWriter failed. 102 */ 103 public MyContentHandler(String outputFileName) throws SAXException { 104 try { 105 bWriter = new BufferedWriter(new FileWriter(outputFileName)); 106 } catch (IOException ex) { 107 throw new SAXException(ex); 108 } 109 } 110 111 /** 112 * Write characters tag along with content of characters when meet 113 * characters event. 114 * @throws IOException error happen when writing file. 115 */ 116 @Override 117 public void characters(char[] ch, int start, int length) throws SAXException { 118 String s = new String(ch, start, length); 119 println("characters...\n" + s); 120 } 121 122 /** 123 * Write endDocument tag then flush the content and close the file when meet 124 * endDocument event. 125 * @throws IOException error happen when writing file or closing file. 126 */ 127 @Override 128 public void endDocument() throws SAXException { 129 try { 130 println("endDocument..."); 131 bWriter.flush(); 132 bWriter.close(); 133 } catch (IOException ex) { 134 throw new SAXException(WRITE_ERROR, ex); 135 } 136 } 137 138 /** 139 * Write endElement tag with namespaceURI, localName, qName to the file when 140 * meet endElement event. 141 * @throws IOException error happen when writing file. 142 */ 143 @Override 144 public void endElement(String namespaceURI,String localName,String qName) 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...\n" + "prefix: " + prefix); 157 } 158 159 /** 160 * Write ignorableWhitespace tag along with white spaces when meet 161 * ignorableWhitespace event. 162 * @throws IOException error happen when writing file. 163 */ 164 @Override 165 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { 166 String s = new String(ch, start, length); 167 println("ignorableWhitespace...\n" + s + 168 " ignorable white space string length: " + s.length()); 169 } 170 171 /** 172 * Write processingInstruction tag along with target name and target data 173 * when meet processingInstruction event. 174 * @throws IOException error happen when writing file. 175 */ 176 @Override 177 public void processingInstruction(String target, String data) throws SAXException { 178 println("processingInstruction...target:" + target + 179 " data: " + data); 180 } 181 182 /** 183 * Write setDocumentLocator tag when meet setDocumentLocator event. 184 */ 185 @Override 186 public void setDocumentLocator(Locator locator) { 187 try { 188 this.locator = locator; 189 println("setDocumentLocator..."); 190 } catch (SAXException ex) { 191 System.err.println(WRITE_ERROR + ex); 192 } 193 } 194 195 /** 196 * Write skippedEntity tag along with entity name when meet skippedEntity 197 * event. 198 * @throws IOException error happen when writing file. 199 */ 200 @Override 201 public void skippedEntity(String name) throws SAXException { 202 println("skippedEntity...\n" + "name: " + name); 203 } 204 205 /** 206 * Write startDocument tag when meet startDocument event. 207 * @throws IOException error happen when writing file. 208 */ 209 @Override 210 public void startDocument() throws SAXException { 211 println("startDocument..."); 212 } 213 214 /** 215 * Write startElement tag along with namespaceURI, localName, qName, number 216 * of attributes and line number when meet startElement event. 217 * @throws IOException error happen when writing file. 218 */ 219 @Override 220 public void startElement(String namespaceURI, String localName, 221 String qName, Attributes atts) throws SAXException { 222 println("startElement...\n" + "namespaceURI: " + namespaceURI + 223 " localName: " + localName + " qName: " + qName + 224 " Number of Attributes: " + atts.getLength() + 225 " Line# " + locator.getLineNumber()); 226 } 227 228 /** 229 * Write startPrefixMapping tag along with prefix and uri when meet 230 * startPrefixMapping event. 231 * @throws IOException error happen when writing file. 232 */ 233 @Override 234 public void startPrefixMapping(String prefix, String uri) throws SAXException { 235 println("startPrefixMapping...\n" + "prefix: " + prefix + 236 " uri: " + uri); 237 } 238 239 /** 240 * Write outString to file. 241 * @param outString String to be written to File 242 * @throws SAXException if write file failed 243 */ 244 private void println(String outString) throws SAXException { 245 try { 246 bWriter.write( outString, 0, outString.length()); 247 bWriter.newLine(); 248 } catch (IOException ex) { 249 throw new SAXException(WRITE_ERROR, ex); 250 } 251 } 252 253 /** 254 * Close the writer if it's initiated. 255 * @throws IOException if any IO error when close buffered writer. 256 */ 257 @Override 258 public void close() throws IOException { 259 if (bWriter != null) 260 bWriter.close(); 261 } 262 }