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