KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > search > Grep


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: Grep.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.search;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.nio.CharBuffer JavaDoc;
26 import java.nio.MappedByteBuffer JavaDoc;
27 import java.nio.channels.FileChannel JavaDoc;
28 import java.nio.charset.Charset JavaDoc;
29 import java.nio.charset.CharsetDecoder JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.regex.Matcher JavaDoc;
33 import java.util.regex.Pattern JavaDoc;
34
35 /**
36  * Utility class to provide a subset of the grep functionality.
37  */

38 public class Grep {
39
40     // Charset and decoder for ISO-8859-15
41
private static Charset JavaDoc charset = Charset.forName("ISO-8859-15");
42     private static CharsetDecoder JavaDoc decoder = charset.newDecoder();
43
44     /**
45      * Check if the given file contains the pattern
46      *
47      * @param file the file which is to be searched for the pattern
48      * @param pattern the pattern that is being searched.
49      *
50      * @return true if the file contains the string, false otherwise.
51      *
52      * @throws IOException
53      */

54     public static boolean containsPattern(File JavaDoc file, Pattern JavaDoc pattern)
55         throws IOException JavaDoc {
56
57         // Open the file and then get a channel from the stream
58
FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
59         FileChannel JavaDoc fc = fis.getChannel();
60
61         // Get the file's size and then map it into memory
62
int sz = (int)fc.size();
63         MappedByteBuffer JavaDoc bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz);
64
65         // Decode the file into a char buffer
66
CharBuffer JavaDoc cb = decoder.decode(bb);
67
68         // Perform the search
69
Matcher JavaDoc pm = pattern.matcher(cb); // Pattern matcher
70

71         boolean result = pm.find();
72
73         // Close the channel and the stream
74
fc.close();
75         fis.close();
76
77         return result;
78     }
79
80     /**
81      * Find all occurences of pattern in a file.
82      *
83      * @param file the file to search for occurences of pattern
84      * @param pattern the pattern to search for
85      * @param group which group in the pattern to return
86      *
87      * @return an <code>array</code> of occurences of pattern
88      * (i.e. the groupth group of the match)
89      *
90      * @throws IOException if the file could not be read.
91      *
92      */

93     public static String JavaDoc[] findPattern(File JavaDoc file, Pattern JavaDoc pattern, int group)
94         throws IOException JavaDoc {
95
96         ArrayList JavaDoc occurences = new ArrayList JavaDoc();
97
98         // Open the file and then get a channel from the stream
99
FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
100         FileChannel JavaDoc fc = fis.getChannel();
101
102         // Get the file's size and then map it into memory
103
int sz = (int)fc.size();
104         MappedByteBuffer JavaDoc bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz);
105
106         // Decode the file into a char buffer
107
CharBuffer JavaDoc cb = decoder.decode(bb);
108
109         // Perform the search
110
Matcher JavaDoc pm = pattern.matcher(cb); // Pattern matcher
111

112         while (pm.find()) {
113             occurences.add(pm.group(group));
114         }
115
116         // Close the channel and the stream
117
fc.close();
118         fis.close();
119
120         return (String JavaDoc[])occurences.toArray(new String JavaDoc[occurences.size()]);
121
122     }
123
124     /**
125      * Find all files below the given file which contain the given pattern.
126      *
127      * @param file the file where to start the search for the pattern.
128      * @param pattern the pattern to search for.
129      *
130      * @return an array of files which contain the pattern
131      *
132      * @throws IOException if any of the files could not be opened.
133      */

134     private static List JavaDoc find_internal(File JavaDoc file, Pattern JavaDoc pattern)
135         throws IOException JavaDoc {
136         ArrayList JavaDoc fileList = new ArrayList JavaDoc();
137
138         if (file.isDirectory()) {
139             String JavaDoc[] children = file.list();
140             for (int i = 0; i < children.length; i++) {
141                 fileList.addAll(
142                     find_internal(
143                         new File JavaDoc(file.getAbsolutePath(), children[i]),
144                         pattern));
145             }
146         } else if (file.isFile() && containsPattern(file, pattern)) {
147             fileList.add(file);
148         }
149         return fileList;
150     }
151
152     /**
153      * Find all files below the given file which contain the given search string.
154      *
155      * @param file the where to start the search
156      * @param searchString the string to search for.
157      *
158      * @return an array of files which contain the search string.
159      *
160      * @throws IOException if any of the files could not be opened.
161      */

162     public static File JavaDoc[] find(File JavaDoc file, String JavaDoc searchString)
163         throws IOException JavaDoc {
164         Pattern JavaDoc pattern = Pattern.compile(searchString);
165         List JavaDoc fileList = find_internal(file, pattern);
166         return (File JavaDoc[])fileList.toArray(new File JavaDoc[fileList.size()]);
167     }
168 }
169
Popular Tags