/src/org/apache/webdav/lib/util/XMLPrinter.java
Java | 279 lines | 115 code | 27 blank | 137 comment | 27 complexity | 75ff86a35731dc7ca3f4e9196b36ccab MD5 | raw file
1/* 2 * $Header$ 3 * $Revision:207563 $ 4 * $Date:2004-08-03 01:45:51 +1000 (Tue, 03 Aug 2004) $ 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 24package org.apache.webdav.lib.util; 25 26import java.io.IOException; 27import java.io.Writer; 28 29/** 30 * XMLPrinter helper class. 31 */ 32public class XMLPrinter { 33 34 // -------------------------------------------------------------- Constants 35 36 /** 37 * Opening tag. 38 */ 39 public static final int OPENING = 0; 40 41 /** 42 * Closing tag. 43 */ 44 public static final int CLOSING = 1; 45 46 /** 47 * Element with no content. 48 */ 49 public static final int NO_CONTENT = 2; 50 51 // ----------------------------------------------------- Instance Variables 52 53 /** 54 * Buffer. 55 */ 56 protected StringBuffer buffer = new StringBuffer(); 57 58 /** 59 * Writer. 60 */ 61 protected Writer writer = null; 62 63 // ----------------------------------------------------------- Constructors 64 65 /** 66 * Constructor. 67 */ 68 public XMLPrinter() { 69 } 70 71 /** 72 * Constructor. 73 */ 74 public XMLPrinter(Writer writer) { 75 this.writer = writer; 76 } 77 78 // --------------------------------------------------------- Public Methods 79 80 /** 81 * Retrieve generated XML. 82 * 83 * @return String containing the generated XML 84 */ 85 public String toString() { 86 return buffer.toString(); 87 } 88 89 /** 90 * Write property to the XML. 91 * 92 * @param namespace 93 * Namespace 94 * @param namespaceInfo 95 * Namespace info 96 * @param name 97 * Property name 98 * @param value 99 * Property value 100 */ 101 public void writeProperty(String namespace, String namespaceInfo, 102 String name, String value, boolean cdata) { 103 writeElement(namespace, namespaceInfo, name, OPENING); 104 if (cdata) 105 writeData(value); 106 else 107 writeText(value); 108 writeElement(namespace, namespaceInfo, name, CLOSING); 109 } 110 111 /** 112 * Write property to the XML. 113 * 114 * @param namespace 115 * Namespace 116 * @param namespaceInfo 117 * Namespace info 118 * @param name 119 * Property name 120 * @param value 121 * Property value 122 */ 123 public void writeProperty(String namespace, String namespaceInfo, 124 String name, String value) { 125 writeProperty(namespace, namespaceInfo, name, value, false); 126 } 127 128 /** 129 * Write property to the XML. 130 * 131 * @param namespace 132 * Namespace 133 * @param name 134 * Property name 135 * @param value 136 * Property value 137 */ 138 public void writeProperty(String namespace, String name, String value) { 139 writeProperty(namespace, null, name, value); 140 } 141 142 /** 143 * Write property to the XML. 144 * 145 * @param namespace 146 * Namespace 147 * @param name 148 * Property name 149 */ 150 public void writeProperty(String namespace, String name) { 151 writeElement(namespace, name, NO_CONTENT); 152 } 153 154 /** 155 * Write an element. 156 * 157 * @param name 158 * Element name 159 * @param namespace 160 * Namespace abbreviation 161 * @param type 162 * Element type 163 */ 164 public void writeElement(String namespace, String name, int type) { 165 writeElement(namespace, null, name, type); 166 } 167 168 public void writeElement(String namespace, String namespaceInfo, 169 String name, int type) { 170 writeElement(namespace, namespaceInfo, name, type, null); 171 } 172 173 /** 174 * Write an element. 175 * 176 * @param namespace 177 * Namespace abbreviation 178 * @param namespaceInfo 179 * Namespace info 180 * @param name 181 * Element name 182 * @param type 183 * Element type 184 */ 185 public void writeElement(String namespace, String namespaceInfo, 186 String name, int type, String definitionNamespace) { 187 if (definitionNamespace == null) 188 definitionNamespace = namespace; 189 if ((namespace != null) && (namespace.length() > 0)) { 190 switch (type) { 191 case OPENING: 192 if ((namespaceInfo != null) && (namespaceInfo.length() > 0)) { 193 buffer.append("<" + namespace + ":" + name + " xmlns:" 194 + definitionNamespace + "=\"" + namespaceInfo 195 + "\">"); 196 } else { 197 buffer.append("<" + namespace + ":" + name + ">"); 198 } 199 break; 200 case CLOSING: 201 buffer.append("</" + namespace + ":" + name + ">"); 202 break; 203 case NO_CONTENT: 204 default: 205 if ((namespaceInfo != null) && (namespaceInfo.length() > 0)) { 206 buffer.append("<" + namespace + ":" + name + " xmlns:" 207 + definitionNamespace + "=\"" + namespaceInfo 208 + "\"/>"); 209 } else { 210 buffer.append("<" + namespace + ":" + name + "/>"); 211 } 212 break; 213 } 214 } else { 215 switch (type) { 216 case OPENING: 217 if ((namespaceInfo != null) && (namespaceInfo.length() > 0)) { 218 buffer.append("<" + name + " xmlns=\"" + namespaceInfo 219 + "\">"); 220 } else { 221 buffer.append("<" + name + ">"); 222 } 223 break; 224 case CLOSING: 225 buffer.append("</" + name + ">"); 226 break; 227 case NO_CONTENT: 228 default: 229 if ((namespaceInfo != null) && (namespaceInfo.length() > 0)) { 230 buffer.append("<" + name + " xmlns=\"" + namespaceInfo 231 + "\"/>"); 232 } else { 233 buffer.append("<" + name + "/>"); 234 } 235 break; 236 } 237 } 238 } 239 240 /** 241 * Write text. 242 * 243 * @param text 244 * Text to append 245 */ 246 public void writeText(String text) { 247 buffer.append(text); 248 } 249 250 /** 251 * Write data. 252 * 253 * @param data 254 * Data to append 255 */ 256 public void writeData(String data) { 257 buffer.append("<![CDATA["); 258 buffer.append(data); 259 buffer.append("]]>"); 260 } 261 262 /** 263 * Write XML Header. 264 */ 265 public void writeXMLHeader() { 266 buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>"); 267 } 268 269 /** 270 * Send data and reinitializes buffer. 271 */ 272 public void sendData() throws IOException { 273 if (writer != null) { 274 writer.write(buffer.toString()); 275 buffer = new StringBuffer(); 276 } 277 } 278 279}