refactor code that removes parts

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1917154 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2024-04-19 14:12:13 +00:00
parent f335554254
commit ee525b6477
4 changed files with 18 additions and 22 deletions

View File

@ -47,7 +47,7 @@ public abstract class ReferenceRelationship {
protected ReferenceRelationship(POIXMLDocumentPart container, URI targetUri, boolean isExternal, String relationshipType, String id) { protected ReferenceRelationship(POIXMLDocumentPart container, URI targetUri, boolean isExternal, String relationshipType, String id) {
if (targetUri == null) { if (targetUri == null) {
throw new IllegalArgumentException("targetUri"); throw new NullPointerException("targetUri cannot be null");
} }
this.container = container; this.container = container;

View File

@ -1025,7 +1025,6 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
if (this.partList.containsKey(partName)) { if (this.partList.containsKey(partName)) {
this.partList.get(partName).setDeleted(true); this.partList.get(partName).setDeleted(true);
this.removePartImpl(partName); this.removePartImpl(partName);
this.partList.remove(partName);
} else { } else {
this.removePartImpl(partName); this.removePartImpl(partName);
} }
@ -1551,8 +1550,16 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
* *
* @param partName * @param partName
* The URI of the part to delete. * The URI of the part to delete.
* @throws IllegalArgumentException if the partName is null.
* @throws InvalidOperationException if the package is in read-only mode.
*/ */
protected abstract void removePartImpl(PackagePartName partName); protected void removePartImpl(PackagePartName partName) {
if (partName == null) {
throw new IllegalArgumentException("partName cannot be null");
}
throwExceptionIfReadOnly();
this.partList.remove(partName);
}
/** /**
* Flush the package but not save. * Flush the package but not save.

View File

@ -85,18 +85,20 @@ public final class PackageRelationship {
/** /**
* Constructor. * Constructor.
*
* @throws NullPointerException if inputs are null
*/ */
public PackageRelationship(OPCPackage pkg, PackagePart sourcePart, public PackageRelationship(OPCPackage pkg, PackagePart sourcePart,
URI targetUri, TargetMode targetMode, String relationshipType, URI targetUri, TargetMode targetMode, String relationshipType,
String id) { String id) {
if (pkg == null) if (pkg == null)
throw new IllegalArgumentException("pkg"); throw new NullPointerException("pkg cannot be null");
if (targetUri == null) if (targetUri == null)
throw new IllegalArgumentException("targetUri"); throw new NullPointerException("targetUri cannot be null");
if (relationshipType == null) if (relationshipType == null)
throw new IllegalArgumentException("relationshipType"); throw new NullPointerException("relationshipType cannot be null");
if (id == null) if (id == null)
throw new IllegalArgumentException("id"); throw new NullPointerException("id cannot be null");
this.container = pkg; this.container = pkg;
this.source = sourcePart; this.source = sourcePart;

View File

@ -401,11 +401,11 @@ public final class ZipPackage extends OPCPackage {
protected PackagePart createPartImpl(PackagePartName partName, protected PackagePart createPartImpl(PackagePartName partName,
String contentType, boolean loadRelationships) { String contentType, boolean loadRelationships) {
if (contentType == null) { if (contentType == null) {
throw new IllegalArgumentException("contentType"); throw new IllegalArgumentException("contentType cannot be null");
} }
if (partName == null) { if (partName == null) {
throw new IllegalArgumentException("partName"); throw new IllegalArgumentException("partName cannot be null");
} }
try { try {
@ -424,19 +424,6 @@ public final class ZipPackage extends OPCPackage {
} }
} }
/**
* Delete a part from the package
*
* @throws IllegalArgumentException
* Throws if the part URI is null or invalid.
*/
@Override
protected void removePartImpl(PackagePartName partName) {
if (partName == null) {
throw new IllegalArgumentException("partUri");
}
}
/** /**
* Flush the package. Do nothing. * Flush the package. Do nothing.
*/ */