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]");
}
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]");
}
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");
}
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();
}
}
}
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;
}
Aggregations