<commit_before>package org.talend.dataprep.schema;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class XlsUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(XlsUtils.class);

    protected static String getCellValueAsString(Cell cell) {
        if (cell == null) {
            return StringUtils.EMPTY;
        }
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_BLANK:
            return "";
        case Cell.CELL_TYPE_BOOLEAN:
            return cell.getBooleanCellValue() ? Boolean.TRUE.toString() : Boolean.FALSE.toString();
        case Cell.CELL_TYPE_ERROR:
            return "Cell Error type";
        case Cell.CELL_TYPE_FORMULA:
            return cell.getCellFormula();
        case Cell.CELL_TYPE_NUMERIC:
            // TODO configurable??
            if (HSSFDateUtil.isCellDateFormatted(cell)) {
                DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
                return sdf.format(cell.getDateCellValue());
            }
            return String.valueOf(cell.getNumericCellValue());
        case Cell.CELL_TYPE_STRING:
            return StringUtils.trim(cell.getStringCellValue());
        default:
            return "Unknown Cell Type: " + cell.getCellType();
        }

    }

    protected static Workbook getWorkbook(InputStream stream) throws IOException {

        // TODO that's a pain as we have to keep this :-(
        // but for some reasons new HSSFWorkbook consume part of the stream
        byte[] bytes = IOUtils.toByteArray(stream);

        try {
            return new HSSFWorkbook(new ByteArrayInputStream(bytes));
        } catch (OfficeXmlFileException e) {
            LOGGER.debug("OfficeXmlFileException so try XSSFWorkbook");
            return new XSSFWorkbook(new ByteArrayInputStream(bytes));
        }
    }

}<commit_msg>add comment for XlsUtils guess the xls file format<commit_after>package org.talend.dataprep.schema;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class XlsUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(XlsUtils.class);

    protected static String getCellValueAsString(Cell cell) {
        if (cell == null) {
            return StringUtils.EMPTY;
        }
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_BLANK:
            return "";
        case Cell.CELL_TYPE_BOOLEAN:
            return cell.getBooleanCellValue() ? Boolean.TRUE.toString() : Boolean.FALSE.toString();
        case Cell.CELL_TYPE_ERROR:
            return "Cell Error type";
        case Cell.CELL_TYPE_FORMULA:
            return cell.getCellFormula();
        case Cell.CELL_TYPE_NUMERIC:
            // TODO configurable??
            if (HSSFDateUtil.isCellDateFormatted(cell)) {
                DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
                return sdf.format(cell.getDateCellValue());
            }
            return String.valueOf(cell.getNumericCellValue());
        case Cell.CELL_TYPE_STRING:
            return StringUtils.trim(cell.getStringCellValue());
        default:
            return "Unknown Cell Type: " + cell.getCellType();
        }

    }

    protected static Workbook getWorkbook(InputStream stream) throws IOException {

        // Depending on the excel file used the poi object to use is different
        // so we try one (catch exception then try the other one)


        // TODO that's a pain as we have to keep this :-(
        // TODO use ByteBuffer
        // but for some reasons new HSSFWorkbook consume part of the stream
        byte[] bytes = IOUtils.toByteArray(stream);

        try {
            return new HSSFWorkbook(new ByteArrayInputStream(bytes));
        } catch (OfficeXmlFileException e) {
            LOGGER.debug("OfficeXmlFileException so try XSSFWorkbook");
            return new XSSFWorkbook(new ByteArrayInputStream(bytes));
        }
    }

}
{'fraction_non_alphanumeric': 0.09257415886920167, 'fraction_numerical': 0.0007963368504877563, 'mean_word_length': 2.780285929270128, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3959497', 'n_tokens_mistral': 1502, 'n_tokens_neox': 1477, 'n_words': 323}