Package com.asakusafw.testdriver.excel

Source Code of com.asakusafw.testdriver.excel.Util

/**
* Copyright 2011-2014 Asakusa Framework Team.
*
* 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 com.asakusafw.testdriver.excel;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.text.MessageFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Common utilities for this package.
* @since 0.2.0
* @version 0.5.3
*/
final class Util {

    static final Logger LOG = LoggerFactory.getLogger(Util.class);

    private static final Pattern FRAGMENT = Pattern.compile(":(\\d+)|([^:].*)");

    private static final String HSSF_EXTENSION = ".xls";

    private static final String XSSF_EXTENSION = ".xlsx";

    private static final String FRAGMENT_FIRST_SHEET = ":0";

    static Sheet extract(URI source) throws IOException {
        assert source != null;
        String path = source.getSchemeSpecificPart();
        if (isHssf(path) == false && isXssf(path) == false) {
            LOG.debug("Not a Excel workbook: {}", source);
            return null;
        }
        String fragment = source.getFragment();
        if (fragment == null) {
            // the first sheet
            fragment = FRAGMENT_FIRST_SHEET;
            LOG.debug("Fragment is not set, using first sheet: {}", source);
        }
        Matcher matcher = FRAGMENT.matcher(fragment);
        if (matcher.matches() == false) {
            LOG.info("Invalid fragment: {}", source);
            return null;
        }

        LOG.debug("Processing Excel workbook: {}", source);
        URL url = source.toURL();
        InputStream in = url.openStream();
        Workbook book;
        try {
            InputStream bin = new BufferedInputStream(in);
            book = openWorkbookFor(path, bin);
            bin.close();
        } catch (IOException e) {
            throw new IOException(MessageFormat.format(
                    "Excelファイルの展開に失敗しました: {0}",
                    source));
        } finally {
            in.close();
        }

        if (matcher.group(1) != null) {
            int sheetNumber = Integer.parseInt(matcher.group(1));
            LOG.debug("Opening sheet by index : {}", sheetNumber);
            try {
                Sheet sheet = book.getSheetAt(sheetNumber);
                assert sheet != null;
                return sheet;
            } catch (RuntimeException e) {
                throw new IOException(MessageFormat.format(
                        "Excelシートの展開に失敗しました: {0} (シート{1}番がありません)",
                        source,
                        sheetNumber), e);
            }
        } else {
            String sheetName = matcher.group(2);
            LOG.debug("Opening sheet by name : {}", sheetName);
            assert sheetName != null;
            Sheet sheet = book.getSheet(sheetName);
            if (sheet == null) {
                throw new IOException(MessageFormat.format(
                        "Excelシートの展開に失敗しました: {0} (シート\"{1}\"がありません)",
                        source,
                        sheetName));
            }
            return sheet;
        }
    }

    static Workbook openWorkbookFor(String path, InputStream input) throws IOException {
        if (isHssf(path)) {
            return new HSSFWorkbook(input);
        } else if (isXssf(path)) {
            return new XSSFWorkbook(input);
        } else {
            return new HSSFWorkbook(input);
        }
    }

    static Workbook createEmptyWorkbookFor(String path) {
        if (isHssf(path)) {
            return new HSSFWorkbook();
        } else if (isXssf(path)) {
            return new XSSFWorkbook();
        } else {
            return new HSSFWorkbook();
        }
    }

    static SpreadsheetVersion getSpreadsheetVersionFor(String path) {
        if (isHssf(path)) {
            return SpreadsheetVersion.EXCEL97;
        } else if (isXssf(path)) {
            return SpreadsheetVersion.EXCEL2007;
        } else {
            return SpreadsheetVersion.EXCEL97;
        }
    }

    static boolean isXssf(String path) {
        if (path == null) {
            return false;
        }
        return path.endsWith(XSSF_EXTENSION);
    }

    static boolean isHssf(String path) {
        if (path == null) {
            return false;
        }
        return path.endsWith(HSSF_EXTENSION);
    }

    static String buildText(String symbol, String title) {
        assert symbol != null;
        assert title != null;
        if (symbol.equalsIgnoreCase(title)) {
            return symbol;
        } else {
            return MessageFormat.format(
                    "{0} [{1}]", //$NON-NLS-1$
                    title,
                    symbol);
        }
    }

    private static final Pattern TEXT = Pattern.compile(".*\\[(.*?)\\]");
    static String extractSymbol(String text) {
        assert text != null;
        Matcher matcher = TEXT.matcher(text);
        if (matcher.matches()) {
            return matcher.group(1);
        } else {
            return text;
        }
    }

    private Util() {
        return;
    }
}
TOP

Related Classes of com.asakusafw.testdriver.excel.Util

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.