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