2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 package gr.ebs.gss.server.webdav;
19 import java.io.IOException;
20 import java.io.Writer;
23 * XMLWriter helper class.
25 * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
27 public class XMLWriter {
30 // -------------------------------------------------------------- Constants
36 public static final int OPENING = 0;
42 public static final int CLOSING = 1;
46 * Element with no content.
48 public static final int NO_CONTENT = 2;
51 // ----------------------------------------------------- Instance Variables
57 protected StringBuffer buffer = new StringBuffer();
63 protected Writer writer = null;
66 // ----------------------------------------------------------- Constructors
79 * @param theWriter the provided Writer
81 public XMLWriter(Writer theWriter) {
86 // --------------------------------------------------------- Public Methods
90 * Retrieve generated XML.
92 * @return String containing the generated XML
95 public String toString() {
96 return buffer.toString();
101 * Write property to the XML.
103 * @param namespace Namespace
104 * @param namespaceInfo Namespace info
105 * @param name Property name
106 * @param value Property value
108 public void writeProperty(String namespace, String namespaceInfo,
109 String name, String value) {
110 writeElement(namespace, namespaceInfo, name, OPENING);
111 buffer.append(value);
112 writeElement(namespace, namespaceInfo, name, CLOSING);
118 * Write property to the XML.
120 * @param namespace Namespace
121 * @param name Property name
122 * @param value Property value
124 public void writeProperty(String namespace, String name, String value) {
125 writeElement(namespace, name, OPENING);
126 buffer.append(value);
127 writeElement(namespace, name, CLOSING);
132 * Write property to the XML.
134 * @param namespace Namespace
135 * @param name Property name
137 public void writeProperty(String namespace, String name) {
138 writeElement(namespace, name, NO_CONTENT);
145 * @param name Element name
146 * @param namespace Namespace abbreviation
147 * @param type Element type
149 public void writeElement(String namespace, String name, int type) {
150 writeElement(namespace, null, name, type);
157 * @param namespace Namespace abbreviation
158 * @param namespaceInfo Namespace info
159 * @param name Element name
160 * @param type Element type
162 public void writeElement(String namespace, String namespaceInfo,
163 String name, int type) {
164 if (namespace != null && namespace.length() > 0)
167 if (namespaceInfo != null)
168 buffer.append("<" + namespace + ":" + name + " xmlns:"
170 + namespaceInfo + "\">");
172 buffer.append("<" + namespace + ":" + name + ">");
175 buffer.append("</" + namespace + ":" + name + ">\n");
179 if (namespaceInfo != null)
180 buffer.append("<" + namespace + ":" + name + " xmlns:"
182 + namespaceInfo + "\"/>");
184 buffer.append("<" + namespace + ":" + name + "/>");
190 buffer.append("<" + name + ">");
193 buffer.append("</" + name + ">\n");
197 buffer.append("<" + name + "/>");
206 * @param text Text to append
208 public void writeText(String text) {
216 * @param data Data to append
218 public void writeData(String data) {
219 buffer.append("<![CDATA[" + data + "]]>");
226 public void writeXMLHeader() {
227 buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
232 * Send data and reinitializes buffer.
233 * @throws IOException
235 public void sendData() throws IOException {
236 if (writer != null) {
237 writer.write(buffer.toString());
238 buffer = new StringBuffer();