Add a temporary hard-coded notice for the extended token validity period. This should...
[pithos] / gss / src / gr / ebs / gss / server / webdav / XMLWriter.java
1 /*
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 package gr.ebs.gss.server.webdav;
18
19 import java.io.IOException;
20 import java.io.Writer;
21
22 /**
23  * XMLWriter helper class.
24  *
25  * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
26  */
27 public class XMLWriter {
28
29
30     // -------------------------------------------------------------- Constants
31
32
33     /**
34      * Opening tag.
35      */
36     public static final int OPENING = 0;
37
38
39     /**
40      * Closing tag.
41      */
42     public static final int CLOSING = 1;
43
44
45     /**
46      * Element with no content.
47      */
48     public static final int NO_CONTENT = 2;
49
50
51     // ----------------------------------------------------- Instance Variables
52
53
54     /**
55      * Buffer.
56      */
57     protected StringBuffer buffer = new StringBuffer();
58
59
60     /**
61      * Writer.
62      */
63     protected Writer writer = null;
64
65
66     // ----------------------------------------------------------- Constructors
67
68
69     /**
70      * Constructor.
71      */
72     public XMLWriter() {
73     }
74
75
76     /**
77      * Constructor.
78      *
79      * @param theWriter the provided Writer
80      */
81     public XMLWriter(Writer theWriter) {
82         writer = theWriter;
83     }
84
85
86     // --------------------------------------------------------- Public Methods
87
88
89     /**
90      * Retrieve generated XML.
91      *
92      * @return String containing the generated XML
93      */
94     @Override
95         public String toString() {
96         return buffer.toString();
97     }
98
99
100     /**
101      * Write property to the XML.
102      *
103      * @param namespace Namespace
104      * @param namespaceInfo Namespace info
105      * @param name Property name
106      * @param value Property value
107      */
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);
113
114     }
115
116
117     /**
118      * Write property to the XML.
119      *
120      * @param namespace Namespace
121      * @param name Property name
122      * @param value Property value
123      */
124     public void writeProperty(String namespace, String name, String value) {
125         writeElement(namespace, name, OPENING);
126         buffer.append(value);
127         writeElement(namespace, name, CLOSING);
128     }
129
130
131     /**
132      * Write property to the XML.
133      *
134      * @param namespace Namespace
135      * @param name Property name
136      */
137     public void writeProperty(String namespace, String name) {
138         writeElement(namespace, name, NO_CONTENT);
139     }
140
141
142     /**
143      * Write an element.
144      *
145      * @param name Element name
146      * @param namespace Namespace abbreviation
147      * @param type Element type
148      */
149     public void writeElement(String namespace, String name, int type) {
150         writeElement(namespace, null, name, type);
151     }
152
153
154     /**
155      * Write an element.
156      *
157      * @param namespace Namespace abbreviation
158      * @param namespaceInfo Namespace info
159      * @param name Element name
160      * @param type Element type
161      */
162     public void writeElement(String namespace, String namespaceInfo,
163                              String name, int type) {
164         if (namespace != null && namespace.length() > 0)
165                         switch (type) {
166             case OPENING:
167                 if (namespaceInfo != null)
168                                         buffer.append("<" + namespace + ":" + name + " xmlns:"
169                                   + namespace + "=\""
170                                   + namespaceInfo + "\">");
171                                 else
172                                         buffer.append("<" + namespace + ":" + name + ">");
173                 break;
174             case CLOSING:
175                 buffer.append("</" + namespace + ":" + name + ">\n");
176                 break;
177             case NO_CONTENT:
178             default:
179                 if (namespaceInfo != null)
180                                         buffer.append("<" + namespace + ":" + name + " xmlns:"
181                                   + namespace + "=\""
182                                   + namespaceInfo + "\"/>");
183                                 else
184                                         buffer.append("<" + namespace + ":" + name + "/>");
185                 break;
186             }
187                 else
188                         switch (type) {
189             case OPENING:
190                 buffer.append("<" + name + ">");
191                 break;
192             case CLOSING:
193                 buffer.append("</" + name + ">\n");
194                 break;
195             case NO_CONTENT:
196             default:
197                 buffer.append("<" + name + "/>");
198                 break;
199             }
200     }
201
202
203     /**
204      * Write text.
205      *
206      * @param text Text to append
207      */
208     public void writeText(String text) {
209         buffer.append(text);
210     }
211
212
213     /**
214      * Write data.
215      *
216      * @param data Data to append
217      */
218     public void writeData(String data) {
219         buffer.append("<![CDATA[" + data + "]]>");
220     }
221
222
223     /**
224      * Write XML Header.
225      */
226     public void writeXMLHeader() {
227         buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
228     }
229
230
231     /**
232      * Send data and reinitializes buffer.
233      * @throws IOException
234      */
235     public void sendData() throws IOException {
236         if (writer != null) {
237             writer.write(buffer.toString());
238             buffer = new StringBuffer();
239         }
240     }
241
242
243 }