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