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 365 2007-06-29 17:48:07Z maxberger $ */
18  
19  package net.sourceforge.jeuclid.app.foprep;
20  
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.IOException;
24  import java.io.PrintWriter;
25  
26  import javax.xml.transform.Result;
27  import javax.xml.transform.Source;
28  import javax.xml.transform.TransformerException;
29  import javax.xml.transform.stream.StreamResult;
30  import javax.xml.transform.stream.StreamSource;
31  
32  import org.apache.commons.cli.CommandLine;
33  import org.apache.commons.cli.CommandLineParser;
34  import org.apache.commons.cli.GnuParser;
35  import org.apache.commons.cli.HelpFormatter;
36  import org.apache.commons.cli.Option;
37  import org.apache.commons.cli.Options;
38  import org.apache.commons.cli.ParseException;
39  
40  /**
41   * Main class for fo-preprocess application.
42   * 
43   * @author Max Berger
44   * @version $Revision: 365 $
45   */
46  public final class Main {
47  
48      private static final String STD_INOUT = "-";
49  
50      private static final String OPTION_OUT = "out";
51  
52      private static final String OPTION_IN = "in";
53  
54      private Main() {
55          // Empty on purpose.
56      }
57  
58      /**
59       * Application entry point.
60       * 
61       * @param args
62       *            Command line arguments.
63       */
64      public static void main(final String[] args) {
65          final Options options = new Options();
66          final Option in = new Option(Main.OPTION_IN, true, "input (fo) file");
67          in.setRequired(true);
68          options.addOption(in);
69          final Option out = new Option(Main.OPTION_OUT, true,
70                  "output (fo) file");
71          out.setRequired(true);
72          options.addOption(out);
73          final CommandLineParser parser = new GnuParser();
74          try {
75              final CommandLine cmd = parser.parse(options, args);
76              final String inputFile = cmd.getOptionValue(Main.OPTION_IN);
77              final String outputFile = cmd.getOptionValue(Main.OPTION_OUT);
78              final Result result;
79              if (Main.STD_INOUT.equals(outputFile)) {
80                  result = new StreamResult(new PrintWriter(System.out));
81              } else {
82                  result = new StreamResult(new File(outputFile));
83              }
84              final Source source;
85              if (Main.STD_INOUT.equals(inputFile)) {
86                  source = new StreamSource(System.in);
87              } else {
88                  source = new StreamSource(new FileInputStream(inputFile));
89              }
90              Processor.getProcessor().process(source, result);
91          } catch (final ParseException e1) {
92              System.out.println("Invalid command line:" + e1.getMessage());
93              new HelpFormatter().printHelp(
94                      "foprep -in input.fo -out output.fo", options);
95          } catch (final TransformerException e) {
96              System.out.println("An error occurred during processing: "
97                      + e.getMessage());
98              e.printStackTrace();
99          } catch (final IOException e) {
100             System.out.println("An I/O error occurred during processing: "
101                     + e.getMessage());
102             e.printStackTrace();
103         }
104     }
105 }