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); 59 } 60 } 61 62 /** 63 * Write characters tag along with content of characters when meet 64 * characters event. 65 * @throws IOException error happen when writing file. 66 */ 67 @Override 68 public void characters(char[] ch, int start, int length) 69 throws SAXException { 70 String s = new String(ch, start, length); 71 println("characters...length is:" + s.length() + "\n" 72 + "<" + s + ">"); 73 } 74 75 /** 76 * Write endDocument tag then flush the content and close the file when meet 77 * endDocument event. 78 * @throws IOException error happen when writing file or closing file. 79 */ 80 @Override 81 public void endDocument() throws SAXException { 82 try { 83 println("endDocument..."); 84 bWriter.flush(); 85 bWriter.close(); 86 } catch (IOException ex) { 87 throw new SAXException(WRITE_ERROR, ex); 88 } 89 } 90 91 /** 92 * Write endElement tag with namespaceURI, localName, qName to the file when 93 * meet endElement event. 94 * @throws IOException error happen when writing file. 95 */ 96 @Override 97 public void endElement(String namespaceURI, String localName, String qName) 98 throws SAXException { 99 println("endElement...\n" + "namespaceURI: <" + namespaceURI 100 + "> localName: <" + localName + "> qName: <" + qName + ">"); 101 } 102 103 /** 104 * Write endPrefixMapping tag along with prefix to the file when meet 105 * endPrefixMapping event. 106 * @throws IOException error happen when writing file. 107 */ 108 @Override 109 public void endPrefixMapping(String prefix) throws SAXException { 110 println("endPrefixMapping...\n" + "prefix: <" + prefix + ">"); 111 } 112 113 /** 114 * Write ignorableWhitespace tag along with white spaces when meet 115 * ignorableWhitespace event. 116 * @throws IOException error happen when writing file. 117 */ 118 @Override 119 public void ignorableWhitespace(char[] ch, int start, int length) 120 throws SAXException { 121 String s = new String(ch, start, length); 122 println("ignorableWhitespace...\n" + s 123 + " ignorable white space string length: " + s.length()); 124 } 125 126 /** 127 * Write processingInstruction tag along with target name and target data 128 * when meet processingInstruction event. 129 * @throws IOException error happen when writing file. 130 */ 131 @Override 132 public void processingInstruction(String target, String data) 133 throws SAXException { 134 println("processingInstruction...target:<" + target 135 + "> data: <" + data + ">"); 136 } 137 138 /** 139 * Write setDocumentLocator tag when meet setDocumentLocator event. 140 */ 141 @Override 142 public void setDocumentLocator(Locator locator) { 143 try { 144 this.locator = locator; 145 println("setDocumentLocator..."); 146 } catch (SAXException ex) { 147 System.err.println(WRITE_ERROR + ex); 148 } 149 } 150 151 /** 152 * Write skippedEntity tag along with entity name when meet skippedEntity 153 * event. 154 * @throws IOException error happen when writing file. 155 */ 156 @Override 157 public void skippedEntity(String name) throws SAXException { 158 println("skippedEntity...\n" + "name: <" + name + ">"); 159 } 160 161 /** 162 * Write startDocument tag when meet startDocument event. 163 * @throws IOException error happen when writing file. 164 */ 165 @Override 166 public void startDocument() throws SAXException { 167 println("startDocument..."); 168 } 169 170 /** 171 * Write startElement tag along with namespaceURI, localName, qName, number 172 * of attributes and line number when meet startElement event. 173 * @throws IOException error happen when writing file. 174 */ 175 @Override 176 public void startElement(String namespaceURI, String localName, 177 String qName, Attributes atts) throws SAXException { 178 println("startElement...\n" + "namespaceURI: <" + namespaceURI 179 + "> localName: <" + localName + "> qName: <" + qName 180 + "> Number of Attributes: <" + atts.getLength() 181 + "> Line# <" + locator.getLineNumber() + ">"); 182 } 183 184 /** 185 * Write startPrefixMapping tag along with prefix and uri when meet 186 * startPrefixMapping event. 187 * @throws IOException error happen when writing file. 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 }