View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/SAXBeanWriter.java,v 1.6 2003/01/08 22:07:21 rdonkin Exp $ 3 * $Revision: 1.6 $ 4 * $Date: 2003/01/08 22:07:21 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>;. 59 * 60 * $Id: SAXBeanWriter.java,v 1.6 2003/01/08 22:07:21 rdonkin Exp $ 61 */ 62 package org.apache.commons.betwixt.io; 63 64 import java.util.Stack; 65 66 import org.apache.commons.logging.Log; 67 import org.apache.commons.logging.LogFactory; 68 import org.xml.sax.ContentHandler; 69 import org.xml.sax.SAXException; 70 import org.xml.sax.helpers.AttributesImpl; 71 72 // FIX ME 73 // At the moment, namespaces are NOT supported! 74 75 /*** 76 * The SAXBeanwriter will send events to a ContentHandler 77 * 78 * @author <a href="mailto:rdonkin@apache.org">Robert Burrell Donkin</a> 79 * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a> 80 * @version $Id: SAXBeanWriter.java,v 1.6 2003/01/08 22:07:21 rdonkin Exp $ 81 */ 82 public class SAXBeanWriter extends AbstractBeanWriter { 83 84 /*** Where the output goes */ 85 private ContentHandler contentHandler; 86 /*** Log used for logging (Doh!) */ 87 private Log log = LogFactory.getLog( SAXBeanWriter.class ); 88 89 /*** 90 * Place holder for elements that are started. 91 */ 92 private Stack elementStack; 93 /*** Current element's attributes. */ 94 private AttributesImpl attributes; 95 /*** Is there a element currently waiting to be written out? */ 96 private boolean elementWaiting = false; 97 98 /*** 99 * <p> Constructor sets writer used for output.</p> 100 * 101 * @param contentHandler feed events to this content handler 102 */ 103 public SAXBeanWriter(ContentHandler contentHandler) { 104 this.contentHandler = contentHandler; 105 } 106 107 /*** 108 * <p> Set the log implementation used. </p> 109 * 110 * @return <code>Log</code> implementation that this class logs to 111 */ 112 public Log getLog() { 113 return log; 114 } 115 116 /*** 117 * <p> Set the log implementation used. </p> 118 * 119 * @param log <code>Log</code> implementation to use 120 */ 121 public void setLog(Log log) { 122 this.log = log; 123 } 124 125 126 // Expression methods 127 //------------------------------------------------------------------------- 128 129 /*** 130 * Express an element tag start using given qualified name 131 * 132 * @param qualifiedName the fully qualified element name 133 * @throws SAXException if the <code>ContentHandler</code> has a problem 134 */ 135 protected void expressElementStart(String qualifiedName) throws SAXException { 136 if (elementStack == null) { 137 elementStack = new Stack(); 138 } 139 if (elementWaiting) { 140 sendElementStart(); 141 } 142 attributes = new AttributesImpl(); 143 elementStack.push(qualifiedName); 144 elementWaiting = true; 145 } 146 147 /*** Element end */ 148 protected void expressTagClose() { 149 // using this could probably make life easier 150 // but i only know that i needed it after i'd written the rest 151 } 152 153 /*** 154 * Express an element end tag 155 * 156 * @param qualifiedName the fully qualified name of the element 157 * @throws SAXException if the <code>ContentHandler</code> has a problem 158 */ 159 protected void expressElementEnd(String qualifiedName) throws SAXException { 160 if (elementWaiting) { 161 elementWaiting = false; 162 sendElementStart(); 163 } 164 // can't handle namespaces yet 165 contentHandler.endElement("","",qualifiedName); 166 } 167 168 /*** 169 * Express an empty element end 170 * @throws SAXException if the <code>ContentHandler</code> has a problem 171 */ 172 protected void expressElementEnd() throws SAXException { 173 // last element name must be correct since there haven't been any tag in between 174 String lastElement = (String) elementStack.peek(); 175 contentHandler.endElement("","",lastElement); 176 } 177 178 /*** 179 * Express body text 180 * @param text the element body text 181 * @throws SAXException if the <code>ContentHandler</code> has a problem 182 */ 183 protected void expressBodyText(String text) throws SAXException { 184 // FIX ME 185 // CHECK UNICODE->CHAR CONVERSION! 186 // THIS WILL QUITE POSSIBLY BREAK FOR NON-ROMAN 187 if (elementWaiting) { 188 elementWaiting = false; 189 sendElementStart(); 190 } 191 char[] body = text.toCharArray(); 192 contentHandler.characters(body, 0, body.length); 193 } 194 195 /*** 196 * Express an attribute 197 * @param qualifiedName the fully qualified attribute name 198 * @param value the attribute value 199 * @throws SAXException if the <code>ContentHandler</code> has a problem 200 */ 201 protected void expressAttribute( 202 String qualifiedName, 203 String value) 204 throws 205 SAXException { 206 // FIX ME 207 // SHOULD PROBABLY SUPPORT ID IDREF HERE 208 attributes.addAttribute("", "", qualifiedName, "CDATA", value); 209 } 210 211 212 // Implementation methods 213 //------------------------------------------------------------------------- 214 215 /*** 216 * Send the start element event to the <code>ContentHandler</code> 217 * @throws SAXException if the <code>ContentHandler</code> has a problem 218 */ 219 private void sendElementStart() throws SAXException { 220 String lastElement = (String)elementStack.peek(); 221 contentHandler.startElement("","",lastElement,attributes); 222 } 223 /*** 224 * This will announce the start of the document 225 * to the contenthandler. 226 * 227 * @see org.apache.commons.betwixt.io.AbstractBeanWriter#end() 228 */ 229 public void start() throws SAXException { 230 contentHandler.startDocument(); 231 } 232 233 /*** 234 * This method will announce the end of the document to 235 * the contenthandler. 236 * 237 * @see org.apache.commons.betwixt.io.AbstractBeanWriter#start() 238 */ 239 public void end() throws SAXException { 240 contentHandler.endDocument(); 241 } 242 243 }

This page was automatically generated by Maven