1 23 24 package org.apache.webdav.lib.util; 25 26 import java.io.IOException ; 27 import java.io.Writer ; 28 29 32 public class XMLPrinter { 33 34 35 37 38 41 public static final int OPENING = 0; 42 43 44 47 public static final int CLOSING = 1; 48 49 50 53 public static final int NO_CONTENT = 2; 54 55 56 58 59 62 protected StringBuffer buffer = new StringBuffer (); 63 64 65 68 protected Writer writer = null; 69 70 71 73 74 77 public XMLPrinter() { 78 } 79 80 81 84 public XMLPrinter(Writer writer) { 85 this.writer = writer; 86 } 87 88 89 91 92 97 public String toString() { 98 return buffer.toString(); 99 } 100 101 102 110 public void writeProperty(String namespace, String namespaceInfo, 111 String name, String 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 129 public void writeProperty(String namespace, String namespaceInfo, 130 String name, String value) { 131 writeProperty(namespace, namespaceInfo, name, value, false); 132 } 133 134 135 142 public void writeProperty(String namespace, String name, String value) { 143 writeProperty(namespace, null, name, value); 144 } 145 146 147 153 public void writeProperty(String namespace, String name) { 154 writeElement(namespace, name, NO_CONTENT); 155 } 156 157 158 165 public void writeElement(String namespace, String name, int type) { 166 writeElement(namespace, null, name, type); 167 } 168 169 170 178 public void writeElement(String namespace, String namespaceInfo, 179 String 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 237 public void writeText(String text) { 238 buffer.append(text); 239 } 240 241 242 247 public void writeData(String data) { 248 buffer.append("<![CDATA["); 249 buffer.append(data); 250 buffer.append("]]>"); 251 } 252 253 254 257 public void writeXMLHeader() { 258 buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>"); 259 } 260 261 262 265 public void sendData() 266 throws IOException { 267 if (writer != null) { 268 writer.write(buffer.toString()); 269 buffer = new StringBuffer (); 270 } 271 } 272 273 274 } 275 | Popular Tags |