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.File; 33 import java.io.FileWriter; 34 import java.io.IOException; 35 36 import javax.xml.parsers.SAXParser; 37 import javax.xml.parsers.SAXParserFactory; 38 39 import org.testng.annotations.Listeners; 40 import org.testng.annotations.Test; 41 import org.xml.sax.Attributes; 42 import org.xml.sax.Locator; 43 import org.xml.sax.SAXException; 44 import org.xml.sax.SAXParseException; 45 import org.xml.sax.helpers.DefaultHandler; 46 47 /** 48 * XMLReader parse XML with default handler that transverses XML and 49 * print all visited node. Test verifies output is same as the golden file. 50 */ 51 @Listeners({jaxp.library.FilePolicy.class}) 52 public class DefaultHandlerTest { 53 /** 54 * Test default handler that transverses XML and print all visited node. 55 * 56 * @throws Exception If any errors occur. 57 */ 58 @Test 59 public void testDefaultHandler() throws Exception { 60 String outputFile = USER_DIR + "DefaultHandler.out"; 61 String goldFile = GOLDEN_DIR + "DefaultHandlerGF.out"; 62 String xmlFile = XML_DIR + "namespace1.xml"; 63 64 SAXParserFactory spf = SAXParserFactory.newInstance(); 65 spf.setNamespaceAware(true); 66 SAXParser saxparser = spf.newSAXParser(); 67 68 MyDefaultHandler handler = new MyDefaultHandler(outputFile); 69 File file = new File(xmlFile); 70 String Absolutepath = file.getAbsolutePath(); 71 String newAbsolutePath = Absolutepath; 72 if (File.separatorChar == '\\') 73 newAbsolutePath = Absolutepath.replace('\\', '/'); 74 saxparser.parse("file:///" + newAbsolutePath, handler); 75 76 assertTrue(compareWithGold(goldFile, outputFile)); 77 78 } 79 } 80 81 class MyDefaultHandler extends DefaultHandler { 82 /** 83 * Prefix to every exception. 84 */ 85 private final static String WRITE_ERROR = "bWrite error"; 86 87 /** 88 * FileWriter to write string to output file. 89 */ 90 private final BufferedWriter bWriter; 91 92 /** 93 * Initiate FileWriter when construct a MyContentHandler. 94 * @param outputFileName output file name. 95 * @throws SAXException creation of FileWriter failed. 96 */ 97 MyDefaultHandler(String outputFileName) throws SAXException { 98 try { 99 bWriter = new BufferedWriter(new FileWriter(outputFileName)); 100 } catch (IOException ex) { 101 throw new SAXException(ex); 102 } 103 } 104 105 /** 106 * Write characters tag along with content of characters when meet 107 * characters event. 108 * @throws IOException error happen when writing file. 109 */ 110 @Override 111 public void characters(char[] ch, int start, int length) throws SAXException { 112 println("characters...\n" + new String(ch, start, length)); 113 } 114 115 /** 116 * Write endDocument tag then flush the content and close the file when meet 117 * endDocument event. 118 * @throws IOException error happen when writing file or closing file. 119 */ 120 @Override 121 public void endDocument() throws SAXException { 122 try { 123 println("endDocument..."); 124 bWriter.flush(); 125 bWriter.close(); 126 } catch (IOException ex) { 127 throw new SAXException(WRITE_ERROR, ex); 128 } 129 } 130 131 /** 132 * Write endElement tag with namespaceURI, localName, qName to the file when 133 * meet endElement event. 134 * @throws IOException error happen when writing file. 135 */ 136 @Override 137 public void endElement(String namespaceURI,String localName,String qName) throws SAXException{ 138 println("endElement...\n" + "namespaceURI: " + namespaceURI + 139 " localName: "+ localName + " qName: " + qName); 140 } 141 142 /** 143 * Write endPrefixMapping tag along with prefix to the file when meet 144 * endPrefixMapping event. 145 * @throws IOException error happen when writing file. 146 */ 147 @Override 148 public void endPrefixMapping(String prefix) throws SAXException { 149 println("endPrefixmapping .." + prefix); 150 } 151 152 /** 153 * Write error tag along with exception to the file when meet recoverable 154 * error event. 155 * @throws IOException error happen when writing file. 156 */ 157 @Override 158 public void error(SAXParseException e) throws SAXException { 159 println("error: " + e.getMessage()); 160 } 161 162 /** 163 * Write fatalError tag along with exception to the file when meet 164 * unrecoverable error event. 165 * @throws IOException error happen when writing file. 166 */ 167 @Override 168 public void fatalError(SAXParseException e) throws SAXException { 169 println("fatal error: "); 170 } 171 172 /** 173 * Write warning tag along with exception to the file when meet warning event. 174 * @throws IOException error happen when writing file. 175 */ 176 @Override 177 public void warning(SAXParseException e) throws SAXException { 178 println("warning : "); 179 } 180 181 /** 182 * Write ignorableWhitespace tag along with white spaces when meet 183 * ignorableWhitespace event. 184 * @throws IOException error happen when writing file. 185 */ 186 @Override 187 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { 188 String s = new String(ch, start, length); 189 println("ignorableWhitespace...\n" + s + 190 " ignorable white space string length: " + s.length()); 191 } 192 193 /** 194 * Write processingInstruction tag along with target name and target data 195 * when meet processingInstruction event. 196 * @throws IOException error happen when writing file. 197 */ 198 @Override 199 public void processingInstruction(String target, String data) throws SAXException { 200 println("processingInstruction...target:" + target + 201 " data: " + data); 202 } 203 204 @Override 205 public void setDocumentLocator(Locator locator) { 206 try { 207 println("setDocumentLocator..."); 208 } catch (SAXException ex) { 209 System.err.println(WRITE_ERROR + 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: " + prefix + " uri: " + uri); 253 } 254 255 /** 256 * Write outString to file. 257 * @param outString String to be written to File 258 * @throws SAXException if write file failed 259 */ 260 private void println(String outString) throws SAXException { 261 try { 262 bWriter.write( outString, 0, outString.length()); 263 bWriter.newLine(); 264 } catch (IOException ex) { 265 throw new SAXException(WRITE_ERROR, ex); 266 } 267 } 268 }