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 org.xml.sax.helpers.DefaultHandler; 26 import org.xml.sax.helpers.LocatorImpl; 27 import org.xml.sax.Locator; 28 import org.xml.sax.Attributes; 29 import java.io.BufferedWriter; 30 import java.io.IOException; 31 import java.io.FileWriter; 32 import org.xml.sax.SAXException; 33 34 class MyNSContentHandler extends DefaultHandler { 35 /** 36 * Prefix for written string. 37 */ 38 private final static String WRITE_ERROR = "bWrite error"; 39 /** 40 * FileWriter to write output file. 41 */ 42 private final BufferedWriter bWriter; 43 44 /** 45 * Default locator. 46 */ 47 Locator locator = new LocatorImpl(); 48 49 /** 50 * Initiate FileWrite. 51 * @param outputFileName file name of output file. 52 * @throws SAXException when open output file failed. 53 */ 54 public MyNSContentHandler(String outputFileName) throws SAXException { 55 try { 56 bWriter = new BufferedWriter(new FileWriter(outputFileName)); 57 } catch (IOException ex) { 58 throw new SAXException(ex); 188 */ 189 @Override 190 public void startPrefixMapping(String prefix, String uri) 191 throws SAXException { 192 println("startPrefixMapping...\n" + "prefix: <" + prefix 193 + "> uri: <" + uri + ">"); 194 } 195 /** 196 * Write outString to output file. 197 * @param outString string to be written. 198 * @throws SAXException 199 */ 200 private void println(String outString) throws SAXException { 201 try { 202 bWriter.write( outString, 0, outString.length()); 203 bWriter.newLine(); 204 } catch (IOException ex) { 205 throw new SAXException(WRITE_ERROR, ex); 206 } 207 } 208 } | 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 org.xml.sax.helpers.DefaultHandler; 26 import org.xml.sax.helpers.LocatorImpl; 27 import org.xml.sax.Locator; 28 import org.xml.sax.Attributes; 29 import java.io.BufferedWriter; 30 import java.io.IOException; 31 import java.io.FileWriter; 32 import org.xml.sax.SAXException; 33 34 class MyNSContentHandler extends DefaultHandler implements AutoCloseable{ 35 /** 36 * Prefix for written string. 37 */ 38 private final static String WRITE_ERROR = "bWrite error"; 39 40 /** 41 * FileWriter to write output file. 42 */ 43 private final BufferedWriter bWriter; 44 45 /** 46 * Default locator. 47 */ 48 Locator locator = new LocatorImpl(); 49 50 /** 51 * Initiate FileWrite. 52 * @param outputFileName file name of output file. 53 * @throws SAXException when open output file failed. 54 */ 55 public MyNSContentHandler(String outputFileName) throws SAXException { 56 try { 57 bWriter = new BufferedWriter(new FileWriter(outputFileName)); 58 } catch (IOException ex) { 59 throw new SAXException(ex); 189 */ 190 @Override 191 public void startPrefixMapping(String prefix, String uri) 192 throws SAXException { 193 println("startPrefixMapping...\n" + "prefix: <" + prefix 194 + "> uri: <" + uri + ">"); 195 } 196 /** 197 * Write outString to output file. 198 * @param outString string to be written. 199 * @throws SAXException 200 */ 201 private void println(String outString) throws SAXException { 202 try { 203 bWriter.write( outString, 0, outString.length()); 204 bWriter.newLine(); 205 } catch (IOException ex) { 206 throw new SAXException(WRITE_ERROR, ex); 207 } 208 } 209 210 /** 211 * Close writer if it's initiated. 212 * @throws IOException if any I/O error when close writer. 213 */ 214 @Override 215 public void close() throws IOException { 216 if (bWriter != null) 217 bWriter.close(); 218 } 219 } |