001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/XMLWriter.java,v 1.6 2001/07/22 20:25:14 pier Exp $
003: * $Revision: 1.6 $
004: * $Date: 2001/07/22 20:25:14 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.catalina.util;
065:
066: import java.io.IOException;
067: import java.io.Writer;
068:
069: /**
070: * XMLWriter helper class.
071: *
072: * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
073: */
074: public class XMLWriter {
075:
076: // -------------------------------------------------------------- Constants
077:
078: /**
079: * Opening tag.
080: */
081: public static final int OPENING = 0;
082:
083: /**
084: * Closing tag.
085: */
086: public static final int CLOSING = 1;
087:
088: /**
089: * Element with no content.
090: */
091: public static final int NO_CONTENT = 2;
092:
093: // ----------------------------------------------------- Instance Variables
094:
095: /**
096: * Buffer.
097: */
098: protected StringBuffer buffer = new StringBuffer();
099:
100: /**
101: * Writer.
102: */
103: protected Writer writer = null;
104:
105: // ----------------------------------------------------------- Constructors
106:
107: /**
108: * Constructor.
109: */
110: public XMLWriter() {
111: }
112:
113: /**
114: * Constructor.
115: */
116: public XMLWriter(Writer writer) {
117: this .writer = writer;
118: }
119:
120: // --------------------------------------------------------- Public Methods
121:
122: /**
123: * Retrieve generated XML.
124: *
125: * @return String containing the generated XML
126: */
127: public String toString() {
128: return buffer.toString();
129: }
130:
131: /**
132: * Write property to the XML.
133: *
134: * @param namespace Namespace
135: * @param namespaceInfo Namespace info
136: * @param name Property name
137: * @param value Property value
138: */
139: public void writeProperty(String namespace, String namespaceInfo,
140: String name, String value) {
141: writeElement(namespace, namespaceInfo, name, OPENING);
142: buffer.append(value);
143: writeElement(namespace, namespaceInfo, name, CLOSING);
144:
145: }
146:
147: /**
148: * Write property to the XML.
149: *
150: * @param namespace Namespace
151: * @param name Property name
152: * @param value Property value
153: */
154: public void writeProperty(String namespace, String name,
155: String value) {
156: writeElement(namespace, name, OPENING);
157: buffer.append(value);
158: writeElement(namespace, name, CLOSING);
159: }
160:
161: /**
162: * Write property to the XML.
163: *
164: * @param namespace Namespace
165: * @param name Property name
166: */
167: public void writeProperty(String namespace, String name) {
168: writeElement(namespace, name, NO_CONTENT);
169: }
170:
171: /**
172: * Write an element.
173: *
174: * @param name Element name
175: * @param namespace Namespace abbreviation
176: * @param type Element type
177: */
178: public void writeElement(String namespace, String name, int type) {
179: writeElement(namespace, null, name, type);
180: }
181:
182: /**
183: * Write an element.
184: *
185: * @param namespace Namespace abbreviation
186: * @param namespaceInfo Namespace info
187: * @param name Element name
188: * @param type Element type
189: */
190: public void writeElement(String namespace, String namespaceInfo,
191: String name, int type) {
192: if ((namespace != null) && (namespace.length() > 0)) {
193: switch (type) {
194: case OPENING:
195: if (namespaceInfo != null) {
196: buffer.append("<" + namespace + ":" + name
197: + " xmlns:" + namespace + "=\""
198: + namespaceInfo + "\">");
199: } else {
200: buffer.append("<" + namespace + ":" + name + ">");
201: }
202: break;
203: case CLOSING:
204: buffer.append("</" + namespace + ":" + name + ">\n");
205: break;
206: case NO_CONTENT:
207: default:
208: if (namespaceInfo != null) {
209: buffer.append("<" + namespace + ":" + name
210: + " xmlns:" + namespace + "=\""
211: + namespaceInfo + "\"/>");
212: } else {
213: buffer.append("<" + namespace + ":" + name + "/>");
214: }
215: break;
216: }
217: } else {
218: switch (type) {
219: case OPENING:
220: buffer.append("<" + name + ">");
221: break;
222: case CLOSING:
223: buffer.append("</" + name + ">\n");
224: break;
225: case NO_CONTENT:
226: default:
227: buffer.append("<" + name + "/>");
228: break;
229: }
230: }
231: }
232:
233: /**
234: * Write text.
235: *
236: * @param text Text to append
237: */
238: public void writeText(String text) {
239: buffer.append(text);
240: }
241:
242: /**
243: * Write data.
244: *
245: * @param data Data to append
246: */
247: public void writeData(String data) {
248: buffer.append("<![CDATA[" + data + "]]>");
249: }
250:
251: /**
252: * Write XML Header.
253: */
254: public void writeXMLHeader() {
255: buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
256: }
257:
258: /**
259: * Send data and reinitializes buffer.
260: */
261: public void sendData() throws IOException {
262: if (writer != null) {
263: writer.write(buffer.toString());
264: buffer = new StringBuffer();
265: }
266: }
267:
268: }
|