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

jncharpin at slf4j.org jncharpin at slf4j.org
Fri Jun 8 14:35:24 CEST 2007


Author: jncharpin
Date: Fri Jun  8 14:35:23 2007
New Revision: 819

Added:
   slf4j/trunk/slf4j-converter/src/
   slf4j/trunk/slf4j-converter/src/main/
   slf4j/trunk/slf4j-converter/src/main/java/
   slf4j/trunk/slf4j-converter/src/main/java/org/
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/AbstractMatcher.java
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Converter.java
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Converterold.java
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/JCLMatcher.java
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Writer.java
   slf4j/trunk/slf4j-converter/src/test/
   slf4j/trunk/slf4j-converter/src/test/java/
   slf4j/trunk/slf4j-converter/src/test/java/org/
   slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/
   slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/
   slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/ConverterTest.java
Modified:
   slf4j/trunk/slf4j-converter/pom.xml

Log:
on going work 

Modified: slf4j/trunk/slf4j-converter/pom.xml
==============================================================================
--- slf4j/trunk/slf4j-converter/pom.xml	(original)
+++ slf4j/trunk/slf4j-converter/pom.xml	Fri Jun  8 14:35:23 2007
@@ -6,8 +6,8 @@
  
 	<parent>
 		<groupId>org.slf4j</groupId>
-		<artifactId>slf4j-parent</artifactId>
-    	<version>1.4.0-SNAPSHOT</version>
+		<artifactId>slf4j</artifactId>
+    	<version>1.4.0</version>
 	</parent>
 	
 	<modelVersion>4.0.0</modelVersion>

Added: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/AbstractMatcher.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/AbstractMatcher.java	Fri Jun  8 14:35:23 2007
@@ -0,0 +1,50 @@
+package org.slf4j.converter;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.slf4j.Logger;
+
+public abstract class AbstractMatcher {
+
+  protected Logger logger;
+
+  protected HashMap<Pattern, String> rulesMap;
+
+  protected Writer writer;
+
+  public static AbstractMatcher getMatcherImpl() {
+    // TODO criterias
+    return new JCLMatcher();
+  }
+
+  public void setWriter(Writer writer) {
+    this.writer = writer;
+  }
+
+  public void matches(String text) {
+    Pattern pattern;
+    Matcher matcher;
+    String replacement;
+    Iterator rulesIter = rulesMap.keySet().iterator();
+    boolean found = false;
+    while (rulesIter.hasNext()) {      
+      pattern = (Pattern) rulesIter.next();
+      matcher = pattern.matcher(text);//
+      if (matcher.find()) {        
+        logger.info("found " + text);
+        replacement = (String) rulesMap.get(pattern);
+        writer.rewrite(matcher, replacement);        
+        found = true;  
+        break;
+      }      
+    }
+    if(!found){
+      writer.write(text);
+    }
+  }
+
+  protected abstract void initRules();
+}

Added: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Converter.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Converter.java	Fri Jun  8 14:35:23 2007
@@ -0,0 +1,214 @@
+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.nio.channels.FileChannel;
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.ArrayList;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class Converter {
+  private Logger logger;
+
+  private List<File> javaFiles;
+
+  private AbstractMatcher matcher;
+
+  private Writer writer;
+
+  private String source;
+
+  private String destination;
+
+  /**
+   * @param args
+   */
+  public static void main(String[] args) {
+
+    Converter converter = new Converter();
+    converter.init();
+
+    converter.getPaths();
+    File fileSource = converter.initSource();
+    File fileDest = converter.initDestination();
+    converter.copy(fileSource);
+    converter.selectFiles(fileDest);
+    converter.convert(converter.javaFiles);
+  }
+
+  public void init() {
+    logger = LoggerFactory.getLogger(Converter.class);
+    matcher = AbstractMatcher.getMatcherImpl();
+    writer = new Writer();
+    matcher.setWriter(writer);
+  }
+
+  /**
+   * 
+   * 
+   */
+  private void getPaths() {
+    source = "c:/projets/slf4j/slf4j-converter/src";
+    destination = "c:/projets/slf4j/slf4j-converter/witness";
+  }
+
+  /**
+   * 
+   * @return
+   */
+  private File initSource() {
+    File fileSource = new File(source);
+    if (!fileSource.isDirectory()) {
+      logger.info("source path is not a valid source directory");
+    }
+    return fileSource;
+  }
+
+  /**
+   * 
+   * @return
+   */
+  private File initDestination() {
+    File fileDest = new File(destination);
+    if (fileDest.exists()) {
+      delete(fileDest);
+    }
+    fileDest.mkdir();
+    return fileDest;
+  }
+
+  /**
+   * 
+   * @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();
+      //logger.info("Deleting " + fdest.getName());
+    } else {
+      //logger.info("Deleting " + fdest.getName());
+      fdest.delete();
+    }
+  }
+
+  /**
+   * 
+   * @param fsource
+   */
+  private void copy(File fsource) {
+    String curentFileName = fsource.getAbsolutePath()
+        .substring(source.length());
+    File fdest = new File(destination + "/" + curentFileName);
+    if (fsource.isDirectory()) {
+      //logger.info("Current directory " + fsource.getAbsolutePath());
+      fdest.mkdir();
+      //logger.info("New directory " + fdest.getAbsolutePath());
+      File[] files = fsource.listFiles();
+      if (files != null) {
+        for (int i = 0; i < files.length; i++) {
+          copy(files[i]);
+        }
+      }
+    } else {
+          copy(fsource, 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);
+        //logger.info("file " + fsource.getName() + " transfered");
+        channelSource.close();
+        channelDest.close();
+      } else {
+        logger.error("error copying file " + fsource.getAbsolutePath());
+      }
+
+    } catch (FileNotFoundException exc) {
+      logger.error(exc.toString());
+    } catch (IOException e) {
+      logger.error(e.toString());
+    }
+  }
+  
+  /**
+   * 
+   * @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);
+        //logger.info("Adding java file " + file.getAbsolutePath());
+      }
+    }
+    return javaFiles;
+  }
+
+  /**
+   * 
+   * @param lstFiles
+   */
+  private void convert(List<File> lstFiles) {
+    Iterator<File> itFile = lstFiles.iterator();
+    while (itFile.hasNext()) {
+      File currentFile = itFile.next();
+      File newFile = new File(currentFile.getAbsolutePath()+"new");
+      logger.info("reading file " + currentFile.getAbsolutePath());
+      try {
+        boolean isEmpty = false;
+        writer.initFileWriter(newFile);
+        FileReader freader = new FileReader(currentFile);
+        BufferedReader breader = new BufferedReader(freader);
+        String line;        
+        while (!isEmpty) {
+          line = breader.readLine();
+          if (line != null) {
+            //logger.info("reading line " + line);
+            matcher.matches(line);
+          } else {
+            isEmpty = true;
+            writer.closeFileWriter();    
+            copy(newFile, currentFile);
+            delete(newFile);
+          }
+        }
+      } catch (IOException exc) {
+        logger.error("error reading file " + exc);
+      }
+    }
+  }
+}

Added: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Converterold.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Converterold.java	Fri Jun  8 14:35:23 2007
@@ -0,0 +1,229 @@
+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.nio.channels.FileChannel;
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.ArrayList;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class Converterold {
+  private Logger logger;
+
+  private List<File> javaFiles;
+
+  private String source;
+
+  private String destination;
+
+  private static Pattern regex = Pattern.compile("converter");
+
+  /**
+   * @param args
+   */
+  public static void main(String[] args) {
+
+    Converterold converter = new Converterold();
+    converter.init();
+
+    converter.getPaths();
+    File fileSource = converter.initSource();
+    File fileDest = converter.initDestination();
+    converter.copy(fileSource);
+    converter.selectFiles(fileDest);
+    converter.convert(converter.javaFiles);
+  }
+
+  public void init() {
+    logger = LoggerFactory.getLogger(Converterold.class);
+  }
+
+  /**
+   * 
+   * 
+   */
+  private void getPaths() {
+    source = "c:/projets/slf4j/slf4j-converter/src";
+    destination = "c:/temp";
+  }
+
+  /**
+   * 
+   * @return
+   */
+  private File initSource() {
+    File fileSource = new File(source);
+    if (!fileSource.isDirectory()) {
+      logger.info("source path is not a valid source directory");
+    }
+    return fileSource;
+  }
+
+  /**
+   * 
+   * @return
+   */
+  private File initDestination() {
+    File fileDest = new File(destination);
+    if (fileDest.exists()) {
+      delete(fileDest);
+    }
+    fileDest.mkdir();
+    return fileDest;
+  }
+
+  /**
+   * 
+   * @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();
+      logger.info("Deleting " + fdest.getName());
+    } else {
+      logger.info("Deleting " + fdest.getName());
+      fdest.delete();
+    }
+  }
+
+  /**
+   * 
+   * @param fsource
+   */
+  private void copy(File fsource) {
+    String curentFileName = fsource.getAbsolutePath()
+        .substring(source.length());
+    File fdest = new File(destination + "/" + curentFileName);
+    if (fsource.isDirectory()) {
+      logger.info("Current directory " + fsource.getAbsolutePath());
+      fdest.mkdir();
+      logger.info("New directory " + fdest.getAbsolutePath());
+      File[] files = fsource.listFiles();
+      if (files != null) {
+        for (int i = 0; i < files.length; i++) {
+          copy(files[i]);
+        }
+      }
+    } else {
+      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);
+          logger.info("file " + fsource.getName() + " transfered");
+          channelSource.close();
+          channelDest.close();
+        } else {
+          logger.error("error copying file " + fsource.getAbsolutePath());
+        }
+
+      } catch (FileNotFoundException exc) {
+        logger.error(exc.toString());
+      } catch (IOException e) {
+        logger.error(e.toString());
+      }
+    }
+  }
+
+  /**
+   * 
+   * @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);
+        logger.info("Adding java file " + file.getAbsolutePath());
+      }
+    }
+    return javaFiles;
+  }
+
+  /**
+   * 
+   * @param lstFiles
+   */
+  private void convert(List<File> lstFiles) {
+    Iterator<File> itFile = lstFiles.iterator();
+    while (itFile.hasNext()) {
+      File currentFile = itFile.next();
+      logger.info("reading file " + currentFile.getAbsolutePath());
+      try {
+        boolean isEmpty = false;
+        FileReader freader = new FileReader(currentFile);
+        BufferedReader breader = new BufferedReader(freader);
+        String line = breader.readLine();
+        logger.info("reading line " + line);
+        while (!isEmpty) {
+          line = breader.readLine();
+          if (line != null) {
+            logger.info("reading line " + line);
+            Matcher matcher = isMatching(line);
+            if (matcher != null) {
+              replace(matcher, line);
+            }
+          } else {
+            isEmpty = true;
+          }
+        }
+      } catch (IOException exc) {
+        logger.error("error reading file " + exc);
+      }
+    }
+  }
+
+  public Matcher isMatching(String line) {
+    Matcher matcher = regex.matcher(line);
+    if (matcher.find()) {
+      logger.info("found regex in" + line);
+      return matcher;
+    }
+    // if(matcher.matches()){
+    // logger.info("found regex " + line);
+    // return true;
+    // }
+    return null;
+  }
+
+  /**
+   * Rewrite the line
+   * 
+   * @param line
+   */
+  public void replace(Matcher matcher, String line) {
+    // String replacement = getReplacement(regex);
+    logger.info("replacing into" + line);
+    line = matcher.replaceAll("");
+    logger.info("new line " + line);
+  }
+}

Added: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/JCLMatcher.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/JCLMatcher.java	Fri Jun  8 14:35:23 2007
@@ -0,0 +1,21 @@
+package org.slf4j.converter;
+
+import java.util.HashMap;
+import java.util.regex.Pattern;
+
+import org.slf4j.LoggerFactory;
+
+public class JCLMatcher extends AbstractMatcher {
+
+  public JCLMatcher() {
+    super();
+    logger = LoggerFactory.getLogger(JCLMatcher.class);
+    initRules();
+  }
+
+  protected void initRules() {
+    rulesMap = new HashMap<Pattern, String>();
+    rulesMap.put(Pattern.compile("import org.slf4j.Logger;"), "import org.toto.Log;");
+    rulesMap.put(Pattern.compile("Logger"), "Log");    
+  }
+}

Added: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Writer.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Writer.java	Fri Jun  8 14:35:23 2007
@@ -0,0 +1,66 @@
+package org.slf4j.converter;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.regex.Matcher;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class Writer {
+
+  Logger logger;
+
+  BufferedWriter bwriter;
+
+  public Writer() {
+    logger = LoggerFactory.getLogger(Writer.class);
+  }
+
+  public void initFileWriter(File file) {
+    try {
+      FileWriter fileWriter = new FileWriter(file);
+      bwriter = new BufferedWriter(fileWriter);
+    } catch (IOException exc) {
+      logger.error("error creating filewriter " + file.getAbsolutePath());
+    }
+  }
+
+  public void closeFileWriter() {
+    if (bwriter != null) {
+      try {
+        bwriter.flush();
+        bwriter.close();
+      } catch (IOException e) {
+        logger.error("error closing filewriter " + bwriter.toString());
+      }
+    }
+  }
+
+  public void rewrite(Matcher matcher, String replacement) {
+    String text = matcher.replaceAll(replacement);    
+    if (bwriter != null) {
+      try {
+        bwriter.newLine();
+        bwriter.write(text);
+        logger.info("new entry "+text);
+      } catch (IOException exc) {
+        logger.error("error writing file " + bwriter.toString());
+      }
+    }
+  }
+
+  public void write(String text) {
+    if (bwriter != null) {
+      try {
+        bwriter.newLine();
+        bwriter.write(text);
+      } catch (IOException exc) {
+        logger.error("error writing file " + bwriter.toString());
+      }
+    }
+  }
+
+}

Added: slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/ConverterTest.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/ConverterTest.java	Fri Jun  8 14:35:23 2007
@@ -0,0 +1,21 @@
+package org.slf4j.converter;
+
+import junit.framework.TestCase;
+
+public class ConverterTest extends TestCase {
+
+  public void testMatch() {
+//   TestMatcher matcher = new TestMatcher();
+//    String smatch = "converter";
+//    assertNotNull(matcher.matches(smatch));
+//    smatch = " converter";
+//    assertNotNull(matcher.matches(smatch));
+//    smatch = " converter toto";
+//    assertNotNull(matcher.matches(smatch));
+//    smatch = "blabla converter toto";
+//    assertNotNull(matcher.matches(smatch));
+//    smatch = "con verter";
+//    assertNull(matcher.matches(smatch));
+    }
+
+}



More information about the slf4j-dev mailing list