001: package com.flexive.war.webdav.catalina;
002:
003: import java.io.IOException;
004: import java.io.Writer;
005:
006: /**
007: * Catalina sources cloned for packaging issues to the flexive source tree.
008: * Refactored to JDK 1.5 compatibility.
009: * Licensed under the Apache License, Version 2.0
010: * <p/>
011: * XMLWriter helper class.
012: *
013: * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
014: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
015: */
016: public class XMLWriter {
017:
018: // -------------------------------------------------------------- Constants
019:
020: /**
021: * Opening tag.
022: */
023: public static final int OPENING = 0;
024:
025: /**
026: * Closing tag.
027: */
028: public static final int CLOSING = 1;
029:
030: /**
031: * Element with no content.
032: */
033: public static final int NO_CONTENT = 2;
034:
035: // ----------------------------------------------------- Instance Variables
036:
037: /**
038: * Buffer.
039: */
040: protected StringBuilder buffer = new StringBuilder();
041:
042: /**
043: * Writer.
044: */
045: protected Writer writer = null;
046:
047: // ----------------------------------------------------------- Constructors
048:
049: /**
050: * Constructor.
051: */
052: public XMLWriter() {
053: }
054:
055: /**
056: * Constructor.
057: */
058: public XMLWriter(Writer writer) {
059: this .writer = writer;
060: }
061:
062: // --------------------------------------------------------- Public Methods
063:
064: /**
065: * Retrieve generated XML.
066: *
067: * @return String containing the generated XML
068: */
069: public String toString() {
070: return buffer.toString();
071: }
072:
073: /**
074: * Write property to the XML.
075: *
076: * @param namespace Namespace
077: * @param namespaceInfo Namespace info
078: * @param name Property name
079: * @param value Property value
080: */
081: public void writeProperty(String namespace, String namespaceInfo,
082: String name, String value) {
083: writeElement(namespace, namespaceInfo, name, OPENING);
084: buffer.append(value);
085: writeElement(namespace, namespaceInfo, name, CLOSING);
086:
087: }
088:
089: /**
090: * Write property to the XML.
091: *
092: * @param namespace Namespace
093: * @param name Property name
094: * @param value Property value
095: */
096: public void writeProperty(String namespace, String name,
097: String value) {
098: writeElement(namespace, name, OPENING);
099: buffer.append(value);
100: writeElement(namespace, name, CLOSING);
101: }
102:
103: /**
104: * Write property to the XML.
105: *
106: * @param namespace Namespace
107: * @param name Property name
108: */
109: public void writeProperty(String namespace, String name) {
110: writeElement(namespace, name, NO_CONTENT);
111: }
112:
113: /**
114: * Write an element.
115: *
116: * @param name Element name
117: * @param namespace Namespace abbreviation
118: * @param type Element type
119: */
120: public void writeElement(String namespace, String name, int type) {
121: writeElement(namespace, null, name, type);
122: }
123:
124: /**
125: * Write an element.
126: *
127: * @param namespace Namespace abbreviation
128: * @param namespaceInfo Namespace info
129: * @param name Element name
130: * @param type Element type
131: */
132: public void writeElement(String namespace, String namespaceInfo,
133: String name, int type) {
134: if ((namespace != null) && (namespace.length() > 0)) {
135: switch (type) {
136: case OPENING:
137: if (namespaceInfo != null) {
138: buffer.append("<").append(namespace).append(":")
139: .append(name).append(" xmlns:").append(
140: namespace).append("=\"").append(
141: namespaceInfo).append("\">");
142: } else {
143: buffer.append("<").append(namespace).append(":")
144: .append(name).append(">");
145: }
146: break;
147: case CLOSING:
148: buffer.append("</").append(namespace).append(":")
149: .append(name).append(">\n");
150: break;
151: case NO_CONTENT:
152: default:
153: if (namespaceInfo != null) {
154: buffer.append("<").append(namespace).append(":")
155: .append(name).append(" xmlns:").append(
156: namespace).append("=\"").append(
157: namespaceInfo).append("\"/>");
158: } else {
159: buffer.append("<").append(namespace).append(":")
160: .append(name).append("/>");
161: }
162: break;
163: }
164: } else {
165: switch (type) {
166: case OPENING:
167: buffer.append("<").append(name).append(">");
168: break;
169: case CLOSING:
170: buffer.append("</").append(name).append(">\n");
171: break;
172: case NO_CONTENT:
173: default:
174: buffer.append("<").append(name).append("/>");
175: break;
176: }
177: }
178: }
179:
180: /**
181: * Write text.
182: *
183: * @param text Text to append
184: */
185: public void writeText(String text) {
186: buffer.append(text);
187: }
188:
189: /**
190: * Write data.
191: *
192: * @param data Data to append
193: */
194: public void writeData(String data) {
195: buffer.append("<![CDATA[").append(data).append("]]>");
196: }
197:
198: /**
199: * Write XML Header.
200: */
201: public void writeXMLHeader() {
202: buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
203: }
204:
205: /**
206: * Send data and reinitializes buffer.
207: */
208: public void sendData() throws IOException {
209: if (writer != null) {
210: writer.write(buffer.toString());
211: buffer = new StringBuilder();
212: }
213: }
214:
215: }
|