use of org.apache.poi.openxml4j.exceptions.ODFNotOfficeXmlFileException in project poi by apache.
the class TestZipPackage method testTidyStreamOnInvalidFile.
/**
* If ZipPackage is passed an invalid file, a call to close
* (eg from the OPCPackage open method) should tidy up the
* stream / file the broken file is being read from.
* See bug #60128 for more
*/
@Test
public void testTidyStreamOnInvalidFile() throws Exception {
// Spreadsheet has a good mix of alternate file types
POIDataSamples files = POIDataSamples.getSpreadSheetInstance();
File[] notValidF = new File[] { files.getFile("SampleSS.ods"), files.getFile("SampleSS.txt") };
InputStream[] notValidS = new InputStream[] { files.openResourceAsStream("SampleSS.ods"), files.openResourceAsStream("SampleSS.txt") };
for (File notValid : notValidF) {
ZipPackage pkg = new ZipPackage(notValid, PackageAccess.READ);
assertNotNull(pkg.getZipArchive());
assertFalse(pkg.getZipArchive().isClosed());
try {
pkg.getParts();
fail("Shouldn't work");
} catch (ODFNotOfficeXmlFileException e) {
} catch (NotOfficeXmlFileException ne) {
}
pkg.close();
assertNotNull(pkg.getZipArchive());
assertTrue(pkg.getZipArchive().isClosed());
}
for (InputStream notValid : notValidS) {
ZipPackage pkg = new ZipPackage(notValid, PackageAccess.READ);
assertNotNull(pkg.getZipArchive());
assertFalse(pkg.getZipArchive().isClosed());
try {
pkg.getParts();
fail("Shouldn't work");
} catch (ODFNotOfficeXmlFileException e) {
} catch (NotOfficeXmlFileException ne) {
}
pkg.close();
assertNotNull(pkg.getZipArchive());
assertTrue(pkg.getZipArchive().isClosed());
}
}
use of org.apache.poi.openxml4j.exceptions.ODFNotOfficeXmlFileException in project poi by apache.
the class ZipPackage method getPartsImpl.
/**
* Retrieves the parts from this package. We assume that the package has not
* been yet inspect to retrieve all the parts, this method will open the
* archive and look for all parts contain inside it. If the package part
* list is not empty, it will be emptied.
*
* @return All parts contain in this package.
* @throws InvalidFormatException if the package is not valid.
*/
@Override
protected PackagePart[] getPartsImpl() throws InvalidFormatException {
if (this.partList == null) {
// The package has just been created, we create an empty part
// list.
this.partList = new PackagePartCollection();
}
if (this.zipArchive == null) {
return this.partList.sortedValues().toArray(new PackagePart[this.partList.size()]);
}
// First we need to parse the content type part
Enumeration<? extends ZipEntry> entries = this.zipArchive.getEntries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (entry.getName().equalsIgnoreCase(ContentTypeManager.CONTENT_TYPES_PART_NAME)) {
try {
this.contentTypeManager = new ZipContentTypeManager(getZipArchive().getInputStream(entry), this);
} catch (IOException e) {
throw new InvalidFormatException(e.getMessage(), e);
}
break;
}
}
// At this point, we should have loaded the content type part
if (this.contentTypeManager == null) {
// Is it a different Zip-based format?
int numEntries = 0;
boolean hasMimetype = false;
boolean hasSettingsXML = false;
entries = this.zipArchive.getEntries();
while (entries.hasMoreElements()) {
final ZipEntry entry = entries.nextElement();
final String name = entry.getName();
if (MIMETYPE.equals(name)) {
hasMimetype = true;
}
if (SETTINGS_XML.equals(name)) {
hasSettingsXML = true;
}
numEntries++;
}
if (hasMimetype && hasSettingsXML) {
throw new ODFNotOfficeXmlFileException("The supplied data appears to be in ODF (Open Document) Format. " + "Formats like these (eg ODS, ODP) are not supported, try Apache ODFToolkit");
}
if (numEntries == 0) {
throw new NotOfficeXmlFileException("No valid entries or contents found, this is not a valid OOXML " + "(Office Open XML) file");
}
// Fallback exception
throw new InvalidFormatException("Package should contain a content type part [M1.13]");
}
// Now create all the relationships
// (Need to create relationships before other
// parts, otherwise we might create a part before
// its relationship exists, and then it won't tie up)
entries = this.zipArchive.getEntries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
PackagePartName partName = buildPartName(entry);
if (partName == null) {
continue;
}
// Only proceed for Relationships at this stage
String contentType = contentTypeManager.getContentType(partName);
if (contentType != null && contentType.equals(ContentTypes.RELATIONSHIPS_PART)) {
try {
PackagePart part = new ZipPackagePart(this, entry, partName, contentType);
partList.put(partName, part);
} catch (InvalidOperationException e) {
throw new InvalidFormatException(e.getMessage(), e);
}
}
}
// Then we can go through all the other parts
entries = this.zipArchive.getEntries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
PackagePartName partName = buildPartName(entry);
if (partName == null) {
continue;
}
String contentType = contentTypeManager.getContentType(partName);
if (contentType != null && contentType.equals(ContentTypes.RELATIONSHIPS_PART)) {
// Already handled
} else if (contentType != null) {
try {
PackagePart part = new ZipPackagePart(this, entry, partName, contentType);
partList.put(partName, part);
} catch (InvalidOperationException e) {
throw new InvalidFormatException(e.getMessage(), e);
}
} else {
throw new InvalidFormatException("The part " + partName.getURI().getPath() + " does not have any content type ! Rule: Package require content types when retrieving a part from a package. [M.1.14]");
}
}
return partList.sortedValues().toArray(new PackagePart[partList.size()]);
}
Aggregations