KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > util > ExprEditor


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002,2003 Fraunhofer Gesellschaft
5  * Fraunhofer Institut for Computer Architecture and Software Technology
6  * All Rights Reserved.
7  *
8  * Please visit http://snipsnap.org/ for updates and contact.
9  *
10  * --LICENSE NOTICE--
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  * --LICENSE NOTICE--
25  */

26 package org.snipsnap.util;
27
28 import java.io.BufferedReader JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.FileReader JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.FilenameFilter JavaDoc;
33 import java.io.BufferedWriter JavaDoc;
34 import java.io.FileWriter JavaDoc;
35 import java.util.regex.Pattern JavaDoc;
36 import java.util.regex.Matcher JavaDoc;
37
38 /**
39  * A utility to find files matching a certain regular expression and
40  * potentially replacing the matched part with the file specified.
41  *
42  * Example: "(/\*.*?part of.*?\* /)" (remove last space!)
43  *
44  * @author Matthias L. Jugel
45  * @version $Id: ExprEditor.java 1606 2004-05-17 10:56:18Z leo $
46  */

47 public class ExprEditor {
48
49   public static void main(String JavaDoc[] args) {
50     String JavaDoc dir = args[0];
51     String JavaDoc regex = args[1];
52     String JavaDoc replacement = null;
53
54     if(regex.startsWith("file:")) {
55       try {
56         regex = getFileContent(new File JavaDoc(regex.substring("file:".length())));
57         System.err.println("searching for files matching: "+regex);
58       } catch (IOException JavaDoc e) {
59         System.err.println("Error: can't find file '"+regex+"'");
60         System.exit(1);
61       }
62     }
63
64
65     if(args.length > 2) {
66       replacement = args[2];
67       if (replacement.startsWith("file:")) {
68         try {
69           replacement = getFileContent(new File JavaDoc(replacement.substring("file:".length())));
70         } catch (IOException JavaDoc e) {
71           System.err.println("Error: can't find file '" + replacement + "'");
72           System.exit(1);
73         }
74       }
75     }
76
77     Pattern JavaDoc pattern = Pattern.compile(regex, Pattern.DOTALL|Pattern.MULTILINE);
78
79     traverse(new File JavaDoc(dir), pattern, replacement);
80   }
81
82   private static String JavaDoc getFileContent(File JavaDoc file) throws IOException JavaDoc {
83     BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
84     StringBuffer JavaDoc content = new StringBuffer JavaDoc();
85     char[] buffer = new char[1024];
86     int len = 0;
87     while((len = reader.read(buffer)) != -1) {
88       content.append(buffer, 0, len);
89     }
90     reader.close();
91     return content.toString();
92   }
93
94   private static void traverse(File JavaDoc root, Pattern JavaDoc pattern, String JavaDoc replacement) {
95     File JavaDoc[] files = root.listFiles(new FilenameFilter JavaDoc() {
96       public boolean accept(File JavaDoc file, String JavaDoc s) {
97         return new File JavaDoc(file, s).isDirectory() || s.endsWith(".java");
98       }
99     });
100     for (int entry = 0; files != null && entry < files.length; entry++) {
101       if (files[entry].isDirectory()) {
102         traverse(files[entry], pattern, replacement);
103       } else {
104         replace(files[entry], pattern, replacement);
105       }
106     }
107   }
108
109   private static void replace(File JavaDoc file, Pattern JavaDoc pattern, String JavaDoc replacement) {
110     try {
111       String JavaDoc content = getFileContent(file);
112 // System.err.println("checking '" + file + "' ("+content.length()+")");
113
Matcher JavaDoc matcher = pattern.matcher(content);
114       if(matcher.find()) {
115         if(null != replacement) {
116           content = matcher.replaceFirst(replacement);
117           storeFileContent(file, content);
118           System.err.println("modified '"+file.getPath()+"' ("+matcher.groupCount()+")");
119         } else {
120           System.err.println("found '"+file.getPath()+"' ("+matcher.groupCount()+")");
121         }
122       }
123     } catch (IOException JavaDoc e) {
124       System.err.println("unable to load '"+file+"'");
125     }
126   }
127
128   private static void storeFileContent(File JavaDoc file, String JavaDoc content) throws IOException JavaDoc {
129     File JavaDoc modifiedFile = new File JavaDoc(file.getParentFile(), file.getName() + ".new");
130     BufferedWriter JavaDoc writer = new BufferedWriter JavaDoc(new FileWriter JavaDoc(modifiedFile));
131     writer.write(content);
132     writer.close();
133     modifiedFile.renameTo(file);
134   }
135
136 }
137
Popular Tags