1 16 17 package org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements; 18 19 import java.io.IOException ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 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 44 public abstract class BaseElementProcessor extends AbstractLogEnabled 45 implements POIFSElementProcessor 46 { 47 private Map _attributes; 48 private POIFSFileSystem _filesystem; 49 private BaseElementProcessor _parent; 50 private StringBuffer _data; 51 52 61 62 protected BaseElementProcessor(final Attribute [] implied_attributes) { 63 _attributes = new HashMap (); 64 _filesystem = null; 65 _parent = null; 66 _data = new StringBuffer (); 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 80 81 protected Iterator getAttributes() { 82 return _attributes.values().iterator(); 83 } 84 85 93 94 protected String getValue(final String name) { 95 String 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 107 108 protected POIFSFileSystem getFilesystem() { 109 return _filesystem; 110 } 111 112 113 116 117 protected ElementProcessor getParent() { 118 return _parent; 119 } 120 121 130 131 protected ElementProcessor getAncestor(final Class 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 144 145 protected String getData() { 146 return _data.toString().trim(); 147 } 148 149 157 158 protected Workbook getWorkbook() throws IOException { 159 if (_parent != null) { 160 return _parent.getWorkbook(); 161 } else { 162 throw new IOException ("Cannot find the workbook object"); 165 } 166 } 167 168 176 177 protected Sheet getSheet() throws IOException { 178 if (_parent != null) { 179 return _parent.getSheet(); 180 } else { 181 throw new IOException ("Cannot find the sheet object"); 184 } 185 } 186 187 195 196 protected Cell getCell() throws IOException { 197 if (_parent != null) { 198 return _parent.getCell(); 199 } else { 200 throw new IOException ("Cannot find the cell object"); 203 } 204 } 205 206 207 208 252 253 public void initialize(final Attribute [] attributes, 254 final ElementProcessor parent) throws IOException { 255 try { 256 _parent = ( BaseElementProcessor ) parent; 257 } catch (ClassCastException ignored) { 258 throw new CascadingIOException( 259 "parent is not compatible with this serializer", ignored); 260 } 261 262 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 287 288 public void acceptCharacters(final char [] data) { 289 if (data != null) { 290 _data.append(data); 291 } 292 } 293 294 310 311 public void acceptWhitespaceCharacters(final char [] data) { 312 if (data != null) { 313 _data.append(data); 314 } 315 } 316 317 331 332 public void endProcessing() throws IOException { 333 _filesystem = null; 334 _parent = null; 335 } 336 337 338 339 340 345 346 public void setFilesystem(POIFSFileSystem fs) { 347 _filesystem = fs; 348 } 349 350 351 } | Popular Tags |