[slf4j-dev] svn commit: r859 - 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 Aug 3 11:04:44 CEST 2007


Author: jncharpin
Date: Fri Aug  3 11:04:43 2007
New Revision: 859

Added:
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Constant.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/Writer.java
   slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/JCLMatcherTest.java

Log:
AbstractMatcher replacement method refactoring
Removing slf4j logging call
Params management

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	Fri Aug  3 11:04:43 2007
@@ -5,43 +5,27 @@
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import org.slf4j.Logger;
 
 public abstract class AbstractMatcher {
 
-	protected Logger logger;
-
 	protected TreeMap<PatternWrapper, ReplacementWrapper> rulesMap;
 
-	protected Writer writer;
-
 	protected boolean commentConversion = true;
 
 	protected boolean blockComment = false;
 
-	private final static String LINE_COMMENT = "//";
-
-	private final static String BLOCK_COMMENT_START = "/*";
-
-	private final static String BLOCK_COMMENT_END = "*/";
 
 	public AbstractMatcher() {
 	}
 
-	public static AbstractMatcher getMatcherImpl() {
-		// TODO criterias to select concrete matcher impl. and comentConversion mode
-		return new JCLMatcher();
+	public static AbstractMatcher getMatcherImpl(int conversionType) {
+		if(conversionType==Constant.JCL_TO_SLF4J){
+			return new JCLMatcher();
+		}
+		return null;
 	}
 
-	public void setWriter(Writer writer) {
-		this.writer = writer;
-	}
-	
-	public Writer getWriter(){
-		return writer;
-	}
-	
-	public void setCommentConversion(boolean commentConversion){
+	public void setCommentConversion(boolean commentConversion) {
 		this.commentConversion = commentConversion;
 	}
 
@@ -49,7 +33,7 @@
 	 * 
 	 * @param text
 	 */
-	public void matches(String text) {
+	public String replace(String text) {
 		if (isTextConvertible(text)) {
 			PatternWrapper patternWrapper;
 			Pattern pattern;
@@ -57,43 +41,35 @@
 			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("matching " + text);
+					System.out.println("matching " + 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);
+							System.out.println("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));
+						} 
+						else if (group > 0) {
+						System.out.println("conserving group " + group + " : "
+						 + matcher.group(group));
+						replacementBuffer.append(matcher.group(group));
 						}
 					}
-					writer.rewrite(matcher, replacementBuffer.toString());
-					found = true;
-					break;
+					return replacementBuffer.toString();					
 				}
 			}
-			if (!found) {
-				writer.write(text);
-			}
-		} else {
-			writer.write(text);
 		}
+		return text;
 	}
 
-	
-	
 	/**
 	 * 
 	 * @param text
@@ -105,14 +81,13 @@
 			isConvertible = false;
 		} else if (commentConversion) {
 			isConvertible = true;
-		} else if (blockComment || text.startsWith(LINE_COMMENT)) {
+		} else if (blockComment || text.startsWith(Constant.LINE_COMMENT)) {
 			isConvertible = false;
-		}
-		else if (text.startsWith(BLOCK_COMMENT_START)) {
+		} else if (text.startsWith(Constant.BLOCK_COMMENT_START)) {
 			blockComment = true;
 			isConvertible = false;
 		}
-		if (text.endsWith(BLOCK_COMMENT_END)) {
+		if (text.endsWith(Constant.BLOCK_COMMENT_END)) {
 			blockComment = false;
 		}
 		return isConvertible;

Added: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Constant.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Constant.java	Fri Aug  3 11:04:43 2007
@@ -0,0 +1,13 @@
+package org.slf4j.converter;
+
+public class Constant {
+
+	public final static int    JCL_TO_SLF4J = 1;
+	
+	public final static String LINE_COMMENT = "//";
+
+	public final static String BLOCK_COMMENT_START = "/*";
+
+	public final static String BLOCK_COMMENT_END = "*/";
+	
+}

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 Aug  3 11:04:43 2007
@@ -12,11 +12,8 @@
 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;
 
@@ -27,6 +24,12 @@
 	private String source;
 
 	private String destination;
+	
+	private boolean delDestination;
+	
+	private int conversionType; 
+	
+	private boolean commentConversion;
 
 	/**
 	 * @param args
@@ -34,31 +37,45 @@
 	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);
+		
+		int params = args.length;
+		String param;
+		for(int i=0; i<params; i++){
+			param = args[i];
+			if(param.startsWith("src=")){
+				converter.source = param.substring(4);
+			}
+			else if(param.startsWith("dest=")){
+				converter.destination = param.substring(5);
+			}
+			else if(param.startsWith("del=")){
+				converter.delDestination = Boolean.parseBoolean(param.substring(4));
+			}
+			else if(param.startsWith("all=")){
+				converter.commentConversion = Boolean.parseBoolean(param.substring(4));
+			}
+		}
+		
+		converter.conversionType = Constant.JCL_TO_SLF4J;
+		if(converter.init()){
+			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);
+	public boolean init() {
+		matcher = AbstractMatcher.getMatcherImpl(conversionType);
+		if(matcher==null){
+			return false;
+		}
+		matcher.setCommentConversion(commentConversion);
+		writer = new Writer();		
+		return true;
 	}
 
-	/**
-	 * 
-	 * 
-	 */
-	private void getPaths() {
-		source = "/Users/jean-noelcharpin/dev/slf4j/slf4j-converter/jcl";
-		destination = "/Users/jean-noelcharpin/dev/slf4j/slf4j-converter/witness";
-	}
 
 	/**
 	 * 
@@ -67,7 +84,7 @@
 	private File initSource() {
 		File fileSource = new File(source);
 		if (!fileSource.isDirectory()) {
-			logger.info("source path is not a valid source directory");
+			System.out.println("source path is not a valid source directory");
 		}
 		return fileSource;
 	}
@@ -78,7 +95,7 @@
 	 */
 	private File initDestination() {
 		File fileDest = new File(destination);
-		if (fileDest.exists()) {
+		if (fileDest.exists() && delDestination) {
 			delete(fileDest);
 		}
 		fileDest.mkdir();
@@ -98,9 +115,7 @@
 				}
 			}
 			fdest.delete();
-			// logger.info("Deleting " + fdest.getName());
 		} else {
-			// logger.info("Deleting " + fdest.getName());
 			fdest.delete();
 		}
 	}
@@ -114,9 +129,7 @@
 				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++) {
@@ -128,6 +141,11 @@
 		}
 	}
 
+	/**
+	 * 
+	 * @param fsource
+	 * @param fdest
+	 */
 	private void copy(File fsource, File fdest) {
 		try {
 			FileInputStream fis = new FileInputStream(fsource);
@@ -136,17 +154,16 @@
 			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());
+				System.out.println("error copying file " + fsource.getAbsolutePath());
 			}
 
 		} catch (FileNotFoundException exc) {
-			logger.error(exc.toString());
+			System.out.println(exc.toString());
 		} catch (IOException e) {
-			logger.error(e.toString());
+			System.out.println(e.toString());
 		}
 	}
 
@@ -169,7 +186,6 @@
 		} else {
 			if (file.getName().endsWith(".java")) {
 				javaFiles.add(file);
-				// logger.info("Adding java file " + file.getAbsolutePath());
 			}
 		}
 		return javaFiles;
@@ -183,29 +199,37 @@
 		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);
-					}
+			convert(currentFile);
+		}
+	}
+	
+	/**
+	 * 
+	 * @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.replace(line);
+					writer.write(newLine);
+				} else {
+					isEmpty = true;
+					writer.closeFileWriter();
+					copy(newFile, file);
+					delete(newFile);
 				}
-			} catch (IOException exc) {
-				logger.error("error reading file " + exc);
 			}
+		} catch (IOException exc) {
+			System.out.println("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	Fri Aug  3 11:04:43 2007
@@ -3,13 +3,11 @@
 import java.util.TreeMap;
 import java.util.regex.Pattern;
 
-import org.slf4j.LoggerFactory;
 
 public class JCLMatcher extends AbstractMatcher {
 
 	public JCLMatcher() {
 		super();
-		logger = LoggerFactory.getLogger(JCLMatcher.class);
 		initRules();
 	}
 

Modified: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Writer.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Writer.java	(original)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Writer.java	Fri Aug  3 11:04:43 2007
@@ -6,19 +6,15 @@
 import java.io.IOException;
 import java.util.regex.Matcher;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 public class Writer {
 
-	Logger logger;
 
 	BufferedWriter bwriter;
 
 	boolean isFirstLine;
 
 	public Writer() {
-		logger = LoggerFactory.getLogger(Writer.class);
 	}
 
 	public void initFileWriter(File file) {
@@ -27,7 +23,7 @@
 			bwriter = new BufferedWriter(fileWriter);
 			isFirstLine = true;
 		} catch (IOException exc) {
-			logger.error("error creating filewriter " + file.getAbsolutePath());
+			System.out.println("error creating filewriter " + file.getAbsolutePath());
 		}
 	}
 
@@ -37,7 +33,7 @@
 				bwriter.flush();
 				bwriter.close();
 			} catch (IOException e) {
-				logger.error("error closing filewriter " + bwriter.toString());
+				System.out.println("error closing filewriter " + bwriter.toString());
 			}
 		}
 	}
@@ -52,9 +48,9 @@
 					isFirstLine = false;
 				}
 				bwriter.write(text);
-				logger.info("new entry " + text);
+				System.out.println("new entry " + text);
 			} catch (IOException exc) {
-				logger.error("error writing file " + bwriter.toString());
+				System.out.println("error writing file " + bwriter.toString());
 			}
 		}
 	}
@@ -68,8 +64,9 @@
 					isFirstLine = false;
 				}
 				bwriter.write(text);
+				System.out.println("new entry " + text);
 			} catch (IOException exc) {
-				logger.error("error writing file " + bwriter.toString());
+				System.out.println("error writing file " + bwriter.toString());
 			}
 		}
 	}

Modified: slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/JCLMatcherTest.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/JCLMatcherTest.java	(original)
+++ slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/JCLMatcherTest.java	Fri Aug  3 11:04:43 2007
@@ -1,8 +1,7 @@
 package org.slf4j.converter;
 
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileReader;
+
+
 import java.io.IOException;
 
 import junit.framework.TestCase;
@@ -12,54 +11,27 @@
 	
 	
 	public void testConversion() throws IOException {
-		AbstractMatcher jclMatcher = AbstractMatcher.getMatcherImpl();
-		Writer writer = new Writer();
-		File jclFile = new File("jclConversionTest");
-		writer.initFileWriter(jclFile);
-		jclMatcher.setWriter(writer);
+		AbstractMatcher jclMatcher = AbstractMatcher.getMatcherImpl(1);
 		jclMatcher.setCommentConversion(true);
-
-		jclMatcher.matches("import org.apache.commons.logging.LogFactory;");
-		jclMatcher.matches("import org.apache.commons.logging.Log;");
-		jclMatcher.matches("Log l = LogFactory.getLog(MyClass.class);");
-		jclMatcher.matches("public Log mylog=LogFactory.getLog(MyClass.class);");
-		jclMatcher.matches("public static Log mylog1 = LogFactory.getLog(MyClass.class);");
-		jclMatcher.matches("Log log3=LogFactory.getFactory().getInstance(MyClass.class);");
-		jclMatcher.matches("Log mylog4 = LogFactory.getFactory().getInstance(MyClass.class);//logger instanciation");
-		jclMatcher.matches("Log myLog6;//logger declaration");
-		jclMatcher.matches("//log7=LogFactory.getFactory().getInstance(MyClass.class);");
-		jclMatcher.matches("log8 =LogFactory.getFactory().getInstance(MyClass.class);");
-		jclMatcher.matches("myLog9 = LogFactory.getLog(MyClass.class);");
-		jclMatcher.matches("private Log mylog10;");
-		jclMatcher.matches("protected final Log myLog11;");
-		jclMatcher.matches("public static final Log myLog12;");
-		jclMatcher.matches("System.out.println(\"Log\") ;System.out.println(\"Log2\");  public static final Log myLog13;");
-		jclMatcher.matches("public static final Log myLog14;System.out.println(\"Log\");");
-		jclMatcher.matches("System.out.println(\"\");public static final Log myLog15;System.out.println(\"Log\")  ;System.out.println(\"Log2\");");
-		jclMatcher.matches("((Pojo)pojo.getPojo()).get(\"pojo\",pojo);public static final Log myLog16;");
-		
-		jclMatcher.getWriter().closeFileWriter();
-		BufferedReader breader = new BufferedReader(new FileReader(jclFile));
-	
-		
-		assertEquals(breader.readLine(),"import org.slf4j.LoggerFactory;");
-		assertEquals(breader.readLine(),"import org.slf4j.Logger;");
-		assertEquals(breader.readLine(),"Logger l = LoggerFactory.getLogger(MyClass.class);");
-		assertEquals(breader.readLine(),"public Logger mylog=LoggerFactory.getLogger(MyClass.class);");
-		assertEquals(breader.readLine(),"public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);");
-		assertEquals(breader.readLine(),"Logger log3=LoggerFactory.getLogger(MyClass.class);");
-		assertEquals(breader.readLine(),"Logger mylog4 = LoggerFactory.getLogger(MyClass.class);//logger instanciation");
-		assertEquals(breader.readLine(),"Logger myLog6;//logger declaration");
-		assertEquals(breader.readLine(),"//log7=LoggerFactory.getLogger(MyClass.class);");
-		assertEquals(breader.readLine(),"log8 =LoggerFactory.getLogger(MyClass.class);");
-		assertEquals(breader.readLine(),"myLog9 = LoggerFactory.getLogger(MyClass.class);");
-		assertEquals(breader.readLine(),"private Logger mylog10;");
-		assertEquals(breader.readLine(),"protected final Logger myLog11;");
-		assertEquals(breader.readLine(),"public static final Logger myLog12;");
-		assertEquals(breader.readLine(),"System.out.println(\"Log\") ;System.out.println(\"Log2\");  public static final Logger myLog13;");
-		assertEquals(breader.readLine(),"public static final Logger myLog14;System.out.println(\"Log\");");
-		assertEquals(breader.readLine(),"System.out.println(\"\");public static final Logger myLog15;System.out.println(\"Log\")  ;System.out.println(\"Log2\");");
-		assertEquals(breader.readLine(),"((Pojo)pojo.getPojo()).get(\"pojo\",pojo);public static final Logger myLog16;");
+			
+		assertEquals(jclMatcher.replace("import org.apache.commons.logging.LogFactory;"),"import org.slf4j.LoggerFactory;");
+		assertEquals(jclMatcher.replace("import org.apache.commons.logging.Log;"),"import org.slf4j.Logger;");
+		assertEquals(jclMatcher.replace("Log l = LogFactory.getLog(MyClass.class);"),"Logger l = LoggerFactory.getLogger(MyClass.class);");
+		assertEquals(jclMatcher.replace("public Log mylog=LogFactory.getLog(MyClass.class);"),"public Logger mylog=LoggerFactory.getLogger(MyClass.class);");
+		assertEquals(jclMatcher.replace("public static Log mylog1 = LogFactory.getLog(MyClass.class);"),"public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);");
+		assertEquals(jclMatcher.replace("Log log3=LogFactory.getFactory().getInstance(MyClass.class);"),"Logger log3=LoggerFactory.getLogger(MyClass.class);");
+		assertEquals(jclMatcher.replace("Log mylog4 = LogFactory.getFactory().getInstance(MyClass.class);//logger instanciation"),"Logger mylog4 = LoggerFactory.getLogger(MyClass.class);//logger instanciation");
+		assertEquals(jclMatcher.replace("Log myLog6;//logger declaration"),"Logger myLog6;//logger declaration");
+		assertEquals(jclMatcher.replace("//log7=LogFactory.getFactory().getInstance(MyClass.class);"),"//log7=LoggerFactory.getLogger(MyClass.class);");
+		assertEquals(jclMatcher.replace("log8 =LogFactory.getFactory().getInstance(MyClass.class);"),"log8 =LoggerFactory.getLogger(MyClass.class);");
+		assertEquals(jclMatcher.replace("myLog9 = LogFactory.getLog(MyClass.class);"),"myLog9 = LoggerFactory.getLogger(MyClass.class);");
+		assertEquals(jclMatcher.replace("private Log mylog10;"),"private Logger mylog10;");
+		assertEquals(jclMatcher.replace("protected final Log myLog11;"),"protected final Logger myLog11;");
+		assertEquals(jclMatcher.replace("public static final Log myLog12;"),"public static final Logger myLog12;");
+		assertEquals(jclMatcher.replace("System.out.println(\"Log\") ;System.out.println(\"Log2\");  public static final Log myLog13;"),"System.out.println(\"Log\") ;System.out.println(\"Log2\");  public static final Logger myLog13;");
+		assertEquals(jclMatcher.replace("public static final Log myLog14;System.out.println(\"Log\");"),"public static final Logger myLog14;System.out.println(\"Log\");");
+		assertEquals(jclMatcher.replace("System.out.println(\"\");public static final Log myLog15;System.out.println(\"Log\")  ;System.out.println(\"Log2\");"),"System.out.println(\"\");public static final Logger myLog15;System.out.println(\"Log\")  ;System.out.println(\"Log2\");");
+		assertEquals(jclMatcher.replace("((Pojo)pojo.getPojo()).get(\"pojo\",pojo);public static final Log myLog16;"),"((Pojo)pojo.getPojo()).get(\"pojo\",pojo);public static final Logger myLog16;");
 	}
 	
 



More information about the slf4j-dev mailing list