KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > diagnostics > report > html > HTMLDocument


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.diagnostics.report.html;
24
25 import java.io.File JavaDoc;
26 import java.io.FileWriter JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.Writer JavaDoc;
29 import java.util.List JavaDoc;
30
31 /*
32  * Basic implementation of a document.
33  */

34 public class HTMLDocument implements Document {
35     
36     /**
37      * The root HTML element.
38      */

39     private Element html = new HTMLElement(HTMLReportConstants.HTML);
40     
41     
42     /**
43      * The head element.
44      */

45     private Element head = html.addElement(HTMLReportConstants.HEAD);
46     
47     
48     /**
49      * The body element.
50      */

51     private Element body = html.addElement(HTMLReportConstants.BODY);
52     
53     
54     /**
55      * The doctype.
56      */

57     private String JavaDoc doctype = "-//W3C//DTD HTML 4.01 Transitional//EN";
58   
59     /**
60      */

61     public Element getBody() {
62         return body;
63     }
64
65
66     /**
67      */

68     public Element getHead() {
69         return head;
70     }
71
72
73     /**
74      */

75     public String JavaDoc getDoctype() {
76         return doctype;
77     }
78
79
80     /**
81      */

82     public void setDoctype(String JavaDoc raw) {
83         if (raw == null) {
84             throw new NullPointerException JavaDoc("Doctype string is null.");
85         }
86         doctype = raw;
87     }
88     
89
90     /**
91      * @see java.lang.Object#toString()
92      */

93     public String JavaDoc toString() {
94         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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     /**
104      */

105     public void write(Writer JavaDoc output) throws IOException JavaDoc {
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     /**
115      */

116     public void write(File JavaDoc file) throws IOException JavaDoc {
117         FileWriter JavaDoc fw = new FileWriter JavaDoc(file);
118         write(fw);
119         fw.close();
120     }
121
122
123     /**
124      */

125     public void set(Element head, Element body) {
126         if (head == null) {
127             throw new NullPointerException JavaDoc("Head element is null.");
128         }
129         if (body == null) {
130             throw new NullPointerException JavaDoc("Body element is null.");
131         }
132         
133         // Check the element names.
134
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         // Discard old elements.
142
List JavaDoc<Element> elements = html.getElements("BODY");
143         for (Element element : elements) {
144             html.delete(element);
145         } // Loop discarding body elements.
146
elements = html.getElements("HEAD");
147         for (Element element : elements) {
148             html.delete(element);
149         } // Loop discarding head elements.
150

151         // Add the new elements.
152
html.add(head);
153         html.add(body);
154         this.head = head;
155         this.body = body;
156     }
157
158
159   
160 }
161
Popular Tags