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 24 package javax.xml.transform.ptests; 25 26 import org.xml.sax.ContentHandler; 27 import org.xml.sax.Attributes; 28 import org.xml.sax.SAXException; 29 import org.xml.sax.Locator; 30 import java.io.BufferedWriter; 31 import java.io.FileWriter; 32 import java.io.IOException; 33 34 /** 35 * A customized ContentHandler. It writes whole XML file with extra tag on every 36 * XML elements. 37 */ 38 public class MyContentHandler implements ContentHandler { 39 /** 40 * FileWrite to write content to. 41 */ 42 private final BufferedWriter bWriter; 43 44 /** 45 * Create FileWiter for the processing. 46 * @param fileName Output file name. 47 * @throws org.xml.sax.SAXException 48 */ 49 public MyContentHandler(String fileName) throws SAXException { 50 try { 51 bWriter = new BufferedWriter(new FileWriter(fileName)); 52 } catch (IOException ex) { 53 throw new SAXException("Open file error", ex); 54 } 55 } 56 57 /** 58 * Do nothing when set document locator. 59 */ 60 @Override 61 public void setDocumentLocator (Locator locator) { 62 } 63 64 /** 65 * Open the output file when start to process the document. Write a 66 * startDocument tag to output File if opening file successfully. 67 * @throws SAXException if file writing failed. 68 */ 69 @Override 70 public void startDocument () throws SAXException { 71 //Bug # 4448884 filed. setDocumentLocator method should be called 72 //first. The bug won't be fixed. So startDocument is the next method 73 println("startDocument"); 74 } 75 76 /** 77 * Write a startDocument tag to output File after processing whole document. 78 * Follow with closing the output file. 79 * @throws SAXException if file writing failed. 80 */ 81 @Override 82 public void endDocument() throws SAXException { 83 println("endDocument"); 84 85 try { 86 bWriter.flush(); 87 bWriter.close(); 88 } catch (IOException ex) { 89 throw new SAXException("Close file error", ex); 90 } 91 } 92 93 /** 94 * Write a startPrefixMapping appending with prefix and URI to output File 95 * before entering the scope of a prefix-URI mapping. 96 * @param prefix the Namespace prefix being declared. 97 * @param uri the Namespace URI the prefix is mapped to. 98 * @throws SAXException if file writing failed. 99 */ 100 @Override 101 public void startPrefixMapping (String prefix, String uri) 102 throws SAXException { 103 println("startPrefixMapping: " + prefix + ", " + uri); 104 } 105 106 /** 107 * Write a endPrefixMapping appending with prefix to output File after a 108 * prefix-URI mapping. 109 * @param prefix the Namespace prefix being declared. 110 * @throws SAXException if file writing failed. 111 */ 112 @Override 113 public void endPrefixMapping (String prefix) throws SAXException { 114 println("endPrefixMapping: " + prefix); 115 } 116 117 /** 118 * Write a startElement appending with namespaceURI,localName,qName and 119 * iteration on attributes to output File when start processing element. 120 * @param namespaceURI the Namespace URI. 121 * @param localName the local name (without prefix). 122 * @param qName the qualified name (with prefix). 123 * @param atts the attributes attached to the element. 124 * @throws SAXException if file writing failed. 125 */ 126 @Override 127 public void startElement (String namespaceURI, String localName, 128 String qName, Attributes atts) 129 throws SAXException { 130 String str = "startElement: " + namespaceURI + ", " + namespaceURI + 131 ", " + qName; 132 int n = atts.getLength(); 133 for(int i = 0; i < n; i++) { 134 str = str + ", " + atts.getQName(i); 135 } 136 137 println(str); 138 } 139 140 /** 141 * Write a startElement appending with namespaceURI,qName and to output File 142 * after processing element finished. 143 * @param namespaceURI the Namespace URI. 144 * @param localName the local name (without prefix). 145 * @param qName the qualified name (with prefix). 146 * @throws SAXException if file writing failed. 147 */ 148 @Override 149 public void endElement (String namespaceURI, String localName, 150 String qName) throws SAXException { 151 println("endElement: " + namespaceURI + ", " + namespaceURI + ", " + qName); 152 } 153 154 155 /** 156 * Write characters tag to file when receive character data. 157 * @param ch the characters from the XML document 158 * @param start the start position in the array 159 * @param length the number of characters to read from the array 160 * @throws SAXException if file writing failed. 161 */ 162 @Override 163 public void characters (char ch[], int start, int length) 164 throws SAXException { 165 println("characters"); 166 } 167 168 /** 169 * Write ignorableWhitespace tag to file when receive notification of 170 * ignorable whitespace in element content. 171 * @param ch an array holds all ignorable whitespace. 172 * @param start start position of ignorable whitespace. 173 * @param length length of ignorable whitespace. 174 * @throws SAXException if file writing failed. 175 */ 176 @Override 177 public void ignorableWhitespace (char ch[], int start, int length) 178 throws SAXException { 179 println("ignorableWhitespace"); 180 } 181 182 /** 183 * Write processingInstruction tag when receive notification of a processing 184 * instruction. 185 * @param target the processing instruction target 186 * @param data the processing instruction data, or null if none was 187 * supplied. The data does not include any whitespace 188 * separating it from the target. 189 * @throws SAXException if file writing failed. 190 */ 191 @Override 192 public void processingInstruction (String target, String data) 193 throws SAXException { 194 println("processingInstruction: " + target + ", " + target); 195 } 196 197 /** 198 * Write the entity name to file when receive notification of a skipped entity. 199 * @param name entity name that skipped 200 * @throws SAXException if file writing failed. 201 */ 202 @Override 203 public void skippedEntity (String name) throws SAXException { 204 println("skippedEntity: " + name); 205 } 206 207 /** 208 * Print a string output to file along with a new line. 209 * @param output string needed to be written to file. 210 * @throws SAXException if file writing failed. 211 */ 212 private void println(String output) throws SAXException { 213 try { 214 bWriter.write(output, 0, output.length()); 215 bWriter.newLine(); 216 } catch (IOException ex) { 217 throw new SAXException("bWriter error", ex); 218 } 219 } 220 }