HTMLElement.java
1/*******************************************************************************
2 * Copyright (c) 2009 Mountainminds GmbH & Co. KG and others
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Marc R. Hoffmann - initial API and implementation
10 *
11 * $Id: $
12 *******************************************************************************/
13package org.jacoco.report.html;
14
15import java.io.IOException;
16import java.io.Writer;
17
18import org.jacoco.report.xml.XMLElement;
19
20/**
21 * A {@link XMLElement} with utility methods to create XHTML documents. It
22 * provides methods of HTML tags to avoid magic strings in the generators.
23 *
24 * @author Marc R. Hoffmann
25 * @version $Revision: $
26 */
27public class HTMLElement extends XMLElement {
28
29 /**
30 * Creates a new element for a HTML document.
31 *
32 * @param writer
33 * all output will be written directly to this
34 * @param name
35 * element name
36 */
37 protected HTMLElement(final Writer writer, final String name) {
38 super(writer, name);
39 }
40
41 @Override
42 public HTMLElement element(final String name) throws IOException {
43 final HTMLElement element = new HTMLElement(writer, name);
44 addChildElement(element);
45 return element;
46 }
47
48 /**
49 * Creates a 'meta' element.
50 *
51 * @param httpequivattr
52 * value of the http-equiv attribute
53 * @param contentattr
54 * value for the content attribute
55 * @return 'meta' element
56 * @throws IOException
57 * in case of problems with the writer
58 */
59 public HTMLElement meta(final String httpequivattr, final String contentattr)
60 throws IOException {
61 final HTMLElement meta = element("meta");
62 meta.attr("http-equiv", httpequivattr);
63 meta.attr("content", contentattr);
64 return meta;
65 }
66
67 /**
68 * Creates a 'link' element.
69 *
70 * @param relattr
71 * value of the rel attribute
72 * @param hrefattr
73 * value for the href attribute
74 * @param typeattr
75 * value for the type attribute
76 * @return 'link' element
77 * @throws IOException
78 * in case of problems with the writer
79 */
80 public HTMLElement link(final String relattr, final String hrefattr,
81 final String typeattr) throws IOException {
82 final HTMLElement link = element("link");
83 link.attr("rel", relattr);
84 link.attr("href", hrefattr);
85 link.attr("type", typeattr);
86 return link;
87 }
88
89 /**
90 * Creates a 'title' element.
91 *
92 * @return 'title' element
93 * @throws IOException
94 * in case of problems with the writer
95 */
96 public HTMLElement title() throws IOException {
97 return element("title");
98 }
99
100 /**
101 * Creates a 'h1' element.
102 *
103 * @return 'h1' element
104 * @throws IOException
105 * in case of problems with the writer
106 */
107 public HTMLElement h1() throws IOException {
108 return element("h1");
109 }
110
111 /**
112 * Creates a 'span' element.
113 *
114 * @param classattr
115 * value of the class attribute
116 * @return 'span' element
117 * @throws IOException
118 * in case of problems with the writer
119 */
120 public HTMLElement span(final String classattr) throws IOException {
121 final HTMLElement span = element("span");
122 span.attr("class", classattr);
123 return span;
124 }
125
126 /**
127 * Creates a 'span' element.
128 *
129 * @param classattr
130 * value of the class attribute
131 * @param idattr
132 * value of the id attribute
133 * @return 'span' element
134 * @throws IOException
135 * in case of problems with the writer
136 */
137 public HTMLElement span(final String classattr, final String idattr)
138 throws IOException {
139 final HTMLElement span = element("span");
140 span.attr("class", classattr);
141 span.attr("id", idattr);
142 return span;
143 }
144
145 /**
146 * Creates a 'div' element.
147 *
148 * @param classattr
149 * value of the class attribute
150 * @return 'div' element
151 * @throws IOException
152 * in case of problems with the writer
153 */
154 public HTMLElement div(final String classattr) throws IOException {
155 final HTMLElement div = element("div");
156 div.attr("class", classattr);
157 return div;
158 }
159
160 /**
161 * Creates a 'pre' element.
162 *
163 * @param classattr
164 * value of the class attribute
165 * @return 'pre' element
166 * @throws IOException
167 * in case of problems with the writer
168 */
169 public HTMLElement pre(final String classattr) throws IOException {
170 final HTMLElement pre = element("pre");
171 pre.attr("class", classattr);
172 return pre;
173 }
174
175 /**
176 * Creates a empty 'br' element.
177 *
178 * @throws IOException
179 * in case of problems with the writer
180 */
181 public void br() throws IOException {
182 element("br").close();
183 }
184
185 /**
186 * Creates a 'a' element.
187 *
188 * @param hrefattr
189 * value of the href attribute
190 * @return 'a' element
191 * @throws IOException
192 * in case of problems with the writer
193 */
194 public HTMLElement a(final String hrefattr) throws IOException {
195 final HTMLElement a = element("a");
196 a.attr("href", hrefattr);
197 return a;
198 }
199
200 /**
201 * Creates a 'a' element.
202 *
203 * @param hrefattr
204 * value of the href attribute
205 * @param classattr
206 * value of the class attribute
207 * @return 'a' element
208 * @throws IOException
209 * in case of problems with the writer
210 */
211 public HTMLElement a(final String hrefattr, final String classattr)
212 throws IOException {
213 final HTMLElement a = element("a");
214 a.attr("href", hrefattr);
215 a.attr("class", classattr);
216 return a;
217 }
218
219 /**
220 * Creates a 'table' element.
221 *
222 * @param classattr
223 * value of the class attribute
224 * @return 'table' element
225 * @throws IOException
226 * in case of problems with the writer
227 */
228 public HTMLElement table(final String classattr) throws IOException {
229 final HTMLElement table = element("table");
230 table.attr("class", classattr);
231 table.attr("cellspacing", "0");
232 return table;
233 }
234
235 /**
236 * Creates a 'thead' element.
237 *
238 * @return 'thead' element
239 * @throws IOException
240 * in case of problems with the writer
241 */
242 public HTMLElement thead() throws IOException {
243 return element("thead");
244 }
245
246 /**
247 * Creates a 'tfoot' element.
248 *
249 * @return 'tfoot' element
250 * @throws IOException
251 * in case of problems with the writer
252 */
253 public HTMLElement tfoot() throws IOException {
254 return element("tfoot");
255 }
256
257 /**
258 * Creates a 'tbody' element.
259 *
260 * @return 'tbody' element
261 * @throws IOException
262 * in case of problems with the writer
263 */
264 public HTMLElement tbody() throws IOException {
265 return element("tbody");
266 }
267
268 /**
269 * Creates a 'tr' element.
270 *
271 * @return 'tr' element
272 * @throws IOException
273 * in case of problems with the writer
274 */
275 public HTMLElement tr() throws IOException {
276 return element("tr");
277 }
278
279 /**
280 * Creates a 'td' element.
281 *
282 * @return 'td' element
283 * @throws IOException
284 * in case of problems with the writer
285 */
286 public HTMLElement td() throws IOException {
287 return td(null, 1);
288 }
289
290 /**
291 * Creates a 'td' element.
292 *
293 * @param classattr
294 * value of the class attribute
295 * @return 'td' element
296 * @throws IOException
297 * in case of problems with the writer
298 */
299 public HTMLElement td(final String classattr) throws IOException {
300 return td(classattr, 1);
301 }
302
303 /**
304 * Creates a 'td' element.
305 *
306 * @param colspanattr
307 * value of the colspan attribute
308 * @return 'td' element
309 * @throws IOException
310 * in case of problems with the writer
311 */
312 public HTMLElement td(final int colspanattr) throws IOException {
313 return td(null, colspanattr);
314 }
315
316 /**
317 * Creates a 'td' element.
318 *
319 * @param classattr
320 * value of the class attribute
321 * @param colspanattr
322 * value of the colspan attribute
323 * @return 'td' element
324 * @throws IOException
325 * in case of problems with the writer
326 */
327 public HTMLElement td(final String classattr, final int colspanattr)
328 throws IOException {
329 final HTMLElement td = element("td");
330 if (classattr != null) {
331 td.attr("class", classattr);
332 }
333 if (colspanattr > 1) {
334 td.attr("colspan", String.valueOf(colspanattr));
335 }
336 return td;
337 }
338
339 /**
340 * Creates a 'img' element.
341 *
342 * @param srcattr
343 * value of the src attribute
344 * @param widthattr
345 * value of the width attribute
346 * @param heightattr
347 * value of the height attribute
348 * @param altattr
349 * value of the alt attribute
350 * @throws IOException
351 * in case of problems with the writer
352 */
353 public void img(final String srcattr, final int widthattr,
354 final int heightattr, final String altattr) throws IOException {
355 final HTMLElement img = element("img");
356 img.attr("src", srcattr);
357 img.attr("width", String.valueOf(widthattr));
358 img.attr("height", String.valueOf(heightattr));
359 img.attr("alt", String.valueOf(altattr));
360 img.close();
361 }
362
363 /**
364 * Creates a 'script' element.
365 *
366 * @param typeattr
367 * value of the type attribute
368 * @param srcattr
369 * value of the src attribute
370 * @throws IOException
371 * in case of problems with the writer
372 */
373 public void script(final String typeattr, final String srcattr)
374 throws IOException {
375 final HTMLElement script = element("script");
376 script.attr("type", typeattr);
377 script.attr("src", srcattr);
378 // Enforce open and closing tag otherwise it won't work in browsers:
379 script.text("");
380 script.close();
381 }
382
383}