Add HTMLFileRenderer that writes the HTML string into a file.
This commit is contained in:
parent
487ed4604d
commit
8774324b65
3 changed files with 56 additions and 5 deletions
47
src/main/java/ch/fritteli/labyrinth/HTMLFileRenderer.java
Normal file
47
src/main/java/ch/fritteli/labyrinth/HTMLFileRenderer.java
Normal file
|
@ -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<Path> {
|
||||||
|
@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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,13 +4,14 @@ import lombok.NonNull;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(@NonNull final String[] args) {
|
public static void main(@NonNull final String[] args) {
|
||||||
int width = 10;
|
int width = 100;
|
||||||
int height = 10;
|
int height = 100;
|
||||||
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.render(labyrinth));
|
||||||
System.out.println(textRenderer.setRenderingSolution(true).render(labyrinth));
|
System.out.println(textRenderer.setRenderingSolution(true).render(labyrinth));
|
||||||
final HTMLRenderer htmlRenderer = HTMLRenderer.newInstance();
|
final HTMLRenderer htmlRenderer = HTMLRenderer.newInstance();
|
||||||
System.out.println(htmlRenderer.render(labyrinth));
|
System.out.println(htmlRenderer.render(labyrinth));
|
||||||
|
System.out.println(HTMLFileRenderer.newInstance().render(labyrinth));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,11 +13,11 @@ public class TextRenderer implements Renderer<String> {
|
||||||
private int height;
|
private int height;
|
||||||
private boolean renderSolution;
|
private boolean renderSolution;
|
||||||
// column counter
|
// column counter
|
||||||
private int x = 0;
|
private int x;
|
||||||
// row counter
|
// row counter
|
||||||
private int y = 0;
|
private int y;
|
||||||
// line counter (top-, center- or bottom line of a row)
|
// line counter (top-, center- or bottom line of a row)
|
||||||
private int line = 0;
|
private int line;
|
||||||
|
|
||||||
private TextRenderer() {
|
private TextRenderer() {
|
||||||
this.renderSolution = false;
|
this.renderSolution = false;
|
||||||
|
@ -42,6 +42,9 @@ public class TextRenderer implements Renderer<String> {
|
||||||
if (this.width == 0 || this.height == 0) {
|
if (this.width == 0 || this.height == 0) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
this.x = 0;
|
||||||
|
this.y = 0;
|
||||||
|
this.line = 0;
|
||||||
final StringBuilder sb = new StringBuilder();
|
final StringBuilder sb = new StringBuilder();
|
||||||
while (this.hasNext()) {
|
while (this.hasNext()) {
|
||||||
sb.append(this.next());
|
sb.append(this.next());
|
||||||
|
|
Loading…
Reference in a new issue