View Javadoc

1   /*
2    * Fsgrep is a simple Java application which allows a user to
3    * search all files in a directory structure for lines matching
4    * a given pattern.  Its functionality is a combination of the
5    * Unix 'find' and 'grep' utilities.
6    * Visit [http://fsgrep.sourceforge.net/] for more information.
7    * 
8    * Copyright (C) 2003-2006 Murali Krishnan [murali_ca_us@users.sourceforge.net]
9    * 
10   * Fsgrep is free software; you can redistribute it and/or modify
11   * it under the terms of version 2 of the GNU General Public
12   * License as published by the Free Software Foundation.
13   * 
14   * Fsgrep is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public License
20   * along with Fsgrep (see the file named LICENSE.txt); if not, write
21   * to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22   * Boston, MA  02111-1307  USA
23   */
24  
25  package mk.fsgrep.find;
26  
27  
28  import java.io.*;
29  import java.util.*;
30  
31  
32  /***
33   * The representation of the set of files to search.  It encapsulates the
34   * starting directory, and the set of suffixes of files to include.
35   * 
36   * @author  Murali Krishnan
37   *
38   */
39  public class ScanProfile {
40  
41      //------------------------------------------------------------
42      //- Class Variables
43  
44  
45  
46      //------------------------------------------------------------
47      //- Class Functions
48  
49      protected static String makeRegexpFromSuffix(String pSuffix) {
50          String result = pSuffix.trim();
51  
52          while (result.startsWith(".")) {
53              result = result.substring(1);
54          }
55  
56          result = "//." + result + "$";
57  
58          return result;
59      }
60  
61  
62      protected static String makeRegexpFromSuffixList(String pSuffixes) {
63          String result;
64  
65          if (pSuffixes.indexOf(",") == -1) {
66              result = makeRegexpFromSuffix(pSuffixes);
67          } else {
68              boolean first = true;
69              StringBuffer buffer = new StringBuffer("//.(");
70              StringTokenizer tokenizer = new StringTokenizer(pSuffixes, ",");
71              while (tokenizer.hasMoreTokens()) {
72                  if (first) {
73                      first = false;
74                  } else {
75                      buffer.append("|");
76                  }
77                  String token = tokenizer.nextToken().trim();
78                  buffer.append(token);
79              }
80              buffer.append(")$");
81  
82              result = buffer.toString();
83          }
84  
85          return result;
86      }
87  
88  
89      //------------------------------------------------------------
90      //- Instance Variables
91  
92      private File _root = null;
93      private String _filenameSpec = null;
94      private FilenameFilter _filter = null;
95  
96  
97  
98      //------------------------------------------------------------
99      //- Constructors
100 
101     public ScanProfile(String pFullPattern) {
102         _root = new File(pFullPattern.substring(0, pFullPattern.indexOf("**")));
103         {
104             int dot = pFullPattern.lastIndexOf(".")+1;
105             String pPattern = pFullPattern.substring(dot);
106             if (pPattern.startsWith("{")) {
107                 pPattern = pPattern.substring(1);
108             }
109             if (pPattern.endsWith("}")) {
110                 pPattern = pPattern.substring(0, (pPattern.length()-1));
111             }
112 
113             _filenameSpec = pPattern;
114         }
115 
116         setFilter(_filenameSpec);
117     }
118 
119     public ScanProfile(File pFile, String pPattern) {
120         _root = pFile;
121         _filenameSpec = pPattern;
122 
123         setFilter(pPattern);
124     }
125 
126     public ScanProfile(String pFile, String pPattern) {
127         this(new File(pFile), pPattern);
128     }
129 
130 
131 
132     //------------------------------------------------------------
133     //- Accessors
134 
135     public File getRoot() {return _root;}
136     public String getFilenameSpec() {return _filenameSpec;}
137     public FilenameFilter getFilter() {return _filter;}
138 
139 
140 
141     //------------------------------------------------------------
142     //- Settors
143 
144     protected void setFilenameSpec(String val) {_filenameSpec=val;}
145 
146 
147     //------------------------------------------------------------
148     //- Private/Protected Utility Functions
149 
150     protected void setFilter(String pPattern) {
151         if ("*".equals(pPattern)) {
152             _filter = new TextFileFilter();
153         } else {
154             _filter = new PatternFilter(makeRegexpFromSuffixList(pPattern));
155         }
156     }
157 
158 
159 
160     //------------------------------------------------------------
161     //- Public Interface Functions
162 
163     public String toString() {
164         String result = "ScanProfile[" + createSpecification() + "]";
165 
166         return result;
167     }
168 
169 
170     public String createSpecification() {
171         StringBuffer buffer = new StringBuffer();
172 
173         try {
174             buffer.append(getRoot().getCanonicalPath());
175         } catch (IOException ioe) {
176             buffer.append(getRoot().getAbsolutePath());
177         }
178 
179         if (!buffer.toString().endsWith(File.separator)) {
180             buffer.append(File.separator);
181         }
182 
183         buffer.append("**").append(File.separator).append("*.");
184         if (getFilenameSpec().indexOf(",") == -1) {
185             buffer.append(getFilenameSpec());
186         } else {
187             buffer.append("{").append(getFilenameSpec()).append("}");
188         }
189 
190         return buffer.toString();
191     }
192 
193 
194 
195 
196     //------------------------------------------------------------
197     //- Class Interface Functions
198 
199 
200 
201     //------------------------------------------------------------
202     //- Inner Classes
203 
204 
205 
206     //------------------------------------------------------------
207     //- Main
208 
209 
210 
211 }