1 11 package org.eclipse.help.internal.webapp.servlet; 12 13 import java.io.*; 14 15 import org.eclipse.help.internal.base.util.*; 16 import org.eclipse.help.internal.webapp.*; 17 18 21 public class XMLGenerator { 22 private File outFile = null; 23 24 private PrintWriter out = null; 25 26 public int pad = 0; 27 28 private static final String invalidXML[] = { "&", ">", "<", "\"", "\'" }; 31 private static final String escapedXML[] = { 32 "&", ">", "<", """, "'" }; 34 37 public XMLGenerator(Writer writer) { 38 if (writer instanceof PrintWriter) 39 this.out = (PrintWriter) writer; 40 else 41 this.out = new PrintWriter(writer); 42 } 43 44 47 public XMLGenerator(File outFile) { 48 super(); 49 this.outFile = outFile; 50 try { 51 out = new PrintWriter(new BufferedWriter(new OutputStreamWriter( 52 new FileOutputStream(outFile), "UTF8")), false 54 ); 55 println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); } catch (IOException ioe) { 57 HelpWebappPlugin.logError("Error accessing file: " + outFile.getAbsolutePath() + "", ioe); } 60 } 61 62 public static String xmlEscape(String cdata) { 65 for (int i = 0; i < invalidXML.length; i++) 66 cdata = TString.change(cdata, invalidXML[i], escapedXML[i]); 67 return cdata; 68 } 69 70 public void close() { 71 out.flush(); 72 out.close(); 73 if (out.checkError()) 74 if (outFile != null) 75 HelpWebappPlugin.logError("Errors occurred generating file: " + outFile.getAbsolutePath() + "", null); out = null; 78 } 79 80 public void print(Object o) { 81 if (out != null) 82 out.print(o); 83 } 84 85 public void println(Object o) { 86 print(o); 87 print("\n"); } 89 90 public void printPad() { 91 for (int i = 0; i < pad; i++) 92 print(" "); } 94 } 95 | Popular Tags |