KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > index > BasicExpressionTxtContainsSample


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/stores/org/apache/slide/index/BasicExpressionTxtContainsSample.java,v 1.3 2004/07/28 09:34:22 ib Exp $
3  * $Revision: 1.3 $
4  * $Date: 2004/07/28 09:34:22 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2004 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
25
26 package org.apache.slide.index;
27
28 import org.apache.slide.search.basic.*;
29
30 import java.util.Collection JavaDoc;
31 import org.apache.lucene.analysis.Analyzer;
32 import org.apache.lucene.analysis.standard.StandardAnalyzer;
33 import org.apache.lucene.document.Document;
34 import org.apache.lucene.queryParser.QueryParser;
35 import org.apache.lucene.search.Hits;
36 import org.apache.lucene.search.IndexSearcher;
37 import org.apache.lucene.search.Query;
38 import org.apache.lucene.search.Searcher;
39 import org.apache.slide.common.SlideException;
40 import org.apache.slide.search.BadQueryException;
41 import org.apache.slide.search.RequestedResource;
42 import org.apache.slide.search.SearchException;
43 import org.apache.slide.structure.ObjectNode;
44 import org.apache.slide.structure.SubjectNode;
45
46 /**
47  * A very basic sample for a store specific Expression. Depending on the
48  * complexity of the concrete store specific implementation, iut might be
49  * a good idea to have an Expression class for each DAV: expression
50  * (SQLEqExpression, SQLOrExpression, ...)
51  *
52  * @version $Revision: 1.3 $
53  */

54 public class BasicExpressionTxtContainsSample implements IBasicExpression
55 {
56     /** an example for an executable command */
57     String JavaDoc searchedText;
58     
59     String JavaDoc indexPath;
60     
61     /** backptr to the factory */
62     IBasicExpressionFactory factory;
63     
64     /**
65      * constructor for a compare expression like gt, eq, ...
66      * For your concrete implementation you are free, which parameters have to
67      * be passed, let the factory give you everything you need.
68      */

69     BasicExpressionTxtContainsSample (String JavaDoc searchedText, String JavaDoc rootPath)
70     {
71         this.searchedText = searchedText;
72         this.indexPath = rootPath;
73     }
74     
75     /**
76      * constructor for a merge expression
77      */

78     BasicExpressionTxtContainsSample (String JavaDoc mergeOperator,
79                                       Collection JavaDoc children,
80                                       IBasicExpressionFactory factory)
81         throws BadQueryException
82     {
83         // this.factory = factory;
84
// Iterator it = children.iterator();
85
// BasicExpressionTxtContainsSample firstChild = (BasicExpressionTxtContainsSample)it.next();
86
//
87
// if (firstChild == null)
88
// throw new BadQueryException (mergeOperator + " needs at least one nested element");
89
//
90
// theExecutableCommand = firstChild.theExecutableCommand;
91
//
92
// // create the executable command
93
// while (it.hasNext()) {
94
// BasicExpressionTxtContainsSample exp = (BasicExpressionTxtContainsSample)it.next();
95
// theExecutableCommand += " " + mergeOperator + " " + exp.theExecutableCommand;
96
// }
97
}
98     
99     /**
100      * fake executer. The executable command is printed and a fake result is created.
101      *
102      * @return an IBasicResultSet
103      *
104      * @throws SearchException
105      *
106      */

107     public IBasicResultSet execute() throws SearchException
108     {
109         IBasicResultSet result = new BasicResultSetImpl (false);
110         
111         try
112         {
113             Searcher searcher = new IndexSearcher(indexPath);
114             Analyzer analyzer = new StandardAnalyzer();
115             
116             Query query = QueryParser.parse(searchedText, "contents", analyzer);
117             Hits hits = searcher.search (query);
118             int noOfHits = hits.length();
119             
120             for (int i = 0; i < noOfHits; i++)
121             {
122                 Document doc = hits.doc(i);
123                 String JavaDoc uri = doc.get("documentId");
124                 System.out.println(uri);
125                 RequestedResource resource = createResource(uri);
126                 result.add (resource);
127             }
128         }
129         catch (Exception JavaDoc e)
130         {
131             throw new SearchException (e);
132         }
133         
134         return result;
135     }
136     
137     private RequestedResource createResource(String JavaDoc uri) throws SearchException
138     {
139         ObjectNode node = new SubjectNode(uri); // this will return the root folder
140
RequestedResource resource = null;
141         IBasicQuery query = factory.getQuery();
142         
143         try
144         {
145             resource = new ComparableResourceImpl
146                 (node, query.getSearchToken(), query.getScope(),
147                  factory.getPropertyProvider());
148         }
149         catch (SlideException e)
150         {
151             throw new SearchException (e);
152         }
153         return resource;
154     }
155     
156     public void setFactory (IBasicExpressionFactory factory)
157     {
158         this.factory = factory;
159     }
160     
161     public IBasicExpressionFactory getFactory()
162     {
163         return this.factory;
164     }
165 }
166
167
Popular Tags