View Javadoc
1   /*
2    * Copyright 2004 Diogo Quintela (dquintela@users.sourceforge.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  package net.sf.whatsnew.console;
17  
18  import jargs.gnu.CmdLineParser;
19  
20  import net.sf.whatsnew.WhatsNew;
21  import net.sf.whatsnew.exceptions.AppException;
22  import net.sf.whatsnew.mode.Mode;
23  import net.sf.whatsnew.options.Options;
24  
25  
26  /***
27   * <p>
28   * Wrapper class for WhatsNew File Creator aplication
29   * </p>
30   *
31   * @author <a href="mailto:dquintela@users.sourceforge.net">Diogo Quintela</a>
32   * @version $Id: Main.java,v 1.1 2004/05/13 01:22:36 dquintela Exp $
33   */
34  public class Main {
35      /***
36       * Main method
37       *
38       * @param args Program arguments
39       */
40      public static void main(String[] args) {
41          CmdLineParser parser = new CmdLineParser();
42          CmdLineParser.Option file = parser.addStringOption('f', "file");
43          CmdLineParser.Option change = parser.addBooleanOption('c', "change");
44          CmdLineParser.Option release = parser.addBooleanOption('r', "release");
45          CmdLineParser.Option releaseVer = parser.addStringOption('w', "releaseversion");
46          CmdLineParser.Option propertiesExt = parser.addStringOption('p', "properties");
47          CmdLineParser.Option debugOption = parser.addBooleanOption('d', "debug");
48          CmdLineParser.Option changeMsg = parser.addStringOption('m', "message");
49          
50          if (args.length > 0) {
51              try {
52                  parser.parse(args);
53              } catch (CmdLineParser.OptionException e) {
54                  System.err.println(e.getMessage());
55                  printUsage();
56                  System.exit(2);
57              }
58          } else {
59              printUsage();
60              System.exit(2);
61          }
62  
63          String fileName = (String) parser.getOptionValue(file);
64          Boolean changeMode = (Boolean) parser.getOptionValue(change);
65          Boolean releaseMode = (Boolean) parser.getOptionValue(release);
66          String releaseVersion = (String) parser.getOptionValue(releaseVer);
67          String propertiesExternal = (String) parser.getOptionValue(propertiesExt);
68          Boolean debug = (Boolean) parser.getOptionValue(debugOption);
69          String changeMessage = (String) parser.getOptionValue(changeMsg);
70  
71          try {
72              // Generate options
73              Options appOptions = WhatsNew.getConfigOptions(propertiesExternal);
74              appOptions.setValue(Options.MODE, resolveMode(changeMode, releaseMode));
75              if (fileName != null) {
76                  appOptions.setValue(Options.FILE, fileName);
77              }
78              if (releaseVersion != null) {
79                  appOptions.setValue(Options.RELEASE_VERSION, releaseVersion);
80              }
81              if (changeMessage != null) {
82                  appOptions.setValue(Options.CHANGE_MESSAGE, changeMessage);
83              }
84  
85              // Instanciate 
86              WhatsNew prog = new WhatsNew(appOptions);
87  
88              // Unleash it
89              prog.run();
90          } catch (AppException e) {
91              e.print((debug != null) ? debug.booleanValue() : false);
92              System.exit(1);
93          }
94  
95          // Exit gracefully
96          System.exit(0);
97      }
98  
99  	/***
100      * Displays program usage
101      */
102     private static void printUsage() {
103         System.err.println("usage: prog {-f,--file} a_file_name, " + "[{-p, --properties} properties_file], "
104             + "[{-c, --change}, {-m, --message} quick_change_message {-n, --noescape}]" + "| {-r, --release}, {-w, --releaseversion} release_version], {-d, --debug}");
105     }
106 
107     /***
108      * Auxiliar method to resolve the running mode for the application
109      *
110      * @param changeBoolean The change mode argument
111      * @param releaseBoolean The release mode argument
112      *
113      * @return The mode to use
114      *
115      * @throws AppException in case of error
116      */
117     private static Integer resolveMode(Boolean changeBoolean, Boolean releaseBoolean)
118     throws AppException {
119         boolean change = (changeBoolean != null) ? changeBoolean.booleanValue() : false;
120         boolean release = (releaseBoolean != null) ? releaseBoolean.booleanValue() : false;
121         if (change && !release) {
122             return Mode.CHANGE;
123         } else if (!change && release) {
124             return Mode.RELEASE;
125         } else if (change && release) {
126             throw new AppException("error.only.one.mode", true);
127         }
128 
129         // Will use default mode
130         // It is Mode.CHANGE
131         return null;
132     }
133 }
134 
135 
136 // EOF