001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements;
019:
020: import java.io.IOException;
021: import java.util.Locale;
022:
023: import org.apache.cocoon.components.elementprocessor.ElementProcessor;
024: import org.apache.cocoon.components.elementprocessor.LocaleAware;
025: import org.apache.cocoon.components.elementprocessor.types.Attribute;
026: import org.apache.cocoon.components.elementprocessor.types.NumericConverter;
027: import org.apache.cocoon.components.elementprocessor.types.NumericResult;
028: import org.apache.cocoon.components.elementprocessor.types.Validator;
029: import org.apache.poi.hssf.util.Region;
030:
031: /**
032: * Implementation of ElementProcessor to handle the "Cell" tag.
033: * This element has several attributes and may contain other elements.
034: *
035: * @author Marc Johnson (marc_johnson27591@hotmail.com)
036: * @version CVS $Id: EPCell.java 433543 2006-08-22 06:22:54Z crossley $
037: */
038: public class EPCell extends BaseElementProcessor implements LocaleAware {
039:
040: private Cell _cell;
041: private NumericResult _col;
042: private NumericResult _row;
043: private NumericResult _expr_id;
044: private NumericResult _cols;
045: private NumericResult _rows;
046: private NumericResult _value_type;
047: private String _value_format;
048: private boolean _expr_id_fetched;
049: private boolean _cols_fetched;
050: private boolean _rows_fetched;
051: private boolean _value_type_fetched;
052: private boolean _value_format_fetched;
053: private static final String _col_attribute = "Col";
054: private static final String _row_attribute = "Row";
055: private static final String _expr_id_attribute = "ExprID";
056: private static final String _cols_attribute = "Cols";
057: private static final String _rows_attribute = "Rows";
058: private static final String _value_type_attribute = "ValueType";
059: private static final String _value_format_attribute = "ValueFormat";
060: private String locale; // the locale for this EPCell
061: private static final Validator _cell_type_validator = new Validator() {
062: public IOException validate(final Number number) {
063: return CellType.isValid(number.intValue()) ? null
064: : new IOException("\"" + number
065: + "\" is not a legal value");
066: }
067: };
068:
069: /**
070: * constructor
071: */
072: public EPCell() {
073: super (null);
074: _cell = null;
075: _col = null;
076: _row = null;
077: _expr_id = null;
078: _cols = null;
079: _rows = null;
080: _value_type = null;
081: _value_format = null;
082: _expr_id_fetched = false;
083: _cols_fetched = false;
084: _rows_fetched = false;
085: _value_type_fetched = false;
086: _value_format_fetched = false;
087: }
088:
089: /**
090: * @return column
091: *
092: * @exception IOException
093: */
094: public int getColumn() throws IOException {
095: if (_col == null) {
096: _col = NumericConverter
097: .extractNonNegativeInteger(getValue(_col_attribute));
098: }
099: return _col.intValue();
100: }
101:
102: /**
103: * @return row
104: *
105: * @exception IOException
106: */
107: public int getRow() throws IOException {
108: if (_row == null) {
109: _row = NumericConverter
110: .extractNonNegativeInteger(getValue(_row_attribute));
111: }
112: return _row.intValue();
113: }
114:
115: /**
116: * @return expression id
117: *
118: * @exception IOException
119: * @exception NullPointerException
120: */
121: public int getExpressionId() throws IOException,
122: NullPointerException {
123: if (!_expr_id_fetched) {
124: String valueString = getValue(_expr_id_attribute);
125: if (valueString != null) {
126: _expr_id = NumericConverter
127: .extractPositiveInteger(valueString);
128: }
129: _expr_id_fetched = true;
130: }
131: return _expr_id.intValue();
132: }
133:
134: /**
135: * @return columns
136: *
137: * @exception IOException
138: * @exception NullPointerException
139: */
140: public int getColumns() throws IOException, NullPointerException {
141: if (!_cols_fetched) {
142: String valueString = getValue(_cols_attribute);
143: if (valueString != null) {
144: _cols = NumericConverter
145: .extractPositiveInteger(valueString);
146: _cols_fetched = true;
147: }
148:
149: }
150: return _cols_fetched ? _cols.intValue() : -1;
151: }
152:
153: /**
154: * @return rows
155: *
156: * @exception IOException
157: * @exception NullPointerException
158: */
159: public int getRows() throws IOException, NullPointerException {
160: if (!_rows_fetched) {
161: String valueString = getValue(_rows_attribute);
162: if (valueString != null) {
163: _rows = NumericConverter
164: .extractPositiveInteger(valueString);
165: _rows_fetched = true;
166: }
167:
168: }
169: return _rows_fetched ? _rows.intValue() : -1;
170: }
171:
172: /**
173: * @return cell type as a public member of CellType
174: *
175: * @exception IOException
176: * @exception NullPointerException
177: */
178: public int getCellType() throws IOException, NullPointerException {
179: if (!_value_type_fetched) {
180: String valueString = getValue(_value_type_attribute);
181: if (valueString != null) {
182: _value_type = NumericConverter.extractInteger(
183: valueString, _cell_type_validator);
184: }
185: _value_type_fetched = true;
186: }
187: return _value_type.intValue();
188: }
189:
190: /**
191: * @return format string; null if no such attribute
192: *
193: * @exception IOException
194: */
195: public String getFormat() throws IOException {
196: if (!_value_format_fetched) {
197: _value_format = getValue(_value_format_attribute);
198: _value_format_fetched = true;
199: }
200: return _value_format;
201: }
202:
203: /**
204: * Override of initialize() implementation
205: *
206: * @param attributes the array of Attribute instances; may be
207: * empty, will never be null
208: * @param parent the parent ElementProcessor; may be null
209: *
210: * @exception IOException if anything is wrong
211: */
212: public void initialize(final Attribute[] attributes,
213: final ElementProcessor parent) throws IOException {
214: super .initialize(attributes, parent);
215: // default value (when <gmr:Cell> has no ValueType attribute)
216: int cellType = CellType.CELL_TYPE_FORMULA;
217: try {
218: cellType = getCellType();
219: } catch (NullPointerException ignored) {
220: }
221: _cell = getSheet().getRow(getRow()).createCell(getColumn(),
222: cellType);
223: }
224:
225: public String getContent() {
226: String content = getData();
227: return content;
228: }
229:
230: /**
231: * end processing -- apply content to the cell.
232: *
233: * @exception IOException
234: */
235: public void endProcessing() throws IOException {
236: String content = getContent();
237: if (content != null && locale != null) {
238: // if there is a locale then set it (otherwise the default locale
239: // will be used)
240: getCell().setLocale(
241: new Locale(locale, locale.toUpperCase()));
242: }
243: if (content != null && !content.trim().equals("")) {
244: getCell().setContent(getContent());
245: }
246:
247: if (getColumns() != -1 && getRows() != -1) {
248: getSheet().addMergedRegion(
249: new Region(getRow(), (short) getColumn(), getRow()
250: + getRows() - 1, (short) (getColumn()
251: + getColumns() - 1)));
252: }
253:
254: }
255:
256: /**
257: * override of getCell()
258: *
259: * @return the cell
260: */
261: protected Cell getCell() {
262: return _cell;
263: }
264:
265: // from LocaleAware - set the locale for a cell
266: public void setLocale(String locale) {
267: this .locale = locale;
268: }
269:
270: } // end public class EPCell
|