KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > betwixt > io > SAXBeanWriter


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
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 JavaDoc;
21 import org.xml.sax.ContentHandler JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23
24 // FIX ME
25
// At the moment, namespaces are NOT supported!
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  */

33 public class SAXBeanWriter extends AbstractBeanWriter {
34
35     /** Where the output goes */
36     private ContentHandler JavaDoc contentHandler;
37     /** Log used for logging (Doh!) */
38     private Log log = LogFactory.getLog( SAXBeanWriter.class );
39     /** Should document events (ie. start and end) be called? */
40     private boolean callDocumentEvents = true;
41     
42     /**
43      * <p> Constructor sets writer used for output.</p>
44      *
45      * @param contentHandler feed events to this content handler
46      */

47     public SAXBeanWriter(ContentHandler JavaDoc contentHandler) {
48         this.contentHandler = contentHandler;
49     }
50
51     /**
52      * Should document events (ie start and end) be called?
53      *
54      * @return true if this SAXWriter should call start
55      * and end of the content handler
56      * @since 0.5
57      */

58     public boolean getCallDocumentEvents() {
59         return callDocumentEvents;
60     }
61     
62     /**
63      * Sets whether the document events (ie start and end) should be called.
64      *
65      * @param callDocumentEvents should document events be called
66      * @since 0.5
67      */

68     public void setCallDocumentEvents(boolean callDocumentEvents) {
69         this.callDocumentEvents = callDocumentEvents;
70     }
71
72     /**
73      * <p> Set the log implementation used. </p>
74      *
75      * @return <code>Log</code> implementation that this class logs to
76      */

77     public Log getLog() {
78         return log;
79     }
80
81     /**
82      * <p> Set the log implementation used. </p>
83      *
84      * @param log <code>Log</code> implementation to use
85      */

86     public void setLog(Log log) {
87         this.log = log;
88     }
89     
90         
91     // Expression methods
92
//-------------------------------------------------------------------------
93

94     // Replaced by new API
95

96     // New API
97
// -------------------------------------------------------------------------
98

99         
100     /**
101      * Writes the start tag for an element.
102      *
103      * @param uri the element's namespace uri
104      * @param localName the element's local name
105      * @param qName the element's qualified name
106      * @param attributes the element's attributes
107      * @throws SAXException if an SAX problem occurs during writing
108      * @since 0.5
109      */

110     protected void startElement(
111                                 WriteContext context,
112                                 String JavaDoc uri,
113                                 String JavaDoc localName,
114                                 String JavaDoc qName,
115                                 Attributes JavaDoc attributes)
116                                     throws
117                                         SAXException JavaDoc {
118         contentHandler.startElement(
119                                 uri,
120                                 localName,
121                                 qName,
122                                 attributes);
123     }
124     
125     /**
126      * Writes the end tag for an element
127      *
128      * @param uri the element's namespace uri
129      * @param localName the element's local name
130      * @param qName the element's qualified name
131      * @throws SAXException if an SAX problem occurs during writing
132      * @since 0.5
133      */

134     protected void endElement(
135                                 WriteContext context,
136                                 String JavaDoc uri,
137                                 String JavaDoc localName,
138                                 String JavaDoc qName)
139                                     throws
140                                         SAXException JavaDoc {
141         contentHandler.endElement(
142                                 uri,
143                                 localName,
144                                 qName);
145     }
146
147     /**
148      * Express body text
149      * @param text the element body text
150      * @throws SAXException if the <code>ContentHandler</code> has a problem
151      * @since 0.5
152      */

153     protected void bodyText(WriteContext context, String JavaDoc text) throws SAXException JavaDoc {
154         //TODO:
155
// FIX ME
156
// CHECK UNICODE->CHAR CONVERSION!
157
// THIS WILL QUITE POSSIBLY BREAK FOR NON-ROMAN
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 JavaDoc {
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 JavaDoc {
181         if ( callDocumentEvents ) {
182             contentHandler.endDocument();
183         }
184     }
185 }
186
Popular Tags