1 /* FmtSelector.java */
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 /***
21 * Ant-compatible method for creating formatters. Either
22 * the type or the classname must be specified in the build
23 * file or on the command line.
24 *
25 * @todo Modify Textui to accept 'types' on the command line.
26 * @todo Make sure extension corresponds to type.
27 *
28 * @return Formatter as an object
29 */
30 public Formatter createFormatter() throws BuildException {
31 // classname muat have been set either by setClassname or setType
32 if (classname == null) {
33 throw new BuildException("missing formatter class name");
34 }
35 // particularly paranoid way of determining whether class is
36 // suitable. First, is it actually a class?
37 Class fmtClass = null;
38 try {
39 fmtClass = Class.forName(classname);
40 } catch (ClassNotFoundException e) {
41 throw new BuildException(e);
42 }
43 // Get an instance of the class. Is it a Formatter?
44 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 // we are supposed to write to a file
56 try {
57 // open it
58 out = new FileOutputStream(outFile);
59 } catch (java.io.IOException e) {
60 throw new BuildException(e);
61 }
62 }
63 // set the formatter to write to the output file (defaults to
64 // System.out)
65 fmt.setOutput(out);
66 return fmt;
67 }
68 // //////////////////////////////////////////////////////////////
69 // SET TYPE
70 // //////////////////////////////////////////////////////////////
71 /*** File name extensions associated with formatters. */
72 private static Hashtable extensions = null;
73 /*** Table of abbreviations for formatters. */
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 // could save work by just using default except when type is "xml"
83 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 /***
90 * @return Whether the String passed is an alias for a standard formatter
91 */
92 public boolean isKnownType (String t) {
93 return types.containsKey(t);
94 }
95 /***
96 * Routine called by Ant to set the formatter using an alias. This
97 * is an attribute of the formatter element. Example
98 * <pre>
99 *
100 * <formatter type="plain">
101 *
102 * </pre>
103 * @param t 'Type' of the formatter (brief, plain, summary, xml)
104 */
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 // //////////////////////////////////////////////////////////////
114 // OTHER GET/SET METHODS
115 // //////////////////////////////////////////////////////////////
116 /*** @return the class name of the formatter */
117 public String getClassname() {
118 return classname;
119 }
120 /*** Ant-compatible method for selecting formatter by class name. */
121 public void setClassname(String classname) {
122 this.classname = classname;
123 }
124
125 /***
126 * Get the extension of the formatter output file. Defaults to
127 * ".txt". (This differs from the Ant JUnitTask, which requires
128 * that the extension be specified if the formatter is selected by
129 * class name.)
130 *
131 * @return The extension as a String.
132 */
133 public String getExtension() {
134 return extension;
135 }
136 /***
137 * Ant-compatible method for setting the extension of the
138 * formatter output file.
139 *
140 * @see #setOutfile
141 */
142 public void setExtension(String ext) {
143 extension = ext;
144 }
145 /***
146 * Ant-compatible method for setting the base name of the output file.
147 *
148 * @see #setExtension
149 */
150 public void setOutfile(File out) {
151 outFile = out;
152 }
153 public void setOutput(OutputStream out) {
154 out = out;
155 }
156
157 /*** @return Whether output to a file is enabled. */
158 public boolean getUseFile() {
159 return useFile;
160 }
161 /***
162 * Ant-compatible method for enabling output to a file. Defaults to
163 * true.
164 */
165 public void setUseFile(boolean b) {
166 this.useFile = b;
167 }
168 /***
169 * Converts some class information to String format.
170 * @return The string.
171 */
172 public String toString() {
173 return "Formatter: " + classname + ", extension: " + extension;
174 }
175 }
This page was automatically generated by Maven