Skip to content

Commit

Permalink
close #393 Add ability to remove metadata field
Browse files Browse the repository at this point in the history
  • Loading branch information
ediweissmann committed Feb 18, 2021
1 parent 6697121 commit 435cd69
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@
<jdepend.version>2.9.1</jdepend.version>
<hibernate-validator.version>4.2.0.Final</hibernate-validator.version>
<hamcrest.version>1.3</hamcrest.version>
<sambox.version>2.2.12</sambox.version>
<sambox.version>2.2.13</sambox.version>
<sejda.io.version>2.1.3</sejda.io.version>
<bouncycastle.version>1.66</bouncycastle.version>
<twelvemonkeys.version>3.4.2</twelvemonkeys.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
import org.sejda.sambox.util.DateConverter;

import java.io.IOException;
import java.util.Arrays;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

/**
* Test unit for the set metadata task
Expand Down Expand Up @@ -72,6 +74,22 @@ public void testExecuteEncrypted() throws IOException {
doExecute();
}

@Test
public void removingField() throws IOException {
SetMetadataParameters parameters = new SetMetadataParameters();
parameters.addFieldsToRemove(Arrays.asList("Creator", "Author", "RandomStringThatDoesNotExist"));
parameters.setSource(shortInput());

parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE);
testContext.pdfOutputTo(parameters);
execute(parameters);

PDDocument document = testContext.assertTaskCompleted();
PDDocumentInformation info = document.getDocumentInformation();
assertNull(info.getAuthor());
assertNull(info.getCreator());
}

private void doExecute() throws IOException {
testContext.pdfOutputTo(parameters);
execute(parameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.sejda.commons.collection.NullSafeSet;
import org.sejda.model.parameter.base.SinglePdfSourceSingleOutputParameters;
import org.sejda.model.validation.constraint.NotEmpty;
import org.sejda.model.validation.constraint.SingleOutputAllowedExtensions;

import java.util.HashMap;
import java.util.Map;
import java.util.*;

/**
* Parameter class for the set metadata manipulation.
Expand All @@ -40,6 +40,7 @@ public final class SetMetadataParameters extends SinglePdfSourceSingleOutputPara

@NotEmpty
private final Map<String, String> metadata = new HashMap<String, String>();
private final Set<String> toRemove = new NullSafeSet<>();

public Map<String, String> getMetadata() {
return metadata;
Expand All @@ -52,10 +53,22 @@ public void putAll(Map<String, String> m) {
public void put(String key, String metadata) {
this.metadata.put(key, metadata);
}

public void addFieldToRemove(String fieldName) {
toRemove.add(fieldName);
}

public void addFieldsToRemove(Collection<String> fieldNames) {
toRemove.addAll(fieldNames);
}

public Set<String> getToRemove() {
return toRemove;
}

@Override
public int hashCode() {
return new HashCodeBuilder().appendSuper(super.hashCode()).append(metadata).toHashCode();
return new HashCodeBuilder().appendSuper(super.hashCode()).append(metadata).append(toRemove).toHashCode();
}

@Override
Expand All @@ -68,6 +81,7 @@ public boolean equals(Object other) {
}
SetMetadataParameters parameter = (SetMetadataParameters) other;
return new EqualsBuilder().appendSuper(super.equals(other)).append(metadata, parameter.metadata)
.append(toRemove, parameter.toRemove)
.isEquals();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public void onBeforeWrite() throws IOException {
LOG.trace("'{}' -> '{}'", meta.getKey(), meta.getValue());
actualMeta.setCustomMetadataValue(meta.getKey(), meta.getValue());
}

for (String keyToRemove : parameters.getToRemove()) {
LOG.trace("Removing '{}'", keyToRemove);
actualMeta.removeMetadataField(keyToRemove);
}
}
});

Expand Down

0 comments on commit 435cd69

Please sign in to comment.