use of org.apache.poi.openxml4j.exceptions.InvalidOperationException in project jeesuite-libs by vakinge.
the class ExcelPerfModeReader method readAsXLSX.
private List<String> readAsXLSX(String path) {
OPCPackage opcPackage = null;
try {
opcPackage = OPCPackage.open(path, PackageAccess.READ);
XLSX2CSV xlsx2csv = new XLSX2CSV(opcPackage, System.out, -1);
return xlsx2csv.process();
} catch (Exception e) {
if (e instanceof OLE2NotOfficeXmlFileException || e instanceof NotOLE2FileException || e instanceof NotOfficeXmlFileException || e instanceof OfficeXmlFileException) {
throw new ExcelOperBaseException("请选择正确格式excel文件");
}
if (e instanceof IOException) {
throw new ExcelOperBaseException("文件读取失败");
}
if (e instanceof InvalidOperationException) {
throw new ExcelOperBaseException(e);
}
throw new RuntimeException(e);
} finally {
try {
opcPackage.close();
} catch (Exception e) {
}
}
}
use of org.apache.poi.openxml4j.exceptions.InvalidOperationException in project poi by apache.
the class ContentTypeManager method removeContentType.
/**
* <p>
* Delete a content type based on the specified part name. If the specified
* part name is register with an override content type, then this content
* type is remove, else the content type is remove in the default content
* type list if it exists and if no part is associated with it yet.
* </p><p>
* Check rule M2.4: The package implementer shall require that the Content
* Types stream contain one of the following for every part in the package:
* One matching Default element One matching Override element Both a
* matching Default element and a matching Override element, in which case
* the Override element takes precedence.
* </p>
* @param partName
* The part URI associated with the override content type to
* delete.
* @exception InvalidOperationException
* Throws if
*/
public void removeContentType(PackagePartName partName) throws InvalidOperationException {
if (partName == null)
throw new IllegalArgumentException("partName");
/* Override content type */
if (this.overrideContentType != null && (this.overrideContentType.get(partName) != null)) {
// Remove the override definition for the specified part.
this.overrideContentType.remove(partName);
return;
}
/* Default content type */
String extensionToDelete = partName.getExtension();
boolean deleteDefaultContentTypeFlag = true;
if (this.container != null) {
try {
for (PackagePart part : this.container.getParts()) {
if (!part.getPartName().equals(partName) && part.getPartName().getExtension().equalsIgnoreCase(extensionToDelete)) {
deleteDefaultContentTypeFlag = false;
break;
}
}
} catch (InvalidFormatException e) {
throw new InvalidOperationException(e.getMessage());
}
}
// Remove the default content type, no other part use this content type.
if (deleteDefaultContentTypeFlag) {
this.defaultContentType.remove(extensionToDelete);
}
/*
* Check rule 2.4: The package implementer shall require that the
* Content Types stream contain one of the following for every part in
* the package: One matching Default element One matching Override
* element Both a matching Default element and a matching Override
* element, in which case the Override element takes precedence.
*/
if (this.container != null) {
try {
for (PackagePart part : this.container.getParts()) {
if (!part.getPartName().equals(partName) && this.getContentType(part.getPartName()) == null)
throw new InvalidOperationException("Rule M2.4 is not respected: Nor a default element or override element is associated with the part: " + part.getPartName().getName());
}
} catch (InvalidFormatException e) {
throw new InvalidOperationException(e.getMessage());
}
}
}
use of org.apache.poi.openxml4j.exceptions.InvalidOperationException 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()]);
}
use of org.apache.poi.openxml4j.exceptions.InvalidOperationException in project poi by apache.
the class OPCPackage method save.
/**
* Save the document in the specified file.
*
* @param targetFile
* Destination file.
* @throws IOException
* Throws if an IO exception occur.
* @see #save(OutputStream)
*/
public void save(File targetFile) throws IOException {
if (targetFile == null) {
throw new IllegalArgumentException("targetFile");
}
this.throwExceptionIfReadOnly();
// You shouldn't save the the same file, do a close instead
if (targetFile.exists() && targetFile.getAbsolutePath().equals(this.originalPackagePath)) {
throw new InvalidOperationException("You can't call save(File) to save to the currently open " + "file. To save to the current file, please just call close()");
}
// Do the save
FileOutputStream fos = null;
try {
fos = new FileOutputStream(targetFile);
this.save(fos);
} finally {
if (fos != null) {
fos.close();
}
}
}
use of org.apache.poi.openxml4j.exceptions.InvalidOperationException in project poi by apache.
the class PackagePart method getOutputStream.
/**
* Get the output stream of this part. If the part is originally embedded in
* Zip package, it'll be transform into a <i>MemoryPackagePart</i> in
* order to write inside (the standard Java API doesn't allow to write in
* the file)
*
* @see org.apache.poi.openxml4j.opc.internal.MemoryPackagePart
*/
public OutputStream getOutputStream() {
OutputStream outStream;
// this part into a MemoryPackagePart instance for write purpose.
if (this instanceof ZipPackagePart) {
// Delete logically this part
_container.removePart(this._partName);
// Create a memory part
PackagePart part = _container.createPart(this._partName, this._contentType.toString(), false);
if (part == null) {
throw new InvalidOperationException("Can't create a temporary part !");
}
part._relationships = this._relationships;
outStream = part.getOutputStreamImpl();
} else {
outStream = this.getOutputStreamImpl();
}
return outStream;
}
Aggregations