From ca7051680430c78a5f1fa63c226eee38c857ab31 Mon Sep 17 00:00:00 2001
From: JamesStauffer <jstauffer@spscommerce.com>
Date: Tue, 21 May 2013 08:03:11 -0500
Subject: [PATCH 1/2] ConversionRule no longer tied to patterns so
 implementations can do more complex conversions.

---
 .../org/slf4j/migrator/line/ConversionRule.java    | 22 +++--------
 .../org/slf4j/migrator/line/LineConverter.java     | 17 +++------
 .../migrator/line/MultiGroupConversionRule.java    | 44 +++++++++-------------
 .../slf4j/migrator/line/SingleConversionRule.java  | 34 ++++++++---------
 .../org/slf4j/migrator/AternativeApproach.java     |  9 ++---
 5 files changed, 48 insertions(+), 78 deletions(-)

diff --git a/slf4j-migrator/src/main/java/org/slf4j/migrator/line/ConversionRule.java b/slf4j-migrator/src/main/java/org/slf4j/migrator/line/ConversionRule.java
index 548692f..12505ed 100644
--- a/slf4j-migrator/src/main/java/org/slf4j/migrator/line/ConversionRule.java
+++ b/slf4j-migrator/src/main/java/org/slf4j/migrator/line/ConversionRule.java
@@ -29,23 +29,11 @@ import java.util.regex.Pattern;
 
 public interface ConversionRule {
 
-  public Pattern getPattern();
-
-  /**
-   * Given replacement rules, replace each capturing group in matcher's pattern
-   * 
-   * @param matcher
-   * @return String
-   */
-  public String replace(Matcher matcher);
-  
   /**
-   * Returns a non-null value if there should be an additional line
-   * following a match of this rule. In most cases this method 
-   * returns null.
-   * 
-   * @return String
+   * Apply replacement rules to text and return replacement line(s)
+   *
+   * @param line Original line
+   * @return String replacement line
    */
-  public String getAdditionalLine();
-
+  public String[] getReplacement(String line);
 }
\ No newline at end of file
diff --git a/slf4j-migrator/src/main/java/org/slf4j/migrator/line/LineConverter.java b/slf4j-migrator/src/main/java/org/slf4j/migrator/line/LineConverter.java
index 48d1580..3b38fea 100644
--- a/slf4j-migrator/src/main/java/org/slf4j/migrator/line/LineConverter.java
+++ b/slf4j-migrator/src/main/java/org/slf4j/migrator/line/LineConverter.java
@@ -51,25 +51,20 @@ public class LineConverter {
    */
   public String[] getReplacement(String text) {
     ConversionRule conversionRule;
-    Pattern pattern;
-    Matcher matcher;
     Iterator<ConversionRule> conversionRuleIterator = ruleSet.iterator();
     String additionalLine = null;
     while (conversionRuleIterator.hasNext()) {
       conversionRule = conversionRuleIterator.next();
-      pattern = conversionRule.getPattern();
-      matcher = pattern.matcher(text);
-      if (matcher.find()) {
-        // System.out.println("matching " + text);
+      String[] replacementLines = conversionRule.getReplacement(text);
+      if(!text.equals(replacementLines[0])) {
         atLeastOneMatchOccured = true;
-        String replacementText = conversionRule.replace(matcher);
-        text = matcher.replaceAll(replacementText);
-        if(conversionRule.getAdditionalLine() != null) {
-          additionalLine = conversionRule.getAdditionalLine();
+        text = replacementLines[0];
+        if(replacementLines.length > 1) {
+          additionalLine = replacementLines[1];
         }
       }
     }
-    
+
     if(additionalLine == null) {
       return new String[] {text};
     } else {
diff --git a/slf4j-migrator/src/main/java/org/slf4j/migrator/line/MultiGroupConversionRule.java b/slf4j-migrator/src/main/java/org/slf4j/migrator/line/MultiGroupConversionRule.java
index 9e2b498..de536d3 100644
--- a/slf4j-migrator/src/main/java/org/slf4j/migrator/line/MultiGroupConversionRule.java
+++ b/slf4j-migrator/src/main/java/org/slf4j/migrator/line/MultiGroupConversionRule.java
@@ -48,13 +48,6 @@ public class MultiGroupConversionRule implements ConversionRule {
     this.pattern = pattern;
   }
 
-  /* (non-Javadoc)
-   * @see org.slf4j.converter.ConversionRule#getPattern()
-   */
-  public Pattern getPattern() {
-    return pattern;
-  }
-
   public void addReplacement(int groupIndex, String replacement) {
     if(groupIndex == 0) {
       throw new IllegalArgumentException("regex groups start at 1, not zero");
@@ -70,26 +63,25 @@ public class MultiGroupConversionRule implements ConversionRule {
   }
 
   /* (non-Javadoc)
-   * @see org.slf4j.converter.ConversionRule#replace(java.util.regex.Matcher)
+   * @see org.slf4j.converter.ConversionRule#getReplacement(String)
    */
-  public String replace(Matcher matcher) {
-    StringBuffer replacementBuffer = new StringBuffer();
-    String replacementText;
-    
-    for (int group = 1; group <= matcher.groupCount(); group++) {
-      replacementText = getReplacement(group);
-      if (replacementText != null) {
-        //System.out.println("replacing group " + group + " : "
-        //    + matcher.group(group) + " with " + replacementText);
-        replacementBuffer.append(replacementText);
-      } else  {
-        replacementBuffer.append(matcher.group(group));
-      }
+  public String[] getReplacement(String text) {
+    Matcher matcher = pattern.matcher(text);
+    if(matcher.find()) {
+        StringBuffer replacementBuffer = new StringBuffer();
+        for (int group = 1; group <= matcher.groupCount(); group++) {
+          String replacementText = getReplacement(group);
+          if (replacementText != null) {
+            //System.out.println("replacing group " + group + " : "
+            //    + matcher.group(group) + " with " + replacementText);
+            replacementBuffer.append(replacementText);
+          } else  {
+            replacementBuffer.append(matcher.group(group));
+          }
+        }
+        return new String[]{replacementBuffer.toString()};
+    } else {
+        return new String[]{text};
     }
-    return replacementBuffer.toString();
-  }
-
-  public String getAdditionalLine() {
-    return null;
   }
 }
diff --git a/slf4j-migrator/src/main/java/org/slf4j/migrator/line/SingleConversionRule.java b/slf4j-migrator/src/main/java/org/slf4j/migrator/line/SingleConversionRule.java
index ccfa6fd..e8401f9 100644
--- a/slf4j-migrator/src/main/java/org/slf4j/migrator/line/SingleConversionRule.java
+++ b/slf4j-migrator/src/main/java/org/slf4j/migrator/line/SingleConversionRule.java
@@ -37,8 +37,8 @@ import java.util.regex.Pattern;
  */
 public class SingleConversionRule implements ConversionRule {
 
-  final  private Pattern pattern;
-  final private String replacementText ;
+  final private Pattern pattern;
+  final private String replacementText;
   final private String additionalLine;
   
   public SingleConversionRule(Pattern pattern, String replacementText) {
@@ -52,23 +52,19 @@ public class SingleConversionRule implements ConversionRule {
   }
   
   /* (non-Javadoc)
-   * @see org.slf4j.converter.ConversionRule#getPattern()
+   * @see org.slf4j.converter.ConversionRule#getReplacement(String)
    */
-  public Pattern getPattern() {
-    return pattern;
+  public String[] getReplacement(String text) {
+    Matcher matcher = pattern.matcher(text);
+    if(matcher.find()) {
+        String replacementLine = matcher.replaceAll(replacementText);
+        if(additionalLine != null) {
+            return new String[]{replacementLine, additionalLine};
+        } else {
+            return new String[]{replacementLine};
+        }
+    } else {
+        return new String[]{text};
+    }
   }
-
-
-
-  /* (non-Javadoc)
-   * @see org.slf4j.converter.ConversionRule#replace(java.util.regex.Matcher)
-   */
-  public String replace(Matcher matcher) {
-    return replacementText;
-  }
-
-  public String getAdditionalLine() {
-    return additionalLine;
-  }
-
 }
diff --git a/slf4j-migrator/src/test/java/org/slf4j/migrator/AternativeApproach.java b/slf4j-migrator/src/test/java/org/slf4j/migrator/AternativeApproach.java
index 4096580..ca8a568 100644
--- a/slf4j-migrator/src/test/java/org/slf4j/migrator/AternativeApproach.java
+++ b/slf4j-migrator/src/test/java/org/slf4j/migrator/AternativeApproach.java
@@ -39,17 +39,16 @@ public class AternativeApproach extends TestCase {
    * 
    */
   public void test() {
-    MultiGroupConversionRule cr2 = new MultiGroupConversionRule(Pattern
-        .compile("(.*)(Log)"));
+    Pattern pat = Pattern.compile("(.*)(Log)");
+    MultiGroupConversionRule cr2 = new MultiGroupConversionRule(pat);
     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);
+    String[] r = cr2.getReplacement(s);
+    assertEquals("abcd LOGGER", r[0]);
 
     System.out.println(r);
   }
-- 
1.8.1.msysgit.1

