KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > admin > Document


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.admin;
5
6 import java.util.HashMap JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.Map JavaDoc;
9
10 public class Document {
11   private StringBuffer JavaDoc out;
12   int indent = 0;
13
14   private Map JavaDoc fontParamMap = new HashMap JavaDoc();
15   private StringBuffer JavaDoc fontParams = new StringBuffer JavaDoc();
16
17   public Document() {
18     this(new StringBuffer JavaDoc());
19   }
20
21   public Document(Document parent) {
22     this(new StringBuffer JavaDoc());
23     this.indent = parent.indent;
24     this.fontParamMap.putAll(parent.fontParamMap);
25     bakeFontParams();
26   }
27
28   public Document(StringBuffer JavaDoc out) {
29     this.out = out;
30   }
31
32   public String JavaDoc toString() {
33     return out.toString();
34   }
35
36   public Document otag(Object JavaDoc name) {
37     return otag(name, "");
38   }
39
40   public Document otag(Object JavaDoc name, Object JavaDoc params) {
41     indent();
42     print("<" + name);
43     if (params != null) {
44       print(params);
45     }
46     return println(">");
47   }
48
49   public Document ctag(Object JavaDoc name) {
50     return outdent().println("</" + name + ">");
51   }
52
53   public Document octag(Object JavaDoc name) {
54     return octag(name, "");
55   }
56
57   public Document octag(Object JavaDoc name, Object JavaDoc params) {
58     return print("<" + name + (params == null ? "" : params) + "/>");
59   }
60
61   public Document html() {
62     return otag("html");
63   }
64
65   public Document chtml() {
66     return ctag("html");
67   }
68
69   public Document head() {
70     return otag("head");
71   }
72
73   public Document chead() {
74     return ctag("head");
75   }
76
77   public Document title(Object JavaDoc title) {
78     return otag("title").println(title).ctag("title");
79   }
80
81   public Document body() {
82     return otag("body");
83   }
84
85   public Document cbody() {
86     return ctag("body");
87   }
88
89   public Document comment(Object JavaDoc comment) {
90     return println("<!--" + comment + "-->");
91   }
92
93   public String JavaDoc param(Object JavaDoc name, Object JavaDoc value) {
94     return " " + name + "=\"" + value + "\"";
95   }
96
97   public Document text(Object JavaDoc text) {
98     if (text != null) {
99       return print(escape(text.toString()));
100     } else {
101       return this;
102     }
103   }
104
105   private String JavaDoc escape(String JavaDoc source) {
106     if (source == null) return null;
107
108     StringBuffer JavaDoc myOut = new StringBuffer JavaDoc();
109     char[] sourceChars = source.toCharArray();
110     for (int i = 0; i < sourceChars.length; ++i) {
111       char theChar = sourceChars[i];
112       String JavaDoc toAppend = new String JavaDoc(new char[] { theChar });
113
114       if (theChar == '<') toAppend = "&lt;";
115       if (theChar == '>') toAppend = "&gt;";
116       if (theChar == '&') toAppend = "&amp;";
117       if (theChar > 0x7E || theChar < 0x20) toAppend = "&#" + Integer.toHexString(theChar) + ";";
118
119       myOut.append(toAppend);
120     }
121
122     return myOut.toString();
123   }
124
125   public Document img(Object JavaDoc url) {
126     return img(url, " border=0");
127   }
128
129   public Document img(Object JavaDoc url, Object JavaDoc params) {
130     return otag("img SRC=\"" + url + "\" ", params);
131   }
132
133   public Document href(Object JavaDoc url, String JavaDoc text) {
134     return otag("a HREF=\"" + url + "\"").print(text).ctag("a");
135   }
136
137   public Document br() {
138     return octag("br");
139   }
140
141   public Document hr() {
142     return octag("hr");
143   }
144
145   public Document ul() {
146     return ul("");
147   }
148
149   public Document ul(String JavaDoc params) {
150     return otag("ul", params);
151   }
152
153   public Document cul() {
154     return ctag("ul");
155   }
156
157   public Document li() {
158     return li("");
159   }
160
161   public Document li(String JavaDoc params) {
162     return otag("li", params);
163   }
164
165   public Document cli() {
166     return ctag("li");
167   }
168
169   public Document style() {
170     return style("");
171   }
172
173   public Document style(Object JavaDoc params) {
174     return otag("style", params);
175   }
176
177   public Document cstyle() {
178     return ctag("style");
179   }
180
181   private void bakeFontParams() {
182     fontParams.delete(0, fontParams.length());
183     Object JavaDoc key;
184     for (Iterator JavaDoc iter = fontParamMap.keySet().iterator(); iter.hasNext();) {
185       key = iter.next();
186       fontParams.append(param(key, fontParamMap.get(key)));
187     }
188   }
189
190   public Document fontparam(Object JavaDoc name, Object JavaDoc value) {
191     fontParamMap.put(name, value);
192     bakeFontParams();
193     return this;
194   }
195
196   public Document dfontparam(Object JavaDoc name) {
197     fontParamMap.remove(name);
198     bakeFontParams();
199     return this;
200   }
201
202   public Document fontclear() {
203     fontParamMap.clear();
204     bakeFontParams();
205     return this;
206   }
207
208   public Document font() {
209     return font(fontParams);
210   }
211
212   public Document font(Object JavaDoc params) {
213     return otag("font", params);
214   }
215
216   public Document cfont() {
217     return ctag("font");
218   }
219
220   public Document table() {
221     return table("");
222   }
223
224   public Document table(String JavaDoc params) {
225     return otag("table", params);
226   }
227
228   public Document ctable() {
229     return ctag("table");
230   }
231
232   public Document row(String JavaDoc data) {
233     tr().print(data).ctr();
234     return this;
235   }
236
237   public Document cell(Object JavaDoc o) {
238     return cell(o == null ? "" : o.toString());
239   }
240
241   public Document cell(String JavaDoc data) {
242     td().println(data).ctd();
243     return this;
244   }
245
246   public Document tr() {
247     return otag("tr");
248   }
249
250   public Document ctr() {
251     return ctag("tr");
252   }
253
254   public Document th() {
255     return th("");
256   }
257
258   public Document th(Object JavaDoc params) {
259     return otag("th", params);
260   }
261
262   public Document cth() {
263     return ctag("th");
264   }
265
266   public Document td() {
267     return td("");
268   }
269
270   public Document td(Object JavaDoc params) {
271     return otag("td", params);
272   }
273
274   public Document ctd() {
275     return ctag("td");
276   }
277
278   Document indent() {
279     indent += 2;
280     return this;
281   }
282
283   Document outdent() {
284     indent = indent - 2;
285     return this;
286   }
287
288   public Document newline() {
289     out.append("\n");
290     for (int i = 0; i < indent; i++) {
291       out.append(" ");
292     }
293     return this;
294   }
295
296   public Document println(Object JavaDoc data) {
297     print(data);
298     newline();
299     return this;
300   }
301
302   public Document print(Object JavaDoc data) {
303     out.append(data);
304     return this;
305   }
306
307 }
Popular Tags