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  import java.io.InputStreamReader;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import net.sf.whatsnew.exceptions.AppException;
29  import net.sf.whatsnew.filter.ConcatFilter;
30  import net.sf.whatsnew.filter.Filter;
31  import net.sf.whatsnew.filter.OutputWriter;
32  import net.sf.whatsnew.filter.impl.ComposedIndentationFilter;
33  import net.sf.whatsnew.filter.impl.PrependConcatFilter;
34  import net.sf.whatsnew.filter.impl.TabsToSpacesFilter;
35  import net.sf.whatsnew.filter.impl.TrimSpacesFilter;
36  import net.sf.whatsnew.filter.impl.UnixOutputWriter;
37  import net.sf.whatsnew.filter.impl.WordWrapFilter;
38  import net.sf.whatsnew.filter.util.StringUtils;
39  import net.sf.whatsnew.lang.Messages;
40  import net.sf.whatsnew.mode.Mode;
41  import net.sf.whatsnew.options.Options;
42  
43  
44  /***
45   * <p>
46   * The new Change mode
47   * </p>
48   *
49   * @author <a href="mailto:dquintela@users.sourceforge.net">Diogo Quintela</a>
50   * @version $Id: Change.java,v 1.1 2004/05/13 01:22:41 dquintela Exp $
51   */
52  public class Change extends Mode {
53      /***
54       * Creates a new Change object.
55       *
56       * @param options Application options
57       *
58       * @throws AppException in case of options checking error
59       */
60      public Change(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 TrimSpacesFilter());
84          filters.add(new TabsToSpacesFilter());
85          filters.add(new WordWrapFilter());
86          filters.add(new ComposedIndentationFilter());
87  
88          return (Filter[]) filters.toArray(new Filter[0]);
89      }
90  
91      /***
92       * Get extra input
93       *
94       * @return Extra input
95       *
96       * @throws AppException in case of error
97       */
98      protected String[] getExtraInput()
99      throws AppException {
100         if (options.getValue(Options.CHANGE_MESSAGE) != null) {
101             String msg = options.getValue(Options.CHANGE_MESSAGE).getString();
102         	// Now i need to split into lines if exist
103             return StringUtils.splitIntoLines(msg, "\n");            
104         } else {
105             try {
106                 String inputLine;
107                 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
108                 List retVal = new ArrayList();
109 
110                 System.out.println(Messages.getString("mode.change.input"));
111                 while (((inputLine = in.readLine()) != null) && !".".equals(inputLine)) {
112                     retVal.add(inputLine);
113                 }
114                 return (String[]) retVal.toArray(new String[0]);
115             } catch (IOException e) {
116                 throw new AppException(e.getMessage());
117             }
118         }
119     }
120 
121     /***
122      * Get the filters to be applied to file input
123      *
124      * @return The filters to use
125      */
126     protected Filter[] getFileFilters() {
127         return new Filter[] {
128             new TabsToSpacesFilter()
129         };
130     }
131 
132     /***
133      * Get file input
134      *
135      * @param inputFile The file to use
136      *
137      * @return File input
138      *
139      * @throws AppException in case of error
140      */
141     protected String[] getFileInput(File inputFile)
142     throws AppException {
143         try {
144             // Cria novo se não existe
145             inputFile.createNewFile();
146 
147             String inputLine;
148             BufferedReader in = new BufferedReader(new FileReader(inputFile));
149             List retVal = new ArrayList();
150 
151             try {
152                 while ((inputLine = in.readLine()) != null) {
153                     retVal.add(inputLine);
154                 }
155             } finally {
156                 in.close();
157             }
158             return (String[]) retVal.toArray(new String[0]);
159         } catch (FileNotFoundException ex) {
160             throw new AppException(ex.getMessage());
161         } catch (IOException ex) {
162             throw new AppException(ex.getMessage());
163         }
164     }
165 
166     /***
167      * Get output writer
168      *
169      * @return The output writter
170      */
171     protected OutputWriter getOutputWriter() {
172         return new UnixOutputWriter();
173     }
174 
175     /***
176      * Validade minimal mode options
177      *
178      * @throws AppException in case of error
179      */
180     protected void checkOptions()
181     throws AppException {
182         super.checkOptions();
183     }
184 }
185 
186 
187 // EOF