KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.NumberFormat JavaDoc;
21 import java.text.ParseException JavaDoc;
22 import java.util.Locale JavaDoc;
23
24 import org.apache.cocoon.CascadingIOException;
25
26 import org.apache.poi.hssf.usermodel.HSSFCell;
27 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
28
29 /**
30  * internal representation of a Cell
31  *
32  * @author Marc Johnson (marc_johnson27591@hotmail.com)
33  * @version CVS $Id: Cell.java 30932 2004-07-29 17:35:38Z vgritsenko $
34  */

35 // package scope
36

37 class Cell {
38     private HSSFCell _cell;
39
40     // original CellType value
41
private int _celltype;
42     private Locale JavaDoc locale;
43
44     /**
45      * Constructor Cell.
46      * Only a hack as long as the POI stuff is not maintained in the POI CVS:
47      * Setting the encoding to UTF-16 for internationalization
48      * (<a HREF="http://jakarta.apache.org/poi/javadocs/org/apache/poi/hssf/usermodel/HSSFCell.html#getEncoding()">POI API</a>).
49      *
50      * @param cell
51      */

52     Cell(final HSSFCell cell, final int cellType) {
53         _cell = cell;
54         _celltype = cellType;
55         _cell.setEncoding(HSSFCell.ENCODING_UTF_16);
56     }
57
58     /**
59      * if there is a locale that can be used for validation it is
60      * set here. Cell expects a fully constructed locale. It must
61      * be passed in before SetContent can be called.
62      */

63     void setLocale(Locale JavaDoc locale) {
64         this.locale = locale;
65     }
66
67     /**
68      * set content
69      *
70      * @param content the value of the cell, as a string
71      *
72      * @exception IOException
73      */

74     void setContent(final String JavaDoc content) throws IOException JavaDoc {
75         if (_cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
76             try {
77                 if (_celltype == CellType.CELL_TYPE_FLOAT) {
78                     // if there is a locale set then we'll use it to parse the
79
// string form of the number... otherwise we'll use the default.
80
NumberFormat JavaDoc form = null;
81                     if (locale == null) {
82                         form = NumberFormat.getInstance();
83                     } else {
84                         form = NumberFormat.getInstance(locale);
85                     }
86                     _cell.setCellValue(form.parse(content).doubleValue());
87                 } else {
88                     _cell.setCellValue(Integer.parseInt(content));
89                 }
90             } catch (NumberFormatException JavaDoc e) {
91                 throw new CascadingIOException("Invalid value for a numeric cell: " + content, e);
92             } catch (ParseException JavaDoc e) {
93                 throw new CascadingIOException("Invalid value for a numeric cell: " + content, e);
94             }
95         } else if (_cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
96             _cell.setCellValue(content);
97         } else if (_cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
98             _cell.setCellFormula(content.toUpperCase().substring(1));
99         }
100     }
101
102     void setStyle(HSSFCellStyle style) {
103         if (style != null) {
104             _cell.setCellStyle(style);
105         }
106     }
107
108     /**
109      * @return cell type
110      */

111     int getCellType() {
112         return _cell.getCellType();
113     }
114
115     /**
116      * @return string value
117      */

118     String JavaDoc getStringValue() {
119         return _cell.getStringCellValue();
120     }
121
122     /**
123      * @return numeric value
124      */

125     double getNumericValue() {
126         return _cell.getNumericCellValue();
127     }
128 } // end package scope class Cell
129
Popular Tags