Skip to content

Commit

Permalink
use java.nio.files , change testing to jimfs
Browse files Browse the repository at this point in the history
  • Loading branch information
pomali committed Jul 6, 2023
1 parent 3ad25a7 commit f50db47
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 192 deletions.
47 changes: 32 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -132,6 +132,12 @@
<version>${xmlunit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
<version>1.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -213,7 +219,7 @@

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.16.0</version>
</plugin>

Expand Down Expand Up @@ -265,8 +271,10 @@
<goal>download-single</goal>
</goals>
<configuration>
<url>https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdfjs.version}/build/pdf.min.js</url>
<toDir>${project.resources[0].directory}/digital/slovensko/autogram/ui/gui/vendor/pdfjs</toDir>
<url>
https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdfjs.version}/build/pdf.min.js</url>
<toDir>
${project.resources[0].directory}/digital/slovensko/autogram/ui/gui/vendor/pdfjs</toDir>
<skipIfExists>true</skipIfExists>
</configuration>
</execution>
Expand All @@ -277,8 +285,10 @@
<goal>download-single</goal>
</goals>
<configuration>
<url>https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.js</url>
<toDir>${project.resources[0].directory}/digital/slovensko/autogram/ui/gui/vendor/pdfjs</toDir>
<url>
https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.js</url>
<toDir>
${project.resources[0].directory}/digital/slovensko/autogram/ui/gui/vendor/pdfjs</toDir>
<skipIfExists>true</skipIfExists>
</configuration>
</execution>
Expand All @@ -291,7 +301,8 @@
<configuration>
<url>https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdfjs.version}</url>
<fromDir>cmaps</fromDir>
<toDir>${project.resources[0].directory}/digital/slovensko/autogram/ui/gui/vendor/pdfjs/cmaps</toDir>
<toDir>
${project.resources[0].directory}/digital/slovensko/autogram/ui/gui/vendor/pdfjs/cmaps</toDir>
<!-- <skip>true</skip> -->
</configuration>
</execution>
Expand Down Expand Up @@ -327,7 +338,8 @@
<goal>jdeps</goal>
</goals>
<configuration>
<!-- The output of jdeps is currently not used anywhere, modules are defined manually -->
<!-- The output of jdeps is currently not used anywhere, modules are
defined manually -->
<skip>true</skip>
<output>${project.build.directory}${file.separator}jdeps.out</output>
<options>
Expand All @@ -338,7 +350,8 @@
<option>${java.version}</option>
<option>-f</option>
<option>.*jar</option>
<option>${project.build.directory}${file.separator}${project.build.finalName}.jar</option>
<option>
${project.build.directory}${file.separator}${project.build.finalName}.jar</option>
</options>
</configuration>
</execution>
Expand Down Expand Up @@ -369,7 +382,8 @@
<module>javafx.fxml</module>
<module>javafx.graphics</module>
<module>javafx.web</module>
<!-- Needed for sun.misc.Unsafe used by JavaFX, shouldn't be used in our code -->
<!-- Needed for sun.misc.Unsafe used by JavaFX, shouldn't be used in
our code -->
<module>jdk.unsupported</module>
<module>jdk.httpserver</module>
</addModules>
Expand Down Expand Up @@ -415,13 +429,16 @@
<version>3.1.0</version>
<configuration>
<executable>bash</executable>
<workingDirectory>${project.build.scriptSourceDirectory}${file.separator}resources</workingDirectory>
<workingDirectory>
${project.build.scriptSourceDirectory}${file.separator}resources</workingDirectory>
<arguments>
<argument>${project.build.scriptSourceDirectory}${file.separator}package.sh</argument>
<argument>${jlink.jdk.path}${file.separator}bin${file.separator}jpackage</argument>
<argument>${project.build.directory}${file.separator}${project.artifactId}-${project.version}-${platform}</argument>
<argument>
${project.build.directory}${file.separator}${project.artifactId}-${project.version}-${platform}</argument>
<argument>${project.build.directory}${file.separator}preparedJDK</argument>
<argument>${project.basedir}${file.separator}src${file.separator}main${file.separator}resources${file.separator}digital${file.separator}slovensko${file.separator}autogram</argument>
<argument>
${project.basedir}${file.separator}src${file.separator}main${file.separator}resources${file.separator}digital${file.separator}slovensko${file.separator}autogram</argument>
<argument>${platform}</argument>
<argument>${project.version}</argument>
<argument>${project.build.directory}</argument>
Expand Down Expand Up @@ -498,4 +515,4 @@
</properties>
</profile>
</profiles>
</project>
</project>
99 changes: 58 additions & 41 deletions src/main/java/digital/slovensko/autogram/core/TargetPath.java
Original file line number Diff line number Diff line change
@@ -1,96 +1,108 @@
package digital.slovensko.autogram.core;

import java.io.File;
import java.nio.file.Paths;

import com.google.common.io.Files;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

import digital.slovensko.autogram.core.errors.SourceAndTargetTypeMismatchException;
import digital.slovensko.autogram.core.errors.TargetAlreadyExistsException;
import digital.slovensko.autogram.core.errors.TargetDirectoryDoesNotExistException;
import digital.slovensko.autogram.core.errors.UnableToCreateDirectoryException;

public class TargetPath {
private final File targetDirectory;
private final Path targetDirectory;
private final String targetName;
private final File sourceFile;
private final Path sourceFile;
private final boolean isForce;
private final boolean isGenerated;
private final boolean isForMultipleFiles;
private final boolean isParents;

public TargetPath(String target, File source, boolean force, boolean parents) {
private final FileSystem fs;

public TargetPath(String target, Path source, boolean force, boolean parents, FileSystem fileSystem) {
fs = fileSystem;
sourceFile = source;
isForce = force;
isParents = parents;

isGenerated = target == null;
isForMultipleFiles = source.isDirectory();
isForMultipleFiles = Files.isDirectory(source);
if (isGenerated) {
if (isForMultipleFiles) {

targetDirectory = new File(generateUniqueName(source.getParent(), source.getName() + "_signed", ""));
targetDirectory = fs.getPath(
generateUniqueName(source.getParent().toString(), source.getFileName().toString() + "_signed",
""));
targetName = null;

} else {
targetDirectory = source.getParentFile();
targetDirectory = source.getParent();
targetName = null;
}

} else {
var targetFile = new File(target);
var targetFile = fs.getPath(target);
if (!hasSourceAndTargetMatchingType(sourceFile, targetFile))
throw new SourceAndTargetTypeMismatchException();

if (targetFile.exists() && !isForce)
if (Files.exists(targetFile) && !isForce)
throw new TargetAlreadyExistsException();

if (isForMultipleFiles) {
targetDirectory = targetFile;
targetName = null;

} else {
targetDirectory = targetFile.getParentFile();
targetName = targetFile.getName();
targetDirectory = targetFile.getParent();
targetName = targetFile.getFileName().toString();
}
}
}

public static TargetPath fromParams(CliParameters params) {
return new TargetPath(params.getTarget(), params.getSource(), params.isForce(), params.shouldMakeParentDirectories());
return new TargetPath(params.getTarget(), params.getSource().toPath(), params.isForce(),
params.shouldMakeParentDirectories(), FileSystems.getDefault());
}

public static TargetPath fromSource(File source) {
return new TargetPath(null, source, false, false);
public static TargetPath fromSource(Path source) {
return new TargetPath(null, source, false, false, FileSystems.getDefault());
}

/**
* Create directory when we want to fill it out
*/
public void mkdirIfDir() {
if (targetDirectory == null || targetDirectory.exists())
if (targetDirectory == null || Files.exists(targetDirectory))
return;

if (isParents) {
if (!targetDirectory.mkdirs())
try {
Files.createDirectories(targetDirectory);
} catch (Exception e) {
throw new UnableToCreateDirectoryException();
}

return;
}

if (!isForMultipleFiles)
throw new TargetDirectoryDoesNotExistException();

if (!targetDirectory.mkdir())
try {
Files.createDirectory(targetDirectory);
} catch (Exception e) {
throw new UnableToCreateDirectoryException();
}
}

private static boolean hasSourceAndTargetMatchingType(File source, File target) {
if (!target.exists())
private static boolean hasSourceAndTargetMatchingType(Path source, Path target) {
if (!Files.exists(target))
return true;

var bothAreFiles = target.isFile() && source.isFile();
var bothAreDirectories = target.isDirectory() && source.isDirectory();
var bothAreFiles = Files.isRegularFile(target) && Files.isRegularFile(source);
var bothAreDirectories = Files.isDirectory(target) && Files.isDirectory(source);

return (bothAreDirectories || bothAreFiles);
}
Expand All @@ -100,31 +112,33 @@ private static boolean hasSourceAndTargetMatchingType(File source, File target)
*
*/

public File getSaveFilePath(File singleSourceFile) {
public Path getSaveFilePath(Path singleSourceFile) {
var file = _getSaveFilePath(singleSourceFile);

if (file.exists() && !isForce)
if (Files.exists(file) && !isForce)
throw new TargetAlreadyExistsException();

return file;
}

private File _getSaveFilePath(File singleSourceFile) {
private Path _getSaveFilePath(Path singleSourceFile) {
var targetName = this.targetName == null ? generateTargetName(singleSourceFile) : this.targetName;
var targetDirectoryPath = targetDirectory == null ? "" : targetDirectory.getPath();
File targetSingleFile = Paths.get(targetDirectoryPath, targetName).toFile();
var targetDirectoryPath = targetDirectory == null ? "" : targetDirectory.toString();
Path targetSingleFile = fs.getPath(targetDirectoryPath, targetName);

if (!targetSingleFile.exists())
if (!Files.exists(targetSingleFile))
return targetSingleFile;

if (isForce)
return targetSingleFile;

if (isGenerated) {
var parent = targetSingleFile.getParent();
var baseName = Files.getNameWithoutExtension(targetSingleFile.getName());
var extension = "." + Files.getFileExtension(targetSingleFile.getName());
return new File(generateUniqueName(parent, baseName, extension));
var baseName = com.google.common.io.Files
.getNameWithoutExtension(targetSingleFile.getFileName().toString());
var extension = "."
+ com.google.common.io.Files.getFileExtension(targetSingleFile.getFileName().toString());
return fs.getPath(generateUniqueName(parent.toString(), baseName, extension));
}

throw new TargetAlreadyExistsException();
Expand All @@ -136,8 +150,8 @@ private String generateUniqueName(String parent, String baseName, String extensi
parent = parent == null ? "" : parent;
while (true) {
var newTargetFile = _generateUniqueNameGetNewTargetFile(parent, newBaseName, extension);
if (!newTargetFile.exists())
return newTargetFile.getPath();
if (!Files.exists(newTargetFile))
return newTargetFile.toString();

if (count > 1000)
throw new TargetAlreadyExistsException();
Expand All @@ -147,16 +161,19 @@ private String generateUniqueName(String parent, String baseName, String extensi
}
}

private File _generateUniqueNameGetNewTargetFile(String parent, String baseName, String extension) {
return Paths.get(parent, baseName + extension).toFile();
private Path _generateUniqueNameGetNewTargetFile(String parent, String baseName, String extension) {
return fs.getPath(parent, baseName + extension);
}

private String generateTargetName(File singleSourceFile) {
var extension = singleSourceFile.getName().endsWith(".pdf") ? ".pdf" : ".asice";
private String generateTargetName(Path singleSourceFile) {
var extension = singleSourceFile.getFileName().toString().endsWith(".pdf") ? ".pdf" : ".asice";
if (isGenerated || isForMultipleFiles)
return Files.getNameWithoutExtension(singleSourceFile.getName()) + "_signed" + extension;
return com.google.common.io.Files.getNameWithoutExtension(singleSourceFile.getFileName().toString())
+ "_signed"
+ extension;

else
return Files.getNameWithoutExtension(targetDirectory.getName()) + extension;
return com.google.common.io.Files.getNameWithoutExtension(targetDirectory.getFileName().toString())
+ extension;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class SaveFileResponder extends Responder {
private final TargetPath targetPathBuilder;

public SaveFileResponder(File file, Autogram autogram) {
this(file, autogram, TargetPath.fromSource(file));
this(file, autogram, TargetPath.fromSource(file.toPath()));
}

public SaveFileResponder(File file, Autogram autogram, TargetPath targetPathBuilder) {
Expand All @@ -26,9 +26,9 @@ public SaveFileResponder(File file, Autogram autogram, TargetPath targetPathBuil

public void onDocumentSigned(SignedDocument signedDocument) {
try {
var targetFile = targetPathBuilder.getSaveFilePath(file);
signedDocument.getDocument().save(targetFile.getPath());
autogram.onDocumentSaved(targetFile);
var targetFile = targetPathBuilder.getSaveFilePath(file.toPath());
signedDocument.getDocument().save(targetFile.toString());
autogram.onDocumentSaved(targetFile.toFile());
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Loading

0 comments on commit f50db47

Please sign in to comment.