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 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)); | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -13,11 +13,11 @@ public class TextRenderer implements Renderer<String> { | |||
|     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<String> { | |||
|         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()); | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue