KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > gulden > application > beautyj > BeautyJ


1 /*
2  * Project: BeautyJ - Customizable Java Source Code Transformer
3  * Class: de.gulden.application.beautyj.BeautyJ
4  * Version: 1.1
5  *
6  * Date: 2004-09-29
7  *
8  * Note: Contains auto-generated Javadoc comments created by BeautyJ.
9  *
10  * This is licensed under the GNU General Public License (GPL)
11  * and comes with NO WARRANTY. See file license.txt for details.
12  *
13  * Author: Jens Gulden
14  * Email: beautyj@jensgulden.de
15  */

16
17 package de.gulden.application.beautyj;
18
19 import de.gulden.framework.amoda.environment.commandline.*;
20 import de.gulden.framework.amoda.generic.core.*;
21 import de.gulden.framework.amoda.generic.option.*;
22 import de.gulden.framework.amoda.generic.data.GenericValue;
23 import de.gulden.framework.amoda.model.core.ApplicationEnvironment;
24 import de.gulden.framework.amoda.model.data.*;
25 import de.gulden.framework.amoda.model.option.*;
26 import de.gulden.util.javasource.LogPerformer;
27 import de.gulden.util.javasource.sourclet.*;
28 import de.gulden.util.javasource.jjt.ParseException;
29 import java.io.*;
30 import java.util.*;
31
32 /**
33  * The BeautyJ main program.
34  * As this class extends CommandLineApplication, which itself
35  * extends GenericApplication, this program can be used as
36  * ANT-task, too. This feature is provided by the wrapper class
37  * <code>de.gulden.application.beautyj.ant.Task</code>.
38  *
39  * @author Jens Gulden
40  * @version 1.1
41  * @see de.gulden.framework.amoda.environment.commandline.CommandLineApplication
42  * @see de.gulden.framework.amoda.environment.ant.ANTTaskApplicationWrapper
43  * @see de.gulden.framework.amoda.model.core.Application
44  * @see de.gulden.application.beautyj.ant.Task
45  */

46 public class BeautyJ extends CommandLineApplication implements SourcletOptions, LogPerformer {
47
48     // ------------------------------------------------------------------------
49
// --- final static fields ---
50
// ------------------------------------------------------------------------
51

52     /**
53      * Constant OPTION_SOURCLET.
54      */

55     public static final String JavaDoc OPTION_SOURCLET = "sourclet";
56
57     /**
58      * Constant OPTION_INPUT.
59      */

60     public static final String JavaDoc OPTION_INPUT = "input";
61
62     /**
63      * Constant OPTION_OUTDIR.
64      */

65     public static final String JavaDoc OPTION_OUTDIR = "output-directory";
66
67     /**
68      * Constant OPTION_XML_INPUT.
69      */

70     public static final String JavaDoc OPTION_XML_INPUT = "xml.in";
71
72     /**
73      * Constant OPTION_XML_VALIDATE.
74      */

75     public static final String JavaDoc OPTION_XML_VALIDATE = "xml.validate";
76
77     /**
78      * Constant OPTION_XML_OUTPUT.
79      */

80     public static final String JavaDoc OPTION_XML_OUTPUT = "xml.out";
81
82     /**
83      * Constant OPTION_XML_DOCTYPE.
84      */

85     public static final String JavaDoc OPTION_XML_DOCTYPE = "xml.doctype";
86
87     /**
88      * Constant MESSAGE_SUCCESS.
89      */

90     public static final String JavaDoc MESSAGE_SUCCESS = "message.success";
91
92     /**
93      * Constant MESSAGE_XML_CREATED.
94      */

95     public static final String JavaDoc MESSAGE_XML_CREATED = "message.xmlcreated";
96
97
98     // ------------------------------------------------------------------------
99
// --- field ---
100
// ------------------------------------------------------------------------
101

102     /**
103      * Cache for pre-parsed multi-value entries.
104      *
105      * @see #hasOption(String,String)
106      */

107     protected HashMap multiValueCache;
108
109
110     // ------------------------------------------------------------------------
111
// --- methods ---
112
// ------------------------------------------------------------------------
113

114     public void init(ApplicationEnvironment env) {
115         super.init(env);
116         if (getOptions().getBoolean("gui")) {
117             ((GenericOptionEntry)getOptions().getOptionEntry("input")).setSystem(false); // enable option "-input" (to be able to set one input file or diectory via options-dialog when running in auto-gui-mode)
118
}
119     }
120
121     /**
122      * <p>
123      * Run code transformation.
124      * </p>
125      * <p>
126      * Called by the application environment.
127      * </p>
128      */

129     public void perform() {
130         // no batch mode or interaction - just one single action
131
Sourclet sourclet=null;
132         Value[] params=getEnvironment().getInputValues();
133         File input=getOptions().getFile(OPTION_INPUT);
134         File outDir=getOptions().getFile(OPTION_OUTDIR);
135         File xmlIn=getOptions().getFile(OPTION_XML_INPUT);
136         File xmlOut=getOptions().getFile(OPTION_XML_OUTPUT);
137         multiValueCache = new HashMap();
138
139         if (xmlOut==null) { // skip Sourclet if generating XML
140
if (outDir==null) {
141                 error("output directory must be specified" + (getOptions().getBoolean("gui") ? "" : " using option '-d'"),null,true);
142             }
143             if (!outDir.isDirectory()) {
144                 error(outDir.getAbsolutePath()+" is not a valid output directory.",null,true);
145             }
146             sourclet = (Sourclet)getOptions().getClassInstance(OPTION_SOURCLET);
147             sourclet.init(this);
148         } else { // XML output
149
if (outDir!=null) {
150                 error("output directory cannot be specified when using XML output",null,true);
151             }
152         }
153
154         try {
155             de.gulden.util.javasource.Package pakkage=null;
156             de.gulden.util.javasource.SourceParser.verbose= this.isVerbose() || this.getOptions().getBoolean("gui");
157             de.gulden.util.javasource.SourceParser.logPerformer = this;
158             boolean test = this.getOptions().getBoolean("test");
159             File[] inFiles = null;
160             if (xmlIn!=null) { // get input from XML
161
if (params.length==0 && input==null) {
162                     InputStream in;
163                     if (!xmlIn.equals(new File(""))) {
164                         in=new FileInputStream(xmlIn);
165                     } else {
166                         in=System.in;
167                     }
168                     de.gulden.util.javasource.SourceParser.validateXML=isOption(OPTION_XML_VALIDATE);
169                     pakkage=de.gulden.util.javasource.SourceParser.parseXML(in);
170                     if (in instanceof FileInputStream) {
171                         in.close();
172                     }
173                 } else {
174                     error("input files or directories cannot be specified when using XML input",null,true);
175                 }
176             } else { // input from .java-files
177
if (input != null) {
178                     inFiles = new File[] {input};
179                 } else if (params.length>0) {
180                     inFiles=new File[params.length];
181                     for (int i=0;i<inFiles.length;i++) {
182                         inFiles[i]=params[i].getFile();
183                     }
184                 } else {
185                     error("nothing to do - please specify at least one input file or directory",null,true);
186                 }
187                 if (inFiles != null) {
188                     pakkage=de.gulden.util.javasource.SourceParser.parse(inFiles,null);
189                 }
190             }
191
192             if (!test) {
193                 if (xmlOut==null) {
194                     if (pakkage != null) {
195                         de.gulden.util.javasource.SourceParser.buildSource(pakkage, outDir, inFiles, sourclet); // build beautified sources
196
getMessage(MESSAGE_SUCCESS).perform();
197                     }
198                 } else { // XML output
199
de.gulden.util.javasource.SourceParser.includeXMLDoctype=isOption(OPTION_XML_DOCTYPE);
200                     org.w3c.dom.Document JavaDoc doc=de.gulden.util.javasource.SourceParser.buildXML(pakkage);
201                     OutputStream out;
202                     if (!xmlOut.equals(new File(""))) {
203                         out=new FileOutputStream(xmlOut);
204                     } else {
205                         out=System.out;
206                     }
207                     if ((out instanceof FileOutputStream)&&this.isVerbose()) {
208                         System.out.println("writing "+xmlOut.getAbsolutePath());
209                     }
210                     org.apache.xml.serialize.OutputFormat outputFormat=new org.apache.xml.serialize.OutputFormat();
211                     outputFormat.setIndenting(true);
212                     outputFormat.setPreserveSpace(true);
213                     org.apache.xml.serialize.XMLSerializer serializer=new org.apache.xml.serialize.XMLSerializer(out,outputFormat);
214                     org.apache.xml.serialize.DOMSerializer domSerializer=serializer.asDOMSerializer();
215                     domSerializer.serialize(doc);
216                     if (out instanceof FileOutputStream) {
217                         out.close();
218                     }
219                     getMessage(MESSAGE_XML_CREATED).perform();
220                 }
221             }
222         } catch (IOException ioe) {
223             error("an i/o exception occurred",ioe,true);
224         } catch (ParseException pe) {
225             error("Java parser error",pe,true);
226         } catch (org.xml.sax.SAXException JavaDoc se) {
227             error("XML parser error",se,true);
228         }
229     }
230
231     /**
232      * Tests whether the application is set to quiet-mode.
233      */

234     public boolean isQuiet() {
235         File xmlOut=getOptions().getFile(OPTION_XML_OUTPUT);
236         boolean xml_to_stdout=(xmlOut!=null)&&(xmlOut.equals(new File("")));
237         // always be quiet if xml output on stdout is generated
238
return super.isQuiet()||xml_to_stdout;
239     }
240
241     /**
242      * Returns a string option.
243      *
244      * @param name option name
245      */

246     public String JavaDoc getOption(String JavaDoc name) {
247         return getOptions().getString(name);
248     }
249
250     /**
251      * Returns an integer option.
252      *
253      * @param name option name
254      */

255     public int getIntOption(String JavaDoc name) {
256         return getOptions().getInt(name);
257     }
258
259     /**
260      * Returns a boolean option.
261      *
262      * @param name option name
263      */

264     public boolean isOption(String JavaDoc name) {
265         return getOptions().getBoolean(name);
266     }
267
268     /**
269      * Tests if an option has a specified value.
270      *
271      * @param name option name
272      * @param value value to test for
273      */

274     public boolean isOption(String JavaDoc name, String JavaDoc value) {
275         return getOptions().getBoolean(name);
276     }
277
278     /**
279      * Tests if a multi-value option contains a specified value among its set of values.
280      * This uses a comma-sperated string as multi-value set.
281      *
282      * @param name option name
283      * @param value value to test for
284      */

285     public boolean hasOption(String JavaDoc name, String JavaDoc value) {
286         TreeSet cached = (TreeSet)multiValueCache.get(name);
287         if (cached == null) {
288             cached = new TreeSet();
289                String JavaDoc s = getOption(name);
290                if (s != null) {
291                 StringTokenizer st = new StringTokenizer(s, ",", false);
292                 while (st.hasMoreTokens()) {
293                     cached.add(st.nextToken().trim());
294                 }
295                }
296             multiValueCache.put(name, cached);
297         }
298            return cached.contains(value);
299     }
300
301 } // end BeautyJ
302
Popular Tags