001    /*
002     * Copyright 2007 - 2007 JEuclid, http://jeuclid.sf.net
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    /* $Id: Main.java,v bd2e46aac4d4 2008/05/14 15:14:55 maxberger $ */
018    
019    package net.sourceforge.jeuclid.app.foprep;
020    
021    import java.io.PrintWriter;
022    
023    import javax.xml.transform.Result;
024    import javax.xml.transform.Source;
025    import javax.xml.transform.TransformerException;
026    import javax.xml.transform.stream.StreamResult;
027    import javax.xml.transform.stream.StreamSource;
028    
029    import net.sourceforge.jeuclid.converter.Processor;
030    
031    import org.apache.commons.cli.CommandLine;
032    import org.apache.commons.cli.CommandLineParser;
033    import org.apache.commons.cli.GnuParser;
034    import org.apache.commons.cli.HelpFormatter;
035    import org.apache.commons.cli.Option;
036    import org.apache.commons.cli.Options;
037    import org.apache.commons.cli.ParseException;
038    
039    /**
040     * Main class for fo-preprocess application.
041     * 
042     * @version $Revision: bd2e46aac4d4 $
043     */
044    public final class Main {
045    
046        private static final String STD_INOUT = "-";
047    
048        private static final String OPTION_OUT = "out";
049    
050        private static final String OPTION_IN = "in";
051    
052        private Main() {
053            // Empty on purpose.
054        }
055    
056        /**
057         * Application entry point.
058         * 
059         * @param args
060         *            Command line arguments.
061         */
062        public static void main(final String[] args) {
063            final Options options = new Options();
064            final Option in = new Option(Main.OPTION_IN, true, "input (fo) file");
065            in.setRequired(true);
066            options.addOption(in);
067            final Option out = new Option(Main.OPTION_OUT, true,
068                    "output (fo) file");
069            out.setRequired(true);
070            options.addOption(out);
071            final CommandLineParser parser = new GnuParser();
072            try {
073                final CommandLine cmd = parser.parse(options, args);
074                final String inputFile = cmd.getOptionValue(Main.OPTION_IN);
075                final String outputFile = cmd.getOptionValue(Main.OPTION_OUT);
076                final Result result;
077                if (Main.STD_INOUT.equals(outputFile)) {
078                    result = new StreamResult(new PrintWriter(System.out));
079                } else {
080                    result = new StreamResult(outputFile);
081                }
082                final Source source;
083                if (Main.STD_INOUT.equals(inputFile)) {
084                    source = new StreamSource(System.in);
085                } else {
086                    source = new StreamSource(inputFile);
087                }
088                Processor.getInstance().process(source, result);
089            } catch (final ParseException e1) {
090                System.out.println("Invalid command line:" + e1.getMessage());
091                new HelpFormatter().printHelp(
092                        "foprep -in input.fo -out output.fo", options);
093            } catch (final TransformerException e) {
094                System.out.println("An error occurred during processing: "
095                        + e.getMessage());
096                e.printStackTrace();
097            }
098        }
099    }