View Javadoc

1   /*
2    * Copyright 2007 - 2007 JEuclid, http://jeuclid.sf.net
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  /* $Id: Main.java,v bd2e46aac4d4 2008/05/14 15:14:55 maxberger $ */
18  
19  package net.sourceforge.jeuclid.app.foprep;
20  
21  import java.io.PrintWriter;
22  
23  import javax.xml.transform.Result;
24  import javax.xml.transform.Source;
25  import javax.xml.transform.TransformerException;
26  import javax.xml.transform.stream.StreamResult;
27  import javax.xml.transform.stream.StreamSource;
28  
29  import net.sourceforge.jeuclid.converter.Processor;
30  
31  import org.apache.commons.cli.CommandLine;
32  import org.apache.commons.cli.CommandLineParser;
33  import org.apache.commons.cli.GnuParser;
34  import org.apache.commons.cli.HelpFormatter;
35  import org.apache.commons.cli.Option;
36  import org.apache.commons.cli.Options;
37  import org.apache.commons.cli.ParseException;
38  
39  /**
40   * Main class for fo-preprocess application.
41   * 
42   * @version $Revision: bd2e46aac4d4 $
43   */
44  public final class Main {
45  
46      private static final String STD_INOUT = "-";
47  
48      private static final String OPTION_OUT = "out";
49  
50      private static final String OPTION_IN = "in";
51  
52      private Main() {
53          // Empty on purpose.
54      }
55  
56      /**
57       * Application entry point.
58       * 
59       * @param args
60       *            Command line arguments.
61       */
62      public static void main(final String[] args) {
63          final Options options = new Options();
64          final Option in = new Option(Main.OPTION_IN, true, "input (fo) file");
65          in.setRequired(true);
66          options.addOption(in);
67          final Option out = new Option(Main.OPTION_OUT, true,
68                  "output (fo) file");
69          out.setRequired(true);
70          options.addOption(out);
71          final CommandLineParser parser = new GnuParser();
72          try {
73              final CommandLine cmd = parser.parse(options, args);
74              final String inputFile = cmd.getOptionValue(Main.OPTION_IN);
75              final String outputFile = cmd.getOptionValue(Main.OPTION_OUT);
76              final Result result;
77              if (Main.STD_INOUT.equals(outputFile)) {
78                  result = new StreamResult(new PrintWriter(System.out));
79              } else {
80                  result = new StreamResult(outputFile);
81              }
82              final Source source;
83              if (Main.STD_INOUT.equals(inputFile)) {
84                  source = new StreamSource(System.in);
85              } else {
86                  source = new StreamSource(inputFile);
87              }
88              Processor.getInstance().process(source, result);
89          } catch (final ParseException e1) {
90              System.out.println("Invalid command line:" + e1.getMessage());
91              new HelpFormatter().printHelp(
92                      "foprep -in input.fo -out output.fo", options);
93          } catch (final TransformerException e) {
94              System.out.println("An error occurred during processing: "
95                      + e.getMessage());
96              e.printStackTrace();
97          }
98      }
99  }