1 16 package org.apache.cocoon.generation; 17 18 import org.apache.avalon.framework.configuration.Configurable; 19 import org.apache.avalon.framework.configuration.Configuration; 20 import org.apache.avalon.framework.configuration.ConfigurationException; 21 import org.apache.avalon.framework.parameters.Parameters; 22 23 import org.apache.cocoon.ProcessingException; 24 import org.apache.cocoon.components.source.SourceUtil; 25 import org.apache.cocoon.environment.SourceResolver; 26 import org.apache.commons.lang.BooleanUtils; 27 28 import org.apache.excalibur.source.Source; 29 import org.apache.excalibur.source.SourceException; 30 import org.apache.poi.hssf.usermodel.HSSFCell; 31 import org.apache.poi.hssf.usermodel.HSSFCellStyle; 32 import org.apache.poi.hssf.usermodel.HSSFFont; 33 import org.apache.poi.hssf.usermodel.HSSFRow; 34 import org.apache.poi.hssf.usermodel.HSSFSheet; 35 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 36 import org.xml.sax.SAXException ; 37 import org.xml.sax.helpers.AttributesImpl ; 38 39 import java.io.IOException ; 40 import java.util.Iterator ; 41 import java.util.Map ; 42 43 64 public class HSSFGenerator extends AbstractGenerator 65 implements Configurable { 66 67 public static final String NAMESPACE_PREFIX = "gmr"; 68 public static final String NAMESPACE_URI = "http://www.gnome.org/gnumeric/v7"; 69 private static final boolean FORMATTING = false; 70 71 private static final String CONF_NAMESPACE_URI = "uri"; 72 private static final String CONF_NAMESPACE_PREFIX = "prefix"; 73 private static final String CONF_FORMATTING = "formatting"; 74 75 private String defaultUri; 76 private String defaultPrefix; 77 private boolean defaultFormatting; 78 79 private String uri; 80 private String prefix; 81 private boolean formatting; 82 private final AttributesImpl attr; 83 84 protected Source inputSource; 85 86 87 public HSSFGenerator() { 88 this.attr = new AttributesImpl (); 89 } 90 91 public void configure(Configuration configuration) throws ConfigurationException { 92 this.defaultUri = configuration.getChild(CONF_NAMESPACE_URI).getValue(NAMESPACE_URI); 93 this.defaultPrefix = configuration.getChild(CONF_NAMESPACE_PREFIX).getValue(NAMESPACE_PREFIX); 94 this.defaultFormatting = configuration.getChild(CONF_FORMATTING).getValueAsBoolean(FORMATTING); 95 } 96 97 public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par) 98 throws ProcessingException, SAXException , IOException { 99 super.setup(resolver, objectModel, src, par); 100 this.uri = par.getParameter(CONF_NAMESPACE_URI, this.defaultUri); 101 this.prefix = par.getParameter(CONF_NAMESPACE_PREFIX, this.defaultPrefix); 102 this.formatting = par.getParameterAsBoolean(CONF_FORMATTING, this.defaultFormatting); 103 104 try { 105 this.inputSource = super.resolver.resolveURI(src); 106 } catch (SourceException se) { 107 throw SourceUtil.handle("Error resolving '" + src + "'.", se); 108 } 109 } 110 111 115 public void recycle() { 116 if (this.inputSource != null) { 117 super.resolver.release(this.inputSource); 118 this.inputSource = null; 119 } 120 this.attr.clear(); 121 super.recycle(); 122 } 123 124 127 public void generate() throws SAXException , IOException { 128 HSSFWorkbook workbook = 129 new HSSFWorkbook(this.inputSource.getInputStream()); 130 writeXML(workbook); 131 } 132 133 134 137 private void writeXML(HSSFWorkbook workbook) throws SAXException { 138 this.contentHandler.startDocument(); 139 start("Workbook"); 140 start("SheetNameIndex"); 141 for (int i = 0; i < workbook.getNumberOfSheets(); i++) { 142 start("SheetName"); 143 data(workbook.getSheetName(i)); 144 end("SheetName"); 145 } 146 end("SheetNameIndex"); 147 start("Sheets"); 148 for (int i = 0; i < workbook.getNumberOfSheets(); i++) { 149 HSSFSheet sheet = workbook.getSheetAt(i); 150 start("Sheet"); 151 start("Name"); 152 data(workbook.getSheetName(i)); 153 end("Name"); 154 start("MaxCol"); 155 data(Integer.toString(getMaxCol(sheet))); 156 end("MaxCol"); 157 start("MaxRow"); 158 data(Integer.toString(sheet.getLastRowNum())); 159 end("MaxRow"); 160 if (formatting) { 161 writeStyles(workbook, sheet); 162 } 163 164 start("Cells"); 165 final Iterator rows = sheet.rowIterator(); 166 while (rows.hasNext()) { 167 final HSSFRow row = (HSSFRow) rows.next(); 168 final Iterator cells = row.cellIterator(); 169 while (cells.hasNext()) { 170 final HSSFCell cell = (HSSFCell) cells.next(); 171 attribute("Row", Integer.toString(row.getRowNum())); 172 attribute("Col", Short.toString(cell.getCellNum())); 173 attribute("ValueType", getValueType(cell.getCellType())); 174 start("Cell"); 175 data(getValue(cell)); 176 end("Cell"); 177 } 178 } 179 end("Cells"); 180 181 end("Sheet"); 182 } 183 end("Sheets"); 184 end("Workbook"); 185 this.contentHandler.endDocument(); 186 } 187 188 193 private int getMaxCol(HSSFSheet sheet) { 194 int max = -1; 195 HSSFRow row = null; 196 Iterator rows = sheet.rowIterator(); 197 while (rows.hasNext()) { 198 row = (HSSFRow) rows.next(); 199 int lastNum = row.getLastCellNum(); 200 if (lastNum > max) { 201 max = lastNum; 202 } 203 } 204 return max; 205 } 206 207 212 private String getValueType(int cellType) { 213 switch (cellType) { 214 case HSSFCell.CELL_TYPE_BLANK: 215 return "10"; 216 case HSSFCell.CELL_TYPE_BOOLEAN: 217 return "20"; 218 case HSSFCell.CELL_TYPE_NUMERIC: 219 return "40"; 220 case HSSFCell.CELL_TYPE_ERROR: 221 return "50"; 222 case HSSFCell.CELL_TYPE_FORMULA: 223 case HSSFCell.CELL_TYPE_STRING: 224 default: 225 return "60"; 226 } 227 } 228 229 234 private String getValue(HSSFCell cell) { 235 switch (cell.getCellType()) { 236 case HSSFCell.CELL_TYPE_BLANK: 237 return ""; 238 case HSSFCell.CELL_TYPE_BOOLEAN: 239 return BooleanUtils.toStringTrueFalse(cell.getBooleanCellValue()); 240 case HSSFCell.CELL_TYPE_NUMERIC: 241 return Double.toString(cell.getNumericCellValue()); 242 case HSSFCell.CELL_TYPE_ERROR: 243 return "#ERR" + cell.getErrorCellValue(); 244 case HSSFCell.CELL_TYPE_FORMULA: 245 case HSSFCell.CELL_TYPE_STRING: 246 default: 247 return cell.getStringCellValue(); 248 } 249 } 250 251 254 private void writeStyles(HSSFWorkbook workbook, HSSFSheet sheet) 255 throws SAXException { 256 start("Styles"); 257 HSSFRow row = null; 258 HSSFCell cell = null; 259 Iterator cells = null; 260 Iterator rows = sheet.rowIterator(); 261 while (rows.hasNext()) { 262 row = (HSSFRow) rows.next(); 263 cells = row.cellIterator(); 264 while (cells.hasNext()) { 265 cell = (HSSFCell) cells.next(); 266 attribute("startRow", Integer.toString(row.getRowNum())); 267 attribute("endRow", Integer.toString(row.getRowNum())); 268 attribute("startCol", Short.toString(cell.getCellNum())); 269 attribute("endCol", Short.toString(cell.getCellNum())); 270 start("StyleRegion"); 271 HSSFCellStyle style = cell.getCellStyle(); 272 attribute("HAlign", Integer.toString(style.getAlignment())); 273 attribute("VAlign", Integer.toString(style.getVerticalAlignment())); 274 attribute("WrapText", ((style.getWrapText()) ? "1" : "0")); 275 attribute("Orient", Integer.toString(style.getRotation())); 276 attribute("Indent", Integer.toString(style.getIndention())); 277 attribute("Locked", ((style.getLocked()) ? "1" : "0")); 278 attribute("Hidden", ((style.getHidden()) ? "1" : "0")); 279 attribute("Fore", workbook.getCustomPalette().getColor(style.getFillForegroundColor()).getHexString()); 280 attribute("Back", workbook.getCustomPalette().getColor(style.getFillBackgroundColor()).getHexString()); 281 attribute("PatternColor", Integer.toString(style.getFillPattern())); attribute("Format", "General"); start("Style"); 284 HSSFFont font = workbook.getFontAt(style.getFontIndex()); 285 attribute("Unit", Short.toString(font.getFontHeightInPoints())); 286 attribute("Bold", Short.toString(font.getBoldweight())); 287 attribute("Italic", ((font.getItalic()) ? "1" : "0")); 288 attribute("Unterline", Integer.toString(font.getUnderline())); 289 attribute("StrikeThrough", ((font.getStrikeout()) ? "1" : "0")); 290 start("Font"); 291 data(font.getFontName()); 292 end("Font"); 293 end("Style"); 294 end("StyleRegion"); 295 } 296 } 297 end("Styles"); 298 } 299 300 304 307 private void attribute(String name, String value) { 308 attr.addAttribute("", name, name, "CDATA", value); 309 } 310 311 316 private void start(String name) throws SAXException { 317 super.contentHandler.startElement(uri, name, prefix + ":" + name, attr); 318 attr.clear(); 319 } 320 321 326 private void end(String name) throws SAXException { 327 super.contentHandler.endElement(uri, name, prefix + ":" + name); 328 } 329 330 335 private void data(String data) throws SAXException { 336 super.contentHandler.characters(data.toCharArray(), 0, data.length()); 337 } 338 } 339 | Popular Tags |