|
From: <tr...@us...> - 2003-07-19 13:16:50
|
Update of /cvsroot/babeldoc/babeldoc/modules/conversion/src/com/babeldoc/conversion/excel In directory sc8-pr-cvs1:/tmp/cvs-serv25588/src/com/babeldoc/conversion/excel Added Files: ExcelConversionClient.java ExcelConverter.java Log Message: Lots of rearrangement of the conversion module - now using dom4j in the flatfile code - moved packages around. --- NEW FILE: ExcelConversionClient.java --- /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2000 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact ap...@ap.... * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * Portions of this software are based upon public domain software * originally written at the National Center for Supercomputing Applications, * University of Illinois, Urbana-Champaign. * ==================================================================== * * Babeldoc: The Universal Document Processor * * $Header: /cvsroot/babeldoc/babeldoc/modules/conversion/src/com/babeldoc/conversion/excel/ExcelConversionClient.java,v 1.1 2003/07/19 13:16:47 triphop Exp $ * $DateTime$ * $Author: triphop $ * */ package com.babeldoc.conversion.excel; import com.babeldoc.conversion.ConversionException; import com.babeldoc.conversion.ConversionHelper; import com.babeldoc.core.BabeldocCommand; import com.babeldoc.core.I18n; import com.babeldoc.core.LogService; import com.babeldoc.core.ResourceLoader; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.OptionBuilder; import org.apache.commons.cli.Options; import java.io.IOException; import java.io.InputStream; /** * Class to access the Apache POI library to toXml Excel files to XML. * * @author dejank */ public class ExcelConversionClient extends BabeldocCommand { /** * Construct this object and call the super with the argument for setting up * the conversion client. * * @param args */ public ExcelConversionClient(String[] args) { super("conversion", I18n.get("conversion.009"), args); } /** * Main method. * * @param commandline DOCUMENT ME! */ public void execute(CommandLine commandline) { InputStream in = null; if (commandline.hasOption('f')) { String inFile = commandline.getOptionValue('f'); try { in = ResourceLoader.getResourceStream(inFile); } catch (IOException e) { LogService.getInstance().logError(I18n.get( "conversion.exception.io.infile", inFile), e); return; } } if (in != null) { try { ConversionHelper.render(new ExcelConverter().toXml(in), System.out); } catch (ConversionException e) { LogService.getInstance().logError(e); } } else { System.err.println(I18n.get("conversion.008")); } } /** * setup the options on the command line. * * @param options the options to access */ public void setupCommandLine(Options options) { super.setupCommandLine(options); options.addOption(OptionBuilder.isRequired().hasArg(true) .withDescription(I18n.get("conversion.010")) .withLongOpt("file").create('f')); } /** * Main * * @param args the arguments */ public static void main(String[] args) { new ExcelConversionClient(args); } } --- NEW FILE: ExcelConverter.java --- /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2000 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact ap...@ap.... * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * Portions of this software are based upon public domain software * originally written at the National Center for Supercomputing Applications, * University of Illinois, Urbana-Champaign. * ==================================================================== * * Babeldoc: The Universal Document Processor * * $Header: /cvsroot/babeldoc/babeldoc/modules/conversion/src/com/babeldoc/conversion/excel/ExcelConverter.java,v 1.1 2003/07/19 13:16:47 triphop Exp $ * $DateTime$ * $Author: triphop $ * */ package com.babeldoc.conversion.excel; import com.babeldoc.conversion.ConversionException; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import java.io.IOException; import java.io.InputStream; /** * Simple class to toXml an input stream of an excel file to an * xml output stream * * @author dejank */ public class ExcelConverter { /** * Convert a input stream of excel file as input to an xml document. * * @param in excel file stream * @return the document * @throws com.babeldoc.conversion.ConversionException */ public Document toXml(InputStream in) throws ConversionException { POIFSFileSystem fs; try { fs = new POIFSFileSystem(in); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = null; HSSFRow row = null; HSSFCell cell = null; Document document = DocumentHelper.createDocument(); // Element root = document.addElement("workbook"); int numberOfSheets = wb.getNumberOfSheets(); root.addAttribute("number-of-sheets", String.valueOf(numberOfSheets)); for (int i = 0; i < numberOfSheets; i++) { Element sheetElement = root.addElement("sheet"); sheet = wb.getSheetAt(i); String sheetName = wb.getSheetName(i); sheetElement.addAttribute("sheet-name", sheetName); sheetElement.addAttribute("sheet-number", (new Integer(i)).toString()); int rowCount = 0; int firstRowNum = sheet.getFirstRowNum(); int lastRowNum = sheet.getLastRowNum(); for (int m = firstRowNum; m <= lastRowNum; m++) { row = sheet.getRow(m); if (row == null) continue; Element rowElement = sheetElement.addElement("row"); rowElement.addAttribute("row-number", (new Integer(m)).toString()); rowCount++; int firstCellNum = row.getFirstCellNum(); int lastCellNum = row.getLastCellNum(); int cellCount = 0; for (int p = firstCellNum; p <= lastCellNum; p++) { cell = row.getCell((short) p); // if (cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) continue; if (cell == null) continue; cellCount++; Element cellElement = rowElement.addElement("cell"); cellElement.addAttribute("cell-number", String.valueOf(cellCount)); cellElement.addAttribute("cell-colnum", String.valueOf(cell.getCellNum())); int cellType = cell.getCellType(); switch (cellType) { case HSSFCell.CELL_TYPE_NUMERIC : cellElement.addAttribute("type", "Numeric"); cellElement.addText( String.valueOf(cell.getNumericCellValue())); break; case HSSFCell.CELL_TYPE_STRING : cellElement.addAttribute("type", "String"); cellElement.addText(cell.getStringCellValue()); break; case HSSFCell.CELL_TYPE_BOOLEAN : cellElement.addAttribute("type", "Boolean"); cellElement.addText( String.valueOf(cell.getBooleanCellValue())); break; case HSSFCell.CELL_TYPE_BLANK : cellElement.addAttribute("type", "N/A"); break; default : break; } } rowElement.addAttribute( "number-of-cells", String.valueOf(cellCount)); } sheetElement.addAttribute( "number-of-rows", String.valueOf(rowCount)); } return document; } catch(IOException iox) { throw new ConversionException("", iox); } } } |