1 | package de.uka.ipd.sdq.reliability.solver.visualisation; |
2 | |
3 | import java.io.BufferedReader; |
4 | import java.io.IOException; |
5 | import java.io.InputStream; |
6 | import java.io.InputStreamReader; |
7 | import java.io.Reader; |
8 | import java.io.StringWriter; |
9 | import java.io.Writer; |
10 | import java.util.List; |
11 | |
12 | import de.uka.ipd.sdq.reliability.solver.reporting.MarkovReportItem; |
13 | import de.uka.ipd.sdq.reliability.solver.reporting.MarkovReporting; |
14 | import de.uka.ipd.sdq.reliability.solver.reporting.MarkovReportingTable; |
15 | |
16 | /** |
17 | * Class that is responsible for generating HTML code from a given |
18 | * MarkovReporting instance. |
19 | * |
20 | * @author Daniel Patejdl |
21 | * |
22 | */ |
23 | public class MarkovHtmlGenerator { |
24 | /** |
25 | * A MarkovReporting instance; serves as central data source of this class. |
26 | */ |
27 | private List<MarkovReportItem> markovReportItems; |
28 | |
29 | /** |
30 | * Generates a new instance that is responsible for creating a HTML page, |
31 | * given a MarkovReporting instance as data source. |
32 | * |
33 | * @param markovReporting |
34 | * a MarkovReporting instance |
35 | */ |
36 | public MarkovHtmlGenerator(MarkovReporting markovReporting) { |
37 | this.markovReportItems = markovReporting.getMarkovReportItems(); |
38 | } |
39 | |
40 | /** |
41 | * Creates and returns a String containing HTML code. The HTML code reflects |
42 | * the Markov transformation results. All transformation results are |
43 | * represented as HTML tables. |
44 | * |
45 | * @return a String containing HTML code |
46 | */ |
47 | public String getHtml() { |
48 | // Retrieve CSS and JavaScript source code for later use in HTML page: |
49 | String cssCode = ""; |
50 | String jsCode = ""; |
51 | try { |
52 | cssCode = getInputStreamContents(this.getClass() |
53 | .getResourceAsStream("/jsComponents.css")); |
54 | jsCode = getInputStreamContents(this.getClass() |
55 | .getResourceAsStream("/jsComponents.js")); |
56 | } catch (IOException e) { |
57 | // TODO Auto-generated catch block |
58 | e.printStackTrace(); |
59 | } |
60 | // create HTML page header |
61 | StringBuilder htmlCode = new StringBuilder( |
62 | "<html><head>" |
63 | + "<title>Markov Results</title>" |
64 | + "<script type=\"text/javascript\">" |
65 | + jsCode |
66 | + "</script>" |
67 | + "<style type=\"text/css\">" |
68 | + cssCode |
69 | + " body { font-family: Lucida Grande, Arial, Tahoma, Verdana; font-size: 12px; }" |
70 | + " td, th { font-size: 11px; }" |
71 | + " th { background-color: c0c0c0; margin: 1px; padding: 3px 5px 3px 5px; border: 1px solid black; }" |
72 | + " td { background-color: dfdfdf; margin: 1px; padding: 3px 5px 3px 5px; }" |
73 | + "</style>" + "</head><body>"); |
74 | for (MarkovReportItem item : markovReportItems) { |
75 | htmlCode |
76 | .append("<h2>Reliability results for UsageScenario: <font color=\"#606060\">" |
77 | + item.getScenarioName() + "</font></h2>"); |
78 | htmlCode.append("Scenario ID: <font color=\"#606060\">" |
79 | + item.getScenarioId() + "</font><br />"); |
80 | htmlCode.append("Success probability: <font color=\"#606060\">" |
81 | + item.getSuccessProbabilityString() + "</font><br />"); |
82 | // create tables: failure mode tables first, then impact analysis |
83 | // tables |
84 | if (item.getFailureModeTables().size() != 0) { |
85 | // draw horizontal separation line |
86 | htmlCode.append("<hr />"); |
87 | htmlCode.append("<h3>Failure Mode Analysis</h3>"); |
88 | } |
89 | for (MarkovReportingTable table : item.getFailureModeTables()) { |
90 | if (table.getRows().size() == 0) { |
91 | continue; // table contains no rows, thus consider next |
92 | // table in list |
93 | } |
94 | htmlCode |
95 | .append("<div class=\"JSTableStripe\"><div class=\"JSTableSort\">"); |
96 | htmlCode.append("<b>" + table.getTableName() + ":</b><br />"); |
97 | htmlCode |
98 | .append("<table border=\"0\" style=\"margin-top: 1mm; border: 1px solid black;\">"); |
99 | // create header row |
100 | htmlCode.append("<thead><tr>"); |
101 | for (String headerEntry : table.getHeaderRow()) { |
102 | htmlCode |
103 | .append("<th class=\"SortString\" onselectstart=\"return false;\">" |
104 | + headerEntry + "</th>"); |
105 | } |
106 | htmlCode.append("</tr></thead><tbody>"); |
107 | // create table data rows |
108 | for (List<String> row : table.getRows()) { |
109 | htmlCode.append("<tr>"); |
110 | for (String entry : row) { |
111 | htmlCode.append("<td>" + entry + "</td>"); |
112 | } |
113 | htmlCode.append("</tr>"); |
114 | } |
115 | // finish table HTML code |
116 | htmlCode.append("</tbody></table>"); |
117 | htmlCode.append("</div></div><br />"); |
118 | } |
119 | if (item.getImpactAnalysisTables().size() != 0) { |
120 | // draw horizontal separation line |
121 | htmlCode.append("<hr />"); |
122 | // now, consider impact analysis tables |
123 | htmlCode.append("<h3>Impact Analysis</h3>"); |
124 | for (MarkovReportingTable table : item |
125 | .getImpactAnalysisTables()) { |
126 | if (table.getRows().size() == 0) { |
127 | continue; // table contains no rows, thus consider next |
128 | // table in list |
129 | } |
130 | htmlCode |
131 | .append("<div class=\"JSTableStripe\"><div class=\"JSTableSort\">"); |
132 | htmlCode.append("<b>" + table.getTableName() |
133 | + ":</b><br />"); |
134 | htmlCode |
135 | .append("<table border=\"0\" style=\"margin-top: 1mm; border: 1px solid black;\">"); |
136 | // create header row |
137 | htmlCode.append("<thead><tr>"); |
138 | for (String headerEntry : table.getHeaderRow()) { |
139 | htmlCode |
140 | .append("<th class=\"SortString\" onselectstart=\"return false;\">" |
141 | + headerEntry + "</th>"); |
142 | } |
143 | htmlCode.append("</tr></thead><tbody>"); |
144 | // create table data rows |
145 | for (List<String> row : table.getRows()) { |
146 | htmlCode.append("<tr>"); |
147 | for (String entry : row) { |
148 | htmlCode.append("<td>" + entry + "</td>"); |
149 | } |
150 | htmlCode.append("</tr>"); |
151 | } |
152 | // finish table HTML code |
153 | htmlCode.append("</tbody></table>"); |
154 | htmlCode.append("</div></div><br />"); |
155 | } |
156 | } |
157 | // draw horizontal separation lines |
158 | htmlCode.append("<hr>"); |
159 | htmlCode.append("<hr>"); |
160 | } |
161 | |
162 | // finish page |
163 | htmlCode.append("</body></html>"); |
164 | return htmlCode.toString(); |
165 | } |
166 | |
167 | /** |
168 | * Retrieves the contents of an InputStream as a String. |
169 | * |
170 | * @param is |
171 | * the InputStream |
172 | * @return the resulting String |
173 | * @throws IOException |
174 | */ |
175 | private String getInputStreamContents(final InputStream is) |
176 | throws IOException { |
177 | if (is != null) { |
178 | Writer writer = new StringWriter(); |
179 | char[] buffer = new char[1024]; |
180 | try { |
181 | Reader reader = new BufferedReader(new InputStreamReader(is, |
182 | "UTF-8")); |
183 | int n; |
184 | while ((n = reader.read(buffer)) != -1) { |
185 | writer.write(buffer, 0, n); |
186 | } |
187 | } finally { |
188 | is.close(); |
189 | } |
190 | return writer.toString(); |
191 | } else { |
192 | return ""; |
193 | } |
194 | } |
195 | } |