1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.betwixt.io;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.xml.sax.Attributes;
21 import org.xml.sax.ContentHandler;
22 import org.xml.sax.SAXException;
23
24
25
26
27 /***
28 * The SAXBeanwriter will send events to a ContentHandler
29 *
30 * @author <a href="mailto:rdonkin@apache.org">Robert Burrell Donkin</a>
31 * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
32 * @version $Id: SAXBeanWriter.java,v 1.15.2.1 2004/06/19 16:24:10 rdonkin Exp $
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
93
94
95
96
97
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
156
157
158 char[] body = text.toCharArray();
159 contentHandler.characters(body, 0, body.length);
160 }
161
162 /***
163 * This will announce the start of the document
164 * to the contenthandler.
165 *
166 * @see org.apache.commons.betwixt.io.AbstractBeanWriter#end()
167 */
168 public void start() throws SAXException {
169 if ( callDocumentEvents ) {
170 contentHandler.startDocument();
171 }
172 }
173
174 /***
175 * This method will announce the end of the document to
176 * the contenthandler.
177 *
178 * @see org.apache.commons.betwixt.io.AbstractBeanWriter#start()
179 */
180 public void end() throws SAXException {
181 if ( callDocumentEvents ) {
182 contentHandler.endDocument();
183 }
184 }
185 }