[slf4j-dev] svn commit: r857 - 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 Jul 20 17:53:32 CEST 2007
Author: jncharpin
Date: Fri Jul 20 17:53:32 2007
New Revision: 857
Added:
slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/JCLMatcherTest.java
Modified:
slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/AbstractMatcher.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
Log:
adding test for JCLConversion
new parameter to convert or not commented code
on going work to allow conversion of multiple instructions on the same line
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 Jul 20 17:53:32 2007
@@ -15,63 +15,108 @@
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
+ // TODO criterias to select concrete matcher impl. and comentConversion mode
return new JCLMatcher();
}
public void setWriter(Writer writer) {
this.writer = writer;
}
+
+ public Writer getWriter(){
+ return writer;
+ }
+
+ public void setCommentConversion(boolean commentConversion){
+ this.commentConversion = commentConversion;
+ }
+ /**
+ *
+ * @param text
+ */
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));
+ if (isTextConvertible(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("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);
+ 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;
}
- 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) {
+ if (!found) {
+ writer.write(text);
+ }
+ } else {
writer.write(text);
}
}
+
+
+ /**
+ *
+ * @param text
+ * @return
+ */
+ private boolean isTextConvertible(String text) {
+ boolean isConvertible = true;
+ if (text.trim().length() == 0) {
+ isConvertible = false;
+ } else if (commentConversion) {
+ isConvertible = true;
+ } else if (blockComment || text.startsWith(LINE_COMMENT)) {
+ isConvertible = false;
+ }
+ else if (text.startsWith(BLOCK_COMMENT_START)) {
+ blockComment = true;
+ isConvertible = false;
+ }
+ if (text.endsWith(BLOCK_COMMENT_END)) {
+ blockComment = false;
+ }
+ return isConvertible;
+ }
+
protected abstract void initRules();
}
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 Jul 20 17:53:32 2007
@@ -7,40 +7,38 @@
public class JCLMatcher extends AbstractMatcher {
- 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("(\\w+\\s*+=\\s*+)(LogFactory.getFactory\\(\\).getInstance\\()(\\w+)(.class\\);)"));
- private static PatternWrapper p5 = new PatternWrapper("5",Pattern.compile("(\\w+\\s*+=\\s*+)(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("2","LoggerFactory.getLogger(");
- private static ReplacementWrapper r4 = new ReplacementWrapper("1", "Logger");
-
-
- public JCLMatcher() {
- super();
- logger = LoggerFactory.getLogger(JCLMatcher.class);
- initRules();
- }
+ public JCLMatcher() {
+ super();
+ logger = LoggerFactory.getLogger(JCLMatcher.class);
+ initRules();
+ }
- protected void initRules() {
- 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);
- }
+ protected void initRules() {
+ PatternWrapper p0 = new PatternWrapper("0", Pattern.compile("import\\s*+org.apache.commons.logging.LogFactory;"));
+ PatternWrapper p1 = new PatternWrapper("1", Pattern.compile("import\\s*+org.apache.commons.logging.Log;"));
+ PatternWrapper p2 = new PatternWrapper("2",Pattern.compile("((\\w*+\\W*+\\.*)*;*+)(Log)(\\s+\\w+\\s*+=\\s*+)(LogFactory.getFactory\\(\\).getInstance\\()(\\w+)(.class\\);)((\\w*+\\W*+\\.*)*;*+)"));
+ PatternWrapper p3 = new PatternWrapper("3",Pattern.compile("((\\w*+\\W*+\\.*)*;*+)(Log)(\\s+\\w+\\s*+=\\s*+)(LogFactory.getLog\\()(\\w+)(.class\\);)((\\w*+\\W*+\\.*)*;*+)"));
+ PatternWrapper p4 = new PatternWrapper("4",Pattern.compile("((\\w*+\\W*+\\.*)*;*+)(\\w+\\s*+=\\s*+)(LogFactory.getFactory\\(\\).getInstance\\()(\\w+)(.class\\);)((\\w*+\\W*+\\.*)*;*+)"));
+ PatternWrapper p5 = new PatternWrapper("5",Pattern.compile("((\\w*+\\W*+\\.*)*;*+)(\\w+\\s*+=\\s*+)(LogFactory.getLog\\()(\\w+)(.class\\);)((\\w*+\\W*+\\.*)*;*+)"));
+ PatternWrapper p6 = new PatternWrapper("6",Pattern.compile("((\\w*+\\W*+\\.*)*;*+)(Log)(\\s*+\\w+\\s*+;)((\\w*+\\W*+\\.*)*;*+)"));
+
+ ReplacementWrapper r0 = new ReplacementWrapper("0","import org.slf4j.LoggerFactory;");
+ ReplacementWrapper r1 = new ReplacementWrapper("0","import org.slf4j.Logger;");
+ ReplacementWrapper r2 = new ReplacementWrapper("3", "Logger");
+ r2.addReplacement("2", "");
+ r2.addReplacement("5", "LoggerFactory.getLogger(");
+ ReplacementWrapper r3 = new ReplacementWrapper("4","LoggerFactory.getLogger(");
+ r3.addReplacement("2", "");
+ ReplacementWrapper r4 = new ReplacementWrapper("3", "Logger");
+ r4.addReplacement("2", "");
+
+ 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/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 Jul 20 17:53:32 2007
@@ -11,56 +11,67 @@
public class Writer {
- Logger logger;
+ Logger logger;
- BufferedWriter bwriter;
+ 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());
- }
- }
- }
+ boolean isFirstLine;
+
+ public Writer() {
+ logger = LoggerFactory.getLogger(Writer.class);
+ }
+
+ public void initFileWriter(File file) {
+ try {
+ FileWriter fileWriter = new FileWriter(file);
+ bwriter = new BufferedWriter(fileWriter);
+ isFirstLine = true;
+ } 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 {
+ if (!isFirstLine) {
+ bwriter.newLine();
+ } else {
+ isFirstLine = false;
+ }
+ 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 {
+ if (!isFirstLine) {
+ bwriter.newLine();
+ } else {
+ isFirstLine = false;
+ }
+ bwriter.write(text);
+ } catch (IOException exc) {
+ logger.error("error writing file " + bwriter.toString());
+ }
+ }
+ }
}
Added: slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/JCLMatcherTest.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/JCLMatcherTest.java Fri Jul 20 17:53:32 2007
@@ -0,0 +1,70 @@
+package org.slf4j.converter;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+public class JCLMatcherTest extends TestCase {
+
+
+
+ public void testConversion() throws IOException {
+ AbstractMatcher jclMatcher = AbstractMatcher.getMatcherImpl();
+ Writer writer = new Writer();
+ File jclFile = new File("jclConversionTest");
+ writer.initFileWriter(jclFile);
+ jclMatcher.setWriter(writer);
+ 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;");
+ }
+
+
+
+
+
+
+}
More information about the slf4j-dev
mailing list