1 package org.slf4j.converter;
2
3 import java.util.Iterator;
4 import java.util.ArrayList;
5 import java.util.regex.Matcher;
6 import java.util.regex.Pattern;
7
8
9 public abstract class AbstractMatcher {
10
11 protected ArrayList<PatternWrapper> rules;
12
13 protected boolean commentConversion = true;
14
15 protected boolean blockComment = false;
16
17
18 public AbstractMatcher() {
19 }
20
21 public static AbstractMatcher getMatcherImpl(int conversionType) {
22 if(conversionType==Constant.JCL_TO_SLF4J){
23 return new JCLMatcher();
24 }
25 return null;
26 }
27
28 public void setCommentConversion(boolean commentConversion) {
29 this.commentConversion = commentConversion;
30 }
31
32
33
34
35
36 public String replace(String text) {
37 if (isTextConvertible(text)) {
38 PatternWrapper patternWrapper;
39 Pattern pattern;
40 Matcher matcher;
41 String replacementText;
42 Iterator rulesIter = rules.iterator();
43 while (rulesIter.hasNext()) {
44 patternWrapper = (PatternWrapper) rulesIter.next();
45 pattern = patternWrapper.getPattern();
46 matcher = pattern.matcher(text);
47 if (matcher.matches()) {
48 System.out.println("matching " + text);
49 StringBuffer replacementBuffer = new StringBuffer();
50 for (int group = 0; group <= matcher.groupCount(); group++) {
51 replacementText = patternWrapper.getReplacement(group);
52 if (replacementText != null) {
53 System.out.println("replacing group " + group + " : "
54 + matcher.group(group) + " with "
55 + replacementText);
56 replacementBuffer.append(replacementText);
57 }
58 else if (group > 0) {
59 replacementBuffer.append(matcher.group(group));
60 }
61 }
62 return replacementBuffer.toString();
63 }
64 }
65 }
66 return text;
67 }
68
69
70
71
72
73
74 private boolean isTextConvertible(String text) {
75 boolean isConvertible = true;
76 if (text.trim().length() == 0) {
77 isConvertible = false;
78 } else if (commentConversion) {
79 isConvertible = true;
80 } else if (blockComment || text.startsWith(Constant.LINE_COMMENT)) {
81 isConvertible = false;
82 } else if (text.startsWith(Constant.BLOCK_COMMENT_START)) {
83 blockComment = true;
84 isConvertible = false;
85 }
86 if (text.endsWith(Constant.BLOCK_COMMENT_END)) {
87 blockComment = false;
88 }
89 return isConvertible;
90 }
91
92 protected abstract void initRules();
93 }