KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > util > XMLWriter


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
18 package org.apache.catalina.util;
19
20 import java.io.IOException JavaDoc;
21 import java.io.Writer JavaDoc;
22
23 /**
24  * XMLWriter helper class.
25  *
26  * @author <a HREF="mailto:remm@apache.org">Remy Maucherat</a>
27  */

28 public class XMLWriter {
29
30
31     // -------------------------------------------------------------- Constants
32

33
34     /**
35      * Opening tag.
36      */

37     public static final int OPENING = 0;
38
39
40     /**
41      * Closing tag.
42      */

43     public static final int CLOSING = 1;
44
45
46     /**
47      * Element with no content.
48      */

49     public static final int NO_CONTENT = 2;
50
51
52     // ----------------------------------------------------- Instance Variables
53

54
55     /**
56      * Buffer.
57      */

58     protected StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
59
60
61     /**
62      * Writer.
63      */

64     protected Writer JavaDoc writer = null;
65
66
67     // ----------------------------------------------------------- Constructors
68

69
70     /**
71      * Constructor.
72      */

73     public XMLWriter() {
74     }
75
76
77     /**
78      * Constructor.
79      */

80     public XMLWriter(Writer JavaDoc writer) {
81         this.writer = writer;
82     }
83
84
85     // --------------------------------------------------------- Public Methods
86

87
88     /**
89      * Retrieve generated XML.
90      *
91      * @return String containing the generated XML
92      */

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

106     public void writeProperty(String JavaDoc namespace, String JavaDoc namespaceInfo,
107                               String JavaDoc name, String JavaDoc value) {
108         writeElement(namespace, namespaceInfo, name, OPENING);
109         buffer.append(value);
110         writeElement(namespace, namespaceInfo, name, CLOSING);
111
112     }
113
114
115     /**
116      * Write property to the XML.
117      *
118      * @param namespace Namespace
119      * @param name Property name
120      * @param value Property value
121      */

122     public void writeProperty(String JavaDoc namespace, String JavaDoc name, String JavaDoc value) {
123         writeElement(namespace, name, OPENING);
124         buffer.append(value);
125         writeElement(namespace, name, CLOSING);
126     }
127
128
129     /**
130      * Write property to the XML.
131      *
132      * @param namespace Namespace
133      * @param name Property name
134      */

135     public void writeProperty(String JavaDoc namespace, String JavaDoc name) {
136         writeElement(namespace, name, NO_CONTENT);
137     }
138
139
140     /**
141      * Write an element.
142      *
143      * @param name Element name
144      * @param namespace Namespace abbreviation
145      * @param type Element type
146      */

147     public void writeElement(String JavaDoc namespace, String JavaDoc name, int type) {
148         writeElement(namespace, null, name, type);
149     }
150
151
152     /**
153      * Write an element.
154      *
155      * @param namespace Namespace abbreviation
156      * @param namespaceInfo Namespace info
157      * @param name Element name
158      * @param type Element type
159      */

160     public void writeElement(String JavaDoc namespace, String JavaDoc namespaceInfo,
161                              String JavaDoc name, int type) {
162         if ((namespace != null) && (namespace.length() > 0)) {
163             switch (type) {
164             case OPENING:
165                 if (namespaceInfo != null) {
166                     buffer.append("<" + namespace + ":" + name + " xmlns:"
167                                   + namespace + "=\""
168                                   + namespaceInfo + "\">");
169                 } else {
170                     buffer.append("<" + namespace + ":" + name + ">");
171                 }
172                 break;
173             case CLOSING:
174                 buffer.append("</" + namespace + ":" + name + ">\n");
175                 break;
176             case NO_CONTENT:
177             default:
178                 if (namespaceInfo != null) {
179                     buffer.append("<" + namespace + ":" + name + " xmlns:"
180                                   + namespace + "=\""
181                                   + namespaceInfo + "\"/>");
182                 } else {
183                     buffer.append("<" + namespace + ":" + name + "/>");
184                 }
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     /**
205      * Write text.
206      *
207      * @param text Text to append
208      */

209     public void writeText(String JavaDoc text) {
210         buffer.append(text);
211     }
212
213
214     /**
215      * Write data.
216      *
217      * @param data Data to append
218      */

219     public void writeData(String JavaDoc data) {
220         buffer.append("<![CDATA[" + data + "]]>");
221     }
222
223
224     /**
225      * Write XML Header.
226      */

227     public void writeXMLHeader() {
228         buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
229     }
230
231
232     /**
233      * Send data and reinitializes buffer.
234      */

235     public void sendData()
236         throws IOException JavaDoc {
237         if (writer != null) {
238             writer.write(buffer.toString());
239             buffer = new StringBuffer JavaDoc();
240         }
241     }
242
243
244 }
245
Popular Tags