Refactor HTMLFileRenderer and add TextFileRenderer.
This commit is contained in:
parent
cf96949f8e
commit
e52e482b9a
3 changed files with 118 additions and 10 deletions
|
@ -1,17 +1,15 @@
|
||||||
package ch.fritteli.labyrinth;
|
package ch.fritteli.labyrinth;
|
||||||
|
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import lombok.Setter;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.StandardOpenOption;
|
|
||||||
|
|
||||||
public class HTMLFileRenderer implements Renderer<Path> {
|
public class HTMLFileRenderer implements Renderer<Path> {
|
||||||
@NonNull
|
@NonNull
|
||||||
private static final HTMLRenderer HTML_RENDERER = HTMLRenderer.newInstance();
|
private static final HTMLRenderer HTML_RENDERER = HTMLRenderer.newInstance();
|
||||||
@Setter
|
|
||||||
private Path targetFile;
|
private Path targetFile;
|
||||||
|
|
||||||
private HTMLFileRenderer() {
|
private HTMLFileRenderer() {
|
||||||
|
@ -32,14 +30,24 @@ public class HTMLFileRenderer implements Renderer<Path> {
|
||||||
return this.targetFile != null && this.targetFile.toFile().canWrite();
|
return this.targetFile != null && this.targetFile.toFile().canWrite();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public HTMLFileRenderer setTargetFile(@NonNull final Path targetFile) {
|
||||||
|
this.targetFile = targetFile;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Path render(@NonNull final Labyrinth labyrinth) {
|
public Path render(@NonNull final Labyrinth labyrinth) {
|
||||||
if (!this.isTargetFileDefinedAndWritable()) {
|
if (!this.isTargetFileDefinedAndWritable()) {
|
||||||
throw new IllegalArgumentException("Cannot write to target file. See previous log messages for details.");
|
try {
|
||||||
|
Files.createFile(this.targetFile);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IllegalArgumentException("Cannot write to target file.", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
final String html = HTML_RENDERER.render(labyrinth);
|
final String html = HTML_RENDERER.render(labyrinth);
|
||||||
try {
|
try {
|
||||||
Files.writeString(this.targetFile, html, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
|
Files.writeString(this.targetFile, html, StandardCharsets.UTF_8);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Failed writing to file " + this.targetFile.normalize().toString());
|
System.err.println("Failed writing to file " + this.targetFile.normalize().toString());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
|
@ -2,16 +2,31 @@ package ch.fritteli.labyrinth;
|
||||||
|
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
|
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(@NonNull final String[] args) {
|
public static void main(@NonNull final String[] args) {
|
||||||
int width = 100;
|
int width = 80;
|
||||||
int height = 100;
|
int height = 40;
|
||||||
final Labyrinth labyrinth = new Labyrinth(width, height);
|
final Labyrinth labyrinth = new Labyrinth(width, height);
|
||||||
final TextRenderer textRenderer = TextRenderer.newInstance();
|
final TextRenderer textRenderer = TextRenderer.newInstance();
|
||||||
System.out.println(textRenderer.render(labyrinth));
|
|
||||||
System.out.println(textRenderer.setRenderSolution(true).render(labyrinth));
|
|
||||||
final HTMLRenderer htmlRenderer = HTMLRenderer.newInstance();
|
final HTMLRenderer htmlRenderer = HTMLRenderer.newInstance();
|
||||||
|
final Path userHome = Paths.get(System.getProperty("user.home"));
|
||||||
|
final TextFileRenderer textFileRenderer = TextFileRenderer.newInstance()
|
||||||
|
.setTargetLabyrinthFile(userHome.resolve("labyrinth.txt"))
|
||||||
|
.setTargetSolutionFile(userHome.resolve("labyrinth-solution.txt"));
|
||||||
|
final HTMLFileRenderer htmlFileRenderer = HTMLFileRenderer.newInstance()
|
||||||
|
.setTargetFile(userHome.resolve("labyrinth.html"));
|
||||||
|
// Render Labyrinth to stdout
|
||||||
|
System.out.println(textRenderer.render(labyrinth));
|
||||||
|
// Render Labyrinth solution to stdout
|
||||||
|
System.out.println(textRenderer.setRenderSolution(true).render(labyrinth));
|
||||||
|
// Render HTML to stdout
|
||||||
System.out.println(htmlRenderer.render(labyrinth));
|
System.out.println(htmlRenderer.render(labyrinth));
|
||||||
System.out.println(HTMLFileRenderer.newInstance().render(labyrinth));
|
// Render Labyrinth and solution to (separate) files
|
||||||
|
System.out.println(textFileRenderer.render(labyrinth));
|
||||||
|
// Render HTML to file
|
||||||
|
System.out.println(htmlFileRenderer.render(labyrinth));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
85
src/main/java/ch/fritteli/labyrinth/TextFileRenderer.java
Normal file
85
src/main/java/ch/fritteli/labyrinth/TextFileRenderer.java
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
package ch.fritteli.labyrinth;
|
||||||
|
|
||||||
|
import io.vavr.collection.List;
|
||||||
|
import lombok.NonNull;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
public class TextFileRenderer implements Renderer<List<Path>> {
|
||||||
|
@NonNull
|
||||||
|
private static final TextRenderer TEXT_RENDERER = TextRenderer.newInstance();
|
||||||
|
private Path targetLabyrinthFile;
|
||||||
|
private Path targetSolutionFile;
|
||||||
|
|
||||||
|
private TextFileRenderer() {
|
||||||
|
try {
|
||||||
|
this.targetLabyrinthFile = Files.createTempFile("labyrinth_", ".txt");
|
||||||
|
this.targetSolutionFile = this.targetLabyrinthFile.getParent().resolve(
|
||||||
|
this.targetLabyrinthFile.getFileName().toString().replace(".txt", "-solution.txt")
|
||||||
|
);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Unable to set default target file.");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static TextFileRenderer newInstance() {
|
||||||
|
return new TextFileRenderer();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isTargetLabyrinthFileDefinedAndWritable() {
|
||||||
|
return this.targetLabyrinthFile != null && this.targetLabyrinthFile.toFile().canWrite();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isTargetSolutionFileDefinedAndWritable() {
|
||||||
|
return this.targetSolutionFile != null && this.targetSolutionFile.toFile().canWrite();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public TextFileRenderer setTargetLabyrinthFile(@NonNull final Path targetLabyrinthFile) {
|
||||||
|
this.targetLabyrinthFile = targetLabyrinthFile;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public TextFileRenderer setTargetSolutionFile(@NonNull final Path targetSolutionFile) {
|
||||||
|
this.targetSolutionFile = targetSolutionFile;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Path> render(@NonNull final Labyrinth labyrinth) {
|
||||||
|
if (!this.isTargetLabyrinthFileDefinedAndWritable()) {
|
||||||
|
try {
|
||||||
|
Files.createFile(this.targetLabyrinthFile);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IllegalArgumentException("Cannot write to target labyrinth file.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!this.isTargetSolutionFileDefinedAndWritable()) {
|
||||||
|
try {
|
||||||
|
Files.createFile(this.targetSolutionFile);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IllegalArgumentException("Cannot write to target solution file.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
final String text;
|
||||||
|
final String solution;
|
||||||
|
synchronized (TEXT_RENDERER) {
|
||||||
|
text = TEXT_RENDERER.setRenderSolution(false).render(labyrinth).strip();
|
||||||
|
solution = TEXT_RENDERER.setRenderSolution(true).render(labyrinth).strip();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Files.write(this.targetLabyrinthFile, text.getBytes(StandardCharsets.UTF_8));
|
||||||
|
Files.write(this.targetSolutionFile, solution.getBytes(StandardCharsets.UTF_8));
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Failed writing to file " + this.targetLabyrinthFile.normalize().toString());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return List.of(this.targetLabyrinthFile, this.targetSolutionFile);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue