001: /* FmtSelector.java */
002:
003: package org.quilt.reports;
004:
005: import java.io.File;
006: import java.io.FileOutputStream;
007: import java.io.OutputStream;
008: import java.util.Hashtable;
009:
010: import org.apache.tools.ant.BuildException;
011:
012: public class FmtSelector {
013:
014: private String classname;
015: private String extension = ".txt";
016: private OutputStream out = System.out;
017: private File outFile;
018: private boolean useFile = true;
019:
020: /**
021: * Ant-compatible method for creating formatters. Either
022: * the type or the classname must be specified in the build
023: * file or on the command line.
024: *
025: * @todo Modify Textui to accept 'types' on the command line.
026: * @todo Make sure extension corresponds to type.
027: *
028: * @return Formatter as an object
029: */
030: public Formatter createFormatter() throws BuildException {
031: // classname muat have been set either by setClassname or setType
032: if (classname == null) {
033: throw new BuildException("missing formatter class name");
034: }
035: // particularly paranoid way of determining whether class is
036: // suitable. First, is it actually a class?
037: Class fmtClass = null;
038: try {
039: fmtClass = Class.forName(classname);
040: } catch (ClassNotFoundException e) {
041: throw new BuildException(e);
042: }
043: // Get an instance of the class. Is it a Formatter?
044: Formatter fmt = null;
045: try {
046: fmt = (Formatter) fmtClass.newInstance();
047: } catch (IllegalAccessException e) {
048: throw new BuildException(e);
049: } catch (InstantiationException e) {
050: throw new BuildException(e);
051: } catch (ClassCastException e) {
052: throw new BuildException(classname + " is not a Formatter");
053: }
054: if (useFile && outFile != null) {
055: // we are supposed to write to a file
056: try {
057: // open it
058: out = new FileOutputStream(outFile);
059: } catch (java.io.IOException e) {
060: throw new BuildException(e);
061: }
062: }
063: // set the formatter to write to the output file (defaults to
064: // System.out)
065: fmt.setOutput(out);
066: return fmt;
067: }
068:
069: // //////////////////////////////////////////////////////////////
070: // SET TYPE
071: // //////////////////////////////////////////////////////////////
072: /** File name extensions associated with formatters. */
073: private static Hashtable extensions = null;
074: /** Table of abbreviations for formatters. */
075: private static Hashtable types = null;
076: {
077: types = new Hashtable();
078: types.put("brief", "org.quilt.reports.BriefFormatter");
079: types.put("plain", "org.quilt.reports.PlainFormatter");
080: types.put("summary", "org.quilt.reports.SummaryFormatter");
081: types.put("xml", "org.quilt.reports.XMLFormatter");
082:
083: // could save work by just using default except when type is "xml"
084: extensions = new Hashtable();
085: extensions.put("org.quilt.reports.BriefFormatter", ".txt");
086: extensions.put("org.quilt.reports.PlainFormatter", ".txt");
087: extensions.put("org.quilt.reports.SummaryFormatter", ".txt");
088: extensions.put("org.quilt.reports.XMLFormatter", ".xml");
089: }
090:
091: /**
092: * @return Whether the String passed is an alias for a standard formatter
093: */
094: public boolean isKnownType(String t) {
095: return types.containsKey(t);
096: }
097:
098: /**
099: * Routine called by Ant to set the formatter using an alias. This
100: * is an attribute of the formatter element. Example
101: * <pre>
102: *
103: * <formatter type="plain">
104: *
105: * </pre>
106: * @param t 'Type' of the formatter (brief, plain, summary, xml)
107: */
108: public void setType(String t) {
109: if (!types.containsKey(t)) {
110: throw new BuildException("unknown formatter type " + t);
111: }
112: classname = (String) types.get(t);
113: extension = (String) extensions.get(classname);
114: }
115:
116: // //////////////////////////////////////////////////////////////
117: // OTHER GET/SET METHODS
118: // //////////////////////////////////////////////////////////////
119: /** @return the class name of the formatter */
120: public String getClassname() {
121: return classname;
122: }
123:
124: /** Ant-compatible method for selecting formatter by class name. */
125: public void setClassname(String classname) {
126: this .classname = classname;
127: }
128:
129: /**
130: * Get the extension of the formatter output file. Defaults to
131: * ".txt". (This differs from the Ant JUnitTask, which requires
132: * that the extension be specified if the formatter is selected by
133: * class name.)
134: *
135: * @return The extension as a String.
136: */
137: public String getExtension() {
138: return extension;
139: }
140:
141: /**
142: * Ant-compatible method for setting the extension of the
143: * formatter output file.
144: *
145: * @see #setOutfile
146: */
147: public void setExtension(String ext) {
148: extension = ext;
149: }
150:
151: /**
152: * Ant-compatible method for setting the base name of the output file.
153: *
154: * @see #setExtension
155: */
156: public void setOutfile(File out) {
157: outFile = out;
158: }
159:
160: public void setOutput(OutputStream out) {
161: out = out;
162: }
163:
164: /** @return Whether output to a file is enabled. */
165: public boolean getUseFile() {
166: return useFile;
167: }
168:
169: /**
170: * Ant-compatible method for enabling output to a file. Defaults to
171: * true.
172: */
173: public void setUseFile(boolean b) {
174: this .useFile = b;
175: }
176:
177: /**
178: * Converts some class information to String format.
179: * @return The string.
180: */
181: public String toString() {
182: return "Formatter: " + classname + ", extension: " + extension;
183: }
184: }
|