1 2 3 package org.quilt.reports; 4 5 import java.io.File ; 6 import java.io.FileOutputStream ; 7 import java.io.OutputStream ; 8 import java.util.Hashtable ; 9 10 import org.apache.tools.ant.BuildException; 11 12 public class FmtSelector { 13 14 private String classname; 15 private String extension = ".txt"; 16 private OutputStream out = System.out; 17 private File outFile; 18 private boolean useFile = true; 19 20 30 public Formatter createFormatter() throws BuildException { 31 if (classname == null) { 33 throw new BuildException("missing formatter class name"); 34 } 35 Class fmtClass = null; 38 try { 39 fmtClass = Class.forName(classname); 40 } catch (ClassNotFoundException e) { 41 throw new BuildException(e); 42 } 43 Formatter fmt = null; 45 try { 46 fmt = (Formatter) fmtClass.newInstance(); 47 } catch (IllegalAccessException e) { 48 throw new BuildException(e); 49 } catch (InstantiationException e) { 50 throw new BuildException(e); 51 } catch (ClassCastException e) { 52 throw new BuildException(classname + " is not a Formatter"); 53 } 54 if (useFile && outFile != null) { 55 try { 57 out = new FileOutputStream (outFile); 59 } catch (java.io.IOException e) { 60 throw new BuildException(e); 61 } 62 } 63 fmt.setOutput(out); 66 return fmt; 67 } 68 72 private static Hashtable extensions = null; 73 74 private static Hashtable types = null; 75 { 76 types = new Hashtable (); 77 types.put ("brief", "org.quilt.reports.BriefFormatter"); 78 types.put ("plain", "org.quilt.reports.PlainFormatter"); 79 types.put ("summary", "org.quilt.reports.SummaryFormatter"); 80 types.put ("xml", "org.quilt.reports.XMLFormatter"); 81 82 extensions = new Hashtable (); 84 extensions.put ("org.quilt.reports.BriefFormatter", ".txt"); 85 extensions.put ("org.quilt.reports.PlainFormatter", ".txt"); 86 extensions.put ("org.quilt.reports.SummaryFormatter", ".txt"); 87 extensions.put ("org.quilt.reports.XMLFormatter", ".xml"); 88 } 89 92 public boolean isKnownType (String t) { 93 return types.containsKey(t); 94 } 95 105 public void setType (String t) { 106 if (! types.containsKey(t) ) { 107 throw new BuildException ("unknown formatter type " + t); 108 } 109 classname = (String ) types.get(t); 110 extension = (String ) extensions.get(classname); 111 } 112 113 117 public String getClassname() { 118 return classname; 119 } 120 121 public void setClassname(String classname) { 122 this.classname = classname; 123 } 124 125 133 public String getExtension() { 134 return extension; 135 } 136 142 public void setExtension(String ext) { 143 extension = ext; 144 } 145 150 public void setOutfile(File out) { 151 outFile = out; 152 } 153 public void setOutput(OutputStream out) { 154 out = out; 155 } 156 157 158 public boolean getUseFile() { 159 return useFile; 160 } 161 165 public void setUseFile(boolean b) { 166 this.useFile = b; 167 } 168 172 public String toString() { 173 return "Formatter: " + classname + ", extension: " + extension; 174 } 175 } 176 | Popular Tags |