Return-Path: Delivered-To: apmail-cocoon-cvs-archive@www.apache.org Received: (qmail 31618 invoked from network); 7 Jul 2005 19:21:10 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 7 Jul 2005 19:21:10 -0000 Received: (qmail 51715 invoked by uid 500); 7 Jul 2005 19:21:09 -0000 Delivered-To: apmail-cocoon-cvs-archive@cocoon.apache.org Received: (qmail 51615 invoked by uid 500); 7 Jul 2005 19:21:08 -0000 Mailing-List: contact cvs-help@cocoon.apache.org; run by ezmlm Precedence: bulk Reply-To: dev@cocoon.apache.org list-help: list-unsubscribe: List-Post: List-Id: Delivered-To: mailing list cvs@cocoon.apache.org Received: (qmail 51601 invoked by uid 99); 7 Jul 2005 19:21:07 -0000 X-ASF-Spam-Status: No, hits=-9.8 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Thu, 07 Jul 2005 12:20:52 -0700 Received: (qmail 31439 invoked by uid 65534); 7 Jul 2005 19:20:50 -0000 Message-ID: <20050707192050.31438.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r209638 - in /cocoon/branches/BRANCH_2_1_X: ./ src/blocks/poi/java/org/apache/cocoon/generation/ src/blocks/poi/java/org/apache/cocoon/serialization/ Date: Thu, 07 Jul 2005 19:20:50 -0000 To: cvs@cocoon.apache.org From: vgritsenko@apache.org X-Mailer: svnmailer-1.0.2 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: vgritsenko Date: Thu Jul 7 12:20:48 2005 New Revision: 209638 URL: http://svn.apache.org/viewcvs?rev=209638&view=rev Log: POI Block: Added HSSFGenerator. Added: cocoon/branches/BRANCH_2_1_X/src/blocks/poi/java/org/apache/cocoon/generation/ cocoon/branches/BRANCH_2_1_X/src/blocks/poi/java/org/apache/cocoon/generation/HSSFGenerator.java (with props) Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/poi/java/org/apache/cocoon/serialization/HSSFSerializer.java cocoon/branches/BRANCH_2_1_X/status.xml Added: cocoon/branches/BRANCH_2_1_X/src/blocks/poi/java/org/apache/cocoon/generation/HSSFGenerator.java URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/poi/java/org/apache/cocoon/generation/HSSFGenerator.java?rev=209638&view=auto ============================================================================== --- cocoon/branches/BRANCH_2_1_X/src/blocks/poi/java/org/apache/cocoon/generation/HSSFGenerator.java (added) +++ cocoon/branches/BRANCH_2_1_X/src/blocks/poi/java/org/apache/cocoon/generation/HSSFGenerator.java Thu Jul 7 12:20:48 2005 @@ -0,0 +1,319 @@ +/* + * Copyright 2004-2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.generation; + +import org.apache.avalon.framework.configuration.Configurable; +import org.apache.avalon.framework.configuration.Configuration; +import org.apache.avalon.framework.configuration.ConfigurationException; +import org.apache.avalon.framework.parameters.Parameters; + +import org.apache.cocoon.ProcessingException; +import org.apache.cocoon.components.source.SourceUtil; +import org.apache.cocoon.environment.SourceResolver; + +import org.apache.excalibur.source.Source; +import org.apache.excalibur.source.SourceException; +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFCellStyle; +import org.apache.poi.hssf.usermodel.HSSFFont; +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +import java.io.IOException; +import java.util.Iterator; +import java.util.Map; + +/** + * This generator generates - using Apache POI - a Gnumeric compliant XML + * Document from a Microsoft Excel Workbook. + * + *

Sitemap Definition

+ * <map:generator type="xls" src="org.apache.cocoon.generation.HSSFGenerator"> + * <uri>http://www.gnome.org/gnumeric/v7</uri> + * <prefix>gmr</prefix> + * <formatting>false</formatting> + * </map:generator> + * + *

Sitemap Use

+ * <map:generate type="xls" src="spreadsheet.xls"/> + * + *

You can set the parameter formatting to true + * in order to receive not only the data but also the formatting information + * of the workbook.

+ * + * @author Patrick Herber + * @version $Id$ + */ +public class HSSFGenerator extends AbstractGenerator + implements Configurable { + + public static final String NAMESPACE_PREFIX = "gmr"; + public static final String NAMESPACE_URI = "http://www.gnome.org/gnumeric/v7"; + private static final boolean FORMATTING = false; + + private static final String CONF_NAMESPACE_URI = "uri"; + private static final String CONF_NAMESPACE_PREFIX = "prefix"; + private static final String CONF_FORMATTING = "formatting"; + + private String defaultUri; + private String defaultPrefix; + private boolean defaultFormatting; + + private String uri; + private String prefix; + private boolean formatting; + private final AttributesImpl attr; + + protected Source inputSource; + + + public HSSFGenerator() { + this.attr = new AttributesImpl(); + } + + public void configure(Configuration configuration) throws ConfigurationException { + this.defaultUri = configuration.getChild(CONF_NAMESPACE_URI).getValue(NAMESPACE_URI); + this.defaultPrefix = configuration.getChild(CONF_NAMESPACE_PREFIX).getValue(NAMESPACE_PREFIX); + this.defaultFormatting = configuration.getChild(CONF_FORMATTING).getValueAsBoolean(FORMATTING); + } + + public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par) + throws ProcessingException, SAXException, IOException { + super.setup(resolver, objectModel, src, par); + this.uri = par.getParameter(CONF_NAMESPACE_URI, this.defaultUri); + this.prefix = par.getParameter(CONF_NAMESPACE_PREFIX, this.defaultPrefix); + this.formatting = par.getParameterAsBoolean(CONF_FORMATTING, this.defaultFormatting); + + try { + this.inputSource = super.resolver.resolveURI(src); + } catch (SourceException se) { + throw SourceUtil.handle("Error resolving '" + src + "'.", se); + } + } + + /** + * Recycle this component. All instance variables are set to + * null. + */ + public void recycle() { + if (this.inputSource != null) { + super.resolver.release(this.inputSource); + this.inputSource = null; + } + this.attr.clear(); + super.recycle(); + } + + /** + * Generate XML data. + */ + public void generate() throws SAXException { + try { + HSSFWorkbook workbook = + new HSSFWorkbook(this.inputSource.getInputStream()); + writeXML(workbook); + } catch (Exception e) { + getLogger().error("Error generating XML Data", e); + } + } + + + /** + * Writes out the workbook data as XML, without formatting information + */ + private void writeXML(HSSFWorkbook workbook) throws Exception { + this.contentHandler.startDocument(); + start("Workbook"); + start("SheetNameIndex"); + for (int i = 0; i < workbook.getNumberOfSheets(); i++) { + start("SheetName"); + data(workbook.getSheetName(i)); + end("SheetName"); + } + end("SheetNameIndex"); + start("Sheets"); + for (int i = 0; i < workbook.getNumberOfSheets(); i++) { + HSSFSheet sheet = workbook.getSheetAt(i); + start("Sheet"); + start("Name"); + data(workbook.getSheetName(i)); + end("Name"); + start("MaxCol"); + data(Integer.toString(getMaxCol(sheet))); + end("MaxCol"); + start("MaxRow"); + data(Integer.toString(sheet.getLastRowNum())); + end("MaxRow"); + if (formatting) { + writeStyles(workbook, sheet); + } + start("Cells"); + HSSFRow row = null; + HSSFCell cell = null; + Iterator cells = null; + Iterator rows = sheet.rowIterator(); + while (rows.hasNext()) { + row = (HSSFRow) rows.next(); + cells = row.cellIterator(); + while (cells.hasNext()) { + cell = (HSSFCell) cells.next(); + attribute("Row", Integer.toString(row.getRowNum())); + attribute("Col", Short.toString(cell.getCellNum())); + attribute("ValueType", getValueType(cell.getCellType())); + start("Cell"); + data(cell.getStringCellValue()); + end("Cell"); + } + } + end("Cells"); + end("Sheet"); + } + end("Sheets"); + end("Workbook"); + this.contentHandler.endDocument(); + } + + /** + * Returns the max column index of the given sheet + * @param sheet + * @return the max column index + */ + private int getMaxCol(HSSFSheet sheet) { + int max = -1; + HSSFRow row = null; + Iterator rows = sheet.rowIterator(); + while (rows.hasNext()) { + row = (HSSFRow) rows.next(); + int lastNum = row.getLastCellNum(); + if (lastNum > max) { + max = lastNum; + } + } + return max; + } + + /** + * Returns the Gnumeric cell type. + * @param cellType POI cell type + * @return the Gnumeric cell type. + */ + private String getValueType(int cellType) { + switch (cellType) { + case HSSFCell.CELL_TYPE_BLANK: + return "10"; + case HSSFCell.CELL_TYPE_BOOLEAN: + return "20"; + case HSSFCell.CELL_TYPE_NUMERIC: + return "40"; + case HSSFCell.CELL_TYPE_ERROR: + return "50"; + case HSSFCell.CELL_TYPE_FORMULA: + case HSSFCell.CELL_TYPE_STRING: + default: + return "60"; + } + } + + /** + * Writes out the workbook data as XML, with formatting information + */ + private void writeStyles(HSSFWorkbook workbook, HSSFSheet sheet) throws Exception { + start("Styles"); + HSSFRow row = null; + HSSFCell cell = null; + Iterator cells = null; + Iterator rows = sheet.rowIterator(); + while (rows.hasNext()) { + row = (HSSFRow) rows.next(); + cells = row.cellIterator(); + while (cells.hasNext()) { + cell = (HSSFCell) cells.next(); + attribute("startRow", Integer.toString(row.getRowNum())); + attribute("endRow", Integer.toString(row.getRowNum())); + attribute("startCol", Short.toString(cell.getCellNum())); + attribute("endCol", Short.toString(cell.getCellNum())); + start("StyleRegion"); + HSSFCellStyle style = cell.getCellStyle(); + attribute("HAlign", Integer.toString(style.getAlignment())); + attribute("VAlign", Integer.toString(style.getVerticalAlignment())); + attribute("WrapText", ((style.getWrapText()) ? "1" : "0")); + attribute("Orient", Integer.toString(style.getRotation())); + attribute("Indent", Integer.toString(style.getIndention())); + attribute("Locked", ((style.getLocked()) ? "1" : "0")); + attribute("Hidden", ((style.getHidden()) ? "1" : "0")); + attribute("Fore", workbook.getCustomPalette().getColor(style.getFillForegroundColor()).getHexString()); + attribute("Back", workbook.getCustomPalette().getColor(style.getFillBackgroundColor()).getHexString()); + attribute("PatternColor", Integer.toString(style.getFillPattern())); // TODO + attribute("Format", "General"); // TODO + start("Style"); + HSSFFont font = workbook.getFontAt(style.getFontIndex()); + attribute("Unit", Short.toString(font.getFontHeightInPoints())); + attribute("Bold", Short.toString(font.getBoldweight())); + attribute("Italic", ((font.getItalic()) ? "1" : "0")); + attribute("Unterline", Integer.toString(font.getUnderline())); + attribute("StrikeThrough", ((font.getStrikeout()) ? "1" : "0")); + start("Font"); + data(font.getFontName()); + end("Font"); + end("Style"); + end("StyleRegion"); + } + } + end("Styles"); + } + + // + // Utility methods + // + + /** + * Adds an attribute with the given name and value. + */ + private void attribute(String name, String value) { + attr.addAttribute("", name, name, "CDATA", value); + } + + /** + * Starts an element with the given local name. + * @param name local name of the element + * @throws SAXException + */ + private void start(String name) throws SAXException { + super.contentHandler.startElement(uri, name, prefix + ":" + name, attr); + attr.clear(); + } + + /** + * Ends the given element. + * @param name local name of the element + * @throws SAXException + */ + private void end(String name) throws SAXException { + super.contentHandler.endElement(uri, name, prefix + ":" + name); + } + + /** + * Writes the given element data. + * @param data + * @throws SAXException + */ + private void data(String data) throws SAXException { + super.contentHandler.characters(data.toCharArray(), 0, data.length()); + } +} Propchange: cocoon/branches/BRANCH_2_1_X/src/blocks/poi/java/org/apache/cocoon/generation/HSSFGenerator.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: cocoon/branches/BRANCH_2_1_X/src/blocks/poi/java/org/apache/cocoon/generation/HSSFGenerator.java ------------------------------------------------------------------------------ svn:keywords = Id Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/poi/java/org/apache/cocoon/serialization/HSSFSerializer.java URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/poi/java/org/apache/cocoon/serialization/HSSFSerializer.java?rev=209638&r1=209637&r2=209638&view=diff ============================================================================== --- cocoon/branches/BRANCH_2_1_X/src/blocks/poi/java/org/apache/cocoon/serialization/HSSFSerializer.java (original) +++ cocoon/branches/BRANCH_2_1_X/src/blocks/poi/java/org/apache/cocoon/serialization/HSSFSerializer.java Thu Jul 7 12:20:48 2005 @@ -1,12 +1,12 @@ /* * Copyright 1999-2004 The Apache Software Foundation. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -24,19 +24,21 @@ import org.apache.cocoon.components.elementprocessor.impl.poi.hssf.HSSFElementProcessorFactory; /** - * Serializer to produce an HSSF stream. + * Serializer to produce an HSSF stream. * * @author Marc Johnson (marc_johnson27591@hotmail.com) * @author Nicola Ken Barozzi (nicolaken@apache.org) - * @version CVS $Id: HSSFSerializer.java,v 1.4 2004/03/05 13:02:07 bdelacretaz Exp $ + * @version $Id$ */ -public class HSSFSerializer extends POIFSSerializer implements Initializable, Configurable { +public class HSSFSerializer extends POIFSSerializer + implements Initializable, Configurable { + private ElementProcessorFactory _element_processor_factory; private final static String _mime_type = "application/vnd.ms-excel"; String locale; /** - * Constructor + * Constructor */ public HSSFSerializer() { super(); @@ -66,31 +68,30 @@ /** * get the mime type * - *@return application/vnd.ms-excel + * @return application/vnd.ms-excel */ public String getMimeType() { return _mime_type; } /** - * get the ElementProcessorFactory + * get the ElementProcessorFactory * - *@return the ElementProcessorFactory + * @return the ElementProcessorFactory */ protected ElementProcessorFactory getElementProcessorFactory() { return _element_processor_factory; } /** - * post-processing for endDocument + * post-processing for endDocument */ protected void doLocalPostEndDocument() { } /** - * pre-processing for endDocument + * pre-processing for endDocument */ protected void doLocalPreEndDocument() { } - } Modified: cocoon/branches/BRANCH_2_1_X/status.xml URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/status.xml?rev=209638&r1=209637&r2=209638&view=diff ============================================================================== --- cocoon/branches/BRANCH_2_1_X/status.xml (original) +++ cocoon/branches/BRANCH_2_1_X/status.xml Thu Jul 7 12:20:48 2005 @@ -196,6 +196,9 @@ + + POI Block: Added HSSFGenerator. + Added getSitemapURIPrefix method to the Request interface.