@Override protected InputStream getInputStreamImpl() { throw new InvalidOperationException("Operation not authorized. This part may only be manipulated using the getters and setters on PackagePropertiesPart"); }
@Override protected OutputStream getOutputStreamImpl() { throw new InvalidOperationException( "Can't use output stream to set properties !"); }
/** * Throws an exception if this package part is a relationship part. * * @throws InvalidOperationException * If this part is a relationship part. */ private void throwExceptionIfRelationship() throws InvalidOperationException { if (this._isRelationshipPart) throw new InvalidOperationException( "Can do this operation on a relationship part !"); }
@Override public boolean save(OutputStream zos) { throw new InvalidOperationException("Operation not authorized. This part may only be manipulated using the getters and setters on PackagePropertiesPart"); }
@Override public boolean load(InputStream ios) { throw new InvalidOperationException("Operation not authorized. This part may only be manipulated using the getters and setters on PackagePropertiesPart"); }
/** * Throws an exception if the package access mode is in read only mode * (PackageAccess.Read). * * @throws InvalidOperationException * Throws if a writing operation is done on a read only package. * @see org.apache.poi.openxml4j.opc.PackageAccess */ void throwExceptionIfReadOnly() throws InvalidOperationException { if (packageAccess == PackageAccess.READ) { throw new InvalidOperationException( "Operation not allowed, document open in read only mode!"); } }
/** * Throws an exception if the package access mode is in write only mode * (PackageAccess.Write). This method is call when other methods need write * right. * * @throws InvalidOperationException if a read operation is done on a write only package. * @see org.apache.poi.openxml4j.opc.PackageAccess */ void throwExceptionIfWriteOnly() throws InvalidOperationException { if (packageAccess == PackageAccess.WRITE) { throw new InvalidOperationException( "Operation not allowed, document open in write only mode!"); } }
@Override @NotImplemented public boolean load(InputStream ios) { throw new InvalidOperationException("Method not implemented !"); }
private static ZipEntrySource openZipEntrySourceStream(ZipArchiveThresholdInputStream zis) throws InvalidOperationException { // Acquire the final level resource. If this is acquired successfully, the zip package was read successfully from the input stream try { // open the zip entry source stream return new ZipInputStreamZipEntrySource(zis); } catch (IOException e) { throw new InvalidOperationException("Could not open the specified zip entry source stream", e); } }
@Override @NotImplemented public void close() { throw new InvalidOperationException("Method not implemented !"); }
@Override @NotImplemented public void flush() { throw new InvalidOperationException("Method not implemented !"); } }
/** * Validates the package compliance with the OPC specifications. * * @return <b>true</b> if the package is valid else <b>false</b> */ @NotImplemented public boolean validatePackage(OPCPackage pkg) throws InvalidFormatException { throw new InvalidOperationException("Not implemented yet !!!"); }
/** * Creates a new package. * * @param file * Path of the document. * @return A newly created PackageBase ready to use. */ public static OPCPackage create(File file) { if (file == null || (file.exists() && file.isDirectory())) { throw new IllegalArgumentException("file"); } if (file.exists()) { throw new InvalidOperationException( "This package (or file) already exists : use the open() method or delete the file."); } // Creates a new package OPCPackage pkg = new ZipPackage(); pkg.originalPackagePath = file.getAbsolutePath(); configurePackage(pkg); return pkg; }
/** * Check rule [M1.11]: a package implementer shall neither create nor * recognize a part with a part name derived from another part name by * appending segments to it. * * @param partName name of part * @param part part to put * @return the previous value associated with {@code partName}, or * {@code null} if there was no mapping for {@code partName}. * @exception InvalidOperationException * Throws if you try to add a part with a name derived from * another part name. */ public PackagePart put(final PackagePartName partName, final PackagePart part) { final String ppName = partName.getName(); final StringBuilder concatSeg = new StringBuilder(); // split at slash, but keep leading slash final String delim = "(?=["+PackagingURIHelper.FORWARD_SLASH_STRING+".])"; for (String seg : ppName.split(delim)) { concatSeg.append(seg); if (registerPartNameStr.contains(concatSeg.toString())) { throw new InvalidOperationException( "You can't add a part with a part name derived from another part ! [M1.11]"); } } registerPartNameStr.add(ppName); return packagePartLookup.put(ppName, part); }
private static ZipEntrySource openZipEntrySourceStream(File file) throws InvalidOperationException { final FileInputStream fis; // Acquire a resource that is needed to read the next level of openZipEntrySourceStream try { // open the file input stream fis = new FileInputStream(file); // NOSONAR } catch (final FileNotFoundException e) { // If the source cannot be acquired, abort (no resources to free at this level) throw new InvalidOperationException("Can't open the specified file input stream from file: '" + file + "'", e); } // If an error occurs while reading the next level of openZipEntrySourceStream, free the acquired resource try { // read from the file input stream return openZipEntrySourceStream(fis); } catch (final InvalidOperationException|UnsupportedFileFormatException e) { // abort: close the zip input stream IOUtils.closeQuietly(fis); throw e; } catch (final Exception e) { // abort: close the file input stream IOUtils.closeQuietly(fis); throw new InvalidOperationException("Failed to read the file input stream from file: '" + file + "'", e); } }
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()"
private static ZipEntrySource openZipEntrySourceStream(FileInputStream fis) throws InvalidOperationException { final ZipArchiveThresholdInputStream zis; // Acquire a resource that is needed to read the next level of openZipEntrySourceStream try { // open the zip input stream zis = ZipHelper.openZipStream(fis); // NOSONAR } catch (final IOException e) { // If the source cannot be acquired, abort (no resources to free at this level) throw new InvalidOperationException("Could not open the file input stream", e); } // If an error occurs while reading the next level of openZipEntrySourceStream, free the acquired resource try { // read from the zip input stream return openZipEntrySourceStream(zis); } catch (final InvalidOperationException|UnsupportedFileFormatException e) { // abort: close the zip input stream IOUtils.closeQuietly(zis); throw e; } catch (final Exception e) { // abort: close the zip input stream IOUtils.closeQuietly(zis); throw new InvalidOperationException("Failed to read the zip entry source stream", e); } }
throw new InvalidOperationException("Can't be a relationship part");
/** * Constructor. Opens a Zip based Open XML document from a File. * * @param file * The file to open or create. * @param access * The package access mode. * @throws InvalidOperationException If the zip file cannot be opened. */ ZipPackage(File file, PackageAccess access) throws InvalidOperationException { super(access); ZipEntrySource ze; try { final ZipFile zipFile = ZipHelper.openZipFile(file); // NOSONAR ze = new ZipFileZipEntrySource(zipFile); } catch (IOException e) { // probably not happening with write access - not sure how to handle the default read-write access ... if (access == PackageAccess.WRITE) { throw new InvalidOperationException("Can't open the specified file: '" + file + "'", e); } if ("java.util.zip.ZipException: archive is not a ZIP archive".equals(e.getMessage())) { throw new NotOfficeXmlFileException("archive is not a ZIP archive", e); } LOG.log(POILogger.ERROR, "Error in zip file "+file+" - falling back to stream processing (i.e. ignoring zip central directory)"); ze = openZipEntrySourceStream(file); } this.zipArchive = ze; }
/** * 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) * * @return output stream for this part * @see org.apache.poi.openxml4j.opc.internal.MemoryPackagePart */ public OutputStream getOutputStream() { OutputStream outStream; // If this part is a zip package part (read only by design) we convert // 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; }