Skip to content

Commit

Permalink
close #614: Added 'last' as keyword for the page selection in Extract…
Browse files Browse the repository at this point in the history
…Pages so the user can select the last page of multiple documents
  • Loading branch information
torakiki committed Aug 7, 2024
1 parent b6d9f79 commit 9b581ab
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,24 @@
*/
package org.pdfsam.core.support.params;

import org.apache.commons.lang3.StringUtils;
import org.sejda.commons.collection.NullSafeSet;
import org.sejda.conversion.exception.ConversionException;
import org.sejda.model.pdf.page.PageRange;
import org.sejda.model.pdf.page.PagesSelection;

import java.util.Arrays;
import java.util.Collections;
import java.util.Set;

import static java.util.stream.Collectors.toSet;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.split;
import static org.pdfsam.i18n.I18nContext.i18n;
import static org.sejda.conversion.AdapterUtils.splitAndTrim;

/**
* @author Andrea Vacondio
*
*/
public final class ConversionUtils {

Expand All @@ -44,14 +48,22 @@ private ConversionUtils() {
*/
public static Set<PageRange> toPageRangeSet(String selection) throws ConversionException {
if (isNotBlank(selection)) {
Set<PageRange> pageRangeSet = new NullSafeSet<>();
String[] tokens = splitAndTrim(selection, ",");
for (String current : tokens) {
PageRange range = toPageRange(current);
if (range.getEnd() < range.getStart()) {
throw new ConversionException(i18n().tr("Invalid range: {0}.", range.toString()));
}
pageRangeSet.add(range);
var pageRangeSet = new NullSafeSet<PageRange>();
var tokens = Arrays.stream(split(selection, ",")).map(StringUtils::strip).collect(toSet());
for (var token : tokens) {
pageRangeSet.add(toPageRange(token));
}
return pageRangeSet;
}
return Collections.emptySet();
}

public static Set<PagesSelection> toPagesSelectionSet(String selection) throws ConversionException {
if (isNotBlank(selection)) {
var pageRangeSet = new NullSafeSet<PagesSelection>();
var tokens = Arrays.stream(split(selection, ",")).map(StringUtils::strip).collect(toSet());
for (var token : tokens) {
pageRangeSet.add(toPageSelection(token));
}
return pageRangeSet;
}
Expand All @@ -73,9 +85,13 @@ private static PageRange toPageRange(String value) throws ConversionException {
if (value.startsWith("-")) {
return new PageRange(1, limitNumber);
}
return new PageRange(limitNumber, limitNumber);
return PageRange.one(limitNumber);
}
var range = new PageRange(parsePageNumber(limits[0]), parsePageNumber(limits[1]));
if (range.getEnd() < range.getStart()) {
throw new ConversionException(i18n().tr("Invalid range: {0}.", range.toString()));
}
return new PageRange(parsePageNumber(limits[0]), parsePageNumber(limits[1]));
return range;
}

private static int parsePageNumber(String value) throws ConversionException {
Expand All @@ -85,4 +101,11 @@ private static int parsePageNumber(String value) throws ConversionException {
throw new ConversionException(i18n().tr("Invalid number: {0}.", value));
}
}

private static PagesSelection toPageSelection(String value) {
if ("last".equals(value)) {
return PagesSelection.LAST_PAGE;
}
return toPageRange(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import org.pdfsam.core.support.params.ConversionUtils;
import org.pdfsam.core.support.params.TaskParametersBuildStep;
import org.pdfsam.model.ui.ResettableView;
import org.pdfsam.model.ui.workspace.RestorableView;
Expand All @@ -38,7 +37,7 @@

import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.apache.commons.lang3.StringUtils.defaultString;
import static org.pdfsam.core.support.params.ConversionUtils.toPageRangeSet;
import static org.pdfsam.core.support.params.ConversionUtils.toPagesSelectionSet;
import static org.pdfsam.i18n.I18nContext.i18n;
import static org.pdfsam.ui.components.support.Views.helpIcon;

Expand All @@ -62,17 +61,17 @@ class ExtractOptionsPane extends GridPane

this.field.setOnEnterValidation(true);
this.field.setEnableInvalidStyle(true);
this.field.setPromptText(i18n().tr("Pages to extract (ex: 2 or 5-23 or 2,5-7,12-)"));
this.field.setPromptText(i18n().tr("Pages to extract (ex: 2 or 5-23 or 2,5-7,12- or 3,last)"));
this.field.setValidator(v -> {
try {
return !toPageRangeSet(this.field.getText()).isEmpty();
return !toPagesSelectionSet(this.field.getText()).isEmpty();
} catch (ConversionException e) {
return false;
}
});
this.field.setErrorMessage(i18n().tr("Invalid page ranges"));
this.field.setId("extractRanges");
this.field.setPrefWidth(350);
this.field.setPrefWidth(400);
getStyleClass().addAll(Style.CONTAINER.css());
getStyleClass().addAll(Style.GRID.css());
var label = new Label(i18n().tr("Extract pages:"));
Expand All @@ -83,7 +82,7 @@ class ExtractOptionsPane extends GridPane
GridPane.setHalignment(field, HPos.LEFT);
add(field, 1, 0);
var helpIcon = helpIcon(
i18n().tr("Comma separated page numbers or ranges to extract (ex: 2 or 5-23 or 2,5-7,12-)"));
i18n().tr("Comma separated page numbers or ranges to extract (ex: 2 or 5-23 or 2,5-7,12- or 3,last)"));
GridPane.setValignment(helpIcon, VPos.CENTER);
add(helpIcon, 2, 0);
GridPane.setValignment(separateFile, VPos.BOTTOM);
Expand All @@ -96,7 +95,7 @@ public void apply(ExtractParametersBuilder builder, Consumer<String> onError) {
this.field.validate();
if (this.field.getValidationState() == ValidationState.VALID) {
try {
builder.ranges(ConversionUtils.toPageRangeSet(this.field.getText()));
builder.pagesSelection(toPagesSelectionSet(this.field.getText()));
builder.separateForEachRange(separateFile.isSelected());
} catch (ConversionException e) {
onError.accept(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.pdfsam.core.support.params.MultiplePdfSourceMultipleOutputParametersBuilder;
import org.sejda.model.optimization.OptimizationPolicy;
import org.sejda.model.parameter.ExtractPagesParameters;
import org.sejda.model.pdf.page.PageRange;
import org.sejda.model.pdf.page.PagesSelection;

import java.util.Set;

Expand All @@ -36,7 +36,7 @@
class ExtractParametersBuilder extends MultiplePdfSourceMultipleOutputParametersBuilder<ExtractPagesParameters> {

private OptimizationPolicy optimizationPolicy = OptimizationPolicy.AUTO;
private Set<PageRange> ranges;
private Set<PagesSelection> pagesSelection;
private boolean invertSelection = false;
private boolean separateForEachRange = false;

Expand All @@ -51,8 +51,8 @@ public OptimizationPolicy getOptimizationPolicy() {
return OptimizationPolicy.NO;
}

public void ranges(Set<PageRange> ranges) {
this.ranges = ranges;
public void pagesSelection(Set<PagesSelection> pagesSelection) {
this.pagesSelection = pagesSelection;
}

public void invertSelection(boolean invertSelection) {
Expand All @@ -72,7 +72,7 @@ public ExtractPagesParameters build() {
params.setOutput(getOutput());
params.setOptimizationPolicy(getOptimizationPolicy());
params.discardOutline(isDiscardBookmarks());
params.addAllPageRanges(ranges);
pagesSelection.forEach(params::addPageRange);
params.setOutputPrefix(getPrefix());
params.setInvertSelection(invertSelection);
params.setSeparateFileForEachRange(separateForEachRange);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void start(Stage stage) {
public void validSteps() {
robot.clickOn("#extractRanges").type(KeyCode.DIGIT5).push(KeyCode.ENTER);
victim.apply(builder, onError);
verify(builder).ranges(anySet());
verify(builder).pagesSelection(anySet());
verify(onError, never()).accept(anyString());
}

Expand All @@ -85,7 +85,7 @@ public void applyError() {
robot.clickOn("#extractRanges").write("Chuck").push(KeyCode.ENTER);
victim.apply(builder, onError);
verify(onError).accept(anyString());
verify(builder, never()).ranges(anySet());
verify(builder, never()).pagesSelection(anySet());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.sejda.model.output.FileOrDirectoryTaskOutput;
import org.sejda.model.parameter.ExtractPagesParameters;
import org.sejda.model.pdf.PdfVersion;
import org.sejda.model.pdf.page.PageRange;
import org.sejda.model.pdf.page.PagesSelection;

import java.io.IOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -59,15 +59,15 @@ public void build(@TempDir Path folder) throws IOException {
victim.addSource(source);
victim.addSource(PdfFileSource.newInstanceNoPassword(Files.createTempFile(folder, null, ".pdf").toFile()));
victim.version(PdfVersion.VERSION_1_7);
Set<PageRange> ranges = ConversionUtils.toPageRangeSet("2,5-20,33");
victim.ranges(ranges);
Set<PagesSelection> ranges = ConversionUtils.toPagesSelectionSet("2,5-20,33,45,last");
victim.pagesSelection(ranges);
ExtractPagesParameters params = victim.build();
assertTrue(params.isCompress());
assertTrue(params.discardOutline());
assertEquals(ExistingOutputPolicy.OVERWRITE, params.getExistingOutputPolicy());
assertEquals(PdfVersion.VERSION_1_7, params.getVersion());
assertEquals(OptimizationPolicy.AUTO, params.getOptimizationPolicy());
assertEquals(ranges, params.getPageSelection());
assertTrue(params.hasPageSelection());
assertEquals(output, params.getOutput());
assertTrue(params.isInvertSelection());
assertTrue(params.isSeparateFileForEachRange());
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@
<mockito.version>5.11.0</mockito.version>
<tinylog.version>2.7.0</tinylog.version>
<hibernate-validator.version>7.0.5.Final</hibernate-validator.version>
<sejda.version>5.0.20</sejda.version>
<sejda.version>5.1.0</sejda.version>
<sejda-io.version>4.0.0.M4</sejda-io.version>
<sejda.commons.version>3.0.0</sejda.commons.version>
<eventstudio.version>4.0.1</eventstudio.version>
Expand Down

0 comments on commit 9b581ab

Please sign in to comment.