001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.common.markup;
025:
026: import java.io.PrintWriter;
027:
028: /**
029: * Utility class to generate markup code. This example ...
030: * <pre>
031: * DocumentWriter doc = new DocumentWriter(writer);
032: * doc.start("body");
033: * doc.start("p").attribute("class", "foo");
034: * doc.body("Hello ");
035: * doc.start("strong").body("World");
036: * doc.closeAll();
037: * </pre>
038: * ... will produce the following code:
039: * <pre>
040: * <body><p class="foo">Hello <strong>World</strong></p></body>
041: * </pre>
042: */
043: public class DocumentWriter {
044:
045: private TagWriter tagWriter;
046:
047: public DocumentWriter(PrintWriter writer) {
048: this (writer, true);
049: }
050:
051: public DocumentWriter(PrintWriter writer, boolean xhtml) {
052: tagWriter = new TagWriter(writer);
053: tagWriter.setXhtml(xhtml);
054: }
055:
056: public DocumentWriter start(String tagName) {
057: tagWriter = tagWriter.start(tagName);
058: return this ;
059: }
060:
061: public DocumentWriter startEmpty(String tagName) {
062: tagWriter = tagWriter.startEmpty(tagName);
063: return this ;
064: }
065:
066: public DocumentWriter start(String tagName, boolean empty) {
067: tagWriter = tagWriter.start(tagName, empty);
068: return this ;
069: }
070:
071: public DocumentWriter attribute(String name) {
072: tagWriter.attribute(name);
073: return this ;
074: }
075:
076: public DocumentWriter attribute(String name, int value) {
077: tagWriter.attribute(name, value);
078: return this ;
079: }
080:
081: public DocumentWriter attribute(String name, boolean present) {
082: tagWriter.attribute(name, present);
083: return this ;
084: }
085:
086: public DocumentWriter attribute(String name, String value) {
087: tagWriter.attribute(name, value);
088: return this ;
089: }
090:
091: public DocumentWriter attribute(String name, String value,
092: boolean renderEmpty) {
093: tagWriter.attribute(name, value, renderEmpty);
094: return this ;
095: }
096:
097: public DocumentWriter body() {
098: tagWriter.body();
099: return this ;
100: }
101:
102: public DocumentWriter body(String body) {
103: tagWriter.body(body);
104: return this ;
105: }
106:
107: public DocumentWriter body(String body, boolean escapeHtml) {
108: tagWriter.body(body, escapeHtml);
109: return this ;
110: }
111:
112: public DocumentWriter println(String s) {
113: tagWriter.println(s);
114: return this ;
115: }
116:
117: public DocumentWriter end() {
118: tagWriter = tagWriter.end();
119: return this ;
120: }
121:
122: public void closeAll() {
123: tagWriter.closeAll();
124: }
125: }
|