1 package com.stateofflow.eclipse.metrics.export.html;
2
3 import java.io.BufferedWriter;
4 import java.io.File;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.io.PrintWriter;
8 import java.util.Date;
9
10 final class HtmlPageBuilder {
11 private final Date attributionDate;
12 private PrintWriter writer;
13 private final File directory;
14
15 public HtmlPageBuilder(final File directory) {
16 this.directory = directory;
17 attributionDate = new Date();
18 }
19
20 public HtmlPageBuilder closeElement(final String name) {
21 print("</");
22 print(name);
23 print('>');
24 return this;
25 }
26
27 public HtmlPageBuilder closeElementAndNewline(final String name) {
28 closeElement(name);
29 println();
30 return this;
31 }
32
33 public void closePage() {
34 printAttribution();
35
36 closeElementAndNewline("BODY");
37 closeElementAndNewline("HTML");
38 writer.close();
39 }
40
41 public HtmlPageBuilder createElement(final String name, final String cdata) {
42 openElement(name);
43 print(cdata);
44 closeElement(name);
45 return this;
46 }
47
48 public HtmlPageBuilder createElementAndNewline(final String name, final String cdata) {
49 createElement(name, cdata);
50 println();
51 return this;
52 }
53
54 public void createImageFileLink(final String imageFileName) {
55 if (imageFileName != null) {
56 print("<IMG src=\"").print(imageFileName).println("\"/>").openElement("P");
57 }
58 }
59
60 public String getHtmlLink(final String to, final String cdata) {
61 return getLink(to + ".html", cdata);
62 }
63
64 public String getLink(final String url, final String cdata) {
65 final StringBuffer sb = new StringBuffer();
66 sb.append("<A HREF=").append('"').append(url).append("\">");
67 sb.append(cdata);
68 sb.append("</A>");
69
70 return sb.toString();
71 }
72
73 private void initialiseWriter(final String pageName) throws IOException {
74 writer = new PrintWriter(new BufferedWriter(new FileWriter(new File(directory, pageName + ".html"))));
75 }
76
77 public HtmlPageBuilder openElement(final String name) {
78 print('<');
79 print(name);
80 print('>');
81 return this;
82 }
83
84 public HtmlPageBuilder openElementAndNewline(final String name) {
85 openElement(name);
86 println();
87 return this;
88 }
89
90 private void openHtmlPage() {
91 println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"/>");
92 openElementAndNewline("HTML");
93 }
94
95 public HtmlPageBuilder openPage(final String fileName, final String heading) throws IOException {
96 initialiseWriter(fileName);
97 openHtmlPage();
98 writePageHeadElement(heading);
99 openPageBody(heading);
100
101 return this;
102 }
103
104 private void openPageBody(final String heading) {
105 openElementAndNewline("BODY");
106 createElementAndNewline("H1", heading);
107
108 printAttribution();
109 }
110
111 public HtmlPageBuilder print(final char c) {
112 writer.print(c);
113 return this;
114 }
115
116 public HtmlPageBuilder print(final String s) {
117 writer.print(s);
118 return this;
119 }
120
121 private void printAttribution() {
122 openElement("P").openElement("FONT size=\"-2\"");
123 print("Produced by ").print(getLink("http://www.stateofflow.com", "State Of Flow"));
124 print(' ').print(getLink("http://www.stateofflow.com/projects/16/eclipsemetrics", "Eclipse Metrics"));
125 print(" on ").println(attributionDate.toString());
126 closeElement("FONT");
127 }
128
129 public HtmlPageBuilder printLink(final String to, final String cdata) {
130 print(getHtmlLink(to, cdata));
131 return this;
132 }
133
134 public HtmlPageBuilder printLinkAndNewline(final String to, final String cdata) {
135 printLink(to, cdata);
136 println();
137 return this;
138 }
139
140 public HtmlPageBuilder println() {
141 writer.println();
142 return this;
143 }
144
145 public HtmlPageBuilder println(final String s) {
146 writer.println(s);
147 return this;
148 }
149
150 private void writePageHeadElement(final String heading) {
151 openElementAndNewline("HEAD");
152 createElementAndNewline("TITLE", heading);
153 println("<link href=\"http://eclipse-metrics.sourceforge.net/descriptions/style.css\" type=\"text/css\" rel=\"STYLESHEET\"/>");
154 closeElementAndNewline("HEAD");
155 }
156
157 public HtmlPageBuilder printIndexLink() {
158 return printLink("index", "Index");
159 }
160 }
161