KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > elementprocessor > impl > poi > hssf > elements > BaseElementProcessor


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
17 package org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements;
18
19 import java.io.IOException JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.avalon.framework.logger.AbstractLogEnabled;
25 import org.apache.cocoon.CascadingIOException;
26 import org.apache.cocoon.components.elementprocessor.ElementProcessor;
27 import org.apache.cocoon.components.elementprocessor.impl.poi.POIFSElementProcessor;
28 import org.apache.cocoon.components.elementprocessor.types.Attribute;
29 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
30
31 /**
32  * The BaseElementProcessor class provides default behavior for
33  * classes that can handle a particular XML element's content.
34  *
35  * It is intended that all ElementProcessors should extend this class.
36  * This class is declared Abstract although it has no abstract
37  * methods. It can be used 'as is' for a no-op ElementProcessor, and
38  * an ElementProcessor that chooses to simply extend this class with
39  * no overrides will be a no-op ElementProcessor.
40  *
41  * @author Marc Johnson (marc_johnson27591@hotmail.com)
42  * @version CVS $Id: BaseElementProcessor.java 30932 2004-07-29 17:35:38Z vgritsenko $
43  */

44 public abstract class BaseElementProcessor extends AbstractLogEnabled
45      implements POIFSElementProcessor
46 {
47     private Map JavaDoc _attributes;
48     private POIFSFileSystem _filesystem;
49     private BaseElementProcessor _parent;
50     private StringBuffer JavaDoc _data;
51     
52     /**
53      * Constructor
54      *
55      * @param implied_attributes an array of Attributes which might
56      * not appear in the initialization call
57      * because they are implied, and the SAX
58      * processor will not automatically
59      * generate them.
60      */

61
62     protected BaseElementProcessor(final Attribute [] implied_attributes) {
63         _attributes = new HashMap JavaDoc();
64         _filesystem = null;
65         _parent = null;
66         _data = new StringBuffer JavaDoc();
67
68         if (implied_attributes != null) {
69             for (int k = 0; k < implied_attributes.length; k++) {
70                 _attributes.put(implied_attributes[ k ].getName(),
71                                 implied_attributes[ k ]);
72             }
73         }
74     }
75
76     /**
77      * @return the Attributes managed by this ElementProcessor. May be
78      * empty, but never null
79      */

80
81     protected Iterator JavaDoc getAttributes() {
82         return _attributes.values().iterator();
83     }
84
85     /**
86      * Get the value of an attribute
87      *
88      * @param name the name of the attribute to look up
89      *
90      * @return the value of the specified attribute; will be null if
91      * the attribute doesn't exist
92      */

93
94     protected String JavaDoc getValue(final String JavaDoc name) {
95         String JavaDoc value = null;
96         Attribute attr = ( Attribute ) _attributes.get(name);
97
98         if (attr != null) {
99             value = attr.getValue();
100         }
101         return value;
102     }
103
104     /**
105      * @return the POIFSFileSystem object
106      */

107
108     protected POIFSFileSystem getFilesystem() {
109         return _filesystem;
110     }
111
112
113     /**
114      * @return the parent ElementProcessor; may be null
115      */

116
117     protected ElementProcessor getParent() {
118         return _parent;
119     }
120
121     /**
122      * get the nearest ancestor that happens to be an instance of the
123      * specified class
124      *
125      * @param theclass the class we're looking for
126      *
127      * @return an ancestor of the specified class; null if no such
128      * ancestor exists
129      */

130
131     protected ElementProcessor getAncestor(final Class JavaDoc theclass) {
132         ElementProcessor parent = getParent();
133         if (parent == null || parent.getClass().equals(theclass)) {
134             return parent;
135         } else {
136             return ((BaseElementProcessor)parent).getAncestor(theclass);
137         }
138     }
139
140     /**
141      * @return the data, both regular characters and whitespace
142      * characters, collected so far
143      */

144
145     protected String JavaDoc getData() {
146         return _data.toString().trim();
147     }
148
149     /**
150      * get the workbook we're working on. If it isn't in this
151      * instance, check the parent
152      *
153      * @return the workbook we're working on. Never null
154      *
155      * @exception IOException if the workbook is missing
156      */

157
158     protected Workbook getWorkbook() throws IOException JavaDoc {
159         if (_parent != null) {
160             return _parent.getWorkbook();
161         } else {
162             // hit the end of the containment hierarchy without
163
// finding it. This Is Bad
164
throw new IOException JavaDoc("Cannot find the workbook object");
165         }
166     }
167
168     /**
169      * get the current sheet we're working on. If it isn't in this
170      * instance, check the parent
171      *
172      * @return the current sheet we're working on. Never null
173      *
174      * @exception IOException if the sheet is missing
175      */

176
177     protected Sheet getSheet() throws IOException JavaDoc {
178         if (_parent != null) {
179             return _parent.getSheet();
180         } else {
181             // hit the end of the containment hierarchy without
182
// finding it. This Is Bad
183
throw new IOException JavaDoc("Cannot find the sheet object");
184         }
185     }
186
187     /**
188      * get the current cell we're working on. If it isn't in this
189      * instance, check the parent
190      *
191      * @return the current cell we're working on. Never null
192      *
193      * @exception IOException if the cell is missing
194      */

195
196     protected Cell getCell() throws IOException JavaDoc {
197         if (_parent != null) {
198             return _parent.getCell();
199         } else {
200             // hit the end of the containment hierarchy without
201
// finding it. This Is Bad
202
throw new IOException JavaDoc("Cannot find the cell object");
203         }
204     }
205
206     /* ********** START implementation of ElementProcessor ********** */
207
208     /**
209      * The implementation should walk the array of attributes and
210      * perform appropriate actions based on that data. The array of
211      * attributes is guaranteed never to be null, but it can be
212      * empty. An implementation can expect code like this to work
213      * without throwing runtime exceptions:
214      * <br>
215      * <code>
216      * for (int k = 0; k < attributes.length; k++)<br>
217      * {<br>
218      * &nbsp;&nbsp;&nbsp;&nbsp;Attribute attr = attributes[ k ];<br>
219      * &nbsp;&nbsp;&nbsp;&nbsp;// process attribute</br>
220      * }</br>
221      * </code>
222      * <br>
223      * <b>NOTE: if the XML DTD or schema includes <i>IMPLIED</i>
224      * attributes for an element, those attributes are not included in
225      * the Attribute array - they're not in the data provided in the
226      * startElement call. The constructor for the ElementProcessor
227      * should set those implied attributes itself, and allow them to
228      * be overridden if the XML source provided <i>explicit</i> values
229      * for them.</b>
230      * <br>
231      * The parent ElementProcessor is a reference to the
232      * ElementProcessor whose corresponding XML element contains this
233      * ElementProcessor's corresponding XML element. It will not be
234      * null unless this ElementProcessor's corresponding XML element
235      * is the top-level element in the XML document. Whether this
236      * ElementProcessor needs to establish a containment relationship
237      * with its parent or not, and whether it needs to do so at the
238      * beginning, middle, or end of its life cycle, are private
239      * matters left to the discretion of the individual
240      * ElementProcessor implementation. If the ElementProcessor needs
241      * to interact with its parent ElementProcessor, but is not ready
242      * to do so when the initialize method is called, it must cache
243      * the reference to its parent.
244      * <br>
245      *
246      * @param attributes the array of Attribute instances; may be
247      * empty, will never be null
248      * @param parent the parent ElementProcessor; may be null
249      *
250      * @exception IOException if anything is wrong
251      */

252
253     public void initialize(final Attribute [] attributes,
254                            final ElementProcessor parent) throws IOException JavaDoc {
255         try {
256             _parent = ( BaseElementProcessor ) parent;
257         } catch (ClassCastException JavaDoc ignored) {
258             throw new CascadingIOException(
259                 "parent is not compatible with this serializer", ignored);
260         }
261
262         // can't trust the guarantee -- an overriding implementation
263
// may have screwed this up
264
if (attributes != null) {
265             for (int k = 0; k < attributes.length; k++) {
266                 _attributes.put(attributes[ k ].getName(), attributes[ k ]);
267             }
268         }
269     }
270
271     /**
272      * The data provided in this method call comes from the
273      * corresponding XML element's contents. The array is guaranteed
274      * not to be null and will never be empty.
275      * <br>
276      * The POIFSSerializer will make 0 to many calls to this method;
277      * all such calls will occur after the initialize method is called
278      * and before the endProcessing method is called.
279      * <br>
280      * Calls to this method may be interleaved with calls to
281      * acceptWhitespaceCharacters. All calls to acceptCharacters and
282      * acceptWhitespaceCharacters are guaranteed to be in the same
283      * order as their data is in the element.
284      *
285      * @param data the character data
286      */

287
288     public void acceptCharacters(final char [] data) {
289         if (data != null) {
290             _data.append(data);
291         }
292     }
293
294     /**
295      * The data provided in this method call comes from the
296      * corresponding XML element's contents. The array is guaranteed
297      * not to be null and will never be empty.
298      * <br>
299      * The POIFSSerializer will make 0 to many calls to this method;
300      * all such calls will occur after the initialize method is called
301      * and before the endProcessing method is called.
302      * <br>
303      * Calls to this method may be interleaved with calls to
304      * acceptCharacters. All calls to acceptCharacters and
305      * acceptWhitespaceCharacters are guaranteed to be in the same
306      * order as their data is in the element.
307      *
308      * @param data the whitespace characters
309      */

310
311     public void acceptWhitespaceCharacters(final char [] data) {
312         if (data != null) {
313             _data.append(data);
314         }
315     }
316
317     /**
318      * This is the last method call executed by the
319      * POIFSSerializer. When this method is called, the
320      * ElementProcessor should finish its work and perform its final
321      * interactions with its parent ElementProcessor and the
322      * filesystem, if necessary.
323      * <br>
324      * If the implementation cached the parent ElementProcessor
325      * reference, the filesystem reference, or both, when the
326      * initialize method was called, it should null out those
327      * references.
328      *
329      * @exception IOException
330      */

331
332     public void endProcessing() throws IOException JavaDoc {
333         _filesystem = null;
334         _parent = null;
335     }
336
337     /* ********** END implementation of ElementProcessor ********** */
338     /* ********** START implementation of POIFSElementProcessor ********** */
339
340     /**
341      * Set the POIFSFileSystem for the element processor
342      *
343      * @param fs the POIFSFileSystem instance
344      */

345
346     public void setFilesystem(POIFSFileSystem fs) {
347         _filesystem = fs;
348     }
349
350     /* ********** END implementation of POIFSElementProcessor ********** */
351 } // end package scope abstract class BaseElementProcessor
352
Popular Tags