Example usage for org.apache.poi.ss.usermodel WorkbookFactory create

List of usage examples for org.apache.poi.ss.usermodel WorkbookFactory create

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel WorkbookFactory create.

Prototype

public static Workbook create(File file) throws IOException, EncryptedDocumentException 

Source Link

Document

Creates the appropriate HSSFWorkbook / XSSFWorkbook from the given File, which must exist and be readable.

Usage

From source file:org.cytoscape.tableimport.internal.LoadTableReaderTask.java

License:Open Source License

@Override
public void run(TaskMonitor tm) throws Exception {

    tm.setTitle("Loading table data");
    tm.setProgress(0.0);/* www  .j  av a 2s.  c  om*/
    tm.setStatusMessage("Loading table...");

    List<String> attrNameList = new ArrayList<String>();
    int colCount;
    int startLoadRowTemp;
    String[] attributeNames;

    Workbook workbook = null;
    // Load Spreadsheet data for preview.
    if (fileType != null && (fileType.equalsIgnoreCase(SupportedFileType.EXCEL.getExtension())
            || fileType.equalsIgnoreCase(SupportedFileType.OOXML.getExtension())) && workbook == null) {
        try {
            workbook = WorkbookFactory.create(isStart);
        } catch (InvalidFormatException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("Could not read Excel file.  Maybe the file is broken?");
        } finally {
            if (isStart != null) {
                isStart.close();
            }
        }
    }
    if (startLoadRow > 0)
        startLoadRow--;
    startLoadRowTemp = startLoadRow;
    if (firstRowAsColumnNames)
        startLoadRowTemp = 0;

    previewPanel.setPreviewTable(workbook, fileType, inputName, isStart, delimiters.getSelectedValues(), null,
            50, null, startLoadRowTemp);

    colCount = previewPanel.getPreviewTable().getColumnModel().getColumnCount();
    importFlag = new boolean[colCount];
    Object curName = null;

    if (firstRowAsColumnNames) {
        setFirstRowAsColumnNames();
        startLoadRow++;
    }

    for (int i = 0; i < colCount; i++) {
        importFlag[i] = true;
        curName = previewPanel.getPreviewTable().getColumnModel().getColumn(i).getHeaderValue();

        if (attrNameList.contains(curName)) {
            int dupIndex = 0;

            for (int idx = 0; idx < attrNameList.size(); idx++) {
                if (curName.equals(attrNameList.get(idx))) {
                    dupIndex = idx;

                    break;
                }
            }

            if (importFlag[i] && importFlag[dupIndex]) {
                //TODO add message to user
                return;
            }
        }

        if (curName == null) {
            attrNameList.add("Column " + i);
        } else {
            attrNameList.add(curName.toString());
        }
    }
    attributeNames = attrNameList.toArray(new String[0]);

    final Byte[] test = previewPanel.getDataTypes(previewPanel.getSelectedSheetName());

    final Byte[] attributeTypes = new Byte[test.length];

    for (int i = 0; i < test.length; i++) {
        attributeTypes[i] = test[i];
    }

    if (keyColumnIndex > 0)
        keyColumnIndex--;

    amp = new AttributeMappingParameters(delimiters.getSelectedValues(),
            delimitersForDataList.getSelectedValue(), keyColumnIndex, attributeNames, attributeTypes,
            previewPanel.getCurrentListDataTypes(), importFlag, true, startLoadRow, null);

    if (this.fileType.equalsIgnoreCase(SupportedFileType.EXCEL.getExtension())
            || this.fileType.equalsIgnoreCase(SupportedFileType.OOXML.getExtension())) {

        // Fixed bug# 1668, Only load data from the first sheet, ignore the rest sheets
        if (workbook.getNumberOfSheets() > 0) {
            final Sheet sheet = workbook.getSheetAt(0);
            this.reader = new ExcelAttributeSheetReader(sheet, amp);
            loadAnnotation(tm);
        }
    } else {
        this.reader = new DefaultAttributeTableReader(null, amp, this.isEnd);
        loadAnnotation(tm);
    }
}

From source file:org.cytoscape.tableimport.internal.task.ImportAttributeTableReaderTask.java

License:Open Source License

@Override
public void run(TaskMonitor tm) throws Exception {
    tm.setTitle("Loading table data");
    tm.setProgress(0.0);//from www .  jav a  2 s. c o m
    tm.setStatusMessage("Loading table...");

    Workbook workbook = null;

    // Load Spreadsheet data for preview.
    if (fileType != null && (fileType.equalsIgnoreCase(SupportedFileType.EXCEL.getExtension())
            || fileType.equalsIgnoreCase(SupportedFileType.OOXML.getExtension())) && workbook == null) {
        try {
            workbook = WorkbookFactory.create(is);
        } catch (InvalidFormatException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("Could not read Excel file.  Maybe the file is broken?");
        } finally {
            if (is != null)
                is.close();
        }
    }

    if (this.fileType.equalsIgnoreCase(SupportedFileType.EXCEL.getExtension())
            || this.fileType.equalsIgnoreCase(SupportedFileType.OOXML.getExtension())) {

        // Fixed bug# 1668, Only load data from the first sheet, ignore the rest sheets
        // UPDATE: From the user perspective it makes more sense to get the selected tab/sheet instead.
        String networkName = amp.getName();

        if (networkName == null)
            networkName = workbook.getSheetName(0);

        final Sheet sheet = workbook.getSheet(networkName);

        if (sheet != null) {
            reader = new ExcelAttributeSheetReader(sheet, amp, serviceRegistrar);
            loadAnnotation(tm);
        }
    } else {
        try {
            reader = new DefaultAttributeTableReader(null, amp, this.is, serviceRegistrar);
            loadAnnotation(tm);
        } catch (Exception ioe) {
            tm.showMessage(TaskMonitor.Level.ERROR, "Unable to read table: " + ioe.getMessage());
        }
    }
}

From source file:org.cytoscape.tableimport.internal.task.ImportNetworkTableReaderTask.java

License:Open Source License

@Override
public void run(TaskMonitor tm) throws Exception {
    tm.setTitle("Loading network from table");
    tm.setProgress(0.0);//  ww  w. j  a va  2 s.  co m
    tm.setStatusMessage("Loading network...");

    Workbook workbook = null;

    // Load Spreadsheet data for preview.
    if (fileType != null && (fileType.equalsIgnoreCase(SupportedFileType.EXCEL.getExtension())
            || fileType.equalsIgnoreCase(SupportedFileType.OOXML.getExtension())) && workbook == null) {
        try {
            workbook = WorkbookFactory.create(is);
        } catch (InvalidFormatException e) {
            throw new IllegalArgumentException("Could not read Excel file.  Maybe the file is broken?", e);
        } finally {
            if (is != null)
                is.close();
        }
    }

    try {
        if (this.fileType.equalsIgnoreCase(SupportedFileType.EXCEL.getExtension())
                || this.fileType.equalsIgnoreCase(SupportedFileType.OOXML.getExtension())) {
            String networkName = ntmp.getName();

            if (networkName == null)
                networkName = workbook.getSheetName(0);

            final Sheet sheet = workbook.getSheet(networkName);

            reader = new ExcelNetworkSheetReader(networkName, sheet, ntmp, nMap, rootNetwork, serviceRegistrar);
        } else {
            reader = new NetworkTableReader(inputName, is, ntmp, nMap, rootNetwork, serviceRegistrar);
        }
    } catch (Exception ioe) {
        tm.showMessage(TaskMonitor.Level.ERROR, "Unable to read table: " + ioe.getMessage());
        return;
    }

    loadNetwork(tm);
    tm.setProgress(1.0);
}

From source file:org.cytoscape.tableimport.internal.task.LoadNetworkReaderTask.java

License:Open Source License

@Override
public void run(final TaskMonitor tm) throws Exception {
    tm.setTitle("Loading network from table");
    tm.setProgress(0.0);/*w  w  w .j a v  a 2s .  c om*/
    tm.setStatusMessage("Loading network...");
    taskMonitor = tm;

    final List<String> attrNameList = new ArrayList<>();
    int colCount;
    String[] attributeNames;

    final CyNetworkReaderManager networkReaderManager = serviceRegistrar
            .getService(CyNetworkReaderManager.class);

    if (is != null)
        netReader = networkReaderManager.getReader(is, inputName);

    if (netReader == null)
        netReader = networkReaderManager.getReader(uri, inputName);

    if (netReader instanceof CombineReaderAndMappingTask) {
        Workbook workbook = null;

        // Load Spreadsheet data for preview.
        if (fileType != null
                && (fileType.equalsIgnoreCase(SupportedFileType.EXCEL.getExtension())
                        || fileType.equalsIgnoreCase(SupportedFileType.OOXML.getExtension()))
                && workbook == null) {
            try {
                workbook = WorkbookFactory.create(new FileInputStream(tempFile));
            } catch (InvalidFormatException e) {
                //e.printStackTrace();
                throw new IllegalArgumentException("Could not read Excel file.  Maybe the file is broken?", e);
            } finally {

            }
        }

        netReader = null;

        if (startLoadRow > 0)
            startLoadRow--;

        final int startLoadRowTemp = firstRowAsColumnNames ? 0 : startLoadRow;

        previewPanel.updatePreviewTable(workbook, fileType, tempFile.getAbsolutePath(),
                new FileInputStream(tempFile), delimiters.getSelectedValues(), null, startLoadRowTemp);

        colCount = previewPanel.getPreviewTable().getColumnModel().getColumnCount();
        Object curName = null;

        if (firstRowAsColumnNames) {
            previewPanel.setFirstRowAsColumnNames();
            startLoadRow++;
        }

        final SourceColumnSemantic[] types = previewPanel.getTypes();

        for (int i = 0; i < colCount; i++) {
            curName = previewPanel.getPreviewTable().getColumnModel().getColumn(i).getHeaderValue();

            if (attrNameList.contains(curName)) {
                int dupIndex = 0;

                for (int idx = 0; idx < attrNameList.size(); idx++) {
                    if (curName.equals(attrNameList.get(idx))) {
                        dupIndex = idx;

                        break;
                    }
                }

                if (!TypeUtil.allowsDuplicateName(ImportType.NETWORK_IMPORT, types[i], types[dupIndex])) {
                    // TODO add message to user (Duplicate Column Name Found)
                    return;
                }
            }

            if (curName == null)
                attrNameList.add("Column " + i);
            else
                attrNameList.add(curName.toString());
        }

        attributeNames = attrNameList.toArray(new String[attrNameList.size()]);

        final SourceColumnSemantic[] typesCopy = Arrays.copyOf(types, types.length);

        final AttributeDataType[] dataTypes = previewPanel.getDataTypes();
        final AttributeDataType[] dataTypesCopy = Arrays.copyOf(dataTypes, dataTypes.length);

        AttributeDataType[] tunableDataTypes = null;

        if (dataTypeList != null && !dataTypeList.trim().isEmpty())
            tunableDataTypes = TypeUtil.parseDataTypeList(dataTypeList);

        if (tunableDataTypes != null && tunableDataTypes.length > 0)
            System.arraycopy(tunableDataTypes, 0, dataTypesCopy, 0,
                    Math.min(tunableDataTypes.length, dataTypesCopy.length));

        String[] listDelimiters = previewPanel.getListDelimiters();

        if (listDelimiters == null || listDelimiters.length == 0) {
            listDelimiters = new String[dataTypes.length];

            if (delimitersForDataList.getSelectedValue() != null)
                Arrays.fill(listDelimiters, delimitersForDataList.getSelectedValue());
        }

        if (indexColumnSourceInteraction > 0)
            indexColumnSourceInteraction--;

        if (indexColumnTargetInteraction > 0)
            indexColumnTargetInteraction--;

        if (indexColumnTypeInteraction > 0)
            indexColumnTypeInteraction--;

        networkName = previewPanel.getSourceName();

        ntmp = new NetworkTableMappingParameters(networkName, delimiters.getSelectedValues(), listDelimiters,
                attributeNames, dataTypesCopy, typesCopy, indexColumnSourceInteraction,
                indexColumnTargetInteraction, indexColumnTypeInteraction, defaultInteraction, startLoadRow,
                null);

        try {
            if (this.fileType.equalsIgnoreCase(SupportedFileType.EXCEL.getExtension())
                    || this.fileType.equalsIgnoreCase(SupportedFileType.OOXML.getExtension())) {
                final Sheet sheet = workbook.getSheet(networkName);

                reader = new ExcelNetworkSheetReader(networkName, sheet, ntmp, nMap, rootNetwork,
                        serviceRegistrar);
            } else {
                networkName = this.inputName;
                reader = new NetworkTableReader(networkName, new FileInputStream(tempFile), ntmp, nMap,
                        rootNetwork, serviceRegistrar);
            }
        } catch (Exception ioe) {
            tm.showMessage(TaskMonitor.Level.ERROR, "Unable to read network: " + ioe.getMessage());
            return;
        }

        loadNetwork(tm);
        tm.setProgress(1.0);
    } else {
        networkName = this.inputName;
        insertTasksAfterCurrentTask(netReader);
    }
}

From source file:org.cytoscape.tableimport.internal.task.LoadTableReaderTask.java

License:Open Source License

@Override
public void run(final TaskMonitor tm) throws Exception {
    tm.setTitle("Loading table data");
    tm.setProgress(0.0);/*w  w w  . j a  va  2  s . co  m*/
    tm.setStatusMessage("Loading table...");

    List<String> attrNameList = new ArrayList<>();
    int colCount;
    String[] attributeNames;

    Workbook workbook = null;
    // Load Spreadsheet data for preview.
    try {
        if (fileType != null
                && (fileType.equalsIgnoreCase(SupportedFileType.EXCEL.getExtension())
                        || fileType.equalsIgnoreCase(SupportedFileType.OOXML.getExtension()))
                && workbook == null) {
            try {
                workbook = WorkbookFactory.create(isStart);
            } catch (InvalidFormatException e) {
                e.printStackTrace();
                throw new IllegalArgumentException("Could not read Excel file.  Maybe the file is broken?");
            } finally {
                if (isStart != null)
                    isStart.close();
            }
        }
    } catch (Exception ioe) {
        tm.showMessage(TaskMonitor.Level.ERROR, "Unable to read table: " + ioe.getMessage());
        return;
    }

    if (startLoadRow > 0)
        startLoadRow--;

    final int startLoadRowTemp = firstRowAsColumnNames ? 0 : startLoadRow;

    previewPanel.updatePreviewTable(workbook, fileType, inputName, isStart, delimiters.getSelectedValues(),
            null, startLoadRowTemp);

    colCount = previewPanel.getPreviewTable().getColumnModel().getColumnCount();
    Object curName = null;

    if (firstRowAsColumnNames) {
        previewPanel.setFirstRowAsColumnNames();
        startLoadRow++;
    }

    final String sourceName = previewPanel.getSourceName();
    final SourceColumnSemantic[] types = previewPanel.getTypes();

    for (int i = 0; i < colCount; i++) {
        curName = previewPanel.getPreviewTable().getColumnModel().getColumn(i).getHeaderValue();

        if (attrNameList.contains(curName)) {
            int dupIndex = 0;

            for (int idx = 0; idx < attrNameList.size(); idx++) {
                if (curName.equals(attrNameList.get(idx))) {
                    dupIndex = idx;

                    break;
                }
            }

            if (!TypeUtil.allowsDuplicateName(ImportType.TABLE_IMPORT, types[i], types[dupIndex])) {
                //TODO add message to user
                return;
            }
        }

        if (curName == null)
            attrNameList.add("Column " + i);
        else
            attrNameList.add(curName.toString());
    }

    attributeNames = attrNameList.toArray(new String[0]);

    final SourceColumnSemantic[] typesCopy = Arrays.copyOf(types, types.length);

    final AttributeDataType[] dataTypes = previewPanel.getDataTypes();
    final AttributeDataType[] dataTypesCopy = Arrays.copyOf(dataTypes, dataTypes.length);

    AttributeDataType[] tunableDataTypes = null;

    if (dataTypeList != null && !dataTypeList.trim().isEmpty())
        tunableDataTypes = TypeUtil.parseDataTypeList(dataTypeList);

    if (tunableDataTypes != null && tunableDataTypes.length > 0)
        System.arraycopy(tunableDataTypes, 0, dataTypesCopy, 0,
                Math.min(tunableDataTypes.length, dataTypesCopy.length));

    String[] listDelimiters = previewPanel.getListDelimiters();

    if (listDelimiters == null || listDelimiters.length == 0) {
        listDelimiters = new String[dataTypes.length];

        if (delimitersForDataList.getSelectedValue() != null)
            Arrays.fill(listDelimiters, delimitersForDataList.getSelectedValue());
    }

    if (keyColumnIndex > 0)
        keyColumnIndex--;

    amp = new AttributeMappingParameters(sourceName, delimiters.getSelectedValues(), listDelimiters,
            keyColumnIndex, attributeNames, dataTypesCopy, typesCopy, startLoadRow, null);

    if (this.fileType.equalsIgnoreCase(SupportedFileType.EXCEL.getExtension())
            || this.fileType.equalsIgnoreCase(SupportedFileType.OOXML.getExtension())) {

        // Fixed bug# 1668, Only load data from the first sheet, ignore the rest sheets
        // UPDATE: From the user perspective it makes more sense to get the selected tab/sheet than the first one.
        final Sheet sheet = workbook.getSheet(sourceName);

        if (sheet != null) {
            reader = new ExcelAttributeSheetReader(sheet, amp, serviceRegistrar);
            loadAnnotation(tm);
        }
    } else {
        reader = new DefaultAttributeTableReader(null, amp, this.isEnd, serviceRegistrar);
        loadAnnotation(tm);
    }
}

From source file:org.cytoscape.tableimport.internal.ui.ImportTablePanel.java

License:Open Source License

/**
 * Display preview table//from w w  w .j a  v  a  2 s .c om
 */
protected void readAnnotationForPreview(List<String> delimiters) throws IOException {
    /*
     * Load data from the given URL.
     */
    final String commentChar = getCommentLinePrefix();
    int startLine = getStartLineNumber();

    // creating the IS copy
    InputStream tempIs = null;

    if (tempFile != null)
        tempIs = new FileInputStream(tempFile);

    // Load Spreadsheet data for preview.
    if (isSpreadsheetFile() && workbook == null) {
        try {
            workbook = WorkbookFactory.create(tempIs);
        } catch (InvalidFormatException e) {
            tempIs.close();
            throw new IllegalArgumentException("Could not read Excel file.  Maybe the file is broken?", e);
        }
    }

    if (tempIs != null)
        tempIs.close();

    InputStream tempIs2 = null;

    if (tempFile != null)
        tempIs2 = new FileInputStream(tempFile);

    getPreviewPanel().updatePreviewTable(workbook, fileType, "", tempIs2, delimiters, commentChar,
            startLine - 1);

    if (tempIs2 != null)
        tempIs2.close();

    if (getPreviewPanel().getPreviewTable() == null)
        return;

    if (importType != NETWORK_IMPORT) {
        if (getPreviewPanel().getFileType() == FileType.GENE_ASSOCIATION_FILE) {
            final JTable table = getPreviewPanel().getPreviewTable();
            final TableModel previewModel = table.getModel();
            final String[] columnNames = new String[previewModel.getColumnCount()];

            for (int j = 0; j < columnNames.length; j++)
                columnNames[j] = previewModel.getColumnName(j);

            disableComponentsForGA();
        }

        /*
         * If this is not an Excel file, enable delimiter checkboxes.
         */
        if (fileType != null) {
            final FileType type = checkFileType();

            if (type == FileType.GENE_ASSOCIATION_FILE) {
                getPreviewPanel().setType(GO_ID.getPosition(), ONTOLOGY);
                disableComponentsForGA();
            } else if (!isSpreadsheetFile()) {
                nodeRadioButton.setEnabled(true);
                edgeRadioButton.setEnabled(true);
                networkRadioButton.setEnabled(true);
                getImportAllCheckBox().setEnabled(false);
            } else {
                getImportAllCheckBox().setEnabled(false);
            }
        }

        attributeRadioButtonActionPerformed(null);
    }

    getStartRowSpinner().setEnabled(true);

    final Window parent = SwingUtilities.getWindowAncestor(this);

    if (parent != null)
        parent.pack();
}

From source file:org.dash.valid.ars.AntigenRecognitionSiteLoader.java

License:Open Source License

private static HashMap<String, List<String>> loadARSData() throws InvalidFormatException, IOException {
    Workbook workbook = null;/*from   ww  w  .  ja va2 s  .  c o m*/

    workbook = WorkbookFactory
            .create(AntigenRecognitionSiteLoader.class.getClassLoader().getResourceAsStream(DEFAULT_ARS_FILE));

    // Return first sheet from the XLSX workbook
    Sheet mySheet = workbook.getSheetAt(0);

    // Get iterator to all the rows in current sheet
    Iterator<Row> rowIterator = mySheet.iterator();

    String gCode;
    String alleleString;
    List<String> alleles;
    HashMap<String, List<String>> arsMap = new HashMap<String, List<String>>();

    // Traversing over each row of XLSX file
    while (rowIterator.hasNext()) {
        alleles = new ArrayList<String>();
        Row row = rowIterator.next();
        gCode = row.getCell(0).getStringCellValue();
        if (gCode.contains(GLStringConstants.ASTERISK)) {
            alleleString = row.getCell(1).getStringCellValue();
            String[] parts = alleleString.split(GLStringConstants.COMMA);
            for (String part : parts) {
                alleles.add(GLStringConstants.HLA_DASH + part);
            }

            arsMap.put(GLStringConstants.HLA_DASH + gCode, alleles);
        }
    }

    workbook.close();

    return arsMap;
}

From source file:org.dash.valid.freq.HLAFrequenciesLoader.java

License:Open Source License

private List<DisequilibriumElement> loadNMDPLinkageReferenceData(String filename, Locus[] locusPositions)
        throws IOException, InvalidFormatException {
    List<DisequilibriumElement> disequilibriumElements = new ArrayList<DisequilibriumElement>();

    // Finds the workbook instance for XLSX file
    InputStream inStream = HLAFrequenciesLoader.class.getClassLoader().getResourceAsStream(filename);

    if (inStream == null) {
        throw new FileNotFoundException();
    }/*from w w w  .  j  a  va2 s.co  m*/

    Workbook workbook = WorkbookFactory.create(inStream);

    // Return first sheet from the XLSX workbook
    Sheet mySheet = workbook.getSheetAt(0);

    // Get iterator to all the rows in current sheet
    Iterator<Row> rowIterator = mySheet.iterator();

    int firstRow = mySheet.getFirstRowNum();

    List<String> raceHeaders = null;

    // Traversing over each row of XLSX file
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();

        if (row.getRowNum() == firstRow) {
            raceHeaders = readHeaderElementsByRace(row);
        } else {
            disequilibriumElements.add(readDiseqilibriumElementsByRace(row, raceHeaders, locusPositions));
        }
    }

    workbook.close();

    return disequilibriumElements;
}

From source file:org.dash.valid.freq.HLAFrequenciesLoader.java

License:Open Source License

private void loadIndividualLocusFrequency(Frequencies freq, Locus locus)
        throws IOException, InvalidFormatException {
    List<String> singleLocusFrequencies = new ArrayList<String>();
    String extension = freq.equals(Frequencies.NMDP) ? ".xlsx" : ".xls";
    InputStream inputStream = HLAFrequenciesLoader.class.getClassLoader().getResourceAsStream(
            "frequencies/" + freq.getShortName() + "/" + locus.getFrequencyName() + extension);

    if (inputStream == null)
        return;/*  w w w .  j a va2 s .c o  m*/

    Workbook workbook = WorkbookFactory.create(inputStream);

    // Return first sheet from the XLSX workbook
    Sheet mySheet = workbook.getSheetAt(0);

    // Get iterator to all the rows in current sheet
    Iterator<Row> rowIterator = mySheet.iterator();

    int firstRow = mySheet.getFirstRowNum();

    String cellValue = null;

    // Traversing over each row of XLSX file
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();

        if (row.getRowNum() == firstRow) {
            continue;
        } else {
            cellValue = row.getCell(0).getStringCellValue();
            if (!cellValue.contains(GLStringConstants.ASTERISK)) {
                cellValue = locus.getShortName() + GLStringConstants.ASTERISK + cellValue.substring(0, 2)
                        + GLStringUtilities.COLON + cellValue.substring(2);
            }
            singleLocusFrequencies.add(GLStringConstants.HLA_DASH + cellValue);
        }
    }

    individualLocusFrequencies.put(locus, singleLocusFrequencies);

    workbook.close();
}

From source file:org.databene.benerator.template.xmlanon.XmlAnonInputReader.java

License:Open Source License

private static AnonymizationSetup parseXls(String xlsUri) throws IOException, InvalidFormatException {
    Workbook workbook = WorkbookFactory.create(IOUtil.getInputStreamForURI(xlsUri));
    Sheet sheet = workbook.getSheetAt(0);

    // parse header information
    int varnameColumnIndex = -1;
    ArrayList<String> files = new ArrayList<String>();
    Row headerRow = sheet.getRow(0);/*ww  w  .  j  av  a  2  s.c om*/
    Assert.notNull(headerRow, "header row");
    for (int i = 0; i <= headerRow.getLastCellNum(); i++) {
        String header = headerRow.getCell(i).getStringCellValue();
        if ("varname".equals(header)) {
            varnameColumnIndex = i;
            break;
        } else {
            if (StringUtil.isEmpty(header))
                throw new ConfigurationError(
                        "Filename missing in column header #" + i + " of Excel document " + xlsUri);
            files.add(header);
        }
    }
    if (varnameColumnIndex == -1)
        throw new ConfigurationError("No 'varname' header defined in Excel document " + xlsUri);
    if (files.size() == 0)
        throw new ConfigurationError("No files specified in Excel document " + xlsUri);

    // parse anonymization rows
    List<Anonymization> anonymizations = new ArrayList<Anonymization>();
    for (int rownum = 1; rownum <= sheet.getLastRowNum(); rownum++) {
        Row row = sheet.getRow(rownum);
        if (XLSUtil.isEmpty(row))
            continue;
        Cell varnameCell = row.getCell(varnameColumnIndex);
        if (varnameCell == null || StringUtil.isEmpty(varnameCell.getStringCellValue()))
            throw new ConfigurationError("'varname' cell empty in table row #" + (rownum + 1));
        Anonymization anon = new Anonymization(varnameCell.getStringCellValue());
        // parse locators
        for (int colnum = 0; colnum < varnameColumnIndex; colnum++) {
            Cell cell = row.getCell(colnum);
            String path = (cell != null ? cell.getStringCellValue() : null);
            if (!StringUtil.isEmpty(path)) {
                List<String> tokens = XPathTokenizer.tokenize(path);
                String entityPath = XPathTokenizer.merge(tokens, 0, tokens.size() - 2);
                String entity = normalizeXMLPath(XPathTokenizer.nodeName(tokens.get(tokens.size() - 2)));
                String attribute = normalizeXMLPath(tokens.get(tokens.size() - 1));
                anon.addLocator(new Locator(files.get(colnum), path, entityPath, entity, attribute));
            }
        }
        // parse settings
        for (int colnum = varnameColumnIndex + 1; colnum < row.getLastCellNum() - 1; colnum += 2) {
            String key = row.getCell(colnum).getStringCellValue();
            String value = row.getCell(colnum + 1).getStringCellValue();
            if (!StringUtil.isEmpty(key) && !StringUtil.isEmpty(value))
                anon.addSetting(key, value);
        }
        anonymizations.add(anon);
    }
    return new AnonymizationSetup(files, anonymizations);
}