[slf4j-dev] svn commit: r894 - in slf4j/trunk/slf4j-converter/src: main/java/org/slf4j/converter test/java/org/slf4j/converter

jncharpin at slf4j.org jncharpin at slf4j.org
Fri Sep 14 18:51:42 CEST 2007


Author: jncharpin
Date: Fri Sep 14 18:51:41 2007
New Revision: 894

Added:
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/ConverterFrame.java
Modified:
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Constant.java
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Converter.java
   slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/PackageTest.java

Log:
On going work introducing Swing user interface to manage conversion.
To be continued....

Modified: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Constant.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Constant.java	(original)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Constant.java	Fri Sep 14 18:51:41 2007
@@ -2,28 +2,10 @@
 
 public class Constant {
 
-  public final static int JCL_TO_SLF4J = 1;
+  public final static int JCL_TO_SLF4J = 0;
   
-  public final static int LOG4J_TO_SLF4J = 2;
-
-  public final static String LINE_COMMENT = "//";
-
-  public final static String BLOCK_COMMENT_START = "/*";
-
-  public final static String BLOCK_COMMENT_END = "*/";
-
-  public final static Integer INDEX_0 = new Integer(0);
-
-  public final static Integer INDEX_1 = new Integer(1);
-
-  public final static Integer INDEX_2 = new Integer(2);
-
-  public final static Integer INDEX_3 = new Integer(3);
-
-  public final static Integer INDEX_4 = new Integer(4);
-
-  public final static Integer INDEX_5 = new Integer(5);
+  public final static int LOG4J_TO_SLF4J = 1;
 
   public final static int NB_FILES_MAX = 1;
 
-}
+}
\ No newline at end of file

Modified: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Converter.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Converter.java	(original)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Converter.java	Fri Sep 14 18:51:41 2007
@@ -1,222 +1,188 @@
-package org.slf4j.converter;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.FileReader;
-import java.io.BufferedReader;
-import java.io.InputStreamReader;
-import java.nio.channels.FileChannel;
-import java.util.Iterator;
-import java.util.List;
-import java.util.ArrayList;
-
-import javax.swing.JFileChooser;
-
-public class Converter {
-
-  private List<File> javaFiles;
-
-  private AbstractMatcher matcher;
-
-  private Writer writer;
-
-  private String source;
-
-  private int conversionType;
-
-  /**
-   * Run JCL to SLF4J conversion
-   * 
-   * @param args
-   *          source folder directory optional
-   * @throws IOException
-   * @throws IOException
-   */
-  public static void main(String[] args) throws IOException {
-
-    Converter converter = new Converter();
-
-    if (args.length > 0) {
-      converter.source = args[0];
-    } else {
-      JFileChooser selector = new JFileChooser();
-      selector.setDialogTitle("Source folder selector");
-      selector.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
-      int res = selector.showOpenDialog(null);
-      if (res == JFileChooser.APPROVE_OPTION) {
-        File folder = selector.getSelectedFile();
-        converter.source = folder.getAbsolutePath();
-      } else {
-        return;
-      }
-    }
-
-    converter.conversionType = Constant.JCL_TO_SLF4J;
-    if (converter.init()) {
-      converter.convert(converter.javaFiles);
-    }
-  }
-
-  /**
-   * Ask for concrete matcher implementation depending on the conversion mode
-   * Ask for user confirmation to convert the selected source directory if valid
-   * Ask for user confirmation in case of number of files to convert > 1000
-   * 
-   * @return true if init operation complete
-   * @throws IOException
-   */
-  public boolean init() throws IOException {
-    matcher = AbstractMatcher.getMatcherImpl(conversionType);
-    if (matcher == null) {
-      return false;
-    }
-
-    writer = new Writer();
-
-    File fileSource = new File(source);
-    if (!fileSource.isDirectory()) {
-      System.out.println("source path is not a valid source directory");
-      return false;
-    } else {
-      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
-      System.out
-          .println("RUNNING CONVERTER WILL REPLACE JAVA FILES CONTAINED IN "
-              + source + ", DO YOU WANT TO CONTINUE Y / N ?");
-      String response = in.readLine();
-      if (response.equalsIgnoreCase("N")) {
-        return false;
-      }
-
-      selectFiles(fileSource);
-
-      if (javaFiles.size() > Constant.NB_FILES_MAX) {
-        System.out.println("THERE IS " + javaFiles.size()
-            + " FILES TO CONVERT, DO YOU WANT TO CONTINUE Y / N ?");
-        response = in.readLine();
-        if (response.equalsIgnoreCase("N")) {
-          return false;
-        }
-      }
-    }
-    return true;
-  }
-
-  /**
-   * delete a file
-   * 
-   * @param fdest
-   */
-  private void delete(File fdest) {
-    if (fdest.isDirectory()) {
-      File[] files = fdest.listFiles();
-      if (files != null) {
-        for (int i = 0; i < files.length; i++) {
-          delete(files[i]);
-        }
-      }
-      fdest.delete();
-    } else {
-      fdest.delete();
-    }
-  }
-
-  /**
-   * copy a file from source to destination
-   * 
-   * @param fsource
-   * @param fdest
-   */
-  private void copy(File fsource, File fdest) {
-    try {
-      FileInputStream fis = new FileInputStream(fsource);
-      FileOutputStream fos = new FileOutputStream(fdest);
-      FileChannel channelSource = fis.getChannel();
-      FileChannel channelDest = fos.getChannel();
-      if (channelSource.isOpen() && channelDest.isOpen()) {
-        channelSource.transferTo(0, channelSource.size(), channelDest);
-        channelSource.close();
-        channelDest.close();
-      } else {
-        System.out.println("error copying file " + fsource.getAbsolutePath());
-      }
-
-    } catch (FileNotFoundException exc) {
-      System.out.println(exc.toString());
-    } catch (IOException e) {
-      System.out.println(e.toString());
-    }
-  }
-
-  /**
-   * Select java files to be converted
-   * 
-   * @param file
-   * @return
-   */
-  private List<File> selectFiles(File file) {
-    if (javaFiles == null) {
-      javaFiles = new ArrayList<File>();
-    }
-    if (file.isDirectory()) {
-      File[] files = file.listFiles();
-      if (files != null) {
-        for (int i = 0; i < files.length; i++) {
-          selectFiles(files[i]);
-        }
-      }
-    } else {
-      if (file.getName().endsWith(".java")) {
-        javaFiles.add(file);
-      }
-    }
-    return javaFiles;
-  }
-
-  /**
-   * Convert a list of files
-   * 
-   * @param lstFiles
-   */
-  private void convert(List<File> lstFiles) {
-    Iterator<File> itFile = lstFiles.iterator();
-    while (itFile.hasNext()) {
-      File currentFile = itFile.next();
-      convert(currentFile);
-    }
-  }
-
-  /**
-   * Convert the specified file Read each line and ask matcher implementation
-   * for conversion Rewrite the line returned by matcher
-   * 
-   * @param file
-   */
-  private void convert(File file) {
-    File newFile = new File(file.getAbsolutePath() + "new");
-    try {
-      boolean isEmpty = false;
-      writer.initFileWriter(newFile);
-      FileReader freader = new FileReader(file);
-      BufferedReader breader = new BufferedReader(freader);
-      String line;
-      String newLine;
-      while (!isEmpty) {
-        line = breader.readLine();
-        if (line != null) {
-          newLine = matcher.getReplacement(line);
-          writer.write(newLine);
-        } else {
-          isEmpty = true;
-          writer.closeFileWriter();
-          copy(newFile, file);
-          delete(newFile);
-        }
-      }
-    } catch (IOException exc) {
-      System.out.println("error reading file " + exc);
-    }
-  }
-}
+package org.slf4j.converter;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.IOException;
+import java.nio.channels.FileChannel;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+public class Converter {
+
+  private List<File> javaFiles;
+
+  private AbstractMatcher matcher;
+
+  private Writer writer;
+  
+  private File fileSource;
+
+  /**
+   * Run JCL to SLF4J conversion
+   * 
+   * @param args
+   *          source folder directory optional
+   * @throws IOException
+   * @throws IOException
+   */
+  public static void main(String[] args) throws IOException {
+
+    Converter converter = new Converter();
+
+    new ConverterFrame(converter);
+
+  }
+
+  /**
+   * Ask for concrete matcher implementation depending on the conversion mode
+   * Ask for user confirmation to convert the selected source directory if valid
+   * Ask for user confirmation in case of number of files to convert > 1000
+   * 
+   * @return true if init operation complete
+   * @throws IOException
+   */
+  public boolean init(String source, int conversionType) {
+    matcher = AbstractMatcher.getMatcherImpl(conversionType);
+    if (matcher == null) {
+      return false;
+    }
+
+    writer = new Writer();
+
+    fileSource = new File(source);
+    if (!fileSource.isDirectory()) {
+      return false;
+    } else {
+      return true;
+    }
+  }
+
+  /**
+   * delete a file
+   * 
+   * @param fdest
+   */
+  private void delete(File fdest) {
+    if (fdest.isDirectory()) {
+      File[] files = fdest.listFiles();
+      if (files != null) {
+        for (int i = 0; i < files.length; i++) {
+          delete(files[i]);
+        }
+      }
+      fdest.delete();
+    } else {
+      fdest.delete();
+    }
+  }
+
+  /**
+   * copy a file from source to destination
+   * 
+   * @param fsource
+   * @param fdest
+   */
+  private void copy(File fsource, File fdest) {
+    try {
+      FileInputStream fis = new FileInputStream(fsource);
+      FileOutputStream fos = new FileOutputStream(fdest);
+      FileChannel channelSource = fis.getChannel();
+      FileChannel channelDest = fos.getChannel();
+      if (channelSource.isOpen() && channelDest.isOpen()) {
+        channelSource.transferTo(0, channelSource.size(), channelDest);
+        channelSource.close();
+        channelDest.close();
+      } else {
+        System.out.println("error copying file " + fsource.getAbsolutePath());
+      }
+
+    } catch (FileNotFoundException exc) {
+      System.out.println(exc.toString());
+    } catch (IOException e) {
+      System.out.println(e.toString());
+    }
+  }
+
+  /**
+   * Select java files to be converted
+   * 
+   * @param file
+   * @return
+   */
+  private List<File> selectFiles(File file) {
+    if (javaFiles == null) {
+      javaFiles = new ArrayList<File>();
+    }
+    if (file.isDirectory()) {
+      File[] files = file.listFiles();
+      if (files != null) {
+        for (int i = 0; i < files.length; i++) {
+          selectFiles(files[i]);
+        }
+      }
+    } else {
+      if (file.getName().endsWith(".java")) {
+        javaFiles.add(file);
+      }
+    }
+    return javaFiles;
+  }
+
+  public void convert() {
+    convert(javaFiles);
+  }
+  
+  public int selectFiles(){
+    return selectFiles(fileSource).size();
+  }
+
+  /**
+   * Convert a list of files
+   * 
+   * @param lstFiles
+   */
+  private void convert(List<File> lstFiles) {
+    Iterator<File> itFile = lstFiles.iterator();
+    while (itFile.hasNext()) {
+      File currentFile = itFile.next();
+      convert(currentFile);
+    }
+  }
+
+  /**
+   * Convert the specified file Read each line and ask matcher implementation
+   * for conversion Rewrite the line returned by matcher
+   * 
+   * @param file
+   */
+  private void convert(File file) {
+    File newFile = new File(file.getAbsolutePath() + "new");
+    try {
+      boolean isEmpty = false;
+      writer.initFileWriter(newFile);
+      FileReader freader = new FileReader(file);
+      BufferedReader breader = new BufferedReader(freader);
+      String line;
+      String newLine;
+      while (!isEmpty) {
+        line = breader.readLine();
+        if (line != null) {
+          newLine = matcher.getReplacement(line);
+          writer.write(newLine);
+        } else {
+          isEmpty = true;
+          writer.closeFileWriter();
+          copy(newFile, file);
+          delete(newFile);
+        }
+      }
+    } catch (IOException exc) {
+      System.out.println("error reading file " + exc);
+    }
+  }
+}
\ No newline at end of file

Added: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/ConverterFrame.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/ConverterFrame.java	Fri Sep 14 18:51:41 2007
@@ -0,0 +1,108 @@
+package org.slf4j.converter;
+
+import java.awt.Dimension;
+import java.awt.FlowLayout;
+import java.awt.GridLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.io.File;
+
+import javax.swing.JButton;
+import javax.swing.JComboBox;
+import javax.swing.JFileChooser;
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+
+public class ConverterFrame extends JFrame implements ActionListener {
+
+  private Converter converter;
+  private JButton butCancel;
+  private JButton butNext;
+  private JComboBox combo;
+  private JPanel principalPan;
+  private JPanel confirmationPan;
+  private JButton butYes;
+  private JButton butNo;
+
+  public ConverterFrame(Converter parent) {
+    this.converter = parent;
+
+    setTitle("SLF4J CONVERTER");
+    // setIconImage();
+
+    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+    setPreferredSize(new Dimension(300, 200));
+    setLocation(100, 100);
+
+    principalPan = new JPanel();
+    FlowLayout fl = new FlowLayout();
+    principalPan.setLayout(fl);
+
+    confirmationPan = new JPanel();
+    confirmationPan.setLayout(fl);
+
+    JLabel lab = new JLabel("Conversion Mode");
+    principalPan.add(lab);
+    String choice[] = { "JCL to SLF4J", "Log4J to SLF4J" };
+    combo = new JComboBox(choice);
+    principalPan.add(combo);
+
+    butCancel = new JButton("Cancel");
+    butCancel.addActionListener(this);
+    principalPan.add(butCancel);
+    butNext = new JButton("Next");
+    butNext.addActionListener(this);
+    principalPan.add(butNext);
+
+    setContentPane(principalPan);
+    pack();
+    setVisible(true);
+  }
+
+  public void actionPerformed(ActionEvent e) {
+    if (e.getSource() == butCancel) {
+      dispose();
+    } else if (e.getSource() == butNext) {
+      showFolderSelector();
+    } else if (e.getSource() == butYes) {
+      converter.convert();
+      dispose();
+    } else if (e.getSource() == butNo) {
+      dispose();
+    }
+  }
+
+
+  public void showFolderSelector() {
+    JFileChooser selector = new JFileChooser();
+    selector.setDialogTitle("Source folder selector");
+    selector.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
+    int res = selector.showOpenDialog(this);
+    if (res == JFileChooser.APPROVE_OPTION) {
+      File folder = selector.getSelectedFile();
+      if (!converter.init(folder.getAbsolutePath(), combo.getSelectedIndex())) {
+        return;
+      } else {
+        showFileConversionConfirmationPan(converter.selectFiles());
+      }
+    }
+  }
+
+  public void showFileConversionConfirmationPan(int nbFiles) {
+    JLabel lab;
+    lab = new JLabel("RUNNING CONVERTER WILL REPLACE " + nbFiles
+        + " JAVA FILES CONTAINED IN SELECTED FOLDER, DO YOU WANT TO CONTINUE ?");
+    confirmationPan.add(lab);
+    butNo = new JButton("No");
+    butNo.addActionListener(this);
+    confirmationPan.add(butNo);
+    butYes = new JButton("Yes");
+    butYes.addActionListener(this);
+    confirmationPan.add(butYes);
+    principalPan.add(confirmationPan);
+    setSize(900, 200);
+  }
+
+}
\ No newline at end of file

Modified: slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/PackageTest.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/PackageTest.java	(original)
+++ slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/PackageTest.java	Fri Sep 14 18:51:41 2007
@@ -1,39 +1,40 @@
-/* 
- * Copyright (c) 2004-2007 QOS.ch
- * All rights reserved.
- * 
- * Permission is hereby granted, free  of charge, to any person obtaining
- * a  copy  of this  software  and  associated  documentation files  (the
- * "Software"), to  deal in  the Software without  restriction, including
- * without limitation  the rights to  use, copy, modify,  merge, publish,
- * distribute,  sublicense, and/or sell  copies of  the Software,  and to
- * permit persons to whom the Software  is furnished to do so, subject to
- * the following conditions:
- * 
- * The  above  copyright  notice  and  this permission  notice  shall  be
- * included in all copies or substantial portions of the Software.
- * 
- * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
- * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
- * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
- * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
-
-package org.slf4j.converter;
-
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
-public class PackageTest extends TestCase {
-
-  public static Test suite() {
-    TestSuite suite = new TestSuite();
-    suite.addTestSuite(TrivialMatcherTest.class);
-    suite.addTestSuite(JCLMatcherTest.class);
-    return suite;
-  }
-}
+/* 
+ * Copyright (c) 2004-2007 QOS.ch
+ * All rights reserved.
+ * 
+ * Permission is hereby granted, free  of charge, to any person obtaining
+ * a  copy  of this  software  and  associated  documentation files  (the
+ * "Software"), to  deal in  the Software without  restriction, including
+ * without limitation  the rights to  use, copy, modify,  merge, publish,
+ * distribute,  sublicense, and/or sell  copies of  the Software,  and to
+ * permit persons to whom the Software  is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The  above  copyright  notice  and  this permission  notice  shall  be
+ * included in all copies or substantial portions of the Software.
+ * 
+ * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
+ * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
+ * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+package org.slf4j.converter;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class PackageTest extends TestCase {
+
+  public static Test suite() {
+    TestSuite suite = new TestSuite();
+    suite.addTestSuite(TrivialMatcherTest.class);
+    suite.addTestSuite(JCLMatcherTest.class);
+    suite.addTestSuite(AternativeApproach.class);
+    return suite;
+  }
+}
\ No newline at end of file



More information about the slf4j-dev mailing list