1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.betwixt.io; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.xml.sax.Attributes; 22 import org.xml.sax.ContentHandler; 23 import org.xml.sax.SAXException; 24 25 // FIX ME 26 // At the moment, namespaces are NOT supported! 27 28 /** 29 * The SAXBeanwriter will send events to a ContentHandler 30 * 31 * @author <a href="mailto:rdonkin@apache.org">Robert Burrell Donkin</a> 32 * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a> 33 */ 34 public class SAXBeanWriter extends AbstractBeanWriter { 35 36 /** Where the output goes */ 37 private ContentHandler contentHandler; 38 /** Log used for logging (Doh!) */ 39 private Log log = LogFactory.getLog( SAXBeanWriter.class ); 40 /** Should document events (ie. start and end) be called? */ 41 private boolean callDocumentEvents = true; 42 43 /** 44 * <p> Constructor sets writer used for output.</p> 45 * 46 * @param contentHandler feed events to this content handler 47 */ 48 public SAXBeanWriter(ContentHandler contentHandler) { 49 this.contentHandler = contentHandler; 50 } 51 52 /** 53 * Should document events (ie start and end) be called? 54 * 55 * @return true if this SAXWriter should call start 56 * and end of the content handler 57 * @since 0.5 58 */ 59 public boolean getCallDocumentEvents() { 60 return callDocumentEvents; 61 } 62 63 /** 64 * Sets whether the document events (ie start and end) should be called. 65 * 66 * @param callDocumentEvents should document events be called 67 * @since 0.5 68 */ 69 public void setCallDocumentEvents(boolean callDocumentEvents) { 70 this.callDocumentEvents = callDocumentEvents; 71 } 72 73 /** 74 * <p> Set the log implementation used. </p> 75 * 76 * @return <code>Log</code> implementation that this class logs to 77 */ 78 public Log getLog() { 79 return log; 80 } 81 82 /** 83 * <p> Set the log implementation used. </p> 84 * 85 * @param log <code>Log</code> implementation to use 86 */ 87 public void setLog(Log log) { 88 this.log = log; 89 } 90 91 92 // Expression methods 93 //------------------------------------------------------------------------- 94 95 // Replaced by new API 96 97 // New API 98 // ------------------------------------------------------------------------- 99 100 101 /** 102 * Writes the start tag for an element. 103 * 104 * @param uri the element's namespace uri 105 * @param localName the element's local name 106 * @param qName the element's qualified name 107 * @param attributes the element's attributes 108 * @throws SAXException if an SAX problem occurs during writing 109 * @since 0.5 110 */ 111 protected void startElement( 112 WriteContext context, 113 String uri, 114 String localName, 115 String qName, 116 Attributes attributes) 117 throws 118 SAXException { 119 contentHandler.startElement( 120 uri, 121 localName, 122 qName, 123 attributes); 124 } 125 126 /** 127 * Writes the end tag for an element 128 * 129 * @param uri the element's namespace uri 130 * @param localName the element's local name 131 * @param qName the element's qualified name 132 * @throws SAXException if an SAX problem occurs during writing 133 * @since 0.5 134 */ 135 protected void endElement( 136 WriteContext context, 137 String uri, 138 String localName, 139 String qName) 140 throws 141 SAXException { 142 contentHandler.endElement( 143 uri, 144 localName, 145 qName); 146 } 147 148 /** 149 * Express body text 150 * @param text the element body text 151 * @throws SAXException if the <code>ContentHandler</code> has a problem 152 * @since 0.5 153 */ 154 protected void bodyText(WriteContext context, String text) throws SAXException { 155 //TODO: 156 // FIX ME 157 // CHECK UNICODE->CHAR CONVERSION! 158 // THIS WILL QUITE POSSIBLY BREAK FOR NON-ROMAN 159 char[] body = text.toCharArray(); 160 contentHandler.characters(body, 0, body.length); 161 } 162 163 /** 164 * This will announce the start of the document 165 * to the contenthandler. 166 * 167 * @see org.apache.commons.betwixt.io.AbstractBeanWriter#end() 168 */ 169 public void start() throws SAXException { 170 if ( callDocumentEvents ) { 171 contentHandler.startDocument(); 172 } 173 } 174 175 /** 176 * This method will announce the end of the document to 177 * the contenthandler. 178 * 179 * @see org.apache.commons.betwixt.io.AbstractBeanWriter#start() 180 */ 181 public void end() throws SAXException { 182 if ( callDocumentEvents ) { 183 contentHandler.endDocument(); 184 } 185 } 186 }