ProjectConverter xref

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.Iterator;
    31  import java.util.List;
    32
    33  import javax.swing.SwingUtilities;
    34
    35  import org.slf4j.migrator.internal.MigratorFrame;
    36  import org.slf4j.migrator.internal.ProgressListener;
    37  import org.slf4j.migrator.line.RuleSet;
    38
    39  public class ProjectConverter {
    40
    41    private RuleSet ruleSet;
    42    private List<ConversionException> exception;
    43
    44    ProgressListener progressListener;
    45
    46    public static void main(String[] args) throws IOException {
    47      SwingUtilities.invokeLater(new Runnable() {
    48        public void run() {
    49          MigratorFrame inst = new MigratorFrame();
    50          inst.setLocationRelativeTo(null);
    51          inst.setVisible(true);
    52        }
    53      });
    54    }
    55
    56    /**
    57     * Ask for concrete matcher implementation depending on the conversion mode
    58     * Ask for user confirmation to convert the selected source directory if valid
    59     * Ask for user confirmation in case of number of files to convert > 1000
    60     *
    61     * @param conversionType
    62     * @param progressListener
    63     */
    64    public ProjectConverter(int conversionType, ProgressListener progressListener) {
    65      this.progressListener = progressListener;
    66      ruleSet = RuleSetFactory.getMatcherImpl(conversionType);
    67      if (ruleSet == null) {
    68        addException(new ConversionException(ConversionException.NOT_IMPLEMENTED));
    69      }
    70    }
    71
    72    public void convertProject(File folder) {
    73      FileSelector fs = new FileSelector(progressListener);
    74      List<File> fileList = fs.selectJavaFilesInFolder(folder);
    75      scanFileList(fileList);
    76      progressListener.onDone();
    77    }
    78
    79    /**
    80     * Convert a list of files
    81     *
    82     * @param lstFiles
    83     */
    84    private void scanFileList(List<File> lstFiles) {
    85      progressListener.onFileScanBegin();
    86      Iterator<File> itFile = lstFiles.iterator();
    87      while (itFile.hasNext()) {
    88        File currentFile = itFile.next();
    89        progressListener.onFileScan(currentFile);
    90        scanFile(currentFile);
    91      }
    92    }
    93
    94    /**
    95     * Convert the specified file Read each line and ask matcher implementation
    96     * for conversion Rewrite the line returned by matcher
    97     *
    98     * @param file
    99     */
    100   private void scanFile(File file) {
    101     try {
    102       InplaceFileConverter fc = new InplaceFileConverter(ruleSet,
    103           progressListener);
    104       fc.convert(file);
    105     } catch (IOException exc) {
    106       addException(new ConversionException(exc.toString()));
    107     }
    108   }
    109
    110   public void addException(ConversionException exc) {
    111     if (exception == null) {
    112       exception = new ArrayList<ConversionException>();
    113     }
    114     exception.add(exc);
    115   }
    116
    117   public void printException() {
    118     if (exception != null) {
    119       Iterator<ConversionException> iterator = exception.iterator();
    120       while (iterator.hasNext()) {
    121         ConversionException exc = (ConversionException) iterator.next();
    122         exc.print();
    123       }
    124       exception = null;
    125     }
    126   }
    127 }