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