001: /*
002: * Copyright 2004,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.bsf.util.cf;
018:
019: import java.io.FileNotFoundException;
020: import java.io.FileReader;
021: import java.io.FileWriter;
022: import java.io.IOException;
023: import java.io.InputStreamReader;
024: import java.io.OutputStreamWriter;
025: import java.io.Reader;
026: import java.io.Writer;
027:
028: /**
029: * This is an example of how a <code>CodeFormatter</code> bean can be used.
030: * <p>
031: * The CFDriver is a stand-alone tool that will instantiate a
032: * <code>CodeFormatter</code> bean, configure it according to your
033: * command-line arguments, and invoke the formatting. Since the
034: * default source of input is <code>stdin</code>, and the default
035: * target for output is <code>stdout</code>, a <code>CFDriver</code>
036: * can also be used as a filter.
037: *
038: * @see CodeFormatter
039: *
040: * @version 1.0
041: * @author Matthew J. Duftler
042: */
043: public class CFDriver {
044: /**
045: * Not used.
046: */
047: public CFDriver() {
048: }
049:
050: /**
051: * A driver for <code>CodeFormatter</code>.
052: *<p>
053: * Usage:
054: *<code><pre>
055: * java org.apache.cf.CFDriver [args]
056: *<p>
057: * args:
058: *<p>
059: * [-in fileName] default: <STDIN>
060: * [-out fileName] default: <STDOUT>
061: * [-maxLine length] default: 74
062: * [-step size] default: 2
063: * [-delim group] default: (+
064: * [-sdelim group] default: ,
065: *</pre></code>
066: */
067: public static void main(String[] argv) {
068: if (argv.length % 2 == 0) {
069: String inFile = null, outFile = null, maxLine = null, indStep = null, delim = null, sDelim = null;
070: Reader in = null;
071: Writer out = null;
072: CodeFormatter cf = new CodeFormatter();
073:
074: for (int i = 0; i < argv.length; i += 2) {
075: if (argv[i].startsWith("-i"))
076: inFile = argv[i + 1];
077: else if (argv[i].startsWith("-o"))
078: outFile = argv[i + 1];
079: else if (argv[i].startsWith("-m"))
080: maxLine = argv[i + 1];
081: else if (argv[i].startsWith("-st"))
082: indStep = argv[i + 1];
083: else if (argv[i].startsWith("-d"))
084: delim = argv[i + 1];
085: else if (argv[i].startsWith("-sd"))
086: sDelim = argv[i + 1];
087: }
088:
089: if (inFile != null) {
090: try {
091: in = new FileReader(inFile);
092: } catch (FileNotFoundException e) {
093: printError("Cannot open input file: " + inFile);
094:
095: return;
096: }
097: } else {
098: in = new InputStreamReader(System.in);
099: }
100:
101: if (outFile != null) {
102: try {
103: out = new FileWriter(outFile);
104: } catch (IOException e) {
105: printError("Cannot open output file: " + outFile);
106:
107: return;
108: }
109: } else {
110: out = new OutputStreamWriter(System.out);
111: }
112:
113: if (maxLine != null) {
114: try {
115: cf.setMaxLineLength(Integer.parseInt(maxLine));
116: } catch (NumberFormatException nfe) {
117: printError("Not a valid integer: " + maxLine);
118:
119: return;
120: }
121: }
122:
123: if (indStep != null) {
124: try {
125: cf.setIndentationStep(Integer.parseInt(indStep));
126: } catch (NumberFormatException nfe) {
127: printError("Not a valid integer: " + indStep);
128:
129: return;
130: }
131: }
132:
133: if (delim != null)
134: cf.setDelimiters(delim);
135:
136: if (sDelim != null)
137: cf.setStickyDelimiters(sDelim);
138:
139: cf.formatCode(in, out);
140: } else
141: printHelp();
142: }
143:
144: private static void printError(String errMsg) {
145: System.err.println("ERROR: " + errMsg);
146: }
147:
148: private static void printHelp() {
149: System.out.println("Usage:");
150: System.out.println();
151: System.out.println(" java " + CFDriver.class.getName()
152: + " [args]");
153: System.out.println();
154: System.out.println(" args:");
155: System.out.println();
156: System.out
157: .println(" [-in fileName] default: <STDIN>");
158: System.out
159: .println(" [-out fileName] default: <STDOUT>");
160: System.out.println(" [-maxLine length] default: "
161: + CodeFormatter.DEFAULT_MAX);
162: System.out.println(" [-step size] default: "
163: + CodeFormatter.DEFAULT_STEP);
164: System.out.println(" [-delim group] default: "
165: + CodeFormatter.DEFAULT_DELIM);
166: System.out.println(" [-sdelim group] default: "
167: + CodeFormatter.DEFAULT_S_DELIM);
168: }
169: }
|