KickJava   Java API By Example, From Geeks To Geeks.

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


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.Locale JavaDoc;
21
22 import org.apache.cocoon.components.elementprocessor.ElementProcessor;
23 import org.apache.cocoon.components.elementprocessor.LocaleAware;
24 import org.apache.cocoon.components.elementprocessor.types.Attribute;
25 import org.apache.cocoon.components.elementprocessor.types.NumericConverter;
26 import org.apache.cocoon.components.elementprocessor.types.NumericResult;
27 import org.apache.cocoon.components.elementprocessor.types.Validator;
28 import org.apache.poi.hssf.util.Region;
29 /**
30  * Implementation of ElementProcessor to handle the "Cell" tag.
31  * This element has several attributes and may contain other elements.
32  *
33  * @author Marc Johnson (marc_johnson27591@hotmail.com)
34  * @version CVS $Id: EPCell.java 37191 2004-08-30 10:15:06Z antonio $
35  */

36 public class EPCell extends BaseElementProcessor implements LocaleAware {
37
38     private Cell _cell;
39     private NumericResult _col;
40     private NumericResult _row;
41     private NumericResult _expr_id;
42     private NumericResult _cols;
43     private NumericResult _rows;
44     private NumericResult _value_type;
45     private String JavaDoc _value_format;
46     private boolean _expr_id_fetched;
47     private boolean _cols_fetched;
48     private boolean _rows_fetched;
49     private boolean _value_type_fetched;
50     private boolean _value_format_fetched;
51     private static final String JavaDoc _col_attribute = "Col";
52     private static final String JavaDoc _row_attribute = "Row";
53     private static final String JavaDoc _expr_id_attribute = "ExprID";
54     private static final String JavaDoc _cols_attribute = "Cols";
55     private static final String JavaDoc _rows_attribute = "Rows";
56     private static final String JavaDoc _value_type_attribute = "ValueType";
57     private static final String JavaDoc _value_format_attribute = "ValueFormat";
58     private String JavaDoc locale; // the locale for this EPCell
59
private static final Validator _cell_type_validator = new Validator() {
60         public IOException JavaDoc validate(final Number JavaDoc number) {
61             return CellType.isValid(number.intValue()) ? null : new IOException JavaDoc("\"" + number + "\" is not a legal value");
62         }
63     };
64
65     /**
66      * constructor
67      */

68     public EPCell() {
69         super(null);
70         _cell = null;
71         _col = null;
72         _row = null;
73         _expr_id = null;
74         _cols = null;
75         _rows = null;
76         _value_type = null;
77         _value_format = null;
78         _expr_id_fetched = false;
79         _cols_fetched = false;
80         _rows_fetched = false;
81         _value_type_fetched = false;
82         _value_format_fetched = false;
83     }
84
85     /**
86      * @return column
87      *
88      * @exception IOException
89      */

90     public int getColumn() throws IOException JavaDoc {
91         if (_col == null) {
92             _col = NumericConverter.extractNonNegativeInteger(getValue(_col_attribute));
93         }
94         return _col.intValue();
95     }
96
97     /**
98      * @return row
99      *
100      * @exception IOException
101      */

102     public int getRow() throws IOException JavaDoc {
103         if (_row == null) {
104             _row = NumericConverter.extractNonNegativeInteger(getValue(_row_attribute));
105         }
106         return _row.intValue();
107     }
108
109     /**
110      * @return expression id
111      *
112      * @exception IOException
113      * @exception NullPointerException
114      */

115     public int getExpressionId() throws IOException JavaDoc, NullPointerException JavaDoc {
116         if (!_expr_id_fetched) {
117             String JavaDoc valueString = getValue(_expr_id_attribute);
118             if (valueString != null) {
119                 _expr_id = NumericConverter.extractPositiveInteger(valueString);
120             }
121             _expr_id_fetched = true;
122         }
123         return _expr_id.intValue();
124     }
125
126     /**
127      * @return columns
128      *
129      * @exception IOException
130      * @exception NullPointerException
131      */

132      public int getColumns() throws IOException JavaDoc, NullPointerException JavaDoc {
133        if (!_cols_fetched) {
134            String JavaDoc valueString = getValue(_cols_attribute);
135            if (valueString != null) {
136                _cols = NumericConverter.extractPositiveInteger(valueString);
137                 _cols_fetched = true;
138             }
139             
140         }
141         return _cols_fetched ?_cols.intValue() : -1;
142     }
143
144     /**
145      * @return rows
146      *
147      * @exception IOException
148      * @exception NullPointerException
149      */

150      public int getRows() throws IOException JavaDoc, NullPointerException JavaDoc {
151       if (!_rows_fetched) {
152            String JavaDoc valueString = getValue(_rows_attribute);
153            if (valueString != null) {
154                _rows = NumericConverter.extractPositiveInteger(valueString);
155                _rows_fetched = true;
156            }
157            
158         }
159         return _rows_fetched ? _rows.intValue() : -1 ;
160     }
161
162     /**
163      * @return cell type as a public member of CellType
164      *
165      * @exception IOException
166      * @exception NullPointerException
167      */

168     public int getCellType() throws IOException JavaDoc, NullPointerException JavaDoc {
169         if (!_value_type_fetched) {
170             String JavaDoc valueString = getValue(_value_type_attribute);
171             if (valueString != null) {
172                 _value_type = NumericConverter.extractInteger(valueString, _cell_type_validator);
173             }
174             _value_type_fetched = true;
175         }
176         return _value_type.intValue();
177     }
178
179     /**
180      * @return format string; null if no such attribute
181      *
182      * @exception IOException
183      */

184     public String JavaDoc getFormat() throws IOException JavaDoc {
185         if (!_value_format_fetched) {
186             _value_format = getValue(_value_format_attribute);
187             _value_format_fetched = true;
188         }
189         return _value_format;
190     }
191
192     /**
193      * Override of initialize() implementation
194      *
195      * @param attributes the array of Attribute instances; may be
196      * empty, will never be null
197      * @param parent the parent ElementProcessor; may be null
198      *
199      * @exception IOException if anything is wrong
200      */

201     public void initialize(final Attribute[] attributes, final ElementProcessor parent) throws IOException JavaDoc {
202         super.initialize(attributes, parent);
203         // default value (when <gmr:Cell> has no ValueType attribute)
204
int cellType = CellType.CELL_TYPE_FORMULA;
205         try {
206             cellType = getCellType();
207         } catch (NullPointerException JavaDoc ignored) {
208         }
209         _cell = getSheet().getRow(getRow()).createCell(getColumn(), cellType);
210     }
211
212     public String JavaDoc getContent() {
213         String JavaDoc content = getData();
214         return content;
215     }
216
217     /**
218      * end processing -- apply content to the cell.
219      *
220      * @exception IOException
221      */

222     public void endProcessing() throws IOException JavaDoc {
223         String JavaDoc content = getContent();
224         if (content != null && locale != null) {
225             // if there is a locale then set it (otherwise the default locale
226
// will be used)
227
getCell().setLocale(new Locale JavaDoc(locale, locale.toUpperCase()));
228         }
229         if (content != null && !content.trim().equals("")) {
230             getCell().setContent(getContent());
231         }
232         
233         if(getColumns() != -1 && getRows() != -1) {
234             getSheet().addMergedRegion(new Region(getRow(),(short)getColumn(),getRow() + getRows() - 1,(short)(getColumn() + getColumns() - 1)));
235         }
236
237     }
238
239     /**
240      * override of getCell()
241      *
242      * @return the cell
243      */

244     protected Cell getCell() {
245         return _cell;
246     }
247
248     // from LocaleAware - set the locale for a cell
249
public void setLocale(String JavaDoc locale) {
250         this.locale = locale;
251     }
252
253 } // end public class EPCell
254
Popular Tags