Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move from File to Path types #168

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
15 changes: 12 additions & 3 deletions java-src/io/github/erdos/stencil/EvaluatedDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import io.github.erdos.stencil.impl.InputStreamExceptionPropagation;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.ExecutorService;

/**
Expand All @@ -17,10 +19,17 @@ public interface EvaluatedDocument {
* Writes output of this document to a file
*/
default void writeToFile(File output) throws IOException {
if (output.exists()) {
throw new IllegalArgumentException("Output file already exists: " + output);
writeToPath(output.toPath());
}

/**
* Writes output of this document to a path
*/
default void writeToPath(Path output) throws IOException {
if (Files.exists(output)) {
throw new IllegalArgumentException("Output path already exists: " + output);
}
try (FileOutputStream fos = new FileOutputStream(output)) {
try (OutputStream fos = Files.newOutputStream(output)) {
write(fos);
}
}
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.

Loading
Loading