KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > search > SearchImpl


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/search/SearchImpl.java,v 1.19 2004/07/28 09:35:09 ib Exp $
3  * $Revision: 1.19 $
4  * $Date: 2004/07/28 09:35:09 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.search;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.apache.slide.common.Namespace;
30 import org.apache.slide.common.NamespaceConfig;
31 import org.apache.slide.common.ServiceAccessException;
32 import org.apache.slide.common.SlideToken;
33 import org.apache.slide.content.Content;
34 import org.apache.slide.search.basic.BasicSearchLanguage;
35 import org.apache.slide.structure.Structure;
36 import org.apache.slide.event.VetoException;
37 import org.apache.slide.event.EventDispatcher;
38 import org.apache.slide.event.SearchEvent;
39 import org.jdom.Element;
40
41 /**
42  * Search helper.
43  *
44  * @version $Revision: 1.19 $
45  */

46 public final class SearchImpl implements Search {
47     
48     // ----------------------------------------------------------- Constructors
49

50     
51     /**
52      * Constructor.
53      *
54      * @param namespace Namespace
55      * @param namespaceConfig Namespace configuration
56      */

57     public SearchImpl(Namespace namespace,
58                       NamespaceConfig namespaceConfig,
59                       Structure structureHelper,
60                       Content contentHelper) {
61         this.namespace = namespace;
62         this.namespaceConfig = namespaceConfig;
63         this.structureHelper = structureHelper;
64         this.contentHelper = contentHelper;
65     }
66     
67     
68     // ----------------------------------------------------- Instance Variables
69

70     // TODO: must be configurable in domain.xml (Namespace.java)
71
private static final SearchLanguage [] SEARCH_LANGUAGES = {
72         new BasicSearchLanguage ()
73     };
74     
75     
76     private static final Map JavaDoc GRAMMAR_BY_URI = new HashMap JavaDoc ();
77     static {
78         for (int i = 0; i < SEARCH_LANGUAGES.length; i++)
79             GRAMMAR_BY_URI.put (SEARCH_LANGUAGES [i].getGrammarUri(),
80                                 SEARCH_LANGUAGES [i]);
81     }
82     
83     /**
84      * Log channel for logger
85      */

86     private final static String JavaDoc LOG_CHANNEL = SearchImpl.class.getName();
87     
88     
89     /**
90      * Namespace.
91      */

92     private Namespace namespace;
93     
94     /**
95      * Structure.
96      */

97     private Structure structureHelper;
98     
99     
100     /**
101      * Namespace configuration.
102      */

103     private NamespaceConfig namespaceConfig;
104     
105     /**
106      * Namespace configuration.
107      */

108     private Content contentHelper;
109     
110     
111     // ------------------------------------------------------- Security Methods
112

113     
114     /**
115      * Search.
116      *
117      * @param token Credentials token
118      * @param query The query to execute
119      * @exception ServiceAccessException DataSource access error
120      */

121     public SearchQueryResult search (SlideToken token, SearchQuery query)
122         throws ServiceAccessException, VetoException {
123
124         // Fire event
125
if ( SearchEvent.SEARCH.isEnabled() ) EventDispatcher.getInstance().fireVetoableEvent(SearchEvent.SEARCH, new SearchEvent(this, token, namespace, query));
126
127         return query.execute ();
128     }
129     
130     
131     /**
132      * Return the allowed query languages.
133      */

134     public SearchLanguage[] getSupportedLanguages () {
135         return SEARCH_LANGUAGES;
136     }
137     
138     
139     /**
140      * Retrieve a SearchLanguage identified by the grammar uri (namespace)
141      *
142      * @param grammarUri identifier for the SearchLanguage
143      *
144      * @return the SearchLanguage
145      *
146      * @throws BadQueryException
147      *
148      */

149     public SearchLanguage getLanguage (String JavaDoc grammarUri)
150         throws BadQueryException
151     {
152         SearchLanguage result =(SearchLanguage) GRAMMAR_BY_URI.get (grammarUri);
153         if (result == null)
154             throw new BadQueryException ("grammar not found: " + grammarUri);
155         
156         return result;
157     }
158     
159     
160     
161     
162     /**
163      * Creates a SearchQuery.
164      *
165      * @param grammarUri identifier for the SearchLanguage
166      * @param queryElement the JDOM element containing the query
167      * @param token the SlideToken
168      * @param maxDepth may be 0, 1 or INFINIT
169      *
170      * @return the SearchQuery
171      *
172      * @throws BadQueryException
173      *
174      */

175     public SearchQuery createSearchQuery (String JavaDoc grammarUri,
176                                        Element queryElement,
177                                        SlideToken token,
178                                        int maxDepth)
179         throws BadQueryException
180     {
181         return createSearchQuery (grammarUri, queryElement, token, maxDepth, (PropertyProvider)null);
182     }
183     
184     /**
185      * Creates a SearchQuery.
186      *
187      * @param grammarUri identifier for the SearchLanguage.
188      * @param searchRequestElement the JDOM element containing the query
189      * @param token the SlideToken.
190      * @param maxDepth may be 0, 1 or INFINITY.
191      * @param propertyProvider the PropertyProvider to use (may be
192      * <code>null</code>).
193      *
194      * @return the SearchQuery
195      *
196      * @throws BadQueryException
197      */

198     public SearchQuery createSearchQuery (String JavaDoc grammarUri,
199                                    Element searchRequestElement,
200                                    SlideToken token,
201                                    int maxDepth,
202                                    PropertyProvider propertyProvider)
203         throws BadQueryException
204     {
205         return createSearchQuery (grammarUri, searchRequestElement, token, maxDepth, propertyProvider, null);
206     }
207     
208     /**
209      * Creates a SearchQuery.
210      *
211      * @param grammarUri identifier for the SearchLanguage
212      * @param queryElement the JDOM element containing the query
213      * @param token the SlideToken
214      * @param maxDepth may be 0, 1 or INFINIT
215      *
216      * @return the SearchQuery
217      *
218      * @throws BadQueryException
219      *
220      */

221     public SearchQuery createSearchQuery (String JavaDoc grammarUri,
222                                           Element queryElement,
223                                           SlideToken token,
224                                           int maxDepth,
225                                           String JavaDoc requestUri)
226         throws BadQueryException
227     {
228         return createSearchQuery (grammarUri, queryElement, token, maxDepth, null, requestUri);
229     }
230
231     /**
232      * Creates a SearchQuery.
233      *
234      * @param grammarUri identifier for the SearchLanguage.
235      * @param searchRequestElement the JDOM element containing the query
236      * @param token the SlideToken.
237      * @param maxDepth may be 0, 1 or INFINITY.
238      * @param propertyProvider the PropertyProvider to use (may be
239      * <code>null</code>).
240      * @param requestUri the URI of the request.
241      *
242      * @return the SearchQuery
243      *
244      * @throws BadQueryException
245      */

246     public SearchQuery createSearchQuery (String JavaDoc grammarUri,
247                                    Element searchRequestElement,
248                                    SlideToken token,
249                                    int maxDepth,
250                                    PropertyProvider propertyProvider,
251                                    String JavaDoc requestUri)
252         throws BadQueryException
253     {
254         SearchQuery result = null;
255         // create search token
256
SearchToken searchToken =
257             SearchToken.createSearchToken (token, contentHelper,
258                                            structureHelper, maxDepth,
259                                            requestUri, namespace);
260         
261         SearchLanguage language = getLanguage (grammarUri);
262         result = language.parseQuery (searchRequestElement, searchToken, propertyProvider);
263     
264         return result;
265     }
266 }
267
268
Popular Tags