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.match;
26  
27  import java.util.*;
28  
29  import mk.fsgrep.util.Args;
30  
31  /***
32   * Encapsulate the logic that determines which time of search object to
33   * use.
34   * 
35   * @author  Murali Krishnan
36   *
37   */
38  public class SearchFactory {
39  
40      //------------------------------------------------------------
41      //- Class Variables
42  
43      protected static final SearchFactory _instance = new SearchFactory();
44  
45      enum Factory {
46          CODE_CJAVA {
47              public FileSearch create() {
48                  return new CJavaSearch();
49              }
50          },
51          CODE_SQL {
52              public FileSearch create() {
53                  return new SqlSearch();
54              }
55          },
56          CODE_SGML {
57              public FileSearch create() {
58                  return new SgmlSearch();
59              }
60          },
61          CODE_PROPERTIES {
62              public FileSearch create() {
63                  return new PropertiesSearch();
64              }
65          },
66          ARCHIVE_ZIP {
67              public FileSearch create() {
68                  return new ZipSearch();
69              }
70  
71              public boolean isZip() {
72                  return true;
73              }
74  
75              public boolean canHaveComments() {
76                  return false;
77              }
78          },
79          TEXT {
80              public FileSearch create() {
81                  return new TextSearch();
82              }
83  
84              public boolean canHaveComments() {
85                  return false;
86              }
87          };
88  
89          public abstract FileSearch create();
90  
91          public boolean isZip() {
92              return false;
93          }
94  
95          public boolean canHaveComments() {
96              return true;
97          }
98      }
99  
100     protected static final Map<String, Factory> SUFFIXES = new HashMap<String, Factory>();
101 
102     static {
103         SUFFIXES.put("zip", Factory.ARCHIVE_ZIP);
104         SUFFIXES.put("jar", Factory.ARCHIVE_ZIP);
105 
106         SUFFIXES.put("java", Factory.CODE_CJAVA);
107         SUFFIXES.put("h", Factory.CODE_CJAVA);
108         SUFFIXES.put("c", Factory.CODE_CJAVA);
109         SUFFIXES.put("cc", Factory.CODE_CJAVA);
110         SUFFIXES.put("cpp", Factory.CODE_CJAVA);
111 
112         SUFFIXES.put("sql", Factory.CODE_SQL);
113 
114         SUFFIXES.put("sgml", Factory.CODE_SGML);
115         SUFFIXES.put("xml", Factory.CODE_SGML);
116         SUFFIXES.put("xsd", Factory.CODE_SGML);
117         SUFFIXES.put("html", Factory.CODE_SGML);
118         SUFFIXES.put("htm", Factory.CODE_SGML);
119 
120         SUFFIXES.put("properties", Factory.CODE_PROPERTIES);
121     }
122 
123     //------------------------------------------------------------
124     //- Class Functions
125 
126     public static SearchFactory getInstance() {return _instance;}
127 
128 
129     public static Factory lookupFactory(String pSuffix) {
130         Factory factory = null;
131         if (pSuffix != null) {
132             factory = SUFFIXES.get(pSuffix.toLowerCase());
133         }
134 
135         if (factory == null) {
136             // if the suffix is not defined, just assume it is a text file.
137             factory = Factory.TEXT;
138         }
139 
140         return factory;
141     }
142 
143 
144     //------------------------------------------------------------
145     //- Instance Variables
146 
147 
148 
149     //------------------------------------------------------------
150     //- Constructors
151 
152     protected SearchFactory() {
153     }
154 
155 
156     //------------------------------------------------------------
157     //- Accessors
158 
159 
160 
161     //------------------------------------------------------------
162     //- Settors
163 
164 
165 
166     //------------------------------------------------------------
167     //- Private/Protected Utility Functions
168 
169 
170 
171     //------------------------------------------------------------
172     //- Public Interface Functions
173 
174     public FileSearch create(String suffix, Args args) {
175         FileSearch result;
176 
177         Factory factory = lookupFactory(suffix);
178 
179         if (args.isSearchFileNames()) {
180             result = new FilenameSearch();
181         } else if (args.isSearchingComments() && factory.canHaveComments()) {
182             // override to text search if allowing comments to be searched.
183             result = new TextSearch();
184         } else {
185             result = factory.create();
186         }
187 
188         return result;
189     }
190 
191     //------------------------------------------------------------
192     //- Class Interface Functions
193 
194 
195 
196     //------------------------------------------------------------
197     //- Inner Classes
198 
199 
200 
201     //------------------------------------------------------------
202     //- Main
203 
204 
205 
206 }