KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webdav > lib > util > XMLPrinter


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/util/XMLPrinter.java,v 1.1 2004/08/02 15:45:49 unico Exp $
3  * $Revision: 1.1 $
4  * $Date: 2004/08/02 15:45:49 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.webdav.lib.util;
25
26 import java.io.IOException JavaDoc;
27 import java.io.Writer JavaDoc;
28
29 /**
30  * XMLPrinter helper class.
31  */

32 public class XMLPrinter {
33     
34     
35     // -------------------------------------------------------------- Constants
36

37     
38     /**
39      * Opening tag.
40      */

41     public static final int OPENING = 0;
42     
43     
44     /**
45      * Closing tag.
46      */

47     public static final int CLOSING = 1;
48     
49     
50     /**
51      * Element with no content.
52      */

53     public static final int NO_CONTENT = 2;
54     
55     
56     // ----------------------------------------------------- Instance Variables
57

58     
59     /**
60      * Buffer.
61      */

62     protected StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
63     
64     
65     /**
66      * Writer.
67      */

68     protected Writer JavaDoc writer = null;
69     
70     
71     // ----------------------------------------------------------- Constructors
72

73     
74     /**
75      * Constructor.
76      */

77     public XMLPrinter() {
78     }
79     
80     
81     /**
82      * Constructor.
83      */

84     public XMLPrinter(Writer JavaDoc writer) {
85         this.writer = writer;
86     }
87     
88     
89     // --------------------------------------------------------- Public Methods
90

91     
92     /**
93      * Retrieve generated XML.
94      *
95      * @return String containing the generated XML
96      */

97     public String JavaDoc toString() {
98         return buffer.toString();
99     }
100     
101     
102     /**
103      * Write property to the XML.
104      *
105      * @param namespace Namespace
106      * @param namespaceInfo Namespace info
107      * @param name Property name
108      * @param value Property value
109      */

110     public void writeProperty(String JavaDoc namespace, String JavaDoc namespaceInfo,
111                               String JavaDoc name, String JavaDoc value, boolean cdata) {
112         writeElement(namespace, namespaceInfo, name, OPENING);
113         if (cdata)
114             writeData(value);
115         else
116             writeText(value);
117         writeElement(namespace, namespaceInfo, name, CLOSING);
118     }
119     
120     
121     /**
122      * Write property to the XML.
123      *
124      * @param namespace Namespace
125      * @param namespaceInfo Namespace info
126      * @param name Property name
127      * @param value Property value
128      */

129     public void writeProperty(String JavaDoc namespace, String JavaDoc namespaceInfo,
130                               String JavaDoc name, String JavaDoc value) {
131         writeProperty(namespace, namespaceInfo, name, value, false);
132     }
133     
134     
135     /**
136      * Write property to the XML.
137      *
138      * @param namespace Namespace
139      * @param name Property name
140      * @param value Property value
141      */

142     public void writeProperty(String JavaDoc namespace, String JavaDoc name, String JavaDoc value) {
143         writeProperty(namespace, null, name, value);
144     }
145     
146     
147     /**
148      * Write property to the XML.
149      *
150      * @param namespace Namespace
151      * @param name Property name
152      */

153     public void writeProperty(String JavaDoc namespace, String JavaDoc name) {
154         writeElement(namespace, name, NO_CONTENT);
155     }
156     
157     
158     /**
159      * Write an element.
160      *
161      * @param name Element name
162      * @param namespace Namespace abbreviation
163      * @param type Element type
164      */

165     public void writeElement(String JavaDoc namespace, String JavaDoc name, int type) {
166         writeElement(namespace, null, name, type);
167     }
168     
169     
170     /**
171      * Write an element.
172      *
173      * @param namespace Namespace abbreviation
174      * @param namespaceInfo Namespace info
175      * @param name Element name
176      * @param type Element type
177      */

178     public void writeElement(String JavaDoc namespace, String JavaDoc namespaceInfo,
179                              String JavaDoc name, int type) {
180         if ((namespace != null) && (namespace.length() > 0)) {
181             switch (type) {
182             case OPENING:
183                 if ((namespaceInfo != null) && (namespaceInfo.length() > 0)) {
184                     buffer.append("<" + namespace + ":" + name + " xmlns:"
185                                   + namespace + "=\""
186                                   + namespaceInfo + "\">");
187                 } else {
188                     buffer.append("<" + namespace + ":" + name + ">");
189                 }
190                 break;
191             case CLOSING:
192                 buffer.append("</" + namespace + ":" + name + ">");
193                 break;
194             case NO_CONTENT:
195             default:
196                 if ((namespaceInfo != null) && (namespaceInfo.length() > 0)) {
197                     buffer.append("<" + namespace + ":" + name + " xmlns:"
198                                   + namespace + "=\""
199                                   + namespaceInfo + "\"/>");
200                 } else {
201                     buffer.append("<" + namespace + ":" + name + "/>");
202                 }
203                 break;
204             }
205         } else {
206             switch (type) {
207             case OPENING:
208                 if ((namespaceInfo != null) && (namespaceInfo.length()>0)) {
209                     buffer.append("<" + name + " xmlns=\"" + namespaceInfo
210                                   + "\">");
211                 } else {
212                     buffer.append("<" + name + ">");
213                 }
214                 break;
215             case CLOSING:
216                 buffer.append("</" + name + ">");
217                 break;
218             case NO_CONTENT:
219             default:
220                 if ((namespaceInfo != null) && (namespaceInfo.length()>0)) {
221                     buffer.append("<" + name + " xmlns=\"" + namespaceInfo
222                                   + "\"/>");
223                 } else {
224                     buffer.append("<" + name + "/>");
225                 }
226                 break;
227             }
228         }
229     }
230     
231     
232     /**
233      * Write text.
234      *
235      * @param text Text to append
236      */

237     public void writeText(String JavaDoc text) {
238         buffer.append(text);
239     }
240     
241     
242     /**
243      * Write data.
244      *
245      * @param data Data to append
246      */

247     public void writeData(String JavaDoc data) {
248         buffer.append("<![CDATA[");
249         buffer.append(data);
250         buffer.append("]]>");
251     }
252     
253     
254     /**
255      * Write XML Header.
256      */

257     public void writeXMLHeader() {
258         buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
259     }
260     
261     
262     /**
263      * Send data and reinitializes buffer.
264      */

265     public void sendData()
266         throws IOException JavaDoc {
267         if (writer != null) {
268             writer.write(buffer.toString());
269             buffer = new StringBuffer JavaDoc();
270         }
271     }
272     
273     
274 }
275
Popular Tags