Search in sources :

Example 11 with InvalidOperationException

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

the class TestOPCCompliancePackageModel method testPartNameDerivationAdditionFailure.

/**
     * A package implementer shall neither create nor recognize a part with a
     * part name derived from another part name by appending segments to it.
     * [M1.11]
     */
@Test
public void testPartNameDerivationAdditionFailure() {
    OPCPackage pkg = OPCPackage.create("TODELETEIFEXIST.docx");
    try {
        PackagePartName name = PackagingURIHelper.createPartName("/word/document.xml");
        PackagePartName nameDerived = PackagingURIHelper.createPartName("/word/document.xml/image1.gif");
        pkg.createPart(name, ContentTypes.XML);
        pkg.createPart(nameDerived, ContentTypes.EXTENSION_GIF);
    } catch (InvalidOperationException e) {
        pkg.revert();
        return;
    } catch (InvalidFormatException e) {
        fail(e.getMessage());
    }
    fail("A package implementer shall neither create nor recognize a part with a" + " part name derived from another part name by appending segments to it." + " [M1.11]");
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) Test(org.junit.Test)

Example 12 with InvalidOperationException

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

the class TestOPCCompliancePackageModel method testAddPackageAlreadyAddFailure2.

/**
     * Rule M1.12 : Packages shall not contain equivalent part names and package
     * implementers shall neither create nor recognize packages with equivalent
     * part names.
     */
@Test
public void testAddPackageAlreadyAddFailure2() throws Exception {
    OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
    PackagePartName partName = null;
    try {
        partName = PackagingURIHelper.createPartName("/word/document.xml");
    } catch (InvalidFormatException e) {
        throw new Exception(e.getMessage());
    }
    pkg.createPart(partName, ContentTypes.XML);
    try {
        pkg.createPart(partName, ContentTypes.XML);
    } catch (InvalidOperationException e) {
        return;
    }
    fail("Packages shall not contain equivalent part names and package implementers shall neither create nor recognize packages with equivalent part names. [M1.12]");
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) IOException(java.io.IOException) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) PartAlreadyExistsException(org.apache.poi.openxml4j.exceptions.PartAlreadyExistsException) Test(org.junit.Test)

Example 13 with InvalidOperationException

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

the class TestOPCCompliancePackageModel method testAddRelationshipRelationshipsPartFailure.

/**
     * Try to add a relationship to a relationship part.
     *
     * Check rule M1.25: The Relationships part shall not have relationships to
     * any other part. Package implementers shall enforce this requirement upon
     * the attempt to create such a relationship and shall treat any such
     * relationship as invalid.
     */
@Test
public void testAddRelationshipRelationshipsPartFailure() {
    OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
    PackagePartName name1 = null;
    try {
        name1 = PackagingURIHelper.createPartName("/test/_rels/document.xml.rels");
    } catch (InvalidFormatException e) {
        fail("This exception should never happen !");
    }
    try {
        pkg.addRelationship(name1, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT);
    } catch (InvalidOperationException e) {
        return;
    }
    fail("Fail test -> M1.25: The Relationships part shall not have relationships to any other part");
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) Test(org.junit.Test)

Example 14 with InvalidOperationException

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

the class TestXSSFBugs method bug57482.

/**
     * A .xlsx file with no Shared Strings table should open fine
     * in read-only mode
     */
@SuppressWarnings("resource")
@Test
public void bug57482() throws IOException, InvalidFormatException {
    for (PackageAccess access : new PackageAccess[] { PackageAccess.READ_WRITE, PackageAccess.READ }) {
        File file = HSSFTestDataSamples.getSampleFile("57482-OnlyNumeric.xlsx");
        OPCPackage pkg = OPCPackage.open(file, access);
        try {
            // Try to open it and read the contents
            XSSFWorkbook wb1 = new XSSFWorkbook(pkg);
            assertNotNull(wb1.getSharedStringSource());
            assertEquals(0, wb1.getSharedStringSource().getCount());
            DataFormatter fmt = new DataFormatter();
            XSSFSheet s = wb1.getSheetAt(0);
            assertEquals("1", fmt.formatCellValue(s.getRow(0).getCell(0)));
            assertEquals("11", fmt.formatCellValue(s.getRow(0).getCell(1)));
            assertEquals("5", fmt.formatCellValue(s.getRow(4).getCell(0)));
            // Add a text cell
            s.getRow(0).createCell(3).setCellValue("Testing");
            assertEquals("Testing", fmt.formatCellValue(s.getRow(0).getCell(3)));
            // Try to write-out and read again, should only work
            //  in read-write mode, not read-only mode
            XSSFWorkbook wb2 = null;
            try {
                wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
                if (access == PackageAccess.READ) {
                    fail("Shouln't be able to write from read-only mode");
                }
                // Check again
                s = wb2.getSheetAt(0);
                assertEquals("1", fmt.formatCellValue(s.getRow(0).getCell(0)));
                assertEquals("11", fmt.formatCellValue(s.getRow(0).getCell(1)));
                assertEquals("5", fmt.formatCellValue(s.getRow(4).getCell(0)));
                assertEquals("Testing", fmt.formatCellValue(s.getRow(0).getCell(3)));
            } catch (InvalidOperationException e) {
                if (access == PackageAccess.READ_WRITE) {
                    // Shouldn't occur in write-mode
                    throw e;
                }
            } finally {
                if (wb2 != null) {
                    wb2.getPackage().revert();
                }
            }
            wb1.getPackage().revert();
        } finally {
            pkg.revert();
        }
    }
}
Also used : PackageAccess(org.apache.poi.openxml4j.opc.PackageAccess) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) TempFile(org.apache.poi.util.TempFile) ZipSecureFile(org.apache.poi.openxml4j.util.ZipSecureFile) File(java.io.File) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 15 with InvalidOperationException

use of org.apache.poi.openxml4j.exceptions.InvalidOperationException in project goci by EBISPOT.

the class AssociationFileUploadService method processAndValidateAssociationFile.

/**
 * Process uploaded file and return a list of errors
 *
 * @param file XLSX file supplied by user
 */
public ValidationSummary processAndValidateAssociationFile(File file, String validationLevel) throws FileNotFoundException, SheetProcessingException {
    ValidationSummary validationSummary = new ValidationSummary();
    Collection<RowValidationSummary> rowValidationSummaries = new ArrayList<>();
    Collection<AssociationSummary> associationSummaries = new ArrayList<>();
    Collection<AssociationUploadRow> fileRows = new ArrayList<>();
    if (file.exists()) {
        // Create sheet
        XSSFSheet sheet = null;
        try {
            // Create a sheet for reading
            sheet = sheetCreationService.createSheet(file.getAbsolutePath());
            // Process file, depending on validation level, into a generic row object
            UploadSheetProcessor uploadSheetProcessor = uploadSheetProcessorBuilder.buildProcessor(validationLevel);
            fileRows = uploadSheetProcessor.readSheetRows(sheet);
        } catch (InvalidFormatException | InvalidOperationException | IOException | NullPointerException e) {
            getLog().error("File: " + file.getName() + " cannot be processed", e);
            file.delete();
            throw new SheetProcessingException("File: " + file.getName() + " cannot be processed", e);
        }
    } else {
        getLog().error("File: " + file.getName() + " cannot be found");
        throw new FileNotFoundException("File does not exist");
    }
    String eRelease = ensemblRestTemplateService.getRelease();
    // Error check each row
    if (!fileRows.isEmpty()) {
        // Check for missing values and syntax errors that would prevent code creating an association
        for (AssociationUploadRow row : fileRows) {
            getLog().info("Syntax checking row: " + row.getRowNumber() + " of file, " + file.getAbsolutePath());
            RowValidationSummary rowValidationSummary = createRowValidationSummary(row, eRelease);
            // Only store summary if there is an error
            if (!rowValidationSummary.getErrors().isEmpty()) {
                rowValidationSummaries.add(rowValidationSummary);
            }
        }
        if (rowValidationSummaries.isEmpty()) {
            // Proceed to carry out full checks of values
            fileRows.forEach(row -> {
                associationSummaries.add(createAssociationSummary(row, validationLevel, eRelease));
            });
        }
    }
    validationSummary.setAssociationSummaries(associationSummaries);
    validationSummary.setRowValidationSummaries(rowValidationSummaries);
    return validationSummary;
}
Also used : SheetProcessingException(uk.ac.ebi.spot.goci.exception.SheetProcessingException) RowValidationSummary(uk.ac.ebi.spot.goci.model.RowValidationSummary) RowValidationSummary(uk.ac.ebi.spot.goci.model.RowValidationSummary) ValidationSummary(uk.ac.ebi.spot.goci.model.ValidationSummary) AssociationSummary(uk.ac.ebi.spot.goci.model.AssociationSummary) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) AssociationUploadRow(uk.ac.ebi.spot.goci.model.AssociationUploadRow)

Aggregations

InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)15 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)9 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)7 IOException (java.io.IOException)6 Test (org.junit.Test)6 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)3 File (java.io.File)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 NotOfficeXmlFileException (org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException)2 TempFile (org.apache.poi.util.TempFile)2 XLSX2CSV (com.jeesuite.common2.excel.convert.XLSX2CSV)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 ODFNotOfficeXmlFileException (org.apache.poi.openxml4j.exceptions.ODFNotOfficeXmlFileException)1