001/** 002 * Copyright (c) 2004-2011 QOS.ch 003 * All rights reserved. 004 * 005 * Permission is hereby granted, free of charge, to any person obtaining 006 * a copy of this software and associated documentation files (the 007 * "Software"), to deal in the Software without restriction, including 008 * without limitation the rights to use, copy, modify, merge, publish, 009 * distribute, sublicense, and/or sell copies of the Software, and to 010 * permit persons to whom the Software is furnished to do so, subject to 011 * the following conditions: 012 * 013 * The above copyright notice and this permission notice shall be 014 * included in all copies or substantial portions of the Software. 015 * 016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 017 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 018 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 021 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 023 * 024 */ 025package org.slf4j.migrator; 026 027import java.io.File; 028import java.io.IOException; 029import java.util.ArrayList; 030import java.util.List; 031 032import javax.swing.SwingUtilities; 033 034import org.slf4j.migrator.internal.MigratorFrame; 035import org.slf4j.migrator.internal.ProgressListener; 036import org.slf4j.migrator.line.RuleSet; 037 038public class ProjectConverter { 039 040 private final RuleSet ruleSet; 041 private List<ConversionException> exception; 042 043 ProgressListener progressListener; 044 045 public static void main(String[] args) { 046 SwingUtilities.invokeLater(() -> { 047 MigratorFrame inst = new MigratorFrame(); 048 inst.setLocationRelativeTo(null); 049 inst.setVisible(true); 050 }); 051 } 052 053 /** 054 * Ask for concrete matcher implementation depending on the conversion mode 055 * Ask for user confirmation to convert the selected source directory if valid 056 * Ask for user confirmation in case of number of files to convert > 1000 057 * 058 * @param conversionType 059 * @param progressListener 060 */ 061 public ProjectConverter(int conversionType, ProgressListener progressListener) { 062 this.progressListener = progressListener; 063 ruleSet = RuleSetFactory.getMatcherImpl(conversionType); 064 if (ruleSet == null) { 065 addException(new ConversionException(ConversionException.NOT_IMPLEMENTED)); 066 } 067 } 068 069 public void convertProject(File folder) { 070 FileSelector fs = new FileSelector(progressListener); 071 List<File> fileList = fs.selectJavaFilesInFolder(folder); 072 scanFileList(fileList); 073 progressListener.onDone(); 074 } 075 076 /** 077 * Convert a list of files 078 * 079 * @param lstFiles 080 */ 081 private void scanFileList(List<File> lstFiles) { 082 progressListener.onFileScanBegin(); 083 for (File currentFile : lstFiles) { 084 progressListener.onFileScan(currentFile); 085 scanFile(currentFile); 086 } 087 } 088 089 /** 090 * Convert the specified file Read each line and ask matcher implementation 091 * for conversion Rewrite the line returned by matcher 092 * 093 * @param file 094 */ 095 private void scanFile(File file) { 096 try { 097 InplaceFileConverter fc = new InplaceFileConverter(ruleSet, progressListener); 098 fc.convert(file); 099 } 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}