1 23 package com.sun.enterprise.diagnostics.report.html; 24 25 import java.io.File ; 26 import java.io.FileWriter ; 27 import java.io.IOException ; 28 import java.io.Writer ; 29 import java.util.List ; 30 31 34 public class HTMLDocument implements Document { 35 36 39 private Element html = new HTMLElement(HTMLReportConstants.HTML); 40 41 42 45 private Element head = html.addElement(HTMLReportConstants.HEAD); 46 47 48 51 private Element body = html.addElement(HTMLReportConstants.BODY); 52 53 54 57 private String doctype = "-//W3C//DTD HTML 4.01 Transitional//EN"; 58 59 61 public Element getBody() { 62 return body; 63 } 64 65 66 68 public Element getHead() { 69 return head; 70 } 71 72 73 75 public String getDoctype() { 76 return doctype; 77 } 78 79 80 82 public void setDoctype(String raw) { 83 if (raw == null) { 84 throw new NullPointerException ("Doctype string is null."); 85 } 86 doctype = raw; 87 } 88 89 90 93 public String toString() { 94 StringBuffer buf = new StringBuffer (); 95 buf.append("<!DOCTYPE html PUBLIC \"") 96 .append(Escape.getInstance().encodeEntities(doctype, "")) 97 .append("\">\n") 98 .append(html.toString()); 99 return buf.toString(); 100 } 101 102 103 105 public void write(Writer output) throws IOException { 106 output.append("<!DOCTYPE html PUBLIC \"") 107 .append(Escape.getInstance().encodeEntities(doctype, "")) 108 .append("\">\n") 109 .append(html.toString()); 110 output.flush(); 111 } 112 113 114 116 public void write(File file) throws IOException { 117 FileWriter fw = new FileWriter (file); 118 write(fw); 119 fw.close(); 120 } 121 122 123 125 public void set(Element head, Element body) { 126 if (head == null) { 127 throw new NullPointerException ("Head element is null."); 128 } 129 if (body == null) { 130 throw new NullPointerException ("Body element is null."); 131 } 132 133 if (!head.getName().equalsIgnoreCase(HTMLReportConstants.HEAD)) { 135 new HTMLElement("HEAD").add(head); 136 } 137 if (!body.getName().equalsIgnoreCase(HTMLReportConstants.BODY)) { 138 new HTMLElement("BODY").add(head); 139 } 140 141 List <Element> elements = html.getElements("BODY"); 143 for (Element element : elements) { 144 html.delete(element); 145 } elements = html.getElements("HEAD"); 147 for (Element element : elements) { 148 html.delete(element); 149 } 151 html.add(head); 153 html.add(body); 154 this.head = head; 155 this.body = body; 156 } 157 158 159 160 } 161 | Popular Tags |