Search in sources :

Example 76 with InvalidFormatException

use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project poi by apache.

the class XSLFShape method selectPaint.

protected static PaintStyle selectPaint(final CTBlipFillProperties blipFill, final PackagePart parentPart) {
    final CTBlip blip = blipFill.getBlip();
    return new TexturePaint() {

        private PackagePart getPart() {
            try {
                String blipId = blip.getEmbed();
                PackageRelationship rel = parentPart.getRelationship(blipId);
                return parentPart.getRelatedPart(rel);
            } catch (InvalidFormatException e) {
                throw new RuntimeException(e);
            }
        }

        public InputStream getImageData() {
            try {
                return getPart().getInputStream();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        public String getContentType() {
            /* TOOD: map content-type */
            return getPart().getContentType();
        }

        public int getAlpha() {
            return (blip.sizeOfAlphaModFixArray() > 0) ? blip.getAlphaModFixArray(0).getAmt() : 100000;
        }
    };
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) TexturePaint(org.apache.poi.sl.usermodel.PaintStyle.TexturePaint) CTBlip(org.openxmlformats.schemas.drawingml.x2006.main.CTBlip) IOException(java.io.IOException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 77 with InvalidFormatException

use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project poi by apache.

the class XSLFSheet method importBlip.

/**
     * Import a picture data from another document.
     *
     * @param blipId        ID of the package relationship to retrieve.
     * @param packagePart   package part containing the data to import
     * @return ID of the created relationship
     */
String importBlip(String blipId, PackagePart packagePart) {
    PackageRelationship blipRel = packagePart.getRelationship(blipId);
    PackagePart blipPart;
    try {
        blipPart = packagePart.getRelatedPart(blipRel);
    } catch (InvalidFormatException e) {
        throw new POIXMLException(e);
    }
    XSLFPictureData data = new XSLFPictureData(blipPart);
    XMLSlideShow ppt = getSlideShow();
    XSLFPictureData pictureData = ppt.addPicture(data.getData(), data.getType());
    PackagePart pic = pictureData.getPackagePart();
    RelationPart rp = addRelation(blipId, XSLFRelation.IMAGES, new XSLFPictureData(pic));
    return rp.getRelationship().getId();
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) POIXMLException(org.apache.poi.POIXMLException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 78 with InvalidFormatException

use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project poi by apache.

the class XSLFGraphicFrame method copyDiagram.

// TODO should be moved to a sub-class
private void copyDiagram(CTGraphicalObjectData objData, XSLFGraphicFrame srcShape) {
    String xpath = "declare namespace dgm='http://schemas.openxmlformats.org/drawingml/2006/diagram' $this//dgm:relIds";
    XmlObject[] obj = objData.selectPath(xpath);
    if (obj != null && obj.length == 1) {
        XmlCursor c = obj[0].newCursor();
        XSLFSheet sheet = srcShape.getSheet();
        try {
            String dm = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "dm"));
            PackageRelationship dmRel = sheet.getPackagePart().getRelationship(dm);
            PackagePart dmPart = sheet.getPackagePart().getRelatedPart(dmRel);
            getSheet().importPart(dmRel, dmPart);
            String lo = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "lo"));
            PackageRelationship loRel = sheet.getPackagePart().getRelationship(lo);
            PackagePart loPart = sheet.getPackagePart().getRelatedPart(loRel);
            getSheet().importPart(loRel, loPart);
            String qs = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "qs"));
            PackageRelationship qsRel = sheet.getPackagePart().getRelationship(qs);
            PackagePart qsPart = sheet.getPackagePart().getRelatedPart(qsRel);
            getSheet().importPart(qsRel, qsPart);
            String cs = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "cs"));
            PackageRelationship csRel = sheet.getPackagePart().getRelationship(cs);
            PackagePart csPart = sheet.getPackagePart().getRelatedPart(csRel);
            getSheet().importPart(csRel, csPart);
        } catch (InvalidFormatException e) {
            throw new POIXMLException(e);
        }
        c.dispose();
    }
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) QName(javax.xml.namespace.QName) XmlObject(org.apache.xmlbeans.XmlObject) POIXMLException(org.apache.poi.POIXMLException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 79 with InvalidFormatException

use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project cytoscape-impl by cytoscape.

the class LoadNetworkReaderTask method run.

@Override
public void run(final TaskMonitor tm) throws Exception {
    tm.setTitle("Loading network from table");
    tm.setProgress(0.0);
    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));
        SourceColumnSemantic[] tunableColumnTypes = null;
        if (columnTypeList != null && !columnTypeList.trim().isEmpty()) {
            tunableColumnTypes = TypeUtil.parseColumnTypeList(columnTypeList);
        }
        if (tunableColumnTypes != null && tunableColumnTypes.length > 0) {
            System.arraycopy(tunableColumnTypes, 0, typesCopy, 0, Math.min(tunableColumnTypes.length, typesCopy.length));
            // Set the source and target interaction columns
            int index = 1;
            for (SourceColumnSemantic scs : tunableColumnTypes) {
                if (scs.equals(SourceColumnSemantic.SOURCE))
                    indexColumnSourceInteraction = index;
                else if (scs.equals(SourceColumnSemantic.TARGET))
                    indexColumnTargetInteraction = index;
                index++;
            }
        }
        if (nogui) {
            // Handle the validation
            nogui = false;
            ValidationState state = getValidationState(new StringBuffer(80));
            switch(state) {
                case INVALID:
                    tm.showMessage(TaskMonitor.Level.ERROR, "Source column must be specified");
                    return;
                case REQUEST_CONFIRMATION:
                    tm.showMessage(TaskMonitor.Level.WARN, "Target column is not specified.  No edges will be created");
            }
            nogui = true;
        }
        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);
    }
}
Also used : NetworkTableReader(org.cytoscape.tableimport.internal.reader.NetworkTableReader) ArrayList(java.util.ArrayList) CyNetworkReaderManager(org.cytoscape.io.read.CyNetworkReaderManager) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) Workbook(org.apache.poi.ss.usermodel.Workbook) FileInputStream(java.io.FileInputStream) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) IOException(java.io.IOException) SourceColumnSemantic(org.cytoscape.tableimport.internal.util.SourceColumnSemantic) AttributeDataType(org.cytoscape.tableimport.internal.util.AttributeDataType) NetworkTableMappingParameters(org.cytoscape.tableimport.internal.reader.NetworkTableMappingParameters) ExcelNetworkSheetReader(org.cytoscape.tableimport.internal.reader.ExcelNetworkSheetReader) Sheet(org.apache.poi.ss.usermodel.Sheet)

Example 80 with InvalidFormatException

use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project cytoscape-impl by cytoscape.

the class LoadTableReaderTask method run.

@Override
public void run(final TaskMonitor tm) throws Exception {
    tm.setTitle("Loading table data");
    tm.setProgress(0.0);
    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);
    }
}
Also used : ArrayList(java.util.ArrayList) ExcelAttributeSheetReader(org.cytoscape.tableimport.internal.reader.ExcelAttributeSheetReader) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) Workbook(org.apache.poi.ss.usermodel.Workbook) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) IOException(java.io.IOException) SourceColumnSemantic(org.cytoscape.tableimport.internal.util.SourceColumnSemantic) AttributeDataType(org.cytoscape.tableimport.internal.util.AttributeDataType) DefaultAttributeTableReader(org.cytoscape.tableimport.internal.reader.DefaultAttributeTableReader) AttributeMappingParameters(org.cytoscape.tableimport.internal.reader.AttributeMappingParameters) Sheet(org.apache.poi.ss.usermodel.Sheet)

Aggregations

InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)89 IOException (java.io.IOException)39 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)22 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)18 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)18 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)16 InputStream (java.io.InputStream)15 PackageRelationshipCollection (org.apache.poi.openxml4j.opc.PackageRelationshipCollection)15 Workbook (org.apache.poi.ss.usermodel.Workbook)14 ArrayList (java.util.ArrayList)12 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)11 Sheet (org.apache.poi.ss.usermodel.Sheet)11 Test (org.junit.Test)10 URI (java.net.URI)9 FileInputStream (java.io.FileInputStream)8 POIXMLException (org.apache.poi.POIXMLException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 TikaException (org.apache.tika.exception.TikaException)7 XmlException (org.apache.xmlbeans.XmlException)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6