KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > search > basic > BasicSearchLanguage


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/search/basic/BasicSearchLanguage.java,v 1.14 2004/07/28 09:35:02 ib Exp $
3  * $Revision: 1.14 $
4  * $Date: 2004/07/28 09:35:02 $
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.basic;
25
26 // import list
27
import org.apache.slide.content.NodeProperty.NamespaceCache;
28
29 import org.apache.slide.search.SearchLanguage;
30 import org.apache.slide.search.SearchQuery;
31 import org.apache.slide.search.QueryScope;
32 import org.apache.slide.search.SearchToken;
33 import org.apache.slide.search.BadQueryException;
34 import org.apache.slide.search.PropertyProvider;
35
36 import org.apache.slide.store.AbstractStore;
37 import org.apache.slide.common.SlideRuntimeException;
38 import org.apache.slide.common.Uri;
39
40 import org.jdom.Element;
41 import org.jdom.Document;
42
43 import org.jdom.input.SAXBuilder;
44
45 import java.io.StringReader JavaDoc;
46
47 /**
48  * Represent the BasicSearchLanguage for Slide
49  *
50  * @version $Revision: 1.14 $
51  */

52 public class BasicSearchLanguage extends SearchLanguage {
53     
54     static final String JavaDoc GRAMMAR_NAME =
55         "basicsearch";
56     
57     static final String JavaDoc GRAMMAR_NAMESPACE = NamespaceCache.DEFAULT_URI;
58     
59     /** the property name for the store specific BasicQuery implementation if any */
60     static public final String JavaDoc BASIC_QUERY_CLASS = "basicQueryClass";
61     
62     public BasicSearchLanguage () {}
63     
64     /**
65      * Method getName
66      *
67      * @return the name of this query language
68      */

69     public String JavaDoc getName() {
70         return GRAMMAR_NAME;
71     }
72     
73     /**
74      * Retrieves the URI of this language
75      *
76      * @return the namspace of this language
77      */

78     public String JavaDoc getGrammarUri() {
79         return GRAMMAR_NAMESPACE;
80     }
81     
82     /**
83      * Creates a SearchQuery out of this queryString
84      *
85      * @param queryString an XML String describing this query
86      * @param token SearchToken
87      * @param propertyProvider the PropertyProvider to use (may be
88      * <code>null</code>).
89      *
90      * @return the SearchQuery
91      *
92      * @throws BadQueryException
93      */

94     public SearchQuery parseQuery (String JavaDoc queryString, SearchToken token, PropertyProvider propertyProvider) throws BadQueryException {
95         // TODO: parse queryString and pass to parseQuery(Element, SearchToken);
96
try {
97             Document doc =
98                 new SAXBuilder ().build (new StringReader JavaDoc (queryString));
99             
100             Element root = doc.getRootElement();
101             return parseQuery(root, token, propertyProvider);
102         }
103         catch (Exception JavaDoc e) {
104             e.printStackTrace();
105             throw new BadQueryException (e.getMessage());
106         }
107     }
108     
109     /**
110      * Creates a SearchQuery out of this queryElement
111      *
112      * @param basicSearchElement JDOM Element describing this query
113      * @param token SearchToken
114      * @param propertyProvider the PropertyProvider to use (may be
115      * <code>null</code>).
116      *
117      * @return BasicSearchQuery
118      *
119      * @throws SearchException
120      *
121      */

122     public SearchQuery parseQuery (Element basicSearchElement, SearchToken token, PropertyProvider propertyProvider) throws BadQueryException {
123         boolean useEnvelope = true;
124         
125         
126         QueryScope scope = BasicQueryImpl.getScope (basicSearchElement);
127         IBasicQuery query = null;
128         
129         
130         if (useEnvelope) {
131             query = new BasicQueryEnvelope (token, scope);
132             
133         }
134         else {
135         
136 // Uri uri = new Uri (token.getNamespace(), );
137
Uri uri = token.getNamespace().getUri(token.getSlideToken(), token.getSlideContext().getSlidePath(scope.getHref()));
138         
139         AbstractStore store = (AbstractStore)uri.getStore();
140         String JavaDoc className = (String JavaDoc)store.getParameter (BASIC_QUERY_CLASS);
141         
142         if (className != null) {
143             try {
144                 Class JavaDoc queryClass = Class.forName (className);
145                     query = (IBasicQuery) queryClass.newInstance();
146                     ((IBasicQuery) query).init (token);
147             }
148             catch (Exception JavaDoc e) {
149                 e.printStackTrace();
150                 throw new SlideRuntimeException (e.getMessage());
151             }
152         }
153         
154         else {
155             query = new BasicQueryImpl(token);
156         }
157         }
158         
159         //BasicQueryImpl query = new XBasicQueryImpl(token);
160
query.parseQueryElement(basicSearchElement, propertyProvider);
161         
162         return (SearchQuery) query;
163     }
164 }
165
166
Popular Tags