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 /* 52 * @test 53 * @library /javax/xml/jaxp/libs 54 * @run testng/othervm -DrunSecMngr=true org.xml.sax.ptests.DefaultHandlerTest 55 * @run testng/othervm org.xml.sax.ptests.DefaultHandlerTest 56 */ 57 @Listeners({jaxp.library.FilePolicy.class}) 58 public class DefaultHandlerTest { 59 /** 60 * Test default handler that transverses XML and print all visited node. 61 * 62 * @throws Exception If any errors occur. 63 */ 64 @Test 65 public void testDefaultHandler() throws Exception { 66 String outputFile = USER_DIR + "DefaultHandler.out"; 67 String goldFile = GOLDEN_DIR + "DefaultHandlerGF.out"; 68 String xmlFile = XML_DIR + "namespace1.xml"; 69 70 SAXParserFactory spf = SAXParserFactory.newInstance(); 71 spf.setNamespaceAware(true); 72 SAXParser saxparser = spf.newSAXParser(); 73 74 MyDefaultHandler handler = new MyDefaultHandler(outputFile); 75 File file = new File(xmlFile); 76 String Absolutepath = file.getAbsolutePath(); 77 String newAbsolutePath = Absolutepath; 78 if (File.separatorChar == '\\') 79 newAbsolutePath = Absolutepath.replace('\\', '/'); 80 saxparser.parse("file:///" + newAbsolutePath, handler); 81 82 assertTrue(compareWithGold(goldFile, outputFile)); 83 84 } 85 } 86 87 class MyDefaultHandler extends DefaultHandler { 88 /** 89 * Prefix to every exception. 90 */ 91 private final static String WRITE_ERROR = "bWrite error"; 92 93 /** 94 * FileWriter to write string to output file. 95 */ 96 private final BufferedWriter bWriter; 97 98 /** 99 * Initiate FileWriter when construct a MyContentHandler. 100 * @param outputFileName output file name. 101 * @throws SAXException creation of FileWriter failed. 102 */ 103 MyDefaultHandler(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 println("characters...\n" + new String(ch, start, length)); 119 } 120 121 /** 122 * Write endDocument tag then flush the content and close the file when meet 123 * endDocument event. 124 * @throws IOException error happen when writing file or closing file. 125 */ 126 @Override 127 public void endDocument() throws SAXException { 128 try { 129 println("endDocument..."); 130 bWriter.flush(); 131 bWriter.close(); 132 } catch (IOException ex) { 133 throw new SAXException(WRITE_ERROR, ex); 134 } 135 } 136 137 /** 138 * Write endElement tag with namespaceURI, localName, qName to the file when 139 * meet endElement event. 140 * @throws IOException error happen when writing file. 141 */ 142 @Override 143 public void endElement(String namespaceURI,String localName,String qName) throws SAXException{ 144 println("endElement...\n" + "namespaceURI: " + namespaceURI + 145 " localName: "+ localName + " qName: " + qName); 146 } 147 148 /** 149 * Write endPrefixMapping tag along with prefix to the file when meet 150 * endPrefixMapping event. 151 * @throws IOException error happen when writing file. 152 */ 153 @Override 154 public void endPrefixMapping(String prefix) throws SAXException { 155 println("endPrefixmapping .." + prefix); 156 } 157 158 /** 159 * Write error tag along with exception to the file when meet recoverable 160 * error event. 161 * @throws IOException error happen when writing file. 162 */ 163 @Override 164 public void error(SAXParseException e) throws SAXException { 165 println("error: " + e.getMessage()); 166 } 167 168 /** 169 * Write fatalError tag along with exception to the file when meet 170 * unrecoverable error event. 171 * @throws IOException error happen when writing file. 172 */ 173 @Override 174 public void fatalError(SAXParseException e) throws SAXException { 175 println("fatal error: "); 176 } 177 178 /** 179 * Write warning tag along with exception to the file when meet warning event. 180 * @throws IOException error happen when writing file. 181 */ 182 @Override 183 public void warning(SAXParseException e) throws SAXException { 184 println("warning : "); 185 } 186 187 /** 188 * Write ignorableWhitespace tag along with white spaces when meet 189 * ignorableWhitespace event. 190 * @throws IOException error happen when writing file. 191 */ 192 @Override 193 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { 194 String s = new String(ch, start, length); 195 println("ignorableWhitespace...\n" + s + 196 " ignorable white space string length: " + s.length()); 197 } 198 199 /** 200 * Write processingInstruction tag along with target name and target data 201 * when meet processingInstruction event. 202 * @throws IOException error happen when writing file. 203 */ 204 @Override 205 public void processingInstruction(String target, String data) throws SAXException { 206 println("processingInstruction...target:" + target + 207 " data: " + data); 208 } 209 210 @Override 211 public void setDocumentLocator(Locator locator) { 212 try { 213 println("setDocumentLocator..."); 214 } catch (SAXException ex) { 215 System.err.println(WRITE_ERROR + ex); 216 } 217 } 218 219 /** 220 * Write skippedEntity tag along with entity name when meet skippedEntity 221 * event. 222 * @throws IOException error happen when writing file. 223 */ 224 @Override 225 public void skippedEntity(String name) throws SAXException { 226 println("skippedEntity...\n" + "name: " + name); 227 } 228 229 /** 230 * Write startDocument tag when meet startDocument event. 231 * @throws IOException error happen when writing file. 232 */ 233 @Override 234 public void startDocument() throws SAXException { 235 println("startDocument..."); 236 } 237 238 /** 239 * Write startElement tag along with namespaceURI, localName, qName, number 240 * of attributes and line number when meet startElement event. 241 * @throws IOException error happen when writing file. 242 */ 243 @Override 244 public void startElement(String namespaceURI, String localName, 245 String qName, Attributes atts) throws SAXException { 246 println("startElement...\n" + "namespaceURI: " + namespaceURI + 247 " localName: " + localName + " qName: " + qName + 248 " Number of Attributes: " + atts.getLength()); 249 } 250 251 /** 252 * Write startPrefixMapping tag along with prefix and uri when meet 253 * startPrefixMapping event. 254 * @throws IOException error happen when writing file. 255 */ 256 @Override 257 public void startPrefixMapping(String prefix, String uri) throws SAXException { 258 println("startPrefixMapping...\n" + "prefix: " + prefix + " uri: " + uri); 259 } 260 261 /** 262 * Write outString to file. 263 * @param outString String to be written to File 264 * @throws SAXException if write file failed 265 */ 266 private void println(String outString) throws SAXException { 267 try { 268 bWriter.write( outString, 0, outString.length()); 269 bWriter.newLine(); 270 } catch (IOException ex) { 271 throw new SAXException(WRITE_ERROR, ex); 272 } 273 } 274 } 275 276