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.impl;
17  
18  import java.io.BufferedReader;
19  import java.io.File;
20  import java.io.FileNotFoundException;
21  import java.io.FileReader;
22  import java.io.IOException;
23  
24  import java.text.MessageFormat;
25  import java.text.SimpleDateFormat;
26  
27  import java.util.ArrayList;
28  import java.util.Calendar;
29  import java.util.List;
30  
31  import net.sf.whatsnew.exceptions.AppException;
32  import net.sf.whatsnew.filter.ConcatFilter;
33  import net.sf.whatsnew.filter.Filter;
34  import net.sf.whatsnew.filter.OutputWriter;
35  import net.sf.whatsnew.filter.impl.PrependConcatFilter;
36  import net.sf.whatsnew.filter.impl.TabsToSpacesFilter;
37  import net.sf.whatsnew.filter.impl.UnixOutputWriter;
38  import net.sf.whatsnew.filter.util.StringUtils;
39  import net.sf.whatsnew.mode.Mode;
40  import net.sf.whatsnew.options.Options;
41  
42  
43  /***
44   * <p>
45   * The new Release mode
46   * </p>
47   *
48   * @author <a href="mailto:dquintela@users.sourceforge.net">Diogo Quintela</a>
49   * @version $Id: Release.java,v 1.1 2004/05/13 01:22:41 dquintela Exp $
50   */
51  public class Release
52  extends Mode {
53      /***
54       * Creates a new Release object.
55       *
56       * @param options Application options
57       *
58       * @throws AppException in case of options checking error
59       */
60      public Release(Options options)
61      throws AppException {
62          super(options);
63      }
64  
65      /***
66       * Get concatenation filter to use
67       *
68       * @return The filter to use
69       */
70      protected ConcatFilter getConcatFilter() {
71          return new PrependConcatFilter();
72      }
73  
74      /***
75       * Get the filters to be applied to extra input
76       *
77       * @return The filters to use
78       */
79      protected Filter[] getExtraFilters() {
80          List filters = new ArrayList();
81  
82          // Add filters
83          filters.add(new TabsToSpacesFilter());
84  
85          return (Filter[]) filters.toArray(new Filter[0]);
86      }
87  
88      /***
89       * Get extra input
90       *
91       * @return Extra input
92       */
93      protected String[] getExtraInput() {
94          Calendar now = Calendar.getInstance();
95          String releaseVersion = options.getValue(Options.RELEASE_VERSION)
96                                             .getString();
97          String releaseFormat = options.getValue(Options.RELEASE_FORMAT)
98                                            .getString();
99          String dateFormat = options.getValue(Options.RELEASE_DATE_FORMAT)
100                                        .getString();
101         SimpleDateFormat df = new SimpleDateFormat(dateFormat);
102         String dateFormatted = df.format(now.getTime());
103         MessageFormat mF = new MessageFormat(releaseFormat);
104         String formattedMessage = mF.format(new Object[] {
105                     dateFormatted,
106                     releaseVersion
107                 });
108 
109         // Now i need to split into lines if exist
110         return StringUtils.splitIntoLines(formattedMessage, "\n");
111     }
112 
113     /***
114      * Get the filters to be applied to file input
115      *
116      * @return The filters to use
117      */
118     protected Filter[] getFileFilters() {
119         return new Filter[] {
120             new TabsToSpacesFilter()
121         };
122     }
123 
124     /***
125      * Get file input
126      *
127      * @param inputFile The file to use
128      *
129      * @return File input
130      *
131      * @throws AppException in case of error
132      */
133     protected String[] getFileInput(File inputFile)
134     throws AppException {
135         try {
136             // Cria novo se não existe
137             inputFile.createNewFile();
138 
139             String inputLine;
140             BufferedReader in = new BufferedReader(new FileReader(inputFile));
141             List retVal = new ArrayList();
142 
143             try {
144                 while ((inputLine = in.readLine()) != null) {
145                     retVal.add(inputLine);
146                 }
147             } finally {
148                 in.close();
149             }
150             return (String[]) retVal.toArray(new String[0]);
151         } catch (FileNotFoundException ex) {
152             throw new AppException(ex.getMessage());
153         } catch (IOException ex) {
154             throw new AppException(ex.getMessage());
155         }
156     }
157 
158     /***
159      * Get output writer
160      *
161      * @return The output writter
162      */
163     protected OutputWriter getOutputWriter() {
164         return new UnixOutputWriter();
165     }
166 
167     /***
168      * Validade minimal mode options
169      *
170      * @throws AppException in case of error
171      */
172     protected void checkOptions()
173     throws AppException {
174         super.checkOptions();
175 
176         if (options.getValue(Options.RELEASE_VERSION) == null) {
177             throw new AppException("error.missing.release.version", true);
178         }
179         if (options.getValue(Options.RELEASE_FORMAT) == null) {
180             throw new AppException("error.missing.release.format", true);
181         }
182         if (options.getValue(Options.RELEASE_DATE_FORMAT) == null) {
183             throw new AppException("error.missing.release.dateformat", true);
184         }
185     }
186 }
187 // EOF