Skip to content

Commit c89b4ad

Browse files
committed
move from File to Path object
1 parent c8fca89 commit c89b4ad

15 files changed

+48
-368
lines changed

java-src/io/github/erdos/stencil/API.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import io.github.erdos.stencil.functions.FunctionEvaluator;
55
import io.github.erdos.stencil.impl.NativeTemplateFactory;
66

7-
import java.io.File;
87
import java.io.IOException;
8+
import java.nio.file.Path;
99
import java.util.Collection;
1010
import java.util.Map;
1111

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

2626
/**
2727
* Prepares a document template file from the file system.
2828
*/
29-
public static PreparedTemplate prepare(File templateFile, PrepareOptions options) throws IOException {
30-
return new NativeTemplateFactory().prepareTemplateFile(templateFile, options);
29+
public static PreparedTemplate prepare(Path templateSource, PrepareOptions options) throws IOException {
30+
return new NativeTemplateFactory().prepareTemplateFile(templateSource, options);
3131
}
3232

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

47-
public static PreparedFragment fragment(File fragmentFile) throws IOException {
48-
return fragment(fragmentFile, PrepareOptions.options());
47+
public static PreparedFragment fragment(Path fragmentSource) throws IOException {
48+
return fragment(fragmentSource, PrepareOptions.options());
4949
}
5050

5151
public static EvaluatedDocument render(PreparedTemplate template, TemplateData data) {

java-src/io/github/erdos/stencil/PreparedTemplate.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

5-
import java.io.File;
5+
import java.nio.file.Path;
66
import java.time.LocalDateTime;
77
import java.util.Map;
88

@@ -19,7 +19,7 @@ public interface PreparedTemplate extends AutoCloseable {
1919
*
2020
* @return original template file
2121
*/
22-
File getTemplateFile();
22+
Path getTemplateFile();
2323

2424
/**
2525
* Format of template file. Tries to guess from file name by default.

java-src/io/github/erdos/stencil/TemplateFactory.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.github.erdos.stencil;
22

3-
import java.io.File;
43
import java.io.IOException;
4+
import java.nio.file.Path;
55

66
public interface TemplateFactory {
77

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

20-
default PreparedTemplate prepareTemplateFile(File inputTemplateFile) throws IOException {
20+
default PreparedTemplate prepareTemplateFile(Path inputTemplateFile) throws IOException {
2121
return prepareTemplateFile(inputTemplateFile, PrepareOptions.options());
2222
}
2323
}

java-src/io/github/erdos/stencil/impl/CachingTemplateFactory.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
import io.github.erdos.stencil.PreparedTemplate;
55
import io.github.erdos.stencil.TemplateFactory;
66

7-
import java.io.File;
87
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
910
import java.time.ZoneOffset;
1011
import java.util.Map;
1112
import java.util.concurrent.ConcurrentHashMap;
@@ -16,7 +17,7 @@
1617
@SuppressWarnings("unused")
1718
public final class CachingTemplateFactory implements TemplateFactory {
1819
private final TemplateFactory templateFactory;
19-
private final Map<File, PreparedTemplate> cache = new ConcurrentHashMap<>();
20+
private final Map<Path, PreparedTemplate> cache = new ConcurrentHashMap<>();
2021

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

3435
@Override
35-
public PreparedTemplate prepareTemplateFile(File templateFile, PrepareOptions options) throws IOException {
36+
public PreparedTemplate prepareTemplateFile(Path templateFile, PrepareOptions options) throws IOException {
3637
if (cache.containsKey(templateFile)) {
3738
PreparedTemplate stored = cache.get(templateFile);
38-
if (stored.creationDateTime().toEpochSecond(ZoneOffset.UTC) <= templateFile.lastModified()) {
39+
long fileLastModified = Files.getLastModifiedTime(templateFile).toMillis() / 1000;
40+
if (stored.creationDateTime().toEpochSecond(ZoneOffset.UTC) <= fileLastModified) {
3941
// TODO: this is so not thread safe.
4042
stored.close();
4143
stored = templateFactory.prepareTemplateFile(templateFile, options);

java-src/io/github/erdos/stencil/impl/DirWatcherTemplateFactory.java

-212
This file was deleted.

java-src/io/github/erdos/stencil/impl/NativeTemplateFactory.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import io.github.erdos.stencil.*;
44
import io.github.erdos.stencil.exceptions.ParsingException;
55

6-
import java.io.File;
6+
import java.nio.file.Path;
77
import java.util.Optional;
88

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

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

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

2222
if (options == null) {
@@ -32,17 +32,17 @@ public PreparedTemplate prepareTemplateFile(final File inputTemplateFile, Prepar
3232
}
3333
}
3434

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

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

4444
try {
45-
return (PreparedFragment) ClojureHelper.findFunction("prepare-fragment").invoke(fragmentFile, options);
45+
return (PreparedFragment) ClojureHelper.findFunction("prepare-fragment").invoke(fragmentSource, options);
4646
} catch (ParsingException e) {
4747
throw e;
4848
} catch (Exception e) {

java-src/io/github/erdos/stencil/standalone/StandaloneApplication.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void run() throws IOException {
7575
private void processJobs(Iterator<String> rest) throws IOException {
7676
while (rest.hasNext()) {
7777
final File templateFile = new File(rest.next()).getAbsoluteFile();
78-
final PreparedTemplate template = prepare(templateFile, prepareOptions);
78+
final PreparedTemplate template = prepare(templateFile.toPath(), prepareOptions);
7979

8080
while (rest.hasNext()) {
8181
final File dataFile = new File(rest.next()).getAbsoluteFile();

0 commit comments

Comments
 (0)