From 8774324b65c2ee912e34cfce4dcf095e918e981a Mon Sep 17 00:00:00 2001 From: Manuel Friedli Date: Fri, 2 Oct 2020 22:32:49 +0200 Subject: [PATCH] Add HTMLFileRenderer that writes the HTML string into a file. --- .../fritteli/labyrinth/HTMLFileRenderer.java | 47 +++++++++++++++++++ src/main/java/ch/fritteli/labyrinth/Main.java | 5 +- .../ch/fritteli/labyrinth/TextRenderer.java | 9 ++-- 3 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 src/main/java/ch/fritteli/labyrinth/HTMLFileRenderer.java diff --git a/src/main/java/ch/fritteli/labyrinth/HTMLFileRenderer.java b/src/main/java/ch/fritteli/labyrinth/HTMLFileRenderer.java new file mode 100644 index 0000000..ade1da5 --- /dev/null +++ b/src/main/java/ch/fritteli/labyrinth/HTMLFileRenderer.java @@ -0,0 +1,47 @@ +package ch.fritteli.labyrinth; + +import lombok.NonNull; +import lombok.Setter; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; + +public class HTMLFileRenderer implements Renderer { + @Setter + private Path targetFile; + + private HTMLFileRenderer() { + try { + this.targetFile = Files.createTempFile("labyrinth_", ".html"); + } catch (IOException e) { + System.err.println("Unable to set default target file."); + e.printStackTrace(); + } + } + + @NonNull + public static HTMLFileRenderer newInstance() { + return new HTMLFileRenderer(); + } + + public boolean isTargetFileDefinedAndWritable() { + return this.targetFile != null && this.targetFile.toFile().canWrite(); + } + + @Override + public Path render(@NonNull final Labyrinth labyrinth) { + if (!this.isTargetFileDefinedAndWritable()) { + throw new IllegalArgumentException("Cannot write to target file. See previous log messages for details."); + } + final String html = HTMLRenderer.newInstance().render(labyrinth); + try { + Files.writeString(this.targetFile, html, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE); + } catch (IOException e) { + System.err.println("Failed writing to file " + this.targetFile.normalize().toString()); + e.printStackTrace(); + } + return targetFile; + } +} diff --git a/src/main/java/ch/fritteli/labyrinth/Main.java b/src/main/java/ch/fritteli/labyrinth/Main.java index 0af509a..9e133d4 100644 --- a/src/main/java/ch/fritteli/labyrinth/Main.java +++ b/src/main/java/ch/fritteli/labyrinth/Main.java @@ -4,13 +4,14 @@ import lombok.NonNull; public class Main { public static void main(@NonNull final String[] args) { - int width = 10; - int height = 10; + int width = 100; + int height = 100; final Labyrinth labyrinth = new Labyrinth(width, height); final TextRenderer textRenderer = TextRenderer.newInstance(); System.out.println(textRenderer.render(labyrinth)); System.out.println(textRenderer.setRenderingSolution(true).render(labyrinth)); final HTMLRenderer htmlRenderer = HTMLRenderer.newInstance(); System.out.println(htmlRenderer.render(labyrinth)); + System.out.println(HTMLFileRenderer.newInstance().render(labyrinth)); } } diff --git a/src/main/java/ch/fritteli/labyrinth/TextRenderer.java b/src/main/java/ch/fritteli/labyrinth/TextRenderer.java index f1df456..bc28293 100644 --- a/src/main/java/ch/fritteli/labyrinth/TextRenderer.java +++ b/src/main/java/ch/fritteli/labyrinth/TextRenderer.java @@ -13,11 +13,11 @@ public class TextRenderer implements Renderer { private int height; private boolean renderSolution; // column counter - private int x = 0; + private int x; // row counter - private int y = 0; + private int y; // line counter (top-, center- or bottom line of a row) - private int line = 0; + private int line; private TextRenderer() { this.renderSolution = false; @@ -42,6 +42,9 @@ public class TextRenderer implements Renderer { if (this.width == 0 || this.height == 0) { return ""; } + this.x = 0; + this.y = 0; + this.line = 0; final StringBuilder sb = new StringBuilder(); while (this.hasNext()) { sb.append(this.next());