[slf4j-dev] svn commit: r884 - in slf4j/trunk/slf4j-converter/src: main/java/org/slf4j/converter test/java/org/slf4j/converter
ceki at slf4j.org
ceki at slf4j.org
Thu Aug 30 00:21:07 CEST 2007
Author: ceki
Date: Thu Aug 30 00:21:07 2007
New Revision: 884
Modified:
slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/AbstractMatcher.java
slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Constant.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/PatternWrapper.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:
indentation changes only
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 Thu Aug 30 00:21:07 2007
@@ -5,89 +5,85 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-
public abstract class AbstractMatcher {
- protected ArrayList<PatternWrapper> rules;
+ protected ArrayList<PatternWrapper> rules;
- protected boolean commentConversion = true;
+ protected boolean commentConversion = true;
- protected boolean blockComment = false;
+ protected boolean blockComment = false;
+ public AbstractMatcher() {
+ }
- public AbstractMatcher() {
- }
-
- public static AbstractMatcher getMatcherImpl(int conversionType) {
- if(conversionType==Constant.JCL_TO_SLF4J){
- return new JCLMatcher();
- }
- return null;
- }
-
- public void setCommentConversion(boolean commentConversion) {
- this.commentConversion = commentConversion;
- }
-
- /**
- *
- * @param text
- */
- public String replace(String text) {
- if (isTextConvertible(text)) {
- PatternWrapper patternWrapper;
- Pattern pattern;
- Matcher matcher;
- String replacementText;
- Iterator rulesIter = rules.iterator();
- while (rulesIter.hasNext()) {
- patternWrapper = (PatternWrapper) rulesIter.next();
- pattern = patternWrapper.getPattern();
- matcher = pattern.matcher(text);
- if (matcher.matches()) {
- System.out.println("matching " + text);
- StringBuffer replacementBuffer = new StringBuffer();
- for (int group = 0; group <= matcher.groupCount(); group++) {
- replacementText = patternWrapper.getReplacement(group);
- if (replacementText != null) {
- System.out.println("replacing group " + group + " : "
- + matcher.group(group) + " with "
- + replacementText);
- replacementBuffer.append(replacementText);
- }
- else if (group > 0) {
- replacementBuffer.append(matcher.group(group));
- }
- }
- return replacementBuffer.toString();
- }
- }
- }
- return 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(Constant.LINE_COMMENT)) {
- isConvertible = false;
- } else if (text.startsWith(Constant.BLOCK_COMMENT_START)) {
- blockComment = true;
- isConvertible = false;
- }
- if (text.endsWith(Constant.BLOCK_COMMENT_END)) {
- blockComment = false;
- }
- return isConvertible;
- }
+ public static AbstractMatcher getMatcherImpl(int conversionType) {
+ if (conversionType == Constant.JCL_TO_SLF4J) {
+ return new JCLMatcher();
+ }
+ return null;
+ }
+
+ public void setCommentConversion(boolean commentConversion) {
+ this.commentConversion = commentConversion;
+ }
+
+ /**
+ *
+ * @param text
+ */
+ public String replace(String text) {
+ if (isTextConvertible(text)) {
+ PatternWrapper patternWrapper;
+ Pattern pattern;
+ Matcher matcher;
+ String replacementText;
+ Iterator rulesIter = rules.iterator();
+ while (rulesIter.hasNext()) {
+ patternWrapper = (PatternWrapper) rulesIter.next();
+ pattern = patternWrapper.getPattern();
+ matcher = pattern.matcher(text);
+ if (matcher.matches()) {
+ System.out.println("matching " + text);
+ StringBuffer replacementBuffer = new StringBuffer();
+ for (int group = 0; group <= matcher.groupCount(); group++) {
+ replacementText = patternWrapper.getReplacement(group);
+ if (replacementText != null) {
+ System.out.println("replacing group " + group + " : "
+ + matcher.group(group) + " with " + replacementText);
+ replacementBuffer.append(replacementText);
+ } else if (group > 0) {
+ replacementBuffer.append(matcher.group(group));
+ }
+ }
+ return replacementBuffer.toString();
+ }
+ }
+ }
+ return 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(Constant.LINE_COMMENT)) {
+ isConvertible = false;
+ } else if (text.startsWith(Constant.BLOCK_COMMENT_START)) {
+ blockComment = true;
+ isConvertible = false;
+ }
+ if (text.endsWith(Constant.BLOCK_COMMENT_END)) {
+ blockComment = false;
+ }
+ return isConvertible;
+ }
- protected abstract void initRules();
+ protected abstract void initRules();
}
Modified: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Constant.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Constant.java (original)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Constant.java Thu Aug 30 00:21:07 2007
@@ -2,26 +2,26 @@
public class Constant {
- public final static int JCL_TO_SLF4J = 1;
+ public final static int JCL_TO_SLF4J = 1;
- public final static String LINE_COMMENT = "//";
+ public final static String LINE_COMMENT = "//";
- public final static String BLOCK_COMMENT_START = "/*";
+ public final static String BLOCK_COMMENT_START = "/*";
- public final static String BLOCK_COMMENT_END = "*/";
+ public final static String BLOCK_COMMENT_END = "*/";
- public final static Integer INDEX_0 = new Integer(0);
+ public final static Integer INDEX_0 = new Integer(0);
- public final static Integer INDEX_1 = new Integer(1);
+ public final static Integer INDEX_1 = new Integer(1);
- public final static Integer INDEX_2 = new Integer(2);
+ public final static Integer INDEX_2 = new Integer(2);
- public final static Integer INDEX_3 = new Integer(3);
+ public final static Integer INDEX_3 = new Integer(3);
- public final static Integer INDEX_4 = new Integer(4);
+ public final static Integer INDEX_4 = new Integer(4);
- public final static Integer INDEX_5 = new Integer(5);
+ public final static Integer INDEX_5 = new Integer(5);
- public final static int NB_FILES_MAX = 1000;
+ public final static int NB_FILES_MAX = 1000;
}
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 Thu Aug 30 00:21:07 2007
@@ -17,198 +17,196 @@
public class Converter {
- private List<File> javaFiles;
+ private List<File> javaFiles;
- private AbstractMatcher matcher;
+ private AbstractMatcher matcher;
- private Writer writer;
+ private Writer writer;
- private String source;
+ private String source;
- private int conversionType;
-
- private boolean commentConversion;
-
- /**
- * @param args
- * @throws IOException
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
-
- Converter converter = new Converter();
-
- if (args.length > 0) {
- converter.source = args[0];
- } else {
- // converter.source = new File("").getAbsolutePath();
- JFileChooser selector = new JFileChooser();
- selector.setDialogTitle("Source folder selector");
- selector.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
- int res = selector.showOpenDialog(null);
- if (res == JFileChooser.APPROVE_OPTION) {
- File folder = selector.getSelectedFile();
- converter.source = folder.getAbsolutePath();
- } else {
- return;
- }
- }
-
- converter.conversionType = Constant.JCL_TO_SLF4J;
- if (converter.init()) {
- converter.convert(converter.javaFiles);
- }
- }
-
- /**
- *
- * @return
- * @throws IOException
- */
- public boolean init() throws IOException {
- matcher = AbstractMatcher.getMatcherImpl(conversionType);
- if (matcher == null) {
- return false;
- }
- matcher.setCommentConversion(commentConversion);
- writer = new Writer();
-
- File fileSource = new File(source);
- if (!fileSource.isDirectory()) {
- System.out.println("source path is not a valid source directory");
- return false;
- } else {
- BufferedReader in = new BufferedReader(new InputStreamReader(
- System.in));
- System.out
- .println("RUNNING CONVERTER WILL REPLACE JAVA FILES CONTAINED IN "
- + source + ", DO YOU WANT TO CONTINUE Y / N ?");
- String response = in.readLine();
- if (response.equalsIgnoreCase("N")) {
- return false;
- }
-
- selectFiles(fileSource);
-
- if (javaFiles.size() > 1000) {
- System.out.println("THERE IS " + javaFiles.size()
- + " FILES TO CONVERT, DO YOU WANT TO CONTINUE Y / N ?");
- if (response.equalsIgnoreCase("N")) {
- return false;
- }
- }
- }
- return true;
- }
-
- /**
- *
- * @param fdest
- */
- private void delete(File fdest) {
- if (fdest.isDirectory()) {
- File[] files = fdest.listFiles();
- if (files != null) {
- for (int i = 0; i < files.length; i++) {
- delete(files[i]);
- }
- }
- fdest.delete();
- } else {
- fdest.delete();
- }
- }
-
- /**
- *
- * @param fsource
- * @param fdest
- */
- private void copy(File fsource, File fdest) {
- try {
- FileInputStream fis = new FileInputStream(fsource);
- FileOutputStream fos = new FileOutputStream(fdest);
- FileChannel channelSource = fis.getChannel();
- FileChannel channelDest = fos.getChannel();
- if (channelSource.isOpen() && channelDest.isOpen()) {
- channelSource.transferTo(0, channelSource.size(), channelDest);
- channelSource.close();
- channelDest.close();
- } else {
- System.out.println("error copying file "
- + fsource.getAbsolutePath());
- }
-
- } catch (FileNotFoundException exc) {
- System.out.println(exc.toString());
- } catch (IOException e) {
- System.out.println(e.toString());
- }
- }
-
- /**
- *
- * @param file
- * @return
- */
- private List<File> selectFiles(File file) {
- if (javaFiles == null) {
- javaFiles = new ArrayList<File>();
- }
- if (file.isDirectory()) {
- File[] files = file.listFiles();
- if (files != null) {
- for (int i = 0; i < files.length; i++) {
- selectFiles(files[i]);
- }
- }
- } else {
- if (file.getName().endsWith(".java")) {
- javaFiles.add(file);
- }
- }
- return javaFiles;
- }
-
- /**
- *
- * @param lstFiles
- */
- private void convert(List<File> lstFiles) {
- Iterator<File> itFile = lstFiles.iterator();
- while (itFile.hasNext()) {
- File currentFile = itFile.next();
- 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) {
- System.out.println("error reading file " + exc);
- }
- }
+ private int conversionType;
+
+ private boolean commentConversion;
+
+ /**
+ * @param args
+ * @throws IOException
+ * @throws IOException
+ */
+ public static void main(String[] args) throws IOException {
+
+ Converter converter = new Converter();
+
+ if (args.length > 0) {
+ converter.source = args[0];
+ } else {
+ // converter.source = new File("").getAbsolutePath();
+ JFileChooser selector = new JFileChooser();
+ selector.setDialogTitle("Source folder selector");
+ selector.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
+ int res = selector.showOpenDialog(null);
+ if (res == JFileChooser.APPROVE_OPTION) {
+ File folder = selector.getSelectedFile();
+ converter.source = folder.getAbsolutePath();
+ } else {
+ return;
+ }
+ }
+
+ converter.conversionType = Constant.JCL_TO_SLF4J;
+ if (converter.init()) {
+ converter.convert(converter.javaFiles);
+ }
+ }
+
+ /**
+ *
+ * @return
+ * @throws IOException
+ */
+ public boolean init() throws IOException {
+ matcher = AbstractMatcher.getMatcherImpl(conversionType);
+ if (matcher == null) {
+ return false;
+ }
+ matcher.setCommentConversion(commentConversion);
+ writer = new Writer();
+
+ File fileSource = new File(source);
+ if (!fileSource.isDirectory()) {
+ System.out.println("source path is not a valid source directory");
+ return false;
+ } else {
+ BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
+ System.out
+ .println("RUNNING CONVERTER WILL REPLACE JAVA FILES CONTAINED IN "
+ + source + ", DO YOU WANT TO CONTINUE Y / N ?");
+ String response = in.readLine();
+ if (response.equalsIgnoreCase("N")) {
+ return false;
+ }
+
+ selectFiles(fileSource);
+
+ if (javaFiles.size() > 1000) {
+ System.out.println("THERE IS " + javaFiles.size()
+ + " FILES TO CONVERT, DO YOU WANT TO CONTINUE Y / N ?");
+ if (response.equalsIgnoreCase("N")) {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ /**
+ *
+ * @param fdest
+ */
+ private void delete(File fdest) {
+ if (fdest.isDirectory()) {
+ File[] files = fdest.listFiles();
+ if (files != null) {
+ for (int i = 0; i < files.length; i++) {
+ delete(files[i]);
+ }
+ }
+ fdest.delete();
+ } else {
+ fdest.delete();
+ }
+ }
+
+ /**
+ *
+ * @param fsource
+ * @param fdest
+ */
+ private void copy(File fsource, File fdest) {
+ try {
+ FileInputStream fis = new FileInputStream(fsource);
+ FileOutputStream fos = new FileOutputStream(fdest);
+ FileChannel channelSource = fis.getChannel();
+ FileChannel channelDest = fos.getChannel();
+ if (channelSource.isOpen() && channelDest.isOpen()) {
+ channelSource.transferTo(0, channelSource.size(), channelDest);
+ channelSource.close();
+ channelDest.close();
+ } else {
+ System.out.println("error copying file " + fsource.getAbsolutePath());
+ }
+
+ } catch (FileNotFoundException exc) {
+ System.out.println(exc.toString());
+ } catch (IOException e) {
+ System.out.println(e.toString());
+ }
+ }
+
+ /**
+ *
+ * @param file
+ * @return
+ */
+ private List<File> selectFiles(File file) {
+ if (javaFiles == null) {
+ javaFiles = new ArrayList<File>();
+ }
+ if (file.isDirectory()) {
+ File[] files = file.listFiles();
+ if (files != null) {
+ for (int i = 0; i < files.length; i++) {
+ selectFiles(files[i]);
+ }
+ }
+ } else {
+ if (file.getName().endsWith(".java")) {
+ javaFiles.add(file);
+ }
+ }
+ return javaFiles;
+ }
+
+ /**
+ *
+ * @param lstFiles
+ */
+ private void convert(List<File> lstFiles) {
+ Iterator<File> itFile = lstFiles.iterator();
+ while (itFile.hasNext()) {
+ File currentFile = itFile.next();
+ 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) {
+ 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 Thu Aug 30 00:21:07 2007
@@ -3,68 +3,91 @@
import java.util.ArrayList;
import java.util.regex.Pattern;
-
public class JCLMatcher extends AbstractMatcher {
- public JCLMatcher() {
- super();
- initRules();
- }
+ public JCLMatcher() {
+ super();
+ initRules();
+ }
+
+ protected void initRules() {
+ // matching : import org.apache.commons.logging.LogFactory;
+ PatternWrapper p0 = new PatternWrapper(Pattern
+ .compile("import\\s*+org.apache.commons.logging.LogFactory;"));
+ // matching : import org.apache.commons.logging.Log;
+ PatternWrapper p1 = new PatternWrapper(Pattern
+ .compile("import\\s*+org.apache.commons.logging.Log;"));
+ // matching declaration and instanciation : protected Log myLog =
+ // LogFactory.getFactory().getInstance(MyClass.class); //comment or other
+ // instruction
+ PatternWrapper p2 = new PatternWrapper(
+ Pattern
+ .compile("((\\w*+\\W*+\\.*)*;*+)(Log)(\\s+\\w+\\s*+=\\s*+)(LogFactory.getFactory\\(\\).getInstance\\()(\\w+)(.class\\);)((\\w*+\\W*+\\.*)*;*+)"));
+ // matching declaration and instanciation : protected static Log myLog =
+ // LogFactory.getLog(MyClass.class); //comment or other instruction
+ PatternWrapper p3 = new PatternWrapper(
+ Pattern
+ .compile("((\\w*+\\W*+\\.*)*;*+)(Log)(\\s+\\w+\\s*+=\\s*+)(LogFactory.getLog\\()(\\w+)(.class\\);)((\\w*+\\W*+\\.*)*;*+)"));
+ // matching instanciation without declaration : myLog =
+ // LogFactory.getFactory().getInstance(MyClass.class); //comment or other
+ // instruction
+ PatternWrapper p4 = new PatternWrapper(
+ Pattern
+ .compile("((\\w*+\\W*+\\.*)*;*+)(\\w+\\s*+=\\s*+)(LogFactory.getFactory\\(\\).getInstance\\()(\\w+)(.class\\);)((\\w*+\\W*+\\.*)*;*+)"));
+ // matching instanciation without declaration : myLog =
+ // LogFactory.getLog(MyClass.class); //comment or other instruction
+ PatternWrapper p5 = new PatternWrapper(
+ Pattern
+ .compile("((\\w*+\\W*+\\.*)*;*+)(\\w+\\s*+=\\s*+)(LogFactory.getLog\\()(\\w+)(.class\\);)((\\w*+\\W*+\\.*)*;*+)"));
+ // matching declaration without instanciation : public static final Log
+ // myLog //comment or other instruction
+ PatternWrapper p6 = new PatternWrapper(
+ Pattern
+ .compile("((\\w*+\\W*+\\.*)*;*+)(Log)(\\s*+\\w+\\s*+;)((\\w*+\\W*+\\.*)*;*+)"));
+ // matching incomplete instanciation : protected Log log =
+ PatternWrapper p7 = new PatternWrapper(Pattern
+ .compile("((\\w*+\\W*+\\.*)*;*+)(Log)(\\s+\\w+\\s*+=*\\s*+)"));
+ // matching incomlete instanciation : LogFactory.getLog(MyComponent.class);
+ PatternWrapper p8 = new PatternWrapper(
+ Pattern
+ .compile("((\\w*+\\W*+\\.*)*;*+)(LogFactory.getLog\\()(\\w+)(.class\\);)((\\w*+\\W*+\\.*)*;*+)"));
+ // matching incomlete instanciation :
+ // LogFactory.getFactory().getInstance(MyComponent.class);
+ PatternWrapper p9 = new PatternWrapper(
+ Pattern
+ .compile("((\\w*+\\W*+\\.*)*;*+)(LogFactory.getFactory\\(\\).getInstance\\()(\\w+)(.class\\);)((\\w*+\\W*+\\.*)*;*+)"));
+
+ p0.addReplacement(Constant.INDEX_0, "import org.slf4j.LoggerFactory;");
+ p1.addReplacement(Constant.INDEX_0, "import org.slf4j.Logger;");
+ p2.addReplacement(Constant.INDEX_3, "Logger");
+ p2.addReplacement(Constant.INDEX_2, "");
+ p2.addReplacement(Constant.INDEX_5, "LoggerFactory.getLogger(");
+ p3.addReplacement(Constant.INDEX_3, "Logger");
+ p3.addReplacement(Constant.INDEX_2, "");
+ p3.addReplacement(Constant.INDEX_5, "LoggerFactory.getLogger(");
+ p4.addReplacement(Constant.INDEX_4, "LoggerFactory.getLogger(");
+ p4.addReplacement(Constant.INDEX_2, "");
+ p5.addReplacement(Constant.INDEX_4, "LoggerFactory.getLogger(");
+ p5.addReplacement(Constant.INDEX_2, "");
+ p6.addReplacement(Constant.INDEX_3, "Logger");
+ p6.addReplacement(Constant.INDEX_2, "");
+ p7.addReplacement(Constant.INDEX_3, "Logger");
+ p7.addReplacement(Constant.INDEX_2, "");
+ p8.addReplacement(Constant.INDEX_3, "LoggerFactory.getLogger(");
+ p8.addReplacement(Constant.INDEX_1, "");
+ p9.addReplacement(Constant.INDEX_3, "LoggerFactory.getLogger(");
+ p9.addReplacement(Constant.INDEX_1, "");
- protected void initRules() {
- //matching : import org.apache.commons.logging.LogFactory;
- PatternWrapper p0 = new PatternWrapper(Pattern.compile("import\\s*+org.apache.commons.logging.LogFactory;"));
- //matching : import org.apache.commons.logging.Log;
- PatternWrapper p1 = new PatternWrapper(Pattern.compile("import\\s*+org.apache.commons.logging.Log;"));
- //matching declaration and instanciation : protected Log myLog = LogFactory.getFactory().getInstance(MyClass.class); //comment or other instruction
- PatternWrapper p2 = new PatternWrapper(Pattern.compile("((\\w*+\\W*+\\.*)*;*+)(Log)(\\s+\\w+\\s*+=\\s*+)(LogFactory.getFactory\\(\\).getInstance\\()(\\w+)(.class\\);)((\\w*+\\W*+\\.*)*;*+)"));
- //matching declaration and instanciation : protected static Log myLog = LogFactory.getLog(MyClass.class); //comment or other instruction
- PatternWrapper p3 = new PatternWrapper(Pattern.compile("((\\w*+\\W*+\\.*)*;*+)(Log)(\\s+\\w+\\s*+=\\s*+)(LogFactory.getLog\\()(\\w+)(.class\\);)((\\w*+\\W*+\\.*)*;*+)"));
- //matching instanciation without declaration : myLog = LogFactory.getFactory().getInstance(MyClass.class); //comment or other instruction
- PatternWrapper p4 = new PatternWrapper(Pattern.compile("((\\w*+\\W*+\\.*)*;*+)(\\w+\\s*+=\\s*+)(LogFactory.getFactory\\(\\).getInstance\\()(\\w+)(.class\\);)((\\w*+\\W*+\\.*)*;*+)"));
- //matching instanciation without declaration : myLog = LogFactory.getLog(MyClass.class); //comment or other instruction
- PatternWrapper p5 = new PatternWrapper(Pattern.compile("((\\w*+\\W*+\\.*)*;*+)(\\w+\\s*+=\\s*+)(LogFactory.getLog\\()(\\w+)(.class\\);)((\\w*+\\W*+\\.*)*;*+)"));
- //matching declaration without instanciation : public static final Log myLog //comment or other instruction
- PatternWrapper p6 = new PatternWrapper(Pattern.compile("((\\w*+\\W*+\\.*)*;*+)(Log)(\\s*+\\w+\\s*+;)((\\w*+\\W*+\\.*)*;*+)"));
- //matching incomplete instanciation : protected Log log =
- PatternWrapper p7 = new PatternWrapper(Pattern.compile("((\\w*+\\W*+\\.*)*;*+)(Log)(\\s+\\w+\\s*+=*\\s*+)"));
- //matching incomlete instanciation : LogFactory.getLog(MyComponent.class);
- PatternWrapper p8 = new PatternWrapper(Pattern.compile("((\\w*+\\W*+\\.*)*;*+)(LogFactory.getLog\\()(\\w+)(.class\\);)((\\w*+\\W*+\\.*)*;*+)"));
- //matching incomlete instanciation : LogFactory.getFactory().getInstance(MyComponent.class);
- PatternWrapper p9 = new PatternWrapper(Pattern.compile("((\\w*+\\W*+\\.*)*;*+)(LogFactory.getFactory\\(\\).getInstance\\()(\\w+)(.class\\);)((\\w*+\\W*+\\.*)*;*+)"));
-
- p0.addReplacement(Constant.INDEX_0,"import org.slf4j.LoggerFactory;");
- p1.addReplacement(Constant.INDEX_0,"import org.slf4j.Logger;");
- p2.addReplacement(Constant.INDEX_3,"Logger");
- p2.addReplacement(Constant.INDEX_2,"");
- p2.addReplacement(Constant.INDEX_5,"LoggerFactory.getLogger(");
- p3.addReplacement(Constant.INDEX_3,"Logger");
- p3.addReplacement(Constant.INDEX_2,"");
- p3.addReplacement(Constant.INDEX_5,"LoggerFactory.getLogger(");
- p4.addReplacement(Constant.INDEX_4,"LoggerFactory.getLogger(");
- p4.addReplacement(Constant.INDEX_2,"");
- p5.addReplacement(Constant.INDEX_4,"LoggerFactory.getLogger(");
- p5.addReplacement(Constant.INDEX_2,"");
- p6.addReplacement(Constant.INDEX_3,"Logger");
- p6.addReplacement(Constant.INDEX_2,"");
- p7.addReplacement(Constant.INDEX_3, "Logger");
- p7.addReplacement(Constant.INDEX_2, "");
- p8.addReplacement(Constant.INDEX_3, "LoggerFactory.getLogger(");
- p8.addReplacement(Constant.INDEX_1, "");
- p9.addReplacement(Constant.INDEX_3, "LoggerFactory.getLogger(");
- p9.addReplacement(Constant.INDEX_1, "");
-
-
- rules = new ArrayList<PatternWrapper>();
- rules.add(p0);
- rules.add(p1);
- rules.add(p2);
- rules.add(p3);
- rules.add(p4);
- rules.add(p5);
- rules.add(p6);
- rules.add(p7);
- rules.add(p8);
- rules.add(p9);
- }
+ rules = new ArrayList<PatternWrapper>();
+ rules.add(p0);
+ rules.add(p1);
+ rules.add(p2);
+ rules.add(p3);
+ rules.add(p4);
+ rules.add(p5);
+ rules.add(p6);
+ rules.add(p7);
+ rules.add(p8);
+ rules.add(p9);
+ }
}
Modified: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/PatternWrapper.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/PatternWrapper.java (original)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/PatternWrapper.java Thu Aug 30 00:21:07 2007
@@ -3,28 +3,27 @@
import java.util.HashMap;
import java.util.regex.Pattern;
-public class PatternWrapper {
+public class PatternWrapper {
+
+ private Pattern pattern;
+
+ private HashMap<Integer, String> replacementMap;
+
+ public PatternWrapper(Pattern pattern) {
+ this.pattern = pattern;
+ this.replacementMap = new HashMap<Integer, String>();
+ }
+
+ public Pattern getPattern() {
+ return pattern;
+ }
+
+ public void addReplacement(Integer groupIndex, String replacement) {
+ replacementMap.put(groupIndex, replacement);
+ }
+
+ public String getReplacement(Integer groupIndex) {
+ return replacementMap.get(groupIndex);
+ }
- private Pattern pattern;
-
- private HashMap<Integer, String> replacementMap;
-
- public PatternWrapper(Pattern pattern) {
- this.pattern = pattern;
- this.replacementMap = new HashMap<Integer, String>();
- }
-
-
- public Pattern getPattern() {
- return pattern;
- }
-
- public void addReplacement(Integer groupIndex, String replacement) {
- replacementMap.put(groupIndex, replacement);
- }
-
- public String getReplacement(Integer groupIndex) {
- return replacementMap.get(groupIndex);
- }
-
}
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 Thu Aug 30 00:21:07 2007
@@ -6,69 +6,67 @@
import java.io.IOException;
import java.util.regex.Matcher;
-
public class Writer {
+ BufferedWriter bwriter;
- BufferedWriter bwriter;
-
- boolean isFirstLine;
+ boolean isFirstLine;
- public Writer() {
- }
+ public Writer() {
+ }
- public void initFileWriter(File file) {
- try {
- FileWriter fileWriter = new FileWriter(file);
- bwriter = new BufferedWriter(fileWriter);
- isFirstLine = true;
- } catch (IOException exc) {
- System.out.println("error creating filewriter " + file.getAbsolutePath());
- }
- }
-
- public void closeFileWriter() {
- if (bwriter != null) {
- try {
- bwriter.flush();
- bwriter.close();
- } catch (IOException e) {
- System.out.println("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);
- //System.out.println("new entry " + text);
- } catch (IOException exc) {
- System.out.println("error writing file " + bwriter.toString());
- }
- }
- }
-
- public void write(String text) {
- if (bwriter != null) {
- try {
- if (!isFirstLine) {
- bwriter.newLine();
- } else {
- isFirstLine = false;
- }
- bwriter.write(text);
- //System.out.println("new entry " + text);
- } catch (IOException exc) {
- System.out.println("error writing file " + bwriter.toString());
- }
- }
- }
+ public void initFileWriter(File file) {
+ try {
+ FileWriter fileWriter = new FileWriter(file);
+ bwriter = new BufferedWriter(fileWriter);
+ isFirstLine = true;
+ } catch (IOException exc) {
+ System.out.println("error creating filewriter " + file.getAbsolutePath());
+ }
+ }
+
+ public void closeFileWriter() {
+ if (bwriter != null) {
+ try {
+ bwriter.flush();
+ bwriter.close();
+ } catch (IOException e) {
+ System.out.println("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);
+ // System.out.println("new entry " + text);
+ } catch (IOException exc) {
+ System.out.println("error writing file " + bwriter.toString());
+ }
+ }
+ }
+
+ public void write(String text) {
+ if (bwriter != null) {
+ try {
+ if (!isFirstLine) {
+ bwriter.newLine();
+ } else {
+ isFirstLine = false;
+ }
+ bwriter.write(text);
+ // System.out.println("new entry " + text);
+ } catch (IOException exc) {
+ 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 Thu Aug 30 00:21:07 2007
@@ -1,46 +1,82 @@
package org.slf4j.converter;
-
-
import java.io.IOException;
import junit.framework.TestCase;
public class JCLMatcherTest extends TestCase {
-
-
- public void testConversion() throws IOException {
- AbstractMatcher jclMatcher = AbstractMatcher.getMatcherImpl(1);
- jclMatcher.setCommentConversion(true);
-
- 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;");
- assertEquals(jclMatcher.replace("protected Log log ="), "protected Logger log =");
- assertEquals(jclMatcher.replace(" LogFactory.getLog(MyComponent.class);"), " LoggerFactory.getLogger(MyComponent.class);");
- assertEquals(jclMatcher.replace("protected Log log "), "protected Logger log ");
- assertEquals(jclMatcher.replace(" = LogFactory.getFactory().getInstance(MyComponent.class);"), " = LoggerFactory.getLogger(MyComponent.class);");
- }
-
-
-
-
-
+ public void testConversion() throws IOException {
+ AbstractMatcher jclMatcher = AbstractMatcher.getMatcherImpl(1);
+ jclMatcher.setCommentConversion(true);
+
+ 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;");
+ assertEquals(jclMatcher.replace("protected Log log ="),
+ "protected Logger log =");
+ assertEquals(jclMatcher
+ .replace(" LogFactory.getLog(MyComponent.class);"),
+ " LoggerFactory.getLogger(MyComponent.class);");
+ assertEquals(jclMatcher.replace("protected Log log "),
+ "protected Logger log ");
+ assertEquals(
+ jclMatcher
+ .replace(" = LogFactory.getFactory().getInstance(MyComponent.class);"),
+ " = LoggerFactory.getLogger(MyComponent.class);");
+ }
}
More information about the slf4j-dev
mailing list