1 /* 2 * Redistribution and use of this software and associated documentation 3 * ("Software"), with or without modification, are permitted provided 4 * that the following conditions are met: 5 * 6 * 1. Redistributions of source code must retain copyright 7 * statements and notices. Redistributions must also contain a 8 * copy of this document. 9 * 10 * 2. Redistributions in binary form must reproduce the 11 * above copyright notice, this list of conditions and the 12 * following disclaimer in the documentation and/or other 13 * materials provided with the distribution. 14 * 15 * 3. The name "Exolab" must not be used to endorse or promote 16 * products derived from this Software without prior written 17 * permission of Intalio, Inc. For written permission, 18 * please contact info@exolab.org. 19 * 20 * 4. Products derived from this Software may not be called "Exolab" 21 * nor may "Exolab" appear in their names without prior written 22 * permission of Intalio, Inc. Exolab is a registered 23 * trademark of Intalio, Inc. 24 * 25 * 5. Due credit should be given to the Exolab Project 26 * (http://www.exolab.org/). 27 * 28 * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 32 * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 39 * OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * Copyright 1999 (C) Intalio, Inc. All Rights Reserved. 42 * 43 * $Id$ 44 */ 45 package org.exolab.castor.xml; 46 47 import java.io.PrintWriter; 48 import java.io.Writer; 49 50 import org.xml.sax.AttributeList; 51 import org.xml.sax.DocumentHandler; 52 import org.xml.sax.Locator; 53 54 /** 55 * A Simple SAX1 DocumentHandler that intercepts SAX events and prints them to 56 * the console. This class is not used during normal Castor operation, but 57 * exists so that during debugging one can replace a normal DocumentHandler with 58 * this one (which will proxy to the correct DocumentHandler). 59 * <p> 60 * FIXME: As Castor moves internally to the SAX2 interface, this class should 61 * also be updated for SAX2. 62 * 63 * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a> 64 * @version $Revision$ $Date: 2003-03-03 00:05:44 -0700 (Mon, 03 Mar 2003) $ 65 */ 66 public class DebugHandler implements DocumentHandler { 67 68 /** The writer to report events to. */ 69 private Writer _out = null; 70 /** The DocumentHandler to forward events to. */ 71 private DocumentHandler _handler = null; 72 73 /** 74 * Creates a new DebugHandler which forwards events to the given document 75 * handler. 76 * 77 * @param handler the DocumentHandler to forward events to 78 */ 79 public DebugHandler(final DocumentHandler handler) { 80 this(handler, null); 81 } 82 83 /** 84 * Creates a new DebugHandler which forwards events to the given document 85 * handler. 86 * 87 * @param handler the DocumentHandler to forward events to 88 * @param out the Writer to print debug information to 89 */ 90 public DebugHandler(final DocumentHandler handler, final Writer out) { 91 if (out == null) { 92 this._out = new PrintWriter(System.out); 93 } 94 this._handler = handler; 95 } 96 97 /** 98 * {@inheritDoc} 99 * Proxies the org.sax.xml.DocumentHandler#characters(char[], int, int) 100 * request to the proxy that is provided, printing debugging information 101 * before doing the proxy. 102 */ 103 public void characters(final char[] ch, final int start, final int length) throws org.xml.sax.SAXException { 104 try { 105 _out.write(ch, start, length); 106 _out.flush(); 107 } catch (java.io.IOException ioe) { 108 ioe.printStackTrace(); 109 } 110 111 if (_handler != null) { 112 _handler.characters(ch, start, length); 113 } 114 } 115 116 /** 117 * {@inheritDoc} Proxies the org.sax.xml.DocumentHandler#endDocument() 118 * request to the proxy that is provided, printing debugging information 119 * before doing the proxy. 120 */ 121 public void endDocument() throws org.xml.sax.SAXException { 122 try { 123 _out.write("#endDocument\n"); 124 _out.flush(); 125 } catch (java.io.IOException ioe) { 126 ioe.printStackTrace(); 127 } 128 129 if (_handler != null) { 130 _handler.endDocument(); 131 } 132 } 133 134 /** 135 * {@inheritDoc} Proxies the org.sax.xml.DocumentHandler#endElement(String) 136 * request to the proxy that is provided, printing debugging information 137 * before doing the proxy. 138 */ 139 public void endElement(final String name) throws org.xml.sax.SAXException { 140 try { 141 _out.write("</"); 142 _out.write(name); 143 _out.write(">\n"); 144 _out.flush(); 145 } catch (java.io.IOException ioe) { 146 ioe.printStackTrace(); 147 } 148 149 if (_handler != null) { 150 _handler.endElement(name); 151 } 152 } 153 154 /** 155 * {@inheritDoc} Proxies the 156 * org.sax.xml.DocumentHandler#ignorableWhitespace(char[], int, int) request 157 * to the proxy that is provided, printing debugging information before 158 * doing the proxy. 159 */ 160 public void ignorableWhitespace(final char[] ch, final int start, final int length) throws org.xml.sax.SAXException { 161 if (_handler != null) { 162 _handler.ignorableWhitespace(ch, start, length); 163 } 164 } 165 166 /** 167 * {@inheritDoc} Proxies the 168 * org.sax.xml.DocumentHandler#processingInstruction(String, String) request 169 * to the proxy that is provided, printing debugging information before 170 * doing the proxy. 171 */ 172 public void processingInstruction(final String target, final String data) throws org.xml.sax.SAXException { 173 try { 174 _out.write("--#processingInstruction\n"); 175 _out.write("target: "); 176 _out.write(target); 177 _out.write(" data: "); 178 _out.write(data); 179 _out.write('\n'); 180 _out.flush(); 181 } catch (java.io.IOException ioe) { 182 ioe.printStackTrace(); 183 } 184 185 if (_handler != null) { 186 _handler.processingInstruction(target, data); 187 } 188 } 189 190 /** 191 * {@inheritDoc} Proxies the 192 * org.sax.xml.DocumentHandler#setDocumentLocator(Locator) request to the 193 * proxy that is provided, printing debugging information before doing the 194 * proxy. 195 */ 196 public void setDocumentLocator(final Locator locator) { 197 if (_handler != null) { 198 _handler.setDocumentLocator(locator); 199 } 200 } 201 202 /** 203 * {@inheritDoc} Proxies the org.sax.xml.DocumentHandler#startDocument() 204 * request to the proxy that is provided, printing debugging information 205 * before doing the proxy. 206 */ 207 public void startDocument() throws org.xml.sax.SAXException { 208 try { 209 _out.write("#startDocument\n"); 210 _out.flush(); 211 } catch (java.io.IOException ioe) { 212 ioe.printStackTrace(); 213 } 214 215 if (_handler != null) { 216 _handler.startDocument(); 217 } 218 } 219 220 /** 221 * {@inheritDoc} Proxies the 222 * org.sax.xml.DocumentHandler#startElement(String, AttributeList) request 223 * to the proxy that is provided, printing debugging information before 224 * doing the proxy. 225 */ 226 public void startElement(final String name, final AttributeList atts) throws org.xml.sax.SAXException { 227 try { 228 _out.write('<'); 229 _out.write(name); 230 if (atts != null && atts.getLength() > 0) { 231 for (int i = 0; i < atts.getLength(); i++) { 232 _out.write(' '); 233 _out.write(atts.getName(i)); 234 _out.write("=\""); 235 _out.write(atts.getValue(i)); 236 _out.write("\""); 237 } 238 } 239 _out.write(">\n"); 240 _out.flush(); 241 } catch (java.io.IOException ioe) { 242 ioe.printStackTrace(); 243 } 244 245 if (_handler != null) { 246 _handler.startElement(name, atts); 247 } 248 } 249 250 }