1 16 17 package org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements; 18 19 import java.io.IOException ; 20 import java.util.Locale ; 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 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 _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 _col_attribute = "Col"; 52 private static final String _row_attribute = "Row"; 53 private static final String _expr_id_attribute = "ExprID"; 54 private static final String _cols_attribute = "Cols"; 55 private static final String _rows_attribute = "Rows"; 56 private static final String _value_type_attribute = "ValueType"; 57 private static final String _value_format_attribute = "ValueFormat"; 58 private String locale; private static final Validator _cell_type_validator = new Validator() { 60 public IOException validate(final Number number) { 61 return CellType.isValid(number.intValue()) ? null : new IOException ("\"" + number + "\" is not a legal value"); 62 } 63 }; 64 65 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 90 public int getColumn() throws IOException { 91 if (_col == null) { 92 _col = NumericConverter.extractNonNegativeInteger(getValue(_col_attribute)); 93 } 94 return _col.intValue(); 95 } 96 97 102 public int getRow() throws IOException { 103 if (_row == null) { 104 _row = NumericConverter.extractNonNegativeInteger(getValue(_row_attribute)); 105 } 106 return _row.intValue(); 107 } 108 109 115 public int getExpressionId() throws IOException , NullPointerException { 116 if (!_expr_id_fetched) { 117 String 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 132 public int getColumns() throws IOException , NullPointerException { 133 if (!_cols_fetched) { 134 String 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 150 public int getRows() throws IOException , NullPointerException { 151 if (!_rows_fetched) { 152 String 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 168 public int getCellType() throws IOException , NullPointerException { 169 if (!_value_type_fetched) { 170 String 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 184 public String getFormat() throws IOException { 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 201 public void initialize(final Attribute[] attributes, final ElementProcessor parent) throws IOException { 202 super.initialize(attributes, parent); 203 int cellType = CellType.CELL_TYPE_FORMULA; 205 try { 206 cellType = getCellType(); 207 } catch (NullPointerException ignored) { 208 } 209 _cell = getSheet().getRow(getRow()).createCell(getColumn(), cellType); 210 } 211 212 public String getContent() { 213 String content = getData(); 214 return content; 215 } 216 217 222 public void endProcessing() throws IOException { 223 String content = getContent(); 224 if (content != null && locale != null) { 225 getCell().setLocale(new Locale (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 244 protected Cell getCell() { 245 return _cell; 246 } 247 248 public void setLocale(String locale) { 250 this.locale = locale; 251 } 252 253 } | Popular Tags |