Skip to content

Commit

Permalink
move from File to Path object
Browse files Browse the repository at this point in the history
  • Loading branch information
erdos committed Aug 16, 2024
1 parent c8fca89 commit c89b4ad
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 368 deletions.
20 changes: 10 additions & 10 deletions java-src/io/github/erdos/stencil/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import io.github.erdos.stencil.functions.FunctionEvaluator;
import io.github.erdos.stencil.impl.NativeTemplateFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Map;

Expand All @@ -19,33 +19,33 @@ private API() {}
/**
* Prepares a document template file from the file system.
*/
public static PreparedTemplate prepare(File templateFile) throws IOException {
return prepare(templateFile, PrepareOptions.options());
public static PreparedTemplate prepare(Path templateSource) throws IOException {
return prepare(templateSource, PrepareOptions.options());
}

/**
* Prepares a document template file from the file system.
*/
public static PreparedTemplate prepare(File templateFile, PrepareOptions options) throws IOException {
return new NativeTemplateFactory().prepareTemplateFile(templateFile, options);
public static PreparedTemplate prepare(Path templateSource, PrepareOptions options) throws IOException {
return new NativeTemplateFactory().prepareTemplateFile(templateSource, options);
}

/**
* Prepares a document fragment from the file system. Fragments can be used to embed extra content when rendering
* document templates. For example, custom headers and footers can be reused across documents this way.
*
* @param fragmentFile template file from file system to be used as document fragment
* @param fragmentSource template file from file system to be used as document fragment
* @return fragment instance, not null
* @throws IllegalArgumentException when fragmentFile is null
* @throws IOException on file system error
* @throws java.io.FileNotFoundException when file is not found on file system
*/
public static PreparedFragment fragment(File fragmentFile, PrepareOptions options) throws IOException {
return new NativeTemplateFactory().prepareFragmentFile(fragmentFile, options);
public static PreparedFragment fragment(Path fragmentSource, PrepareOptions options) throws IOException {
return new NativeTemplateFactory().prepareFragmentFile(fragmentSource, options);
}

public static PreparedFragment fragment(File fragmentFile) throws IOException {
return fragment(fragmentFile, PrepareOptions.options());
public static PreparedFragment fragment(Path fragmentSource) throws IOException {
return fragment(fragmentSource, PrepareOptions.options());
}

public static EvaluatedDocument render(PreparedTemplate template, TemplateData data) {
Expand Down
4 changes: 2 additions & 2 deletions java-src/io/github/erdos/stencil/PreparedTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.github.erdos.stencil.functions.FunctionEvaluator;

import java.io.File;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.util.Map;

Expand All @@ -19,7 +19,7 @@ public interface PreparedTemplate extends AutoCloseable {
*
* @return original template file
*/
File getTemplateFile();
Path getTemplateFile();

/**
* Format of template file. Tries to guess from file name by default.
Expand Down
6 changes: 3 additions & 3 deletions java-src/io/github/erdos/stencil/TemplateFactory.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.erdos.stencil;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

public interface TemplateFactory {

Expand All @@ -15,9 +15,9 @@ public interface TemplateFactory {
* @throws IllegalArgumentException when argument is null, unknown type or does not exist
* @throws java.io.FileNotFoundException when file does not exist
*/
PreparedTemplate prepareTemplateFile(File inputTemplateFile, PrepareOptions options) throws IOException;
PreparedTemplate prepareTemplateFile(Path inputTemplateFile, PrepareOptions options) throws IOException;

default PreparedTemplate prepareTemplateFile(File inputTemplateFile) throws IOException {
default PreparedTemplate prepareTemplateFile(Path inputTemplateFile) throws IOException {
return prepareTemplateFile(inputTemplateFile, PrepareOptions.options());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import io.github.erdos.stencil.PreparedTemplate;
import io.github.erdos.stencil.TemplateFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.ZoneOffset;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -16,7 +17,7 @@
@SuppressWarnings("unused")
public final class CachingTemplateFactory implements TemplateFactory {
private final TemplateFactory templateFactory;
private final Map<File, PreparedTemplate> cache = new ConcurrentHashMap<>();
private final Map<Path, PreparedTemplate> cache = new ConcurrentHashMap<>();

/**
* Constructs a new wrapping instance. Caches in memory.
Expand All @@ -32,10 +33,11 @@ public CachingTemplateFactory(TemplateFactory templateFactory) {
}

@Override
public PreparedTemplate prepareTemplateFile(File templateFile, PrepareOptions options) throws IOException {
public PreparedTemplate prepareTemplateFile(Path templateFile, PrepareOptions options) throws IOException {
if (cache.containsKey(templateFile)) {
PreparedTemplate stored = cache.get(templateFile);
if (stored.creationDateTime().toEpochSecond(ZoneOffset.UTC) <= templateFile.lastModified()) {
long fileLastModified = Files.getLastModifiedTime(templateFile).toMillis() / 1000;
if (stored.creationDateTime().toEpochSecond(ZoneOffset.UTC) <= fileLastModified) {
// TODO: this is so not thread safe.
stored.close();
stored = templateFactory.prepareTemplateFile(templateFile, options);
Expand Down
212 changes: 0 additions & 212 deletions java-src/io/github/erdos/stencil/impl/DirWatcherTemplateFactory.java

This file was deleted.

16 changes: 8 additions & 8 deletions java-src/io/github/erdos/stencil/impl/NativeTemplateFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.github.erdos.stencil.*;
import io.github.erdos.stencil.exceptions.ParsingException;

import java.io.File;
import java.nio.file.Path;
import java.util.Optional;

import static io.github.erdos.stencil.TemplateDocumentFormats.ofExtension;
Expand All @@ -12,11 +12,11 @@
public final class NativeTemplateFactory implements TemplateFactory {

@Override
public PreparedTemplate prepareTemplateFile(final File inputTemplateFile, PrepareOptions options) {
final Optional<TemplateDocumentFormats> templateDocFormat = ofExtension(inputTemplateFile.getName());
public PreparedTemplate prepareTemplateFile(final Path inputTemplateFile, PrepareOptions options) {
final Optional<TemplateDocumentFormats> templateDocFormat = ofExtension(inputTemplateFile.toString());

if (!templateDocFormat.isPresent()) {
throw new IllegalArgumentException("Unexpected type of file: " + inputTemplateFile.getName());
throw new IllegalArgumentException("Unexpected type of file: " + inputTemplateFile);
}

if (options == null) {
Expand All @@ -32,17 +32,17 @@ public PreparedTemplate prepareTemplateFile(final File inputTemplateFile, Prepar
}
}

public PreparedFragment prepareFragmentFile(final File fragmentFile, PrepareOptions options) {
if (fragmentFile == null) {
throw new IllegalArgumentException("Fragment file parameter is null!");
public PreparedFragment prepareFragmentFile(final Path fragmentSource, PrepareOptions options) {
if (fragmentSource == null) {
throw new IllegalArgumentException("Fragment source parameter is null!");
}

if (options == null) {
throw new IllegalArgumentException("Template preparation options are missing!");
}

try {
return (PreparedFragment) ClojureHelper.findFunction("prepare-fragment").invoke(fragmentFile, options);
return (PreparedFragment) ClojureHelper.findFunction("prepare-fragment").invoke(fragmentSource, options);
} catch (ParsingException e) {
throw e;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void run() throws IOException {
private void processJobs(Iterator<String> rest) throws IOException {
while (rest.hasNext()) {
final File templateFile = new File(rest.next()).getAbsoluteFile();
final PreparedTemplate template = prepare(templateFile, prepareOptions);
final PreparedTemplate template = prepare(templateFile.toPath(), prepareOptions);

while (rest.hasNext()) {
final File dataFile = new File(rest.next()).getAbsoluteFile();
Expand Down
Loading

0 comments on commit c89b4ad

Please sign in to comment.