001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.admin;
005:
006: import java.util.HashMap;
007: import java.util.Iterator;
008: import java.util.Map;
009:
010: public class Document {
011: private StringBuffer out;
012: int indent = 0;
013:
014: private Map fontParamMap = new HashMap();
015: private StringBuffer fontParams = new StringBuffer();
016:
017: public Document() {
018: this (new StringBuffer());
019: }
020:
021: public Document(Document parent) {
022: this (new StringBuffer());
023: this .indent = parent.indent;
024: this .fontParamMap.putAll(parent.fontParamMap);
025: bakeFontParams();
026: }
027:
028: public Document(StringBuffer out) {
029: this .out = out;
030: }
031:
032: public String toString() {
033: return out.toString();
034: }
035:
036: public Document otag(Object name) {
037: return otag(name, "");
038: }
039:
040: public Document otag(Object name, Object params) {
041: indent();
042: print("<" + name);
043: if (params != null) {
044: print(params);
045: }
046: return println(">");
047: }
048:
049: public Document ctag(Object name) {
050: return outdent().println("</" + name + ">");
051: }
052:
053: public Document octag(Object name) {
054: return octag(name, "");
055: }
056:
057: public Document octag(Object name, Object params) {
058: return print("<" + name + (params == null ? "" : params) + "/>");
059: }
060:
061: public Document html() {
062: return otag("html");
063: }
064:
065: public Document chtml() {
066: return ctag("html");
067: }
068:
069: public Document head() {
070: return otag("head");
071: }
072:
073: public Document chead() {
074: return ctag("head");
075: }
076:
077: public Document title(Object title) {
078: return otag("title").println(title).ctag("title");
079: }
080:
081: public Document body() {
082: return otag("body");
083: }
084:
085: public Document cbody() {
086: return ctag("body");
087: }
088:
089: public Document comment(Object comment) {
090: return println("<!--" + comment + "-->");
091: }
092:
093: public String param(Object name, Object value) {
094: return " " + name + "=\"" + value + "\"";
095: }
096:
097: public Document text(Object text) {
098: if (text != null) {
099: return print(escape(text.toString()));
100: } else {
101: return this ;
102: }
103: }
104:
105: private String escape(String source) {
106: if (source == null)
107: return null;
108:
109: StringBuffer myOut = new StringBuffer();
110: char[] sourceChars = source.toCharArray();
111: for (int i = 0; i < sourceChars.length; ++i) {
112: char theChar = sourceChars[i];
113: String toAppend = new String(new char[] { theChar });
114:
115: if (theChar == '<')
116: toAppend = "<";
117: if (theChar == '>')
118: toAppend = ">";
119: if (theChar == '&')
120: toAppend = "&";
121: if (theChar > 0x7E || theChar < 0x20)
122: toAppend = "&#" + Integer.toHexString(theChar) + ";";
123:
124: myOut.append(toAppend);
125: }
126:
127: return myOut.toString();
128: }
129:
130: public Document img(Object url) {
131: return img(url, " border=0");
132: }
133:
134: public Document img(Object url, Object params) {
135: return otag("img src=\"" + url + "\" ", params);
136: }
137:
138: public Document href(Object url, String text) {
139: return otag("a href=\"" + url + "\"").print(text).ctag("a");
140: }
141:
142: public Document br() {
143: return octag("br");
144: }
145:
146: public Document hr() {
147: return octag("hr");
148: }
149:
150: public Document ul() {
151: return ul("");
152: }
153:
154: public Document ul(String params) {
155: return otag("ul", params);
156: }
157:
158: public Document cul() {
159: return ctag("ul");
160: }
161:
162: public Document li() {
163: return li("");
164: }
165:
166: public Document li(String params) {
167: return otag("li", params);
168: }
169:
170: public Document cli() {
171: return ctag("li");
172: }
173:
174: public Document style() {
175: return style("");
176: }
177:
178: public Document style(Object params) {
179: return otag("style", params);
180: }
181:
182: public Document cstyle() {
183: return ctag("style");
184: }
185:
186: private void bakeFontParams() {
187: fontParams.delete(0, fontParams.length());
188: Object key;
189: for (Iterator iter = fontParamMap.keySet().iterator(); iter
190: .hasNext();) {
191: key = iter.next();
192: fontParams.append(param(key, fontParamMap.get(key)));
193: }
194: }
195:
196: public Document fontparam(Object name, Object value) {
197: fontParamMap.put(name, value);
198: bakeFontParams();
199: return this ;
200: }
201:
202: public Document dfontparam(Object name) {
203: fontParamMap.remove(name);
204: bakeFontParams();
205: return this ;
206: }
207:
208: public Document fontclear() {
209: fontParamMap.clear();
210: bakeFontParams();
211: return this ;
212: }
213:
214: public Document font() {
215: return font(fontParams);
216: }
217:
218: public Document font(Object params) {
219: return otag("font", params);
220: }
221:
222: public Document cfont() {
223: return ctag("font");
224: }
225:
226: public Document table() {
227: return table("");
228: }
229:
230: public Document table(String params) {
231: return otag("table", params);
232: }
233:
234: public Document ctable() {
235: return ctag("table");
236: }
237:
238: public Document row(String data) {
239: tr().print(data).ctr();
240: return this ;
241: }
242:
243: public Document cell(Object o) {
244: return cell(o == null ? "" : o.toString());
245: }
246:
247: public Document cell(String data) {
248: td().println(data).ctd();
249: return this ;
250: }
251:
252: public Document tr() {
253: return otag("tr");
254: }
255:
256: public Document ctr() {
257: return ctag("tr");
258: }
259:
260: public Document th() {
261: return th("");
262: }
263:
264: public Document th(Object params) {
265: return otag("th", params);
266: }
267:
268: public Document cth() {
269: return ctag("th");
270: }
271:
272: public Document td() {
273: return td("");
274: }
275:
276: public Document td(Object params) {
277: return otag("td", params);
278: }
279:
280: public Document ctd() {
281: return ctag("td");
282: }
283:
284: Document indent() {
285: indent += 2;
286: return this ;
287: }
288:
289: Document outdent() {
290: indent = indent - 2;
291: return this ;
292: }
293:
294: public Document newline() {
295: out.append("\n");
296: for (int i = 0; i < indent; i++) {
297: out.append(" ");
298: }
299: return this ;
300: }
301:
302: public Document println(Object data) {
303: print(data);
304: newline();
305: return this ;
306: }
307:
308: public Document print(Object data) {
309: out.append(data);
310: return this;
311: }
312:
313: }
|