| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| GenerateFile |
|
| 1.625;1.625 |
| 1 | package no.sesat.mojo.modes; | |
| 2 | ||
| 3 | import java.io.File; | |
| 4 | import java.io.FileNotFoundException; | |
| 5 | import java.io.PrintStream; | |
| 6 | ||
| 7 | /** | |
| 8 | * This class provide methods that makes it easy to write indented text to a | |
| 9 | * file. | |
| 10 | * | |
| 11 | */ | |
| 12 | 0 | public abstract class GenerateFile { |
| 13 | ||
| 14 | private PrintStream stream; | |
| 15 | 0 | private int depth = 0; |
| 16 | 0 | private boolean indent = true; |
| 17 | private File file; | |
| 18 | ||
| 19 | /** | |
| 20 | * Initialize this generator. It will open the file specified. | |
| 21 | * | |
| 22 | * @param name | |
| 23 | * Filename | |
| 24 | */ | |
| 25 | protected void init(final String name) { | |
| 26 | 0 | file = new File(name); |
| 27 | ||
| 28 | try { | |
| 29 | 0 | stream = new PrintStream(file); |
| 30 | 0 | } catch (FileNotFoundException e) { |
| 31 | 0 | e.printStackTrace(); |
| 32 | 0 | } |
| 33 | 0 | } |
| 34 | ||
| 35 | /** | |
| 36 | * Indent one level. | |
| 37 | */ | |
| 38 | protected void indent() { | |
| 39 | 0 | depth++; |
| 40 | 0 | } |
| 41 | ||
| 42 | /** | |
| 43 | * Unindent one level. | |
| 44 | */ | |
| 45 | protected void unindent() { | |
| 46 | 0 | depth--; |
| 47 | 0 | if (depth < 0) { |
| 48 | 0 | throw new RuntimeException("Indenting below zero"); |
| 49 | } | |
| 50 | 0 | } |
| 51 | ||
| 52 | /** | |
| 53 | * Print text to file with a newline appended and increase indention level | |
| 54 | * by one. | |
| 55 | * | |
| 56 | * @param string | |
| 57 | * String that will be printed | |
| 58 | */ | |
| 59 | protected void printlnI(final String string) { | |
| 60 | 0 | println(string); |
| 61 | 0 | depth++; |
| 62 | 0 | } |
| 63 | ||
| 64 | /** | |
| 65 | * Decrease indention level by one, and print text to file with a newline | |
| 66 | * appended. | |
| 67 | * | |
| 68 | * @param string | |
| 69 | * String that will be printed | |
| 70 | */ | |
| 71 | protected void printlnU(final String string) { | |
| 72 | 0 | depth--; |
| 73 | 0 | println(string); |
| 74 | 0 | } |
| 75 | ||
| 76 | /** | |
| 77 | * Print text to file. | |
| 78 | * | |
| 79 | * @param string | |
| 80 | * String that will be printed | |
| 81 | */ | |
| 82 | protected void print(final String string) { | |
| 83 | 0 | if (indent) { |
| 84 | 0 | for (int i = 1; i <= depth; i++) { |
| 85 | 0 | stream.print(" "); |
| 86 | } | |
| 87 | } | |
| 88 | 0 | stream.print(string); |
| 89 | 0 | indent = false; |
| 90 | 0 | } |
| 91 | ||
| 92 | /** | |
| 93 | * Print text to file and a newline. | |
| 94 | * | |
| 95 | * @param string | |
| 96 | * String that will be printed | |
| 97 | */ | |
| 98 | protected void println(final String string) { | |
| 99 | 0 | print(string + "\n"); |
| 100 | 0 | indent = true; |
| 101 | 0 | } |
| 102 | ||
| 103 | /** | |
| 104 | * Close stream. | |
| 105 | */ | |
| 106 | protected void done() { | |
| 107 | 0 | stream.close(); |
| 108 | 0 | } |
| 109 | } |