001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.betwixt.io;
018
019 import org.apache.commons.logging.Log;
020 import org.apache.commons.logging.LogFactory;
021 import org.xml.sax.Attributes;
022 import org.xml.sax.ContentHandler;
023 import org.xml.sax.SAXException;
024
025 // FIX ME
026 // At the moment, namespaces are NOT supported!
027
028 /**
029 * The SAXBeanwriter will send events to a ContentHandler
030 *
031 * @author <a href="mailto:rdonkin@apache.org">Robert Burrell Donkin</a>
032 * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
033 */
034 public class SAXBeanWriter extends AbstractBeanWriter {
035
036 /** Where the output goes */
037 private ContentHandler contentHandler;
038 /** Log used for logging (Doh!) */
039 private Log log = LogFactory.getLog( SAXBeanWriter.class );
040 /** Should document events (ie. start and end) be called? */
041 private boolean callDocumentEvents = true;
042
043 /**
044 * <p> Constructor sets writer used for output.</p>
045 *
046 * @param contentHandler feed events to this content handler
047 */
048 public SAXBeanWriter(ContentHandler contentHandler) {
049 this.contentHandler = contentHandler;
050 }
051
052 /**
053 * Should document events (ie start and end) be called?
054 *
055 * @return true if this SAXWriter should call start
056 * and end of the content handler
057 * @since 0.5
058 */
059 public boolean getCallDocumentEvents() {
060 return callDocumentEvents;
061 }
062
063 /**
064 * Sets whether the document events (ie start and end) should be called.
065 *
066 * @param callDocumentEvents should document events be called
067 * @since 0.5
068 */
069 public void setCallDocumentEvents(boolean callDocumentEvents) {
070 this.callDocumentEvents = callDocumentEvents;
071 }
072
073 /**
074 * <p> Set the log implementation used. </p>
075 *
076 * @return <code>Log</code> implementation that this class logs to
077 */
078 public Log getLog() {
079 return log;
080 }
081
082 /**
083 * <p> Set the log implementation used. </p>
084 *
085 * @param log <code>Log</code> implementation to use
086 */
087 public void setLog(Log log) {
088 this.log = log;
089 }
090
091
092 // Expression methods
093 //-------------------------------------------------------------------------
094
095 // Replaced by new API
096
097 // New API
098 // -------------------------------------------------------------------------
099
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 }