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

jncharpin at slf4j.org jncharpin at slf4j.org
Tue Jul 10 22:37:06 CEST 2007


Author: jncharpin
Date: Tue Jul 10 22:37:06 2007
New Revision: 840

Added:
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/ReplacementWrapper.java
Removed:
   slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/ConverterTest.java
Modified:
   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/JCLMatcher.java
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/PatternWrapper.java

Log:
on going work  adding JCL to SLF4j conversion rules

Modified: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/AbstractMatcher.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/AbstractMatcher.java	(original)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/AbstractMatcher.java	Tue Jul 10 22:37:06 2007
@@ -9,54 +9,69 @@
 
 public abstract class AbstractMatcher {
 
-  protected Logger logger;
+	protected Logger logger;
 
-  protected TreeMap<PatternWrapper, String> rulesMap;
+	protected TreeMap<PatternWrapper, ReplacementWrapper> rulesMap;
 
-  protected Writer writer;
+	protected Writer writer;
 
-  public AbstractMatcher() {
-  }
+	public AbstractMatcher() {
+	}
 
-  public static AbstractMatcher getMatcherImpl() {
-    // TODO criterias
-    return new JCLMatcher();
-  }
-
-  public void setWriter(Writer writer) {
-    this.writer = writer;
-  }
-
-  public void matches(String text) {
-    PatternWrapper patternWrapper;
-    Pattern pattern;
-    Matcher matcher;
-    String replacement;
-    Iterator rulesIter = rulesMap.keySet().iterator();
-    boolean found = false;
-    while (rulesIter.hasNext()) {
-      patternWrapper = (PatternWrapper) rulesIter.next();
-      pattern = patternWrapper.getPattern();      
-      matcher = pattern.matcher(text);
-      if (matcher.matches()) {
-        logger.info("match " + text);
-        replacement = (String) rulesMap.get(patternWrapper);
-        writer.rewrite(matcher, replacement);
-        found = true;
-        break;
-      } 
-//      else if (matcher.find()) {
-//        logger.info("found " + text + " pattern " + pattern.toString());
-//        replacement = (String) rulesMap.get(patternWrapper);
-//        writer.rewrite(matcher, replacement);
-//        found = true;
-//        break;
-//      }
-    }
-    if (!found) {
-      writer.write(text);
-    }
-  }
+	public static AbstractMatcher getMatcherImpl() {
+		// TODO criterias
+		return new JCLMatcher();
+	}
+
+	public void setWriter(Writer writer) {
+		this.writer = writer;
+	}
+
+	public void matches(String text) {
+		PatternWrapper patternWrapper;
+		Pattern pattern;
+		Matcher matcher;
+		String replacementText;
+		ReplacementWrapper replacementWrapper;
+		Iterator rulesIter = rulesMap.keySet().iterator();
+		boolean found = false;
+		while (rulesIter.hasNext()) {
+			patternWrapper = (PatternWrapper) rulesIter.next();
+			pattern = patternWrapper.getPattern();
+			matcher = pattern.matcher(text);
+			if (matcher.matches()) {
+				logger.info("match " + text);
+				replacementWrapper = rulesMap.get(patternWrapper);
+				StringBuffer replacementBuffer = new StringBuffer();
+				for (int group = 0; group <= matcher.groupCount(); group++) {
+					replacementText = replacementWrapper.getReplacement(group);
+					if (replacementText != null) {
+						logger.info("replacing group " + group + " : "
+								+ matcher.group(group) + " with "
+								+ replacementText);
+						replacementBuffer.append(replacementText);
+					} else if (group > 0) {
+						logger.info("conserving group " + group + " : "
+								+ matcher.group(group));
+						replacementBuffer.append(matcher.group(group));
+					}
+				}
+				writer.rewrite(matcher, replacementBuffer.toString());
+				found = true;
+				break;
+			}
+			// else if (matcher.find()) {
+			// logger.info("found " + text + " pattern " + pattern.toString());
+			// replacement = (String) rulesMap.get(patternWrapper);
+			// writer.rewrite(matcher, replacement);
+			// found = true;
+			// break;
+			// }
+		}
+		if (!found) {
+			writer.write(text);
+		}
+	}
 
-  protected abstract void initRules();
+	protected abstract void initRules();
 }

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	Tue Jul 10 22:37:06 2007
@@ -8,9 +8,6 @@
 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;
@@ -19,196 +16,196 @@
 import org.slf4j.LoggerFactory;
 
 public class Converter {
-  private Logger logger;
+	private Logger logger;
 
-  private List<File> javaFiles;
+	private List<File> javaFiles;
 
-  private AbstractMatcher matcher;
+	private AbstractMatcher matcher;
 
-  private Writer writer;
+	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);
-      }
-    }
-  }
+	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 = "/Users/jean-noelcharpin/dev/slf4j/slf4j-converter/jcl";
+		destination = "/Users/jean-noelcharpin/dev/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);
+			}
+		}
+	}
 }

Modified: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/JCLMatcher.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/JCLMatcher.java	(original)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/JCLMatcher.java	Tue Jul 10 22:37:06 2007
@@ -1,21 +1,32 @@
 package org.slf4j.converter;
 
 import java.util.TreeMap;
-import java.util.HashMap;
 import java.util.regex.Pattern;
 
 import org.slf4j.LoggerFactory;
 
 public class JCLMatcher extends AbstractMatcher {
 
-  private static PatternWrapper p1 =  new PatternWrapper("1",Pattern.compile("import org.apache.commons.logging.LogFactory;"));
-  private static PatternWrapper p2 =  new PatternWrapper("2",Pattern.compile("import org.apache.commons.logging.Log"));
-  private static PatternWrapper p3 =  new PatternWrapper("3",Pattern.compile("LogFactory.getFactory().getInstance"));
-  private static PatternWrapper p4 =  new PatternWrapper("4",Pattern.compile("LogFactory.getLog"));
-  private static PatternWrapper p5 =  new PatternWrapper("5",Pattern.compile("Log"));
-//private static Pattern p6 = new PatternWrapper("6",Pattern.compile("fatal\("));
-//private static Pattern p7 = new PatternWrapper("7",Pattern.compile("isFatalEnabled\("));
-  
+   private static PatternWrapper p0 =  new PatternWrapper("0",Pattern.compile("import\\s*+org.apache.commons.logging.LogFactory;"));
+   private static PatternWrapper p1 =  new PatternWrapper("1",Pattern.compile("import\\s*+org.apache.commons.logging.Log;"));
+   private static PatternWrapper p2 =  new PatternWrapper("2",Pattern.compile("(Log)(\\s+\\w+\\s*+=\\s*+)(LogFactory.getFactory\\(\\).getInstance\\()(\\w+)(.class\\);)"));
+   private static PatternWrapper p3 =  new PatternWrapper("3",Pattern.compile("(Log)(\\s+\\w+\\s*+=\\s*+)(LogFactory.getLog\\()(\\w+)(.class\\);)"));
+   private static PatternWrapper p4 =  new PatternWrapper("4",Pattern.compile("(LogFactory.getFactory\\(\\).getInstance\\()(\\w+)(.class\\);)"));
+   private static PatternWrapper p5 =  new PatternWrapper("5",Pattern.compile("(LogFactory.getLog\\()(\\w+)(.class\\);)"));
+   private static PatternWrapper p6 =  new PatternWrapper("6",Pattern.compile("(Log)(\\s+(\\w+)\\s*;)"));
+//   private static PatternWrapper p8 = new PatternWrapper("8",Pattern.compile("fatal\\("));
+//   private static PatternWrapper p9 = new PatternWrapper("9",Pattern.compile("isFatalEnabled\\("));
+ 
+   private static ReplacementWrapper r0 = new ReplacementWrapper("0","import org.slf4j.LoggerFactory;");
+   private static ReplacementWrapper r1 = new ReplacementWrapper("0","import org.slf4j.Logger;");
+   private static ReplacementWrapper r2 = new ReplacementWrapper("1","Logger");
+   static{
+	   r2.addReplacement("3", "LoggerFactory.getLogger(");
+   }
+   private static ReplacementWrapper r3 = new ReplacementWrapper("1","LoggerFactory.getLogger(");
+   private static ReplacementWrapper r4 = new ReplacementWrapper("1", "Logger");
+   
+   
   public JCLMatcher() {
     super();
     logger = LoggerFactory.getLogger(JCLMatcher.class);    
@@ -23,13 +34,13 @@
   }
 
   protected void initRules() {    
-    rulesMap = new TreeMap<PatternWrapper, String>();
-    rulesMap.put(p1,"import org.slf4j.LoggerFactory;");
-    rulesMap.put(p2,"import org.slf4j.Logger;");    
-    rulesMap.put(p3,"LoggerFactory.getLogger");
-    rulesMap.put(p4,"LoggerFactory.getLogger");
-    rulesMap.put(p5,"Logger");
-//    rulesMap.put(p6,"error(");
-//    rulesMap.put(p7,"isErrorEnabled(");    
+    rulesMap = new TreeMap<PatternWrapper, ReplacementWrapper>();
+    rulesMap.put(p0,r0);
+    rulesMap.put(p1,r1);   
+    rulesMap.put(p2,r2);
+    rulesMap.put(p3,r2);
+    rulesMap.put(p4,r3);
+    rulesMap.put(p5,r3);
+    rulesMap.put(p6,r4);
   }
 }

Modified: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/PatternWrapper.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/PatternWrapper.java	(original)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/PatternWrapper.java	Tue Jul 10 22:37:06 2007
@@ -4,24 +4,25 @@
 
 public class PatternWrapper implements Comparable {
 
-  private String  index;
-  private Pattern pattern;
-  
-  public PatternWrapper(String index, Pattern pattern){
-    this.index = index;
-    this.pattern = pattern;
-  }
-  
-  public String getIndex(){
-    return index;
-  }
-  
-  public Pattern getPattern(){
-    return pattern;
-  }
-
-  public int compareTo(Object o) {    
-    String oIndex = ((PatternWrapper)o).index;  
-    return this.index.compareTo(oIndex);
-  }
+	private String index;
+
+	private Pattern pattern;
+
+	public PatternWrapper(String index, Pattern pattern) {
+		this.index = index;
+		this.pattern = pattern;
+	}
+
+	public String getIndex() {
+		return index;
+	}
+
+	public Pattern getPattern() {
+		return pattern;
+	}
+
+	public int compareTo(Object o) {
+		String oIndex = ((PatternWrapper) o).index;
+		return this.index.compareTo(oIndex);
+	}
 }

Added: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/ReplacementWrapper.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/ReplacementWrapper.java	Tue Jul 10 22:37:06 2007
@@ -0,0 +1,23 @@
+package org.slf4j.converter;
+
+import java.util.HashMap;
+
+public class ReplacementWrapper {
+
+	private HashMap<String, String> replacementMap;
+
+	public ReplacementWrapper(String groupIndex, String replacement) {
+		replacementMap = new HashMap<String, String>();
+		replacementMap.put(groupIndex, replacement);
+	}
+
+	public void addReplacement(String groupIndex, String replacement) {
+		replacementMap.put(groupIndex, replacement);
+	}
+
+	public String getReplacement(int groupIndex) {
+		String resultIndex = Integer.toString(groupIndex);
+		return replacementMap.get(resultIndex);
+	}
+
+}



More information about the slf4j-dev mailing list