file.open.util.parse.XlsParser.java Source code

Java tutorial

Introduction

Here is the source code for file.open.util.parse.XlsParser.java

Source

/*
CSVParser -- a class within the FileManagerFramework.
Copyright (C) 2012, Kaleb Kircher.
    
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package file.open.util.parse;

import file.open.util.datastructures.TwoDimensionalArrayList;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
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;

/**
 * An implementation of ParserIterface.
 * This implementation is used to read a two-dimensional algebraic matrix
 * out of an external CSV file and into an iterator the application can use.
 * 
 */
public class XlsParser extends ParserInterface {

    HSSFSheet m_sheet;
    int m_iNbRows;
    int m_iCurrentRow = 0;
    private static final String JAVA_TOSTRING = "EEE MMM dd HH:mm:ss zzz yyyy";

    /**
     * Call to default constructor.
     */
    public XlsParser() {
        super();

        // Call to the parent class data structure.
        // We use a list because we don't know how big the matrix is.
        super.list = new TwoDimensionalArrayList<String>();
    }

    public void initParser(HSSFSheet sheet) {
        m_sheet = sheet;
        m_iNbRows = sheet.getPhysicalNumberOfRows();
    }

    /* Returns the contents of an Excel row in the
    form of a String array.
     * @see com.ibm.ccd.common.parsing.Parser#splitLine()
     */
    public String[] splitLine() throws Exception {
        if (m_iCurrentRow == m_iNbRows) {
            return null;
        }

        HSSFRow row = m_sheet.getRow(m_iCurrentRow);
        if (row == null) {
            return null;
        } else {
            int cellIndex = 0;
            int noOfCells = row.getPhysicalNumberOfCells();
            String[] values = new String[noOfCells];
            short firstCellNum = row.getFirstCellNum();
            short lastCellNum = row.getLastCellNum();

            if (firstCellNum >= 0 && lastCellNum >= 0) {
                for (short iCurrent = firstCellNum; iCurrent < lastCellNum; iCurrent++) {
                    HSSFCell cell = (HSSFCell) row.getCell((int) iCurrent);
                    if (cell == null) {
                        values[iCurrent] = "";
                        cellIndex++;
                        continue;
                    } else {
                        switch (cell.getCellType()) {
                        case HSSFCell.CELL_TYPE_NUMERIC:
                            double value = cell.getNumericCellValue();
                            if (HSSFDateUtil.isCellDateFormatted(cell)) {
                                if (HSSFDateUtil.isValidExcelDate(value)) {
                                    Date date = HSSFDateUtil.getJavaDate(value);
                                    SimpleDateFormat dateFormat = new SimpleDateFormat(JAVA_TOSTRING);
                                    values[iCurrent] = dateFormat.format(date);
                                } else {
                                    throw new Exception("Invalid Date value found at row number " + row.getRowNum()
                                            + " and column number " + cell.getNumericCellValue());
                                }
                            } else {
                                values[iCurrent] = value + "";
                            }
                            break;

                        case HSSFCell.CELL_TYPE_STRING:
                            values[iCurrent] = cell.getStringCellValue();
                            break;

                        case HSSFCell.CELL_TYPE_BLANK:
                            values[iCurrent] = null;
                            break;

                        default:
                            values[iCurrent] = null;
                        }
                    }
                }
            }
            m_iCurrentRow++;
            return values;
        }

    }

    /**
     * Reads each line of the csv file into a StringTokenizer and returns
     * the data as a two-dimensional Iterator object.
     *
     * @param data
     *            BufferedReader containing the file data.
     * @return Iterator[], the file data.
     */
    @Override
    public Iterator readLinesCollection(String path) {
        HSSFWorkbook workBook = null;
        File file = new File(path);
        InputStream excelDocumentStream = null;
        try {
            excelDocumentStream = new FileInputStream(file);
            POIFSFileSystem fsPOI = new POIFSFileSystem(new BufferedInputStream(excelDocumentStream));
            workBook = new HSSFWorkbook(fsPOI);
            initParser(workBook.getSheetAt(0));
            String[] res;
            int count = 0;
            while ((res = this.splitLine()) != null) {
                //System.out.println("\t");
                for (int i = 0; i < res.length; i++) {
                    //System.out.print(res[i] + ",");
                    list.Add(res[i], count);
                }
                count++;
            }
            excelDocumentStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return createIteratorCollection();
    }
}