Skip to content

Commit

Permalink
FileTemplateLoader using nio files api
Browse files Browse the repository at this point in the history
  • Loading branch information
chbloemer committed Oct 10, 2023
1 parent 927e1dc commit 3598074
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
53 changes: 24 additions & 29 deletions src/main/java/de/neuland/pug4j/template/FileTemplateLoader.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.neuland.pug4j.template;

import de.neuland.pug4j.exceptions.PugTemplateLoaderException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
Expand All @@ -10,12 +9,13 @@
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.*;
import java.util.concurrent.TimeUnit;

public class FileTemplateLoader implements TemplateLoader {

private Charset encoding = StandardCharsets.UTF_8;
private final String separator = FileSystems.getDefault().getSeparator();
private Charset encoding = StandardCharsets.UTF_8;
private String templateLoaderPath = "";
private String extension = "pug";
private String basePath="";
Expand All @@ -35,15 +35,14 @@ public FileTemplateLoader(Charset encoding, String extension) {

public FileTemplateLoader(String templateLoaderPath) {
templateLoaderPath = FilenameUtils.separatorsToSystem(templateLoaderPath);

if(!FileUtils.isDirectory(new File(templateLoaderPath))){
if(!Files.isDirectory(Paths.get(templateLoaderPath))){
throw new PugTemplateLoaderException("Directory '"+ templateLoaderPath +"' does not exist.");
}
if(templateLoaderPath.endsWith(File.separator))
this.templateLoaderPath = templateLoaderPath;
else
this.templateLoaderPath = templateLoaderPath+File.separator;

if(!templateLoaderPath.endsWith(separator)) {
templateLoaderPath += separator;
}
this.templateLoaderPath = templateLoaderPath;
}

public FileTemplateLoader(String templateLoaderPath, Charset encoding) {
Expand All @@ -61,22 +60,19 @@ public FileTemplateLoader(String templateLoaderPath, Charset encoding, String ex
this.encoding = encoding;
}

public long getLastModified(String name) {
name = FilenameUtils.separatorsToSystem(name);
File templateSource = getFile(name);
return templateSource.lastModified();
public long getLastModified(String name) throws IOException {
Path filepath = getFilepath(name);
return Files.getLastModifiedTime(filepath).to(TimeUnit.MILLISECONDS);
}

@Override
public Reader getReader(String name) throws IOException {
if(name == null){
throw new IllegalArgumentException("Filename not provided!");
}
name = FilenameUtils.separatorsToSystem(name);
name = ensurePugExtension(name);
File templateSource = getFile(name);
final Path path = templateSource.toPath();
final InputStream inputStream = Files.newInputStream(path);
Path filepath = getFilepath(name);
logger.debug("Template: "+name+" resolved filepath is " + filepath.toAbsolutePath());
final InputStream inputStream = Files.newInputStream(filepath);
return new InputStreamReader(inputStream, encoding);
}

Expand All @@ -86,22 +82,21 @@ private String ensurePugExtension(String templateName) {
}
return templateName;
}
private File getFile(String name) {
String filepath = getFilepath(name);
logger.debug("Template: "+name+" resolved filepath is " + filepath);
return new File(filepath);
}

private String getFilepath(String name){
private Path getFilepath(String name){
name = FilenameUtils.separatorsToSystem(name);
name = ensurePugExtension(name);
String filePath;
if(!StringUtils.isBlank(templateLoaderPath)) {
if (name.startsWith(File.separator)) {
return FilenameUtils.normalize(templateLoaderPath + basePath + name.substring(1));
if (name.startsWith(separator)) {
filePath = FilenameUtils.normalize(templateLoaderPath + basePath + name.substring(1));
} else {
return FilenameUtils.normalize(templateLoaderPath + name);
filePath = FilenameUtils.normalize(templateLoaderPath + name);
}
} else {
return name;
filePath = name;
}
return Paths.get(filePath);
}

public String getExtension() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public void shouldThrowExceptionIfNull() {
public void shouldGetAbsoluteFile() throws IOException {
FileTemplateLoader fileTemplateLoader = new FileTemplateLoader();
fileTemplateLoader.getReader(RESOURCE_PATH+"/pages/subdir/test.pug");
final long lastModified = fileTemplateLoader.getLastModified(RESOURCE_PATH + "/pages/subdir/test.pug");
assertEquals(1696925587000l,lastModified);
}
@Test
public void shouldGetAbsoluteFileWithBasePath() throws IOException {
Expand Down

0 comments on commit 3598074

Please sign in to comment.