Java tutorial
/************************************************************************* * * Copyright 2009 by bBreak Systems. * * ExCella Core - ExcelJava????? * * $Id: BookController.java 118 2009-06-24 08:45:23Z yuta-takahashi $ * $Revision: 118 $ * * This file is part of ExCella Core. * * ExCella Core is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 3 * only, as published by the Free Software Foundation. * * ExCella Core 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 Lesser General Public License version 3 for more details * (a copy is included in the COPYING.LESSER file that accompanied this code). * * You should have received a copy of the GNU Lesser General Public License * version 3 along with ExCella Core. If not, see * <http://www.gnu.org/licenses/lgpl-3.0-standalone.html> * for a copy of the LGPLv3 License. * ************************************************************************/ package org.bbreak.excella.core; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.bbreak.excella.core.exception.ExportException; import org.bbreak.excella.core.exception.ParseException; import org.bbreak.excella.core.exporter.book.BookExporter; import org.bbreak.excella.core.exporter.sheet.SheetExporter; import org.bbreak.excella.core.handler.ParseErrorHandler; import org.bbreak.excella.core.listener.SheetParseListener; import org.bbreak.excella.core.tag.TagParser; /** * ??? * * @since 1.0 */ public class BookController { /** */ private static Log log = LogFactory.getLog(BookController.class); /** Excel2007? */ public static final String XSSF_SUFFIX = ".xlsx"; /** Excel2003?? */ public static final String HSSF_SUFFIX = ".xls"; /** ? */ public static final String COMMENT_PREFIX = "-"; /** ?? */ private Workbook workbook; /** ??? */ private List<String> sheetNames = new ArrayList<String>(); /** ? */ private List<SheetTagParserInfo> tagParsers = new ArrayList<SheetTagParserInfo>(); /** ?? */ private List<SheetListenerInfo> sheetListeners = new ArrayList<SheetListenerInfo>(); /** ?? */ private List<BookExporter> bookExporters = new ArrayList<BookExporter>(); /** ?? */ private List<SheetExporterInfo> sheetExporters = new ArrayList<SheetExporterInfo>(); /** ? */ private ParseErrorHandler errorHandler = null; /** */ private BookData bookData = new BookData(); /** * <BR> * ????Workbook?? * * @param filepath * @throws IOException ??????? */ public BookController(String filepath) throws IOException { if (log.isInfoEnabled()) { log.info(filepath + "??????"); } if (filepath.endsWith(XSSF_SUFFIX)) { // XSSF? workbook = new XSSFWorkbook(filepath); } else { // HSSF? FileInputStream stream = new FileInputStream(filepath); POIFSFileSystem fs = new POIFSFileSystem(stream); workbook = new HSSFWorkbook(fs); stream.close(); } // ??? int numOfSheets = workbook.getNumberOfSheets(); for (int sheetCnt = 0; sheetCnt < numOfSheets; sheetCnt++) { String sheetName = workbook.getSheetName(sheetCnt); sheetNames.add(sheetName); } } /** * * * @param workbook ?? */ public BookController(Workbook workbook) { this.workbook = workbook; // ??? int numOfSheets = workbook.getNumberOfSheets(); for (int sheetCnt = 0; sheetCnt < numOfSheets; sheetCnt++) { String sheetName = workbook.getSheetName(sheetCnt); sheetNames.add(sheetName); } } /** * ???(??)??? * * @throws ParseException ???? * @throws IOException ???????? */ public void parseBook() throws ParseException, ExportException { parseBook(null); } /** * ???(??)??? * * @param data BookController?parseBook(), parseSheet()? * SheetParser?parseSheet????? * TagParser?????? * @throws ParseException ???? * @throws ExportException ????? */ public void parseBook(Object data) throws ParseException, ExportException { bookData.clear(); for (String sheetName : sheetNames) { if (sheetName.startsWith(COMMENT_PREFIX)) { continue; } // ?? SheetData sheetData = parseSheet(sheetName, data); // ?? bookData.putSheetData(sheetName, sheetData); } // ?? for (BookExporter exporter : bookExporters) { if (exporter != null) { exporter.setup(); try { exporter.export(workbook, bookData); } finally { exporter.tearDown(); } } } } /** * ?????? * * @return ???? */ public BookData getBookData() { return bookData; } /** * ???Workbook?? * * @return ???Workbook */ public Workbook getBook() { return workbook; } /** * ?? * * @param sheetName ???? * @return ??? * @throws ParseException ???? * @throws ExportException ?? */ public SheetData parseSheet(String sheetName) throws ParseException, ExportException { return parseSheet(sheetName, null); } /** * ?? * * @param sheetName ???? * @param data BookController?parseBook(), parseSheet()? * SheetParser?parseSheet????? * TagParser?????? * @return ??? * @throws ParseException ? * @throws ExportException ?? */ public SheetData parseSheet(String sheetName, Object data) throws ParseException, ExportException { Sheet sheet = workbook.getSheet(sheetName); SheetData sheetData = null; try { SheetParser sheetParser = new SheetParser(); if (log.isInfoEnabled()) { log.info("[" + sheetName + "]?????"); } // ? for (SheetTagParserInfo parserInfo : tagParsers) { String targetSheetName = parserInfo.getSheetName(); if (targetSheetName == null || targetSheetName.equals(sheetName)) { sheetParser.addTagParser(parserInfo.getParser()); } } // ??? for (SheetListenerInfo listenerInfo : sheetListeners) { String targetSheetName = listenerInfo.getSheetName(); if (targetSheetName == null || targetSheetName.equals(sheetName)) { listenerInfo.getListener().preParse(sheet, sheetParser); } } // ?? sheetData = sheetParser.parseSheet(sheet, data); // ?? for (SheetListenerInfo listnerInfo : sheetListeners) { String targetSheetName = listnerInfo.getSheetName(); if (targetSheetName == null || targetSheetName.equals(sheetName)) { listnerInfo.getListener().postParse(sheet, sheetParser, sheetData); } } } catch (ParseException ex) { if (log.isWarnEnabled()) { log.warn(sheetName + "???????", ex); } // ??? if (errorHandler != null) { errorHandler.notifyException(workbook, sheet, ex); } throw ex; } // ?? for (SheetExporterInfo exporterInfo : sheetExporters) { String targetSheetName = exporterInfo.getSheetName(); if (targetSheetName == null || targetSheetName.equals(sheetName)) { SheetExporter exporter = exporterInfo.getExporter(); exporter.setup(); try { exporter.export(sheet, sheetData); } finally { exporter.tearDown(); } } } return sheetData; } /** * ???????(?) * * @return ??? */ public List<String> getSheetNames() { return sheetNames; } /** * ? * * @param parser ? */ public void addTagParser(TagParser<?> parser) { tagParsers.add(new SheetTagParserInfo(parser)); } /** * ??? * * @param sheetName ?? * @param parser ? */ public void addTagParser(String sheetName, TagParser<?> parser) { tagParsers.add(new SheetTagParserInfo(sheetName, parser)); } /** * ?? * * @param tag */ public void removeTagParser(String tag) { List<SheetTagParserInfo> removeList = new ArrayList<SheetTagParserInfo>(); for (SheetTagParserInfo sheetTagParserInfo : tagParsers) { if (sheetTagParserInfo.getParser().getTag().equals(tag)) { removeList.add(sheetTagParserInfo); } } tagParsers.removeAll(removeList); } /** * ????? */ public void clearTagParsers() { tagParsers.clear(); } /** * ?? * * @param listener ? */ public void addSheetParseListener(SheetParseListener listener) { sheetListeners.add(new SheetListenerInfo(listener)); } /** * ?? * * @param sheetName ?? * @param listener ? */ public void addSheetParseListener(String sheetName, SheetParseListener listener) { sheetListeners.add(new SheetListenerInfo(sheetName, listener)); } /** * ???? */ public void clearSheetParseListeners() { sheetListeners.clear(); } /** * ??? * * @return ? */ public List<BookExporter> getExporter() { return bookExporters; } /** * ??? * * @param exporter ? */ public void addSheetExporter(SheetExporter exporter) { sheetExporters.add(new SheetExporterInfo(exporter)); } /** * ??? * * @param sheetName ?? * @param exporter ? */ public void addSheetExporter(String sheetName, SheetExporter exporter) { sheetExporters.add(new SheetExporterInfo(sheetName, exporter)); } /** * ??????? */ public void clearSheetExporters() { sheetExporters.clear(); } /** * ??? * * @return ? */ public ParseErrorHandler getErrorHandler() { return errorHandler; } /** * ?? * * @param errorHandler ? */ public void setErrorHandler(ParseErrorHandler errorHandler) { this.errorHandler = errorHandler; } /** * ?? * * @param exporter ? */ public void addBookExporter(BookExporter exporter) { bookExporters.add(exporter); } /** * ???? */ public void clearBookExporters() { bookExporters.clear(); } /** * ?????? * * @since 1.0 */ private class SheetTagParserInfo { /** ?? */ private String sheetName = null; /** */ private TagParser<?> parser = null; /** * * * @param sheetName ?? * @param parser */ public SheetTagParserInfo(String sheetName, TagParser<?> parser) { this.sheetName = sheetName; this.parser = parser; } /** * * * @param parser */ public SheetTagParserInfo(TagParser<?> parser) { this.parser = parser; } public String getSheetName() { return sheetName; } public TagParser<?> getParser() { return parser; } } /** * ?????? * * @since 1.0 */ private class SheetListenerInfo { /** ?? */ private String sheetName = null; /** */ private SheetParseListener listener = null; /** * * * @param sheetName ?? * @param listener */ public SheetListenerInfo(String sheetName, SheetParseListener listener) { this.sheetName = sheetName; this.listener = listener; } /** * * * @param listener */ public SheetListenerInfo(SheetParseListener listener) { this.listener = listener; } public String getSheetName() { return sheetName; } public SheetParseListener getListener() { return listener; } } /** * ???Exporter??? * * @since 1.0 */ private class SheetExporterInfo { /** ?? */ private String sheetName = null; /** ? */ private SheetExporter exporter = null; /** * * * @param sheetName ?? * @param exporter ? */ public SheetExporterInfo(String sheetName, SheetExporter exporter) { this.sheetName = sheetName; this.exporter = exporter; } /** * * * @param exporter ? */ public SheetExporterInfo(SheetExporter exporter) { this.exporter = exporter; } public String getSheetName() { return sheetName; } public SheetExporter getExporter() { return exporter; } } }