KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openidex > search > SearchInfoFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 2004 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openidex.search;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import org.netbeans.api.queries.SharabilityQuery;
25 import org.netbeans.api.queries.VisibilityQuery;
26 import org.openide.filesystems.FileObject;
27 import org.openide.loaders.DataFolder;
28 import org.openide.nodes.Node;
29
30 /**
31  * Factory for creating <code>SearchInfo</code> objects.
32  *
33  * @see SearchInfo
34  * @since org.openidex.util/3 3.3
35  * @author Marian Petras
36  */

37 public final class SearchInfoFactory {
38
39     /**
40      * filter that skips files and folders which are not visible according
41      * to the <code>VisibilityQuery</code>
42      *
43      * @see org.netbeans.api.queries.VisibilityQuery VisibilityQuery
44      */

45     public static final FileObjectFilter VISIBILITY_FILTER
46             = new VisibilityFilter();
47
48     /**
49      * filter that skips files and folders which are not sharable according
50      * to the <code>SharabilityQuery</code>
51      *
52      * @see org.netbeans.api.queries.SharabilityQuery SharabilityQuery
53      */

54     public static final FileObjectFilter SHARABILITY_FILTER
55             = new SharabilityFilter();
56
57     /**
58      */

59     private SearchInfoFactory() {}
60
61 // /**
62
// * Creates a <code>SearchInfo</code> object for a given folder.
63
// * The returned <code>SearchInfo</code> object's method
64
// * {@link SearchInfo#canSearch()} always returns <code>true</code>
65
// * and iterates through <code>DataObject</code>s found in the given
66
// * folder. Non-sharable and/or non-visible <code>FileObject</code>s
67
// * may be skipped, according to enabled filters.
68
// *
69
// * @param folder folder which should be searched
70
// * @param recursive whether the folder's subfolders should be taken
71
// * into account
72
// * @param checkVisibility whether the visibility filter should be used
73
// * @param checkSharability whether the sharability filter should be used
74
// * @return <code>SearchInfo</code> object which iterates through
75
// * <code>DataObject</code>s found in the specified folder
76
// * and (optionally) its subfolders
77
// * @see org.netbeans.api.queries.SharabilityQuery
78
// * @see org.netbeans.api.queries.VisibilityQuery
79
// */
80
// public static SearchInfo createSearchInfo(
81
// FileObject folder,
82
// boolean recursive,
83
// boolean checkVisibility,
84
// boolean checkSharability) {
85
// if (!folder.isFolder()) {
86
// throw new IllegalArgumentException("folder expected"); //NOI18N
87
// }
88
//
89
// DataFolder dataFolder = DataFolder.findFolder(folder);
90
//
91
// int filtersCount = 0;
92
// if (checkVisibility) {
93
// filtersCount++;
94
// }
95
// if (checkSharability) {
96
// filtersCount++;
97
// }
98
//
99
// if (filtersCount == 0) {
100
// return new SimpleSearchInfo(dataFolder, recursive, null);
101
// } else {
102
// FileObjectFilter[] filters = new FileObjectFilter[filtersCount];
103
//
104
// int i = 0;
105
// if (checkVisibility) {
106
// filters[i++] = VISIBILITY_FILTER;
107
// }
108
// if (checkSharability) {
109
// filters[i++] = SHARABILITY_FILTER;
110
// }
111
// return new SimpleSearchInfo(dataFolder, recursive, filters);
112
// }
113
// }
114

115 // /**
116
// * Creates a <code>SearchInfo</code> object for given folders.
117
// * The returned <code>SearchInfo</code> object's method
118
// * {@link SearchInfo#canSearch()} always returns <code>true</code>
119
// * and iterates through <code>DataObject</code>s found in the given
120
// * folders. Non-sharable and/or non-visible <code>FileObject</code>s
121
// * may be skipped, according to enabled filters.
122
// *
123
// * @param folders folders which should be searched
124
// * @param recursive whether the folders' subfolders should be taken
125
// * into account
126
// * @param checkVisibility whether the visibility filter should be used
127
// * @param checkSharability whether the sharability filter should be used
128
// * @return <code>SearchInfo</code> object which iterates through
129
// * <code>DataObject</code>s found in the specified folders
130
// * and (optionally) their subfolders
131
// * @see org.netbeans.api.queries.SharabilityQuery
132
// * @see org.netbeans.api.queries.VisibilityQuery
133
// */
134
// public static SearchInfo createSearchInfo(
135
// final FileObject[] folders,
136
// final boolean recursive,
137
// final boolean checkVisibility,
138
// final boolean checkSharability) {
139
// if (folders.length == 0) {
140
// return SimpleSearchInfo.EMPTY_SEARCH_INFO;
141
// }
142
//
143
// if (folders.length == 1) {
144
// return createSearchInfo(folders[0],
145
// recursive,
146
// checkVisibility,
147
// checkSharability);
148
// }
149
//
150
// for (int i = 0; i < folders.length; i++) {
151
// if (!folders[i].isFolder()) {
152
// throw new IllegalArgumentException(
153
// "folder expected (index " + i + ')'); //NOI18N
154
// }
155
// }
156
//
157
// SearchInfo[] nested = new SearchInfo[folders.length];
158
// for (int i = 0; i < folders.length; i++) {
159
// nested[i] = createSearchInfo(folders[i],
160
// recursive,
161
// checkVisibility,
162
// checkSharability);
163
// }
164
// return new CompoundSearchInfo(nested);
165
// }
166

167     /**
168      * Creates a <code>SearchInfo</code> object for a given folder.
169      * The returned <code>SearchInfo</code> object's method
170      * {@link SearchInfo#canSearch()} always returns <code>true</code>
171      * and iterates through <code>DataObject</code>s found in the given
172      * folder. Files and folders that do not pass any of the given filters
173      * are skipped (not searched). If multiple filters are passed,
174      * the filters are applied on each file/folder in the same order
175      * as in the array passed to this method.
176      *
177      * @param folder folder which should be searched
178      * @param recursive whether the folder's subfolders should be taken
179      * into account
180      * @param filters filters to be used when searching;
181      * or <code>null</code> if no filters should be used
182      * @return <code>SearchInfo</code> object which iterates through
183      * <code>DataObject</code>s found in the specified folder
184      * and (optionally) its subfolders
185      * @see FileObjectFilter
186      */

187     public static SearchInfo createSearchInfo(
188                 final FileObject folder,
189                 final boolean recursive,
190                 final FileObjectFilter[] filters) {
191         if (!folder.isFolder()) {
192             throw new IllegalArgumentException JavaDoc("folder expected"); //NOI18N
193
}
194
195         return new SimpleSearchInfo(DataFolder.findFolder(folder),
196                                     recursive,
197                                     filters);
198     }
199
200     /**
201      * Creates a <code>SearchInfo</code> object for given folders.
202      * The returned <code>SearchInfo</code> object's method
203      * {@link SearchInfo#canSearch()} always returns <code>true</code>
204      * and iterates through <code>DataObject</code>s found in the given
205      * folders. Files and folders that do not pass any of the given filters
206      * are skipped (not searched). If multiple filters are passed,
207      * the filters are applied on each file/folder in the same order
208      * as in the array passed to this method.
209      *
210      * @param folders folders which should be searched
211      * @param recursive whether the folders' subfolders should be taken
212      * into account
213      * @param filters filters to be used when searching;
214      * or <code>null</code> if no filters should be used
215      * @return <code>SearchInfo</code> object which iterates through
216      * <code>DataObject</code>s found in the specified folders
217      * and (optionally) their subfolders
218      * @see FileObjectFilter
219      */

220     public static SearchInfo createSearchInfo(
221                 final FileObject[] folders,
222                 final boolean recursive,
223                 final FileObjectFilter[] filters) {
224         if (folders.length == 0) {
225             return SimpleSearchInfo.EMPTY_SEARCH_INFO;
226         }
227         
228         if (folders.length == 1) {
229             return createSearchInfo(folders[0],
230                                     recursive,
231                                     filters);
232         }
233         
234         for (int i = 0; i < folders.length; i++) {
235             if (!folders[i].isFolder()) {
236                 throw new IllegalArgumentException JavaDoc(
237                           "folder expected (index " + i + ')'); //NOI18N
238
}
239         }
240         
241         SearchInfo[] nested = new SearchInfo[folders.length];
242         for (int i = 0; i < folders.length; i++) {
243             nested[i] = createSearchInfo(folders[i],
244                                          recursive,
245                                          filters);
246         }
247         return new CompoundSearchInfo(nested);
248     }
249     
250     /**
251      * Creates a <code>SearchInfo</code> object combining
252      * <code>SearchInfo</code> objects returned by the node's subnodes.
253      *
254      * Method {@link SearchInfo#canSearch()} of the resulting
255      * <code>SearchInfo</code> objects returns <code>true</code> if and only if
256      * at least one of the nodes is searchable
257      * (its method <code>canSearch()</code> returns <code>true</code>).
258      * The iterator iterates through all <code>DataObject</code>s returned
259      * by the subnode's <code>SearchInfo</code> iterators.
260      *
261      * @param node node to create <code>SearchInfo</code> for
262      * @return <code>SearchInfo</code> object representing combination
263      * of <code>SearchInfo</code> objects of the node's subnodes
264      */

265     public static SearchInfo createSearchInfoBySubnodes(Node node) {
266         return new SubnodesSearchInfo(node);
267     }
268
269 }
270
Popular Tags