1 58 package org.apache.ecs.xml; 59 60 import java.io.Serializable ; 61 import java.io.OutputStream ; 62 import java.io.PrintWriter ; 63 import java.io.IOException ; 64 import java.util.*; 65 66 import org.apache.ecs.MultiPartElement; 67 import org.apache.ecs.ConcreteElement; 68 import org.apache.ecs.xml.XML; 69 import org.apache.ecs.xml.PI; 70 71 81 public class XMLDocument implements Serializable , Cloneable { 82 83 84 private static final float DEFAULT_XML_VERSION = 1.0f; 85 86 87 private String versionDecl; 88 89 90 private Vector prolog; 91 92 93 private XML content; 94 95 96 private String codeset = null; 97 98 104 public XMLDocument() { 105 this(DEFAULT_XML_VERSION, true); 106 } 107 108 116 public XMLDocument(double version) { 117 this(version, true); 118 } 119 120 129 public XMLDocument(double version, boolean standalone) { 130 prolog = new Vector(2); 131 StringBuffer versionStr = new StringBuffer (); 132 versionStr.append("<?xml version=\""); 133 versionStr.append(version); 134 versionStr.append("\" standalone=\""); 135 if (standalone) 136 versionStr.append("yes\"?>"); 137 else 138 versionStr.append("no\"?>\n"); 139 140 this.versionDecl = versionStr.toString(); 141 142 150 162 } 163 164 175 public XMLDocument(double version, boolean standalone, String codeset) { 176 this(version, standalone); 177 setCodeset(codeset); 178 } 179 180 186 public void setCodeset(String codeset) { 187 this.codeset = codeset; 188 } 189 190 195 public String getCodeset() { 196 return codeset; 197 } 198 199 205 public XMLDocument addStylesheet(String href, String type) { 206 PI pi = new PI(); 207 pi.setTarget("xml-stylesheet") 208 .addInstruction("href", href) 209 .addInstruction("type", type); 210 prolog.addElement(pi); 211 212 return(this); 213 } 214 215 221 public XMLDocument addStylesheet(String href) { 222 return addStylesheet(href, "text/xsl"); 223 } 224 225 230 public XMLDocument addToProlog(ConcreteElement element) { 231 prolog.addElement(element); 232 return(this); 233 } 234 235 243 public XMLDocument addElement(XML element) { 244 if (content == null) 245 content = element; 246 else 247 content.addElement(element); 248 249 return(this); 250 } 251 252 257 public void output(OutputStream out) 258 { 259 263 try { 264 out.write(versionDecl.getBytes()); 265 } catch (Exception e) { } 266 267 for (int i=0; i<prolog.size(); i++) { 268 ConcreteElement e = (ConcreteElement)prolog.elementAt(i); 269 e.output(out); 270 try 274 { 275 out.write('\n'); 276 } 277 catch(IOException ioe) 278 { 279 ioe.printStackTrace(new PrintWriter (out)); 280 } 281 } 282 283 if (content != null) 284 content.output(out); 285 } 286 287 292 public void output(PrintWriter out) 293 { 294 295 299 out.write(versionDecl); 300 301 for (int i=0; i<prolog.size(); i++) { 302 ConcreteElement e = (ConcreteElement)prolog.elementAt(i); 303 e.output(out); 304 out.println(); 307 } 308 309 if (content != null) 310 content.output(out); 311 } 312 313 318 public final String toString() { 319 StringBuffer retVal = new StringBuffer (); 320 321 if (codeset != null) { 322 for (int i=0; i<prolog.size(); i++) { 323 ConcreteElement e = (ConcreteElement)prolog.elementAt(i); 324 retVal.append(e.toString(getCodeset()) + "\n"); 325 } 326 327 if (content != null) 328 retVal.append(content.toString(getCodeset())); 329 } else { 330 for (int i=0; i<prolog.size(); i++) { 331 ConcreteElement e = (ConcreteElement)prolog.elementAt(i); 332 retVal.append(e.toString() + "\n"); 333 } 334 335 if (content != null) 336 retVal.append(content.toString()); 337 } 338 339 343 return versionDecl + retVal.toString(); 344 } 345 346 352 public final String toString(String codeset) { 353 StringBuffer retVal = new StringBuffer (); 354 355 for (int i=0; i<prolog.size(); i++) { 356 ConcreteElement e = (ConcreteElement)prolog.elementAt(i); 357 retVal.append(e.toString(getCodeset()) + "\n"); 358 } 359 if (content != null) 360 retVal.append(content.toString(getCodeset()) + "\n"); 361 362 366 return versionDecl + retVal.toString(); 367 } 368 369 374 public Object clone() { 375 return content.clone(); 376 } 377 378 } 379 380 | Popular Tags |