1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
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
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
137 factory = Factory.TEXT;
138 }
139
140 return factory;
141 }
142
143
144
145
146
147
148
149
150
151
152 protected SearchFactory() {
153 }
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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
183 result = new TextSearch();
184 } else {
185 result = factory.create();
186 }
187
188 return result;
189 }
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206 }