KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > markup > DocumentWriter


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.markup;
25
26 import java.io.PrintWriter JavaDoc;
27
28 /**
29  * Utility class to generate markup code. This example ...
30  * <pre>
31  * DocumentWriter doc = new DocumentWriter(writer);
32  * doc.start("body");
33  * doc.start("p").attribute("class", "foo");
34  * doc.body("Hello ");
35  * doc.start("strong").body("World");
36  * doc.closeAll();
37  * </pre>
38  * ... will produce the following code:
39  * <pre>
40  * &lt;body&gt;&lt;p class="foo"&gt;Hello &lt;strong&gt;World&lt;/strong&gt;&lt;/p&gt;&lt;/body&gt;
41  * </pre>
42  */

43 public class DocumentWriter {
44
45     private TagWriter tagWriter;
46     
47     public DocumentWriter(PrintWriter JavaDoc writer) {
48         this(writer, true);
49     }
50     
51     public DocumentWriter(PrintWriter JavaDoc writer, boolean xhtml) {
52         tagWriter = new TagWriter(writer);
53         tagWriter.setXhtml(xhtml);
54     }
55     
56     public DocumentWriter start(String JavaDoc tagName) {
57         tagWriter = tagWriter.start(tagName);
58         return this;
59     }
60     
61     public DocumentWriter startEmpty(String JavaDoc tagName) {
62         tagWriter = tagWriter.startEmpty(tagName);
63         return this;
64     }
65         
66     public DocumentWriter start(String JavaDoc tagName, boolean empty) {
67         tagWriter = tagWriter.start(tagName, empty);
68         return this;
69     }
70     
71     public DocumentWriter attribute(String JavaDoc name) {
72         tagWriter.attribute(name);
73         return this;
74     }
75
76     public DocumentWriter attribute(String JavaDoc name, int value) {
77         tagWriter.attribute(name, value);
78         return this;
79     }
80
81     public DocumentWriter attribute(String JavaDoc name, boolean present) {
82         tagWriter.attribute(name, present);
83         return this;
84     }
85     
86     public DocumentWriter attribute(String JavaDoc name, String JavaDoc value) {
87         tagWriter.attribute(name, value);
88         return this;
89     }
90
91     public DocumentWriter attribute(String JavaDoc name, String JavaDoc value, boolean renderEmpty) {
92         tagWriter.attribute(name ,value, renderEmpty);
93         return this;
94     }
95
96     public DocumentWriter body() {
97         tagWriter.body();
98         return this;
99     }
100     
101     public DocumentWriter body(String JavaDoc body) {
102         tagWriter.body(body);
103         return this;
104     }
105     
106     public DocumentWriter body(String JavaDoc body, boolean escapeHtml) {
107         tagWriter.body(body, escapeHtml);
108         return this;
109     }
110     
111     public DocumentWriter println(String JavaDoc s) {
112         tagWriter.println(s);
113         return this;
114     }
115     
116     public DocumentWriter end() {
117         tagWriter = tagWriter.end();
118         return this;
119     }
120     
121     public void closeAll() {
122         tagWriter.closeAll();
123     }
124 }
125
Popular Tags