Skip to content

Commit

Permalink
Added convenience methods to load from a Path or a File
Browse files Browse the repository at this point in the history
  • Loading branch information
brett-smith committed Feb 29, 2024
1 parent 79449ff commit aa6b395
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/java/com/sshtools/tinytemplate/Templates.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
package com.sshtools.tinytemplate;

import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -343,6 +346,19 @@ public final static Logger defaultStdOutLogger() {

public final static class TemplateModel implements Closeable {

public static TemplateModel ofPath(Path path) {
try(var rdr = Files.newBufferedReader(path)) {
return new TemplateModel(rdr);
}
catch(IOException ioe) {
throw new UncheckedIOException(ioe);
}
}

public static TemplateModel ofFile(File file) {
return ofPath(file.toPath());
}

public static TemplateModel ofContent(String content) {
return new TemplateModel(new StringReader(content));
}
Expand Down

0 comments on commit aa6b395

Please sign in to comment.