1 28 29 package net.n3.nanoxml; 30 31 import java.io.IOException ; 32 import java.io.OutputStream ; 33 import java.io.PrintWriter ; 34 import java.io.Writer ; 35 import java.util.Enumeration ; 36 37 46 public class XMLWriter 47 { 48 49 52 private PrintWriter writer; 53 54 59 public XMLWriter(Writer writer) 60 { 61 if (writer instanceof PrintWriter ) 62 { 63 this.writer = (PrintWriter ) writer; 64 } 65 else 66 { 67 this.writer = new PrintWriter (writer); 68 } 69 } 70 71 76 public XMLWriter(OutputStream stream) 77 { 78 this.writer = new PrintWriter (stream); 79 } 80 81 84 protected void finalize() throws Throwable 85 { 86 this.writer = null; 87 super.finalize(); 88 } 89 90 95 public void write(XMLElement xml) throws IOException 96 { 97 this.write(xml, true, 0); 98 } 99 100 106 public void write(XMLElement xml, boolean prettyPrint) throws IOException 107 { 108 this.write(xml, prettyPrint, 0); 109 } 110 111 118 public void write(XMLElement xml, boolean prettyPrint, int indent) throws IOException 119 { 120 if (prettyPrint) 121 { 122 for (int i = 0; i < indent; i++) 123 { 124 this.writer.print(' '); 125 } 126 } 127 128 if (xml.getName() == null) 129 { 130 if (xml.getContent() != null) 131 { 132 if (prettyPrint) 133 { 134 this.writeEncoded(xml.getContent().trim()); 135 writer.println(); 136 } 137 else 138 { 139 this.writeEncoded(xml.getContent()); 140 } 141 } 142 } 143 else 144 { 145 this.writer.print('<'); 146 this.writer.print(xml.getName()); 147 Enumeration enumeration = xml.enumerateAttributeNames(); 148 149 while (enumeration.hasMoreElements()) 150 { 151 String key = (String ) enumeration.nextElement(); 152 String value = xml.getAttribute(key); 153 this.writer.print(" " + key + "=\""); 154 this.writeEncoded(value); 155 this.writer.print('"'); 156 } 157 158 if ((xml.getContent() != null) && (xml.getContent().length() > 0)) 159 { 160 writer.print('>'); 161 this.writeEncoded(xml.getContent()); 162 writer.print("</" + xml.getName() + '>'); 163 164 if (prettyPrint) 165 { 166 writer.println(); 167 } 168 } 169 else if (xml.hasChildren()) 170 { 171 writer.print('>'); 172 173 if (prettyPrint) 174 { 175 writer.println(); 176 } 177 178 enumeration = xml.enumerateChildren(); 179 180 while (enumeration.hasMoreElements()) 181 { 182 XMLElement child = (XMLElement) enumeration.nextElement(); 183 this.write(child, prettyPrint, indent + 4); 184 } 185 186 if (prettyPrint) 187 { 188 for (int i = 0; i < indent; i++) 189 { 190 this.writer.print(' '); 191 } 192 } 193 194 this.writer.print("</" + xml.getName() + ">"); 195 196 if (prettyPrint) 197 { 198 writer.println(); 199 } 200 } 201 else 202 { 203 this.writer.print("/>"); 204 205 if (prettyPrint) 206 { 207 writer.println(); 208 } 209 } 210 } 211 212 this.writer.flush(); 213 } 214 215 220 private void writeEncoded(String str) 221 { 222 for (int i = 0; i < str.length(); i++) 223 { 224 char c = str.charAt(i); 225 226 switch (c) 227 { 228 case 0x0D: 229 case 0x0A: 230 this.writer.print(c); 231 break; 232 233 case '<': 234 this.writer.print("<"); 235 break; 236 237 case '>': 238 this.writer.print(">"); 239 break; 240 241 case '&': 242 this.writer.print("&"); 243 break; 244 245 case '\'': 246 this.writer.print("'"); 247 break; 248 249 case '"': 250 this.writer.print("""); 251 break; 252 253 default: 254 if ((c < ' ') || (c > 0x7E)) 255 { 256 this.writer.print("&#x"); 257 this.writer.print(Integer.toString(c, 16)); 258 this.writer.print(';'); 259 } 260 else 261 { 262 this.writer.print(c); 263 } 264 } 265 } 266 } 267 268 } 269 | Popular Tags |