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;
17  
18  import java.io.File;
19  import java.io.FileNotFoundException;
20  import java.io.IOException;
21  
22  import net.sf.whatsnew.config.Config;
23  import net.sf.whatsnew.exceptions.*;
24  import net.sf.whatsnew.mode.Mode;
25  import net.sf.whatsnew.options.*;
26  
27  
28  /***
29   * <p>
30   * Main class for WhatsNew File Creator aplication
31   * </p>
32   *
33   * @author <a href="mailto:dquintela@users.sourceforge.net">Diogo Quintela</a>
34   * @version $Id: WhatsNew.java,v 1.1 2004/05/13 01:22:40 dquintela Exp $
35   */
36  public class WhatsNew {
37      /*** Application options */
38      private Options options;
39  
40      /***
41       * Creates a new WhatsNew object.
42       *
43       * @param options The application options
44       *
45       * @throws AppException if options == null, or invalid options
46       */
47      public WhatsNew(Options options)
48      throws AppException {
49          if (options == null) {
50              throw new AppException("error.invalid.options", true);
51          }
52          this.options = options;
53          validateOptions();
54      }
55  
56      /***
57       * Constructs default application options, based on config properties and
58       * external file if specified
59       *
60       * @param externalConfigFile Name of external properties file
61       *
62       * @return Default application options
63       *
64       * @throws AppException in case of error
65       */
66      public static Options getConfigOptions(String externalConfigFile)
67      throws AppException {
68          Options appOptions = new Options();
69  
70          Config config;
71          try {
72              if (externalConfigFile != null) {
73                  config = new Config(new File(externalConfigFile));
74              } else {
75                  config = new Config();
76              }
77  
78              appOptions.setValue(Options.RELEASE_DATE_FORMAT,
79                  config.getString("mode.release.dateformat"));
80              appOptions.setValue(Options.RELEASE_FORMAT,
81                  config.getString("mode.release.format"));
82          } catch (FileNotFoundException e) {
83              throw new AppException("error.external.file.not.found", true);
84          } catch (IOException e) {
85              throw new AppException(e.getMessage());
86          }
87  
88          return appOptions;
89      }
90  
91      /***
92       * Main execution method
93       *
94       * @throws AppException in case of error
95       */
96      public void run()
97      throws AppException {
98          Mode mFactory = Mode.getFactory(options);
99  
100         mFactory.run();
101     }
102 
103     /***
104      * Validade minimal running options
105      *
106      * @throws AppException in case of error
107      */
108     private void validateOptions()
109     throws AppException {
110         if (options.getValue(Options.MODE) == null) {
111             throw new AppException("error.missing.mode", true);
112         } else {
113             int mode = options.getValue(Options.MODE).intValue();
114             if ((mode == Mode.CHANGE.intValue())
115                     || (mode == Mode.RELEASE.intValue())) {
116                 // noop. Thats fine   
117             } else {
118                 throw new AppException("error.invalid.mode", true);
119             }
120         }
121         if (options.getValue(Options.FILE) == null) {
122             throw new AppException("error.missing.file", true);
123         }
124     }
125 }
126 // EOF