1 17 18 package org.apache.catalina.util; 19 20 import java.io.IOException ; 21 import java.io.Writer ; 22 23 28 public class XMLWriter { 29 30 31 33 34 37 public static final int OPENING = 0; 38 39 40 43 public static final int CLOSING = 1; 44 45 46 49 public static final int NO_CONTENT = 2; 50 51 52 54 55 58 protected StringBuffer buffer = new StringBuffer (); 59 60 61 64 protected Writer writer = null; 65 66 67 69 70 73 public XMLWriter() { 74 } 75 76 77 80 public XMLWriter(Writer writer) { 81 this.writer = writer; 82 } 83 84 85 87 88 93 public String toString() { 94 return buffer.toString(); 95 } 96 97 98 106 public void writeProperty(String namespace, String namespaceInfo, 107 String name, String value) { 108 writeElement(namespace, namespaceInfo, name, OPENING); 109 buffer.append(value); 110 writeElement(namespace, namespaceInfo, name, CLOSING); 111 112 } 113 114 115 122 public void writeProperty(String namespace, String name, String value) { 123 writeElement(namespace, name, OPENING); 124 buffer.append(value); 125 writeElement(namespace, name, CLOSING); 126 } 127 128 129 135 public void writeProperty(String namespace, String name) { 136 writeElement(namespace, name, NO_CONTENT); 137 } 138 139 140 147 public void writeElement(String namespace, String name, int type) { 148 writeElement(namespace, null, name, type); 149 } 150 151 152 160 public void writeElement(String namespace, String namespaceInfo, 161 String 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 209 public void writeText(String text) { 210 buffer.append(text); 211 } 212 213 214 219 public void writeData(String data) { 220 buffer.append("<![CDATA[" + data + "]]>"); 221 } 222 223 224 227 public void writeXMLHeader() { 228 buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"); 229 } 230 231 232 235 public void sendData() 236 throws IOException { 237 if (writer != null) { 238 writer.write(buffer.toString()); 239 buffer = new StringBuffer (); 240 } 241 } 242 243 244 } 245 | Popular Tags |