1 /* 2 * Copyright (c) 1999, 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 javax.xml.parsers.ptests; 24 25 import java.io.BufferedWriter; 26 import java.io.File; 27 import java.io.FileWriter; 28 import java.io.IOException; 29 import static jaxp.library.JAXPTestUtilities.ERROR_MSG_HEADER; 30 import org.xml.sax.Attributes; 31 import org.xml.sax.Locator; 32 import org.xml.sax.helpers.DefaultHandler; 33 import org.xml.sax.helpers.LocatorImpl; 34 35 /** 36 * Customized DefaultHandler which writes output document when methods are 37 * called by Transformer. Test may use output document to compare with golden 38 * file for verification. 39 */ 40 class MyCHandler extends DefaultHandler implements AutoCloseable { 41 42 private final BufferedWriter bWriter; 43 private final Locator locator = new LocatorImpl(); 44 45 private MyCHandler(File file) throws IOException { 46 bWriter = new BufferedWriter(new FileWriter(file)); 47 } 48 49 public static MyCHandler newInstance(File file) throws IOException { 50 MyCHandler handler = new MyCHandler(file); 51 return handler; 52 } 53 54 @Override 55 public void characters(char[] ch, int start, int length) { 56 String s = new String(ch, start, length); 57 String str = String.format("characters...length is:%d\n<%s>", s.length(), s); 58 try { 59 bWriter.write(str, 0, str.length()); 60 bWriter.newLine(); 61 } catch (IOException e) { 62 throw new RuntimeException(ERROR_MSG_HEADER, e); 63 } 64 } 65 66 @Override 67 public void endDocument() { 68 String str = "endDocument..."; 69 try { 70 bWriter.write(str, 0, str.length()); 71 bWriter.newLine(); 72 bWriter.flush(); 73 } catch (IOException e) { 74 throw new RuntimeException(ERROR_MSG_HEADER, e); 75 } 76 } 77 78 @Override 79 public void endElement(String namespaceURI, String localName, String qName) { 80 String str = String.format("endElement...\nnamespaceURI: <%s> localName: <%s> qName: <%s>", namespaceURI, localName, qName); 81 try { 82 bWriter.write(str, 0, str.length()); 83 bWriter.newLine(); 84 } catch (IOException e) { 85 throw new RuntimeException(ERROR_MSG_HEADER, e); 86 } 87 } 88 89 @Override 90 public void endPrefixMapping(String prefix) { 91 String str = String.format("endPrefixMapping...\nprefix: <%s>", prefix); 92 try { 93 bWriter.write(str, 0, str.length()); 94 bWriter.newLine(); 95 } catch (IOException e) { 96 throw new RuntimeException(ERROR_MSG_HEADER, e); 97 } 98 } 99 100 @Override 101 public void ignorableWhitespace(char[] ch, int start, int length) { 102 String s = new String(ch, start, length); 103 String str = String.format("ignorableWhitespace...\n%s ignorable white space string length: %d", s, s.length()); 104 try { 105 bWriter.write(str, 0, str.length()); 106 bWriter.newLine(); 107 } catch (IOException e) { 108 throw new RuntimeException(ERROR_MSG_HEADER, e); 109 } 110 } 111 112 @Override 113 public void processingInstruction(String target, String data) { 114 String str = String.format("processingInstruction...target:<%s> data: <%s>", target, data); 115 try { 116 bWriter.write(str, 0, str.length()); 117 bWriter.newLine(); 118 } catch (IOException e) { 119 throw new RuntimeException(ERROR_MSG_HEADER, e); 120 } 121 } 122 123 @Override 124 public void skippedEntity(String name) { 125 String str = String.format("skippedEntity...\nname: <%s>", name); 126 try { 127 bWriter.write(str, 0, str.length()); 128 bWriter.newLine(); 129 } catch (IOException e) { 130 throw new RuntimeException(ERROR_MSG_HEADER, e); 131 } 132 } 133 134 @Override 135 public void startDocument() { 136 String str = "startDocument..."; 137 try { 138 bWriter.write(str, 0, str.length()); 139 bWriter.newLine(); 140 } catch (IOException e) { 141 throw new RuntimeException(ERROR_MSG_HEADER, e); 142 } 143 } 144 145 @Override 146 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) { 147 String str = String.format("startElement...\nnamespaceURI: <%s> localName: <%s> qName: <%s> Number of Attributes: <%d> Line# <%d>", namespaceURI, 148 localName, qName, atts.getLength(), locator.getLineNumber()); 149 try { 150 bWriter.write(str, 0, str.length()); 151 bWriter.newLine(); 152 } catch (IOException e) { 153 throw new RuntimeException(ERROR_MSG_HEADER, e); 154 } 155 } 156 157 @Override 158 public void startPrefixMapping(String prefix, String uri) { 159 String str = String.format("startPrefixMapping...\nprefix: <%s> uri: <%s>", prefix, uri); 160 try { 161 bWriter.write(str, 0, str.length()); 162 bWriter.newLine(); 163 } catch (IOException e) { 164 throw new RuntimeException(ERROR_MSG_HEADER, e); 165 } 166 } 167 168 @Override 169 public void close() throws IOException { 170 if (bWriter != null) 171 bWriter.close(); 172 } 173 }