[slf4j-dev] svn commit: r893 - 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 15:41:25 CEST 2007
Author: jncharpin
Date: Fri Sep 14 15:41:24 2007
New Revision: 893
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/test/java/org/slf4j/converter/AternativeApproach.java
slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/JCLMatcherTest.java
slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/TrivialMatcherTest.java
Log:
Refactoring according to the alternative approach for conversion expressions proposed by Ceki.
Instead of doing conversion with an unique Pattern and it's mutilple capturing groups, we now combine several simpler Patterns.
Each Pattern do a single replacement.
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 Sep 14 15:41:24 2007
@@ -6,8 +6,8 @@
import java.util.regex.Pattern;
/**
- * This class runs Pattern matching with java.util.regex
- * using Patterns defined in concretes implementations
+ * This class runs Pattern matching with java.util.regex using Patterns defined
+ * in concretes implementations
*
* @author jean-noelcharpin
*
@@ -23,27 +23,28 @@
/**
* Return matcher implementation depending on the conversion mode
+ *
* @param conversionType
* @return AbstractMatcher implementation
*/
public static AbstractMatcher getMatcherImpl(int conversionType) {
- switch(conversionType){
- case Constant.JCL_TO_SLF4J :
+ switch (conversionType) {
+ case Constant.JCL_TO_SLF4J:
return new JCLMatcher();
- case Constant.LOG4J_TO_SLF4J :
+ case Constant.LOG4J_TO_SLF4J:
return new Log4jMatcher();
- default :
+ default:
return null;
}
}
-
/**
- * Check if the specified text is matching one of the conversion rules
- * If a rule is resolved, ask for replacement
- * If no rule can be applied the text is returned without change
+ * Check if the specified text is matching some conversions rules If a
+ * rule is resolved, ask for replacement If no rule can be applied the text is
+ * returned without change
+ *
* @param text
- * @return String
+ * @return String
*/
public String getReplacement(String text) {
ConversionRule conversionRule;
@@ -54,9 +55,10 @@
conversionRule = (ConversionRule) rulesIter.next();
pattern = conversionRule.getPattern();
matcher = pattern.matcher(text);
- if (matcher.matches()) {
+ if (matcher.find()) {
System.out.println("matching " + text);
- return conversionRule.replace(matcher);
+ String replacementText = conversionRule.replace(matcher);
+ text = matcher.replaceAll(replacementText);
}
}
return text;
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 Sep 14 15:41:24 2007
@@ -26,70 +26,20 @@
SingleConversionRule cr1 = new SingleConversionRule(Pattern
.compile("import\\s*+org.apache.commons.logging.Log;"),
"import org.slf4j.Logger;");
-
- // matching declaration and instanciation : protected Log myLog =
- // LogFactory.getFactory().getInstance(MyClass.class); //comment or other
- // instruction
- MultiGroupConversionRule cr2 = new MultiGroupConversionRule(
- Pattern
- .compile("((\\w*+\\W*+)*)(Log)(\\s+\\w+\\s*+=\\s*+)(LogFactory.getFactory\\(\\).getInstance\\()(\\w+)(.class\\);)((\\w*+\\W*+)*)"));
- cr2.addReplacement(3, "Logger");
- cr2.addReplacement(2, "");
- cr2.addReplacement(5, "LoggerFactory.getLogger(");
-
- // matching declaration and instanciation : protected static Log myLog =
- // LogFactory.getLog(MyClass.class); //comment or other instruction
- MultiGroupConversionRule cr3 = new MultiGroupConversionRule(
- Pattern
- .compile("((\\w*+\\W*+)*)(Log)(\\s+\\w+\\s*+=\\s*+)(LogFactory.getLog\\()(\\w+)(.class\\);)((\\w*+\\W*+)*)"));
- cr3.addReplacement(2, "");
- cr3.addReplacement(3, "Logger");
- cr3.addReplacement(5, "LoggerFactory.getLogger(");
-
- // matching instanciation without declaration : myLog =
- // LogFactory.getFactory().getInstance(MyClass.class); //comment or other
- // instruction
- MultiGroupConversionRule cr4 = new MultiGroupConversionRule(
- Pattern
- .compile("((\\w*+\\W*+)*)(\\w+\\s*+=\\s*+)(LogFactory.getFactory\\(\\).getInstance\\()(\\w+)(.class\\);)((\\w*+\\W*+)*)"));
- cr4.addReplacement(4, "LoggerFactory.getLogger(");
- cr4.addReplacement(2, "");
-
- // matching instanciation without declaration : myLog =
- // LogFactory.getLog(MyClass.class); //comment or other instruction
- MultiGroupConversionRule cr5 = new MultiGroupConversionRule(
- Pattern
- .compile("((\\w*+\\W*+)*)(\\w+\\s*+=\\s*+)(LogFactory.getLog\\()(\\w+)(.class\\);)((\\w*+\\W*+)*)"));
- cr5.addReplacement(4, "LoggerFactory.getLogger(");
- cr5.addReplacement(2, "");
-
- // matching declaration without instanciation : public static final Log
- // myLog //comment or other instruction
- MultiGroupConversionRule cr6 = new MultiGroupConversionRule(Pattern
- .compile("((\\w*+\\W*+)*)(Log)(\\s*+\\w+\\s*+;)((\\w*+\\W*+)*)"));
- cr6.addReplacement(3, "Logger");
- cr6.addReplacement(2, "");
-
- // matching incomplete instanciation : protected Log log =
- MultiGroupConversionRule cr7 = new MultiGroupConversionRule(Pattern
- .compile("((\\w*+\\W*+)*)(Log)(\\s+\\w+\\s*+=*\\s*+)"));
- cr7.addReplacement(Constant.INDEX_3, "Logger");
- cr7.addReplacement(Constant.INDEX_2, "");
-
- // matching incomlete instanciation : LogFactory.getLog(MyComponent.class);
- MultiGroupConversionRule cr8 = new MultiGroupConversionRule(
- Pattern
- .compile("((\\w*+\\W*+)*)(LogFactory.getLog\\()(\\w+)(.class\\);)((\\w*+\\W*+)*)"));
- cr8.addReplacement(Constant.INDEX_3, "LoggerFactory.getLogger(");
- cr8.addReplacement(Constant.INDEX_1, "");
-
- // matching incomlete instanciation :
- // LogFactory.getFactory().getInstance(MyComponent.class);
- MultiGroupConversionRule cr9 = new MultiGroupConversionRule(
- Pattern
- .compile("((\\w*+\\W*+)*)(LogFactory.getFactory\\(\\).getInstance\\()(\\w+)(.class\\);)((\\w*+\\W*+)*)"));
- cr9.addReplacement(Constant.INDEX_3, "LoggerFactory.getLogger(");
- cr9.addReplacement(Constant.INDEX_1, "");
+
+ SingleConversionRule cr2 = new SingleConversionRule(Pattern
+ .compile("(\\sLog\\b)")," Logger");
+
+ SingleConversionRule cr3 = new SingleConversionRule(Pattern
+ .compile("(^Log\\b)"),"Logger");
+
+ SingleConversionRule cr4 = new SingleConversionRule(Pattern
+ .compile("LogFactory.getFactory\\(\\).getInstance\\("),
+ "LoggerFactory.getLogger(");
+
+ SingleConversionRule cr5 = new SingleConversionRule(Pattern
+ .compile("LogFactory.getLog\\("),"LoggerFactory.getLogger(");
+
rules = new ArrayList<ConversionRule>();
rules.add(cr0);
@@ -98,9 +48,5 @@
rules.add(cr3);
rules.add(cr4);
rules.add(cr5);
- rules.add(cr6);
- rules.add(cr7);
- rules.add(cr8);
- rules.add(cr9);
}
}
Modified: slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/AternativeApproach.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/AternativeApproach.java (original)
+++ slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/AternativeApproach.java Fri Sep 14 15:41:24 2007
@@ -7,33 +7,60 @@
public class AternativeApproach extends TestCase {
+ /**
+ * In this test we see that we cans use more simple Pattern to do the
+ * conversion
+ *
+ */
public void test() {
MultiGroupConversionRule cr2 = new MultiGroupConversionRule(Pattern
.compile("(.*)(Log)"));
cr2.addReplacement(2, "LOGGER");
-
-
+
String s = "abcd Log";
Pattern pat = cr2.getPattern();
Matcher m = pat.matcher(s);
-
+
assertTrue(m.matches());
String r = cr2.replace(m);
assertEquals("abcd LOGGER", r);
-
+
System.out.println(r);
}
-
-
+
+ /**
+ * In this test we replace, using the simple Pattern (Log), the full Log
+ * declaration and instanciation. This is not convenient because we will also
+ * replace all String containing "Log".
+ */
public void test2() {
- Pattern pat = Pattern.compile("Log");
- String s = "abcd Log";
+ Pattern pat = Pattern.compile("(Log)");
+ String s = "abcd Log =";
Matcher m = pat.matcher(s);
assertTrue(m.find());
- String r = m.replaceAll("LOGGER");
- assertEquals("abcd LOGGER", r);
+ String r = m.replaceAll("Logger");
+ assertEquals("abcd Logger =", r);
+
+ String s1 = "Log l = LogFactory.getLog(MyClass.class);";
+ m = pat.matcher(s1);
+ assertTrue(m.find());
+ r = m.replaceAll("Logger");
+ assertEquals("Logger l = LoggerFactory.getLogger(MyClass.class);", r);
+
+ String s2 = "Logabc ";
+ m = pat.matcher(s2);
+ assertTrue(m.find());
+
+ String s3 = "abcLog";
+ m = pat.matcher(s3);
+ assertTrue(m.find());
}
-
+
+ /**
+ * In this test we use a simple Pattern to replace the log instanciation
+ * without influence on Log declaration.
+ *
+ */
public void test3() {
Pattern pat = Pattern.compile("LogFactory.getFactory\\(\\).getInstance\\(");
String s = "Log log = LogFactory.getFactory().getInstance(\"x\");";
@@ -41,8 +68,62 @@
assertTrue(m.find());
String r = m.replaceAll("LoggerFactory.getLogger(");
assertEquals("Log log = LoggerFactory.getLogger(\"x\");", r);
-
+
String nonMatching = "Log log = xxx;";
+ pat.matcher(nonMatching);
assertFalse(m.find());
}
+
+ /**
+ * In this test we try to replace keyword Log without influence on String
+ * containg Log We see that we have to use two differents Patterns
+ */
+ public void test4() {
+ Pattern pat = Pattern.compile("(\\sLog\\b)");
+ String s = "abcd Log =";
+ Matcher m = pat.matcher(s);
+ assertTrue(m.find());
+ String r = m.replaceAll(" Logger");
+ assertEquals("abcd Logger =", r);
+
+ String s2 = "Logabcd ";
+ m = pat.matcher(s2);
+ assertFalse(m.find());
+
+ String s3 = "abcdLogabcd ";
+ m = pat.matcher(s3);
+ assertFalse(m.find());
+
+ String s4 = "abcdLog";
+ m = pat.matcher(s4);
+ assertFalse(m.find());
+
+ String s5 = "Log myLog";
+ m = pat.matcher(s5);
+ assertFalse(m.find());
+
+ Pattern pat2 = Pattern.compile("^Log\\b");
+ Matcher m2 = pat2.matcher(s5);
+ assertTrue(m2.find());
+ r = m2.replaceAll("Logger");
+ assertEquals("Logger myLog", r);
+ }
+
+ /**
+ * In this test we combine two Pattern to achieve the intended conversion
+ */
+ public void test5() {
+ Pattern pat = Pattern.compile("(\\sLog\\b)");
+ String s = "public Log myLog =LogFactory.getFactory().getInstance(myClass.class);";
+ Matcher m = pat.matcher(s);
+ assertTrue(m.find());
+ String r = m.replaceAll(" Logger");
+ assertEquals("public Logger myLog =LogFactory.getFactory().getInstance(myClass.class);", r);
+
+ Pattern pat2 = Pattern.compile("LogFactory.getFactory\\(\\).getInstance\\(");
+ m = pat2.matcher(r);
+ assertTrue(m.find());
+ r = m.replaceAll("LoggerFactory.getLogger(");
+ assertEquals("public Logger myLog =LoggerFactory.getLogger(myClass.class);", r);
+ }
}
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 Sep 14 15:41:24 2007
@@ -19,8 +19,8 @@
public void testLogFactoryGetLogReplacement() {
JCLMatcher jclMatcher = new JCLMatcher();
// Logger declaration and instanciation without modifier
- assertEquals("Logger l = LoggerFactory.getLogger(MyClass.class);",
- jclMatcher.getReplacement("Log l = LogFactory.getLog(MyClass.class);"));
+ assertEquals(" Logger l = LoggerFactory.getLogger(MyClass.class);",
+ jclMatcher.getReplacement(" Log l = LogFactory.getLog(MyClass.class);"));
// Logger declaration and instanciation with one modifier
assertEquals(
"public Logger mylog=LoggerFactory.getLogger(MyClass.class);",
Modified: slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/TrivialMatcherTest.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/TrivialMatcherTest.java (original)
+++ slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/TrivialMatcherTest.java Fri Sep 14 15:41:24 2007
@@ -11,10 +11,6 @@
TrivialMatcher trivialMatcher = new TrivialMatcher();
trivialMatcher.initRules();
- //
- //assertEquals("no replacement for this text, it remains the same!",
- // simpleMatcher.getReplacement("no replacement for this text, it remains the same!"));
-
// "import org.slf4j.converter" -- > simple replacement with an unique capturing group
assertEquals("simple replacement with an unique capturing group",
trivialMatcher.getReplacement("import org.slf4j.converter"));
More information about the slf4j-dev
mailing list