KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > serialization > ElementProcessorSerializer


1 /*
2  * Copyright 1999-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.cocoon.serialization;
17
18 import java.io.IOException JavaDoc;
19 import java.io.OutputStream JavaDoc;
20 import java.util.Stack JavaDoc;
21
22 import org.apache.avalon.framework.logger.AbstractLogEnabled;
23 import org.apache.avalon.framework.service.ServiceManager;
24 import org.apache.avalon.framework.service.Serviceable;
25
26 import org.apache.cocoon.components.elementprocessor.CannotCreateElementProcessorException;
27 import org.apache.cocoon.components.elementprocessor.ElementProcessor;
28 import org.apache.cocoon.components.elementprocessor.ElementProcessorFactory;
29 import org.apache.cocoon.components.elementprocessor.types.Attribute;
30
31 import org.xml.sax.Attributes JavaDoc;
32 import org.xml.sax.Locator JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34
35 /**
36  * An implementation of nearly all of the methods included in the
37  * org.apache.poi.serialization.Serializer interface
38  *
39  * This is an abstract class. Concrete extensions need to implement
40  * the following methods:
41  * <ul>
42  * <li>String getMimeType()</li>
43  * <li>void endDocument()</li>
44  * <li>ElementProcessorFactory getElementProcessorFactory()</li>
45  * <li>void doPreInitialization(ElementProcessor processor)</li>
46  * </ul>
47  *
48  * @author Marc Johnson (marc_johnson27591@hotmail.com)
49  * @author Nicola Ken Barozzi (nicolaken@apache.org)
50  * @version CVS $Id: ElementProcessorSerializer.java 30932 2004-07-29 17:35:38Z vgritsenko $
51  */

52 public abstract class ElementProcessorSerializer
53     extends AbstractLogEnabled implements Serializer, Serviceable
54 {
55     private static final boolean _should_set_content_length = false;
56     private OutputStream JavaDoc _output_stream;
57     private Stack JavaDoc _open_elements;
58     private Locator JavaDoc _locator;
59     /** Service Manager */
60     protected ServiceManager manager = null;
61
62     /**
63      * Constructor
64      */

65
66     public ElementProcessorSerializer() {
67         _output_stream = null;
68         _open_elements = new Stack JavaDoc();
69         _locator = null;
70     }
71
72     public void service(ServiceManager manager) {
73         this.manager = manager;
74     }
75     
76     /**
77      * get the appropriate ElementProcessorFactory
78      *
79      * @return an ElementProcessorFactory suitable for the file type
80      */

81
82     protected abstract ElementProcessorFactory getElementProcessorFactory();
83
84     /**
85      * perform whatever pre-initialization seems good on the
86      * ElementProcessor
87      *
88      * @param processor the processor to be initialized
89      *
90      * @exception SAXException on errors
91      */

92
93     protected abstract void doPreInitialization(ElementProcessor processor)
94         throws SAXException JavaDoc;
95
96     /**
97      * @return the output stream
98      */

99
100     protected OutputStream JavaDoc getOutputStream() {
101         return _output_stream;
102     }
103
104     /**
105      * Create a new SAXException
106      *
107      * @param message the exception message
108      * @param e the underlying exception (may be null)
109      *
110      * @return new SAXException
111      */

112
113     protected SAXException JavaDoc SAXExceptionFactory(final String JavaDoc message,
114                        final Exception JavaDoc e) {
115         StringBuffer JavaDoc message_buffer = new StringBuffer JavaDoc();
116
117         message_buffer.append((message == null) ? "" : message);
118         if (_locator != null) {
119             message_buffer.append("; System id: \"");
120             message_buffer.append(_locator.getSystemId());
121             message_buffer.append("\"; public id: \"");
122             message_buffer.append(_locator.getPublicId());
123             message_buffer.append("\"; line number: ");
124             message_buffer.append(_locator.getLineNumber());
125             message_buffer.append("; column number: ");
126             message_buffer.append(_locator.getColumnNumber());
127         }
128         SAXException JavaDoc rval = null;
129
130         if (e != null) {
131             rval = new SAXException JavaDoc(message_buffer.toString(), e);
132         } else {
133             rval = new SAXException JavaDoc(message_buffer.toString());
134         }
135         return rval;
136     }
137
138     /**
139      * Create a SAXException
140      *
141      * @param message the exception message
142      *
143      * @return new SAXException
144      */

145
146     protected SAXException JavaDoc SAXExceptionFactory(final String JavaDoc message) {
147         return SAXExceptionFactory(message, null);
148     }
149
150     private ElementProcessor getCurrentElementProcessor() {
151         return _open_elements.empty() ? null
152                               : (ElementProcessor) _open_elements.peek();
153     }
154
155     private char [] cleanupArray(final char [] array, final int start,
156                          final int length) {
157         char[] output = new char[length];
158
159         System.arraycopy(array, start, output, 0, length);
160         return output;
161     }
162
163     /* ********** START implementation of SitemapOutputComponent ********** */
164
165     /**
166      * Set the OutputStream where the requested resource should be
167      * serialized.
168      *
169      * @param out the OutputStream to which the serialized data will
170      * be written
171      */

172
173     public void setOutputStream(final OutputStream JavaDoc out) {
174         _output_stream = out;
175     }
176
177     /**
178      * Test if the component wants to set the content length.
179      *
180      * @return false
181      */

182
183     public boolean shouldSetContentLength() {
184         return _should_set_content_length;
185     }
186
187     /* ********** END implementation of SitemapOutputComponent ********** */
188     /* ********** START implementation of LexicalHandler ********** */
189
190     /**
191      * Report an XML comment anywhere in the document. We don't really
192      * care.
193      *
194      * @param ignored_ch
195      * @param ignored_start
196      * @param ignored_length
197      */

198
199     public void comment(final char [] ignored_ch, final int ignored_start,
200                     final int ignored_length) {
201     }
202
203     /**
204      * Report the end of a CDATA section. We don't really care.
205      */

206
207     public void endCDATA() {
208     }
209
210     /**
211      * Report the end of DTD declarations. We don't really care.
212      */

213
214     public void endDTD() {
215     }
216
217     /**
218      * Report the end of an entity. We don't really care.
219      *
220      * @param ignored_name
221      */

222
223     public void endEntity(final String JavaDoc ignored_name) {
224     }
225
226     /**
227      * Report the start of a CDATA section. We don't really care.
228      */

229
230     public void startCDATA() {
231     }
232
233     /**
234      * Report the start of DTD declarations, if any. We don't really
235      * care.
236      *
237      * @param ignored_name
238      * @param ignored_publicId
239      * @param ignored_systemId
240      */

241
242     public void startDTD(final String JavaDoc ignored_name,
243              final String JavaDoc ignored_publicId, final String JavaDoc ignored_systemId) {
244     }
245
246     /**
247      * Report the beginning of some internal and external XML
248      * entities. We don't really care.
249      *
250      * @param ignored_name
251      */

252
253     public void startEntity(final String JavaDoc ignored_name) {
254     }
255
256     /* ********** END implementation of LexicalHandler ********** */
257     /* ********** START implementation of ContentHandler ********** */
258
259     /**
260      * Receive notification of character data.
261      *
262      * @param ch the character array
263      * @param start the start index in ch
264      * @param length the length of the valid part of ch
265      *
266      * @exception SAXException if anything goes wrong in processing
267      * the character data
268      */

269
270     public void characters(final char [] ch, final int start,
271            final int length) throws SAXException JavaDoc {
272         try {
273             getCurrentElementProcessor().acceptCharacters(cleanupArray(ch,
274                     start, length));
275         } catch (Exception JavaDoc e) {
276             throw SAXExceptionFactory("could not process characters event", e);
277         }
278     }
279
280     /**
281      * Receive notification of the end of an element.
282      *
283      * @param ignored_namespaceURI
284      * @param ignored_localName
285      * @param ignored_qName
286      *
287      * @exception SAXException on any errors processing the event.
288      */

289
290     public void endElement(final String JavaDoc ignored_namespaceURI,
291             final String JavaDoc ignored_localName, final String JavaDoc ignored_qName)
292             throws SAXException JavaDoc {
293         try {
294             getCurrentElementProcessor().endProcessing();
295             _open_elements.pop();
296         } catch (Exception JavaDoc e) {
297             throw SAXExceptionFactory("could not process endElement event",
298                                       e);
299         }
300     }
301
302     /**
303      * End the scope of a prefix-URI mapping. We don't really care.
304      *
305      * @param ignored_prefix
306      */

307
308     public void endPrefixMapping(final String JavaDoc ignored_prefix) {
309     }
310
311     /**
312      * Receive notification of ignorable whitespace in element
313      * content.
314      *
315      * @param ch the character array
316      * @param start the start index in ch
317      * @param length the length of the valid part of ch
318      *
319      * @exception SAXException if anything goes wrong in processing
320      * the character data
321      */

322
323     public void ignorableWhitespace(final char [] ch, final int start,
324                     final int length) throws SAXException JavaDoc {
325         try {
326             getCurrentElementProcessor()
327                 .acceptWhitespaceCharacters(cleanupArray(ch, start, length));
328         } catch (Exception JavaDoc e) {
329             throw SAXExceptionFactory(
330                 "could not process ignorableWhitespace event", e);
331         }
332     }
333
334     /**
335      * Receive notification of a processing instruction. We don't
336      * really care.
337      *
338      * @param ignored_target
339      * @param ignored_data
340      */

341
342     public void processingInstruction(final String JavaDoc ignored_target,
343                                       final String JavaDoc ignored_data) {
344     }
345
346     /**
347      * Receive an object for locating the origin of SAX document
348      * events.
349      *
350      * @param locator the Locator object
351      */

352
353     public void setDocumentLocator(final Locator JavaDoc locator) {
354         _locator = locator;
355     }
356
357     /**
358      * Receive notification of a skipped entity. We don't really care.
359      *
360      * @param ignored_name
361      */

362
363     public void skippedEntity(final String JavaDoc ignored_name) {
364     }
365
366     /**
367      * Receive notification of the beginning of a document.
368      */

369
370     public void startDocument() {
371         // nothing to do; should be ready as soon as we were
372
// constructed
373
}
374
375     /**
376      * Receive notification of the beginning of an element.
377      *
378      * @param namespaceURI the namespace this element is in
379      * @param localName the local name of the element
380      * @param qName the qualified name of the element
381      * @param atts the Attributes, if any, of the element
382      *
383      * @exception SAXException if we cannot create an ElementProcessor
384      * to handle the element
385      */

386
387     public void startElement(final String JavaDoc namespaceURI,
388             final String JavaDoc localName, final String JavaDoc qName, final Attributes JavaDoc atts)
389             throws SAXException JavaDoc {
390         String JavaDoc name = "";
391
392         if ((localName != null) && (localName.length() != 0)) {
393             name = localName;
394         } else if ((qName != null) && (qName.length() != 0)) {
395             name = qName;
396         }
397         ElementProcessor processor;
398
399         try {
400             processor =
401                 getElementProcessorFactory().createElementProcessor(name);
402         } catch (CannotCreateElementProcessorException e) {
403             throw SAXExceptionFactory("could not process startElement event",
404                                       e);
405         }
406         doPreInitialization(processor);
407         Attribute[] attributes = (atts == null) ? new Attribute[0]
408                                                 : new Attribute[atts.getLength()];
409
410         for (int j = 0; j < attributes.length; j++) {
411             attributes[j] = new Attribute(atts.getQName(j), atts.getValue(j));
412         }
413         try {
414             processor.initialize(attributes, getCurrentElementProcessor());
415         } catch (IOException JavaDoc e) {
416             throw SAXExceptionFactory("Exception processing startElement", e);
417         }
418         _open_elements.push(processor);
419     }
420
421     /**
422      * Begin the scope of a prefix-URI Namespace mapping. We don't
423      * really care.
424      *
425      * @param ignored_prefix
426      * @param ignored_uri
427      */

428
429     public void startPrefixMapping(final String JavaDoc ignored_prefix,
430                                    final String JavaDoc ignored_uri) {
431     }
432     /* ********** END implementation of ContentHandler ********** */
433 } // end public abstract class ElementProcessorSerializer
434
Popular Tags