001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.catalina.util;
019:
020: import java.io.IOException;
021: import java.io.Writer;
022:
023: /**
024: * XMLWriter helper class.
025: *
026: * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
027: */
028: public class XMLWriter {
029:
030: // -------------------------------------------------------------- Constants
031:
032: /**
033: * Opening tag.
034: */
035: public static final int OPENING = 0;
036:
037: /**
038: * Closing tag.
039: */
040: public static final int CLOSING = 1;
041:
042: /**
043: * Element with no content.
044: */
045: public static final int NO_CONTENT = 2;
046:
047: // ----------------------------------------------------- Instance Variables
048:
049: /**
050: * Buffer.
051: */
052: protected StringBuffer buffer = new StringBuffer();
053:
054: /**
055: * Writer.
056: */
057: protected Writer writer = null;
058:
059: // ----------------------------------------------------------- Constructors
060:
061: /**
062: * Constructor.
063: */
064: public XMLWriter() {
065: }
066:
067: /**
068: * Constructor.
069: */
070: public XMLWriter(Writer writer) {
071: this .writer = writer;
072: }
073:
074: // --------------------------------------------------------- Public Methods
075:
076: /**
077: * Retrieve generated XML.
078: *
079: * @return String containing the generated XML
080: */
081: public String toString() {
082: return buffer.toString();
083: }
084:
085: /**
086: * Write property to the XML.
087: *
088: * @param namespace Namespace
089: * @param namespaceInfo Namespace info
090: * @param name Property name
091: * @param value Property value
092: */
093: public void writeProperty(String namespace, String namespaceInfo,
094: String name, String value) {
095: writeElement(namespace, namespaceInfo, name, OPENING);
096: buffer.append(value);
097: writeElement(namespace, namespaceInfo, name, CLOSING);
098:
099: }
100:
101: /**
102: * Write property to the XML.
103: *
104: * @param namespace Namespace
105: * @param name Property name
106: * @param value Property value
107: */
108: public void writeProperty(String namespace, String name,
109: String value) {
110: writeElement(namespace, name, OPENING);
111: buffer.append(value);
112: writeElement(namespace, name, CLOSING);
113: }
114:
115: /**
116: * Write property to the XML.
117: *
118: * @param namespace Namespace
119: * @param name Property name
120: */
121: public void writeProperty(String namespace, String name) {
122: writeElement(namespace, name, NO_CONTENT);
123: }
124:
125: /**
126: * Write an element.
127: *
128: * @param name Element name
129: * @param namespace Namespace abbreviation
130: * @param type Element type
131: */
132: public void writeElement(String namespace, String name, int type) {
133: writeElement(namespace, null, name, type);
134: }
135:
136: /**
137: * Write an element.
138: *
139: * @param namespace Namespace abbreviation
140: * @param namespaceInfo Namespace info
141: * @param name Element name
142: * @param type Element type
143: */
144: public void writeElement(String namespace, String namespaceInfo,
145: String name, int type) {
146: if ((namespace != null) && (namespace.length() > 0)) {
147: switch (type) {
148: case OPENING:
149: if (namespaceInfo != null) {
150: buffer.append("<" + namespace + ":" + name
151: + " xmlns:" + namespace + "=\""
152: + namespaceInfo + "\">");
153: } else {
154: buffer.append("<" + namespace + ":" + name + ">");
155: }
156: break;
157: case CLOSING:
158: buffer.append("</" + namespace + ":" + name + ">\n");
159: break;
160: case NO_CONTENT:
161: default:
162: if (namespaceInfo != null) {
163: buffer.append("<" + namespace + ":" + name
164: + " xmlns:" + namespace + "=\""
165: + namespaceInfo + "\"/>");
166: } else {
167: buffer.append("<" + namespace + ":" + name + "/>");
168: }
169: break;
170: }
171: } else {
172: switch (type) {
173: case OPENING:
174: buffer.append("<" + name + ">");
175: break;
176: case CLOSING:
177: buffer.append("</" + name + ">\n");
178: break;
179: case NO_CONTENT:
180: default:
181: buffer.append("<" + name + "/>");
182: break;
183: }
184: }
185: }
186:
187: /**
188: * Write text.
189: *
190: * @param text Text to append
191: */
192: public void writeText(String text) {
193: buffer.append(text);
194: }
195:
196: /**
197: * Write data.
198: *
199: * @param data Data to append
200: */
201: public void writeData(String data) {
202: buffer.append("<![CDATA[" + data + "]]>");
203: }
204:
205: /**
206: * Write XML Header.
207: */
208: public void writeXMLHeader() {
209: buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
210: }
211:
212: /**
213: * Send data and reinitializes buffer.
214: */
215: public void sendData() throws IOException {
216: if (writer != null) {
217: writer.write(buffer.toString());
218: buffer = new StringBuffer();
219: }
220: }
221:
222: }
|