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.mode;
17  
18  import java.io.File;
19  
20  import net.sf.whatsnew.exceptions.AppException;
21  import net.sf.whatsnew.filter.ConcatFilter;
22  import net.sf.whatsnew.filter.Filter;
23  import net.sf.whatsnew.filter.OutputWriter;
24  import net.sf.whatsnew.mode.impl.Change;
25  import net.sf.whatsnew.mode.impl.Release;
26  import net.sf.whatsnew.options.Options;
27  
28  
29  /***
30   * <p>
31   * The abtract application execution mode
32   * </p>
33   *
34   * @author <a href="mailto:dquintela@users.sourceforge.net">Diogo Quintela</a>
35   * @version $Id: Mode.java,v 1.1 2004/05/13 01:22:34 dquintela Exp $
36   */
37  public abstract class Mode {
38      /*** Mode new file change */
39      public static final Integer CHANGE = new Integer(0);
40  
41      /*** Mode new file release */
42      public static final Integer RELEASE = new Integer(1);
43  
44      /*** Application options */
45      protected Options options;
46  
47      /***
48       * Constructs a new execution mode
49       *
50       * @param options Application options
51       *
52       * @throws AppException in case of options checking error
53       */
54      public Mode(Options options)
55      throws AppException {
56          this.options = options;
57          checkOptions();
58      }
59  
60      /***
61       * Instantiates a new application mode
62       *
63       * @param options Application options
64       *
65       * @return The created instance
66       *
67       * @throws AppException in case of error
68       */
69      public static Mode getFactory(Options options)
70      throws AppException {
71          if (options.getValue(Options.MODE) == null) {
72              throw new AppException("error.invalid.mode", true);
73          } else {
74              int mode = options.getValue(Options.MODE).intValue();
75              if (mode == Mode.CHANGE.intValue()) {
76                  return new Change(options);
77              } else if (mode == Mode.RELEASE.intValue()) {
78                  return new Release(options);
79              } else {
80                  throw new AppException("error.invalid.mode", true);
81              }
82          }
83      }
84  
85      /***
86       * Main mode execution method
87       *
88       * @throws AppException in case of error
89       */
90      public void run()
91      throws AppException {
92          File inputFile = new File(options.getValue(Options.FILE).getString());
93  
94          String[] extraInput = getExtraInput();
95          String[] fileInput = getFileInput(inputFile);
96          Filter[] extraFilters = getExtraFilters();
97          Filter[] fileFilters = getFileFilters();
98          ConcatFilter concatFilter = getConcatFilter();
99          OutputWriter outputFilter = getOutputWriter();
100 
101         // Process user filters
102         for (int i = 0; i < extraFilters.length; i++) {
103             extraInput = extraFilters[i].filter(extraInput);
104         }
105 
106         // Process file filters
107         for (int i = 0; i < fileFilters.length; i++) {
108             fileInput = fileFilters[i].filter(fileInput);
109         }
110 
111         // Process concatenation filter
112         String[] concatenated = concatFilter.concat(fileInput, extraInput);
113 
114         // Write file
115         outputFilter.output(inputFile, concatenated);
116     }
117 
118     /***
119      * Get concatenation filter to use
120      *
121      * @return The filter to use
122      *
123      * @throws AppException in case of error
124      */
125     protected abstract ConcatFilter getConcatFilter()
126     throws AppException;
127 
128     /***
129      * Get the filters to be applied to extra input
130      *
131      * @return The filters to use
132      *
133      * @throws AppException in case of error
134      */
135     protected abstract Filter[] getExtraFilters()
136     throws AppException;
137 
138     /***
139      * Get extra input
140      *
141      * @return Extra input
142      *
143      * @throws AppException in case of error
144      */
145     protected abstract String[] getExtraInput()
146     throws AppException;
147 
148     /***
149      * Get the filters to be applied to file input
150      *
151      * @return The filters to use
152      *
153      * @throws AppException in case of error
154      */
155     protected abstract Filter[] getFileFilters()
156     throws AppException;
157 
158     /***
159      * Get file input
160      *
161      * @param inputFile The file to use
162      *
163      * @return File input
164      *
165      * @throws AppException in case of error
166      */
167     protected abstract String[] getFileInput(File inputFile)
168     throws AppException;
169 
170     /***
171      * Get output writer
172      *
173      * @return The output writter
174      *
175      * @throws AppException in case of error
176      */
177     protected abstract OutputWriter getOutputWriter()
178     throws AppException;
179 
180     /***
181      * Validade minimal mode options
182      *
183      * @throws AppException in case of error
184      */
185     protected void checkOptions()
186     throws AppException {
187         if (options.getValue(Options.FILE) == null) {
188             throw new AppException("error.missing.file", true);
189         }
190     }
191 }
192 // EOF