KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bsf > util > cf > CFDriver


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2002 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowlegement may appear in the software itself,
24  * if and wherever such third-party acknowlegements normally appear.
25  *
26  * 4. The names "Apache BSF", "Apache", and "Apache Software Foundation"
27  * must not be used to endorse or promote products derived from
28  * this software without prior written permission. For written
29  * permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache"
32  * nor may "Apache" appear in their names without prior written
33  * permission of the Apache Group.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many individuals
50  * on behalf of the Apache Software Foundation and was originally created by
51  * Sanjiva Weerawarana and others at International Business Machines
52  * Corporation. For more information on the Apache Software Foundation,
53  * please see <http://www.apache.org/>.
54  */

55
56 package org.apache.bsf.util.cf;
57
58 import java.io.*;
59 import org.apache.bsf.debug.util.DebugLog;
60
61 /**
62  * This is an example of how a <code>CodeFormatter</code> bean can be used.
63  * <p>
64  * The CFDriver is a stand-alone tool that will instantiate a
65  * <code>CodeFormatter</code> bean, configure it according to your
66  * command-line arguments, and invoke the formatting. Since the
67  * default source of input is <code>stdin</code>, and the default
68  * target for output is <code>stdout</code>, a <code>CFDriver</code>
69  * can also be used as a filter.
70  *
71  * @see CodeFormatter
72  *
73  * @version 1.0
74  * @author Matthew J. Duftler
75  */

76 public class CFDriver
77 {
78     /**
79      * Not used.
80      */

81     public CFDriver()
82   {
83   }
84   /**
85     * A driver for <code>CodeFormatter</code>.
86     *<p>
87     * Usage:
88     *<code><pre>
89     * java org.apache.cf.CFDriver [args]
90     *<p>
91     * args:
92     *<p>
93     * [-in fileName] default: &lt;STDIN&gt;
94     * [-out fileName] default: &lt;STDOUT&gt;
95     * [-maxLine length] default: 74
96     * [-step size] default: 2
97     * [-delim group] default: (+
98     * [-sdelim group] default: ,
99     *</pre></code>
100     */

101     public static void main(String JavaDoc[] argv)
102   {
103     if (argv.length % 2 == 0)
104     {
105       String JavaDoc inFile = null,
106                     outFile = null,
107                     maxLine = null,
108                     indStep = null,
109                     delim = null,
110                     sDelim = null;
111       Reader in = null;
112         Writer out = null;
113       CodeFormatter cf = new CodeFormatter();
114
115       for (int i = 0; i < argv.length; i += 2)
116       {
117         if (argv[i].startsWith("-i"))
118           inFile = argv[i + 1];
119         else if (argv[i].startsWith("-o"))
120           outFile = argv[i + 1];
121         else if (argv[i].startsWith("-m"))
122             maxLine = argv[i + 1];
123         else if (argv[i].startsWith("-st"))
124           indStep = argv[i + 1];
125         else if (argv[i].startsWith("-d"))
126             delim = argv[i + 1];
127         else if (argv[i].startsWith("-sd"))
128           sDelim = argv[i + 1];
129       }
130
131       if (inFile != null)
132       {
133         try
134         {
135           in = new FileReader(inFile);
136         }
137         catch (FileNotFoundException e)
138         {
139           printError("Cannot open input file: " + inFile);
140             
141           return;
142         }
143       }
144       else
145       {
146         in = new InputStreamReader(System.in);
147       }
148
149       if (outFile != null)
150       {
151         try
152         {
153           out = new FileWriter(outFile);
154         }
155         catch (IOException e)
156         {
157           printError("Cannot open output file: " + outFile);
158           
159           return;
160         }
161       }
162       else
163       {
164         out = new OutputStreamWriter(System.out);
165       }
166
167         if (maxLine != null)
168       {
169         try
170         {
171             cf.setMaxLineLength(Integer.parseInt(maxLine));
172         }
173         catch (NumberFormatException JavaDoc nfe)
174         {
175             printError("Not a valid integer: " + maxLine);
176             
177             return;
178         }
179       }
180
181       if (indStep != null)
182       {
183         try
184         {
185           cf.setIndentationStep(Integer.parseInt(indStep));
186         }
187         catch (NumberFormatException JavaDoc nfe)
188         {
189           printError("Not a valid integer: " + indStep);
190           
191           return;
192         }
193       }
194         
195         if (delim != null)
196           cf.setDelimiters(delim);
197         
198         if (sDelim != null)
199         cf.setStickyDelimiters(sDelim);
200             
201         cf.formatCode(in, out);
202     }
203     else
204       printHelp();
205   }
206     private static void printError(String JavaDoc errMsg)
207   {
208     DebugLog.stderrPrintln("ERROR: " + errMsg, DebugLog.BSF_LOG_L2);
209   }
210     private static void printHelp()
211   {
212     System.out.println("Usage:");
213     System.out.println();
214     System.out.println(" java " + CFDriver.class.getName() + " [args]");
215     System.out.println();
216     System.out.println(" args:");
217     System.out.println();
218     System.out.println(" [-in fileName] default: <STDIN>");
219     System.out.println(" [-out fileName] default: <STDOUT>");
220     System.out.println(" [-maxLine length] default: " +
221                        CodeFormatter.DEFAULT_MAX);
222     System.out.println(" [-step size] default: " +
223                        CodeFormatter.DEFAULT_STEP);
224     System.out.println(" [-delim group] default: " +
225                        CodeFormatter.DEFAULT_DELIM);
226     System.out.println(" [-sdelim group] default: " +
227                        CodeFormatter.DEFAULT_S_DELIM);
228   }
229 }
230
Popular Tags