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: // Expression methods
092: //-------------------------------------------------------------------------
093:
094: // Replaced by new API
095:
096: // New API
097: // -------------------------------------------------------------------------
098:
099: /**
100: * Writes the start tag for an element.
101: *
102: * @param uri the element's namespace uri
103: * @param localName the element's local name
104: * @param qName the element's qualified name
105: * @param attributes the element's attributes
106: * @throws SAXException if an SAX problem occurs during writing
107: * @since 0.5
108: */
109: protected void startElement(WriteContext context, String uri,
110: String localName, String qName, Attributes attributes)
111: throws SAXException {
112: contentHandler.startElement(uri, localName, qName, attributes);
113: }
114:
115: /**
116: * Writes the end tag for an element
117: *
118: * @param uri the element's namespace uri
119: * @param localName the element's local name
120: * @param qName the element's qualified name
121: * @throws SAXException if an SAX problem occurs during writing
122: * @since 0.5
123: */
124: protected void endElement(WriteContext context, String uri,
125: String localName, String qName) throws SAXException {
126: contentHandler.endElement(uri, localName, qName);
127: }
128:
129: /**
130: * Express body text
131: * @param text the element body text
132: * @throws SAXException if the <code>ContentHandler</code> has a problem
133: * @since 0.5
134: */
135: protected void bodyText(WriteContext context, String text)
136: throws SAXException {
137: //TODO:
138: // FIX ME
139: // CHECK UNICODE->CHAR CONVERSION!
140: // THIS WILL QUITE POSSIBLY BREAK FOR NON-ROMAN
141: char[] body = text.toCharArray();
142: contentHandler.characters(body, 0, body.length);
143: }
144:
145: /**
146: * This will announce the start of the document
147: * to the contenthandler.
148: *
149: * @see org.apache.commons.betwixt.io.AbstractBeanWriter#end()
150: */
151: public void start() throws SAXException {
152: if (callDocumentEvents) {
153: contentHandler.startDocument();
154: }
155: }
156:
157: /**
158: * This method will announce the end of the document to
159: * the contenthandler.
160: *
161: * @see org.apache.commons.betwixt.io.AbstractBeanWriter#start()
162: */
163: public void end() throws SAXException {
164: if (callDocumentEvents) {
165: contentHandler.endDocument();
166: }
167: }
168: }
|