View Javadoc
1   /**
2    * Copyright (c) 2004-2011 QOS.ch
3    * All rights reserved.
4    *
5    * Permission is hereby granted, free  of charge, to any person obtaining
6    * a  copy  of this  software  and  associated  documentation files  (the
7    * "Software"), to  deal in  the Software without  restriction, including
8    * without limitation  the rights to  use, copy, modify,  merge, publish,
9    * distribute,  sublicense, and/or sell  copies of  the Software,  and to
10   * permit persons to whom the Software  is furnished to do so, subject to
11   * the following conditions:
12   *
13   * The  above  copyright  notice  and  this permission  notice  shall  be
14   * included in all copies or substantial portions of the Software.
15   *
16   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
17   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
18   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
22   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23   *
24   */
25  package org.slf4j.migrator;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  import javax.swing.SwingUtilities;
33  
34  import org.slf4j.migrator.internal.MigratorFrame;
35  import org.slf4j.migrator.internal.ProgressListener;
36  import org.slf4j.migrator.line.RuleSet;
37  
38  public class ProjectConverter {
39  
40      private final RuleSet ruleSet;
41      private List<ConversionException> exception;
42  
43      ProgressListener progressListener;
44  
45      public static void main(String[] args) {
46          SwingUtilities.invokeLater(() -> {
47              MigratorFrame inst = new MigratorFrame();
48              inst.setLocationRelativeTo(null);
49              inst.setVisible(true);
50          });
51      }
52  
53      /**
54       * Ask for concrete matcher implementation depending on the conversion mode
55       * Ask for user confirmation to convert the selected source directory if valid
56       * Ask for user confirmation in case of number of files to convert &gt; 1000
57       *
58       * @param conversionType 
59       * @param progressListener 
60       */
61      public ProjectConverter(int conversionType, ProgressListener progressListener) {
62          this.progressListener = progressListener;
63          ruleSet = RuleSetFactory.getMatcherImpl(conversionType);
64          if (ruleSet == null) {
65              addException(new ConversionException(ConversionException.NOT_IMPLEMENTED));
66          }
67      }
68  
69      public void convertProject(File folder) {
70          FileSelector fs = new FileSelector(progressListener);
71          List<File> fileList = fs.selectJavaFilesInFolder(folder);
72          scanFileList(fileList);
73          progressListener.onDone();
74      }
75  
76      /**
77       * Convert a list of files
78       * 
79       * @param lstFiles
80       */
81      private void scanFileList(List<File> lstFiles) {
82          progressListener.onFileScanBegin();
83          for (File currentFile : lstFiles) {
84              progressListener.onFileScan(currentFile);
85              scanFile(currentFile);
86          }
87      }
88  
89      /**
90       * Convert the specified file Read each line and ask matcher implementation
91       * for conversion Rewrite the line returned by matcher
92       * 
93       * @param file
94       */
95      private void scanFile(File file) {
96          try {
97              InplaceFileConverter fc = new InplaceFileConverter(ruleSet, progressListener);
98              fc.convert(file);
99          } catch (IOException exc) {
100             addException(new ConversionException(exc.toString()));
101         }
102     }
103 
104     public void addException(ConversionException exc) {
105         if (exception == null) {
106             exception = new ArrayList<>();
107         }
108         exception.add(exc);
109     }
110 
111     public void printException() {
112         if (exception != null) {
113             for (ConversionException exc : exception) {
114                 exc.print();
115             }
116             exception = null;
117         }
118     }
119 }