KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/stores/org/apache/slide/index/TextContainsExpression.java,v 1.1.2.2 2004/09/13 16:52:25 unico Exp $
3  * $Revision: 1.1.2.2 $
4  * $Date: 2004/09/13 16:52:25 $
5  *
6  * ====================================================================
7  *
8  * Copyright 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 package org.apache.slide.index;
25
26 import org.apache.slide.search.basic.*;
27 import org.apache.slide.search.BadQueryException;
28 import org.apache.slide.search.SearchException;
29 import org.apache.slide.search.RequestedResource;
30 import org.apache.slide.structure.ObjectNode;
31 import org.apache.slide.structure.SubjectNode;
32 import org.apache.slide.common.SlideException;
33 import org.apache.slide.common.Domain;
34 import org.apache.slide.util.logger.Logger;
35 import org.apache.lucene.search.Searcher;
36 import org.apache.lucene.search.IndexSearcher;
37 import org.apache.lucene.search.Query;
38 import org.apache.lucene.search.Hits;
39 import org.apache.lucene.analysis.Analyzer;
40 import org.apache.lucene.queryParser.QueryParser;
41 import org.apache.lucene.document.Document;
42
43 import java.util.Collection JavaDoc;
44
45 /**
46  * Date: Jun 24, 2004
47  * Time: 11:45:30 PM
48  */

49 public class TextContainsExpression implements IBasicExpression {
50
51     protected static final String JavaDoc LOG_CHANNEL = TextContainsExpression.class.getName();
52
53     String JavaDoc searchedText;
54     String JavaDoc indexPath;
55     Analyzer analyzer;
56
57     /** backptr to the factory */
58     IBasicExpressionFactory factory;
59
60     /**
61      * constructor for a compare expression like gt, eq, ...
62      * For your concrete implementation you are free, which parameters have to
63      * be passed, let the factory give you everything you need.
64      */

65     TextContainsExpression (String JavaDoc searchedText, String JavaDoc rootPath, Analyzer analyzer)
66     {
67         this.searchedText = searchedText;
68         this.indexPath = rootPath;
69         this.analyzer = analyzer;
70     }
71
72     /**
73      * constructor for a merge expression
74      */

75     TextContainsExpression (String JavaDoc mergeOperator,
76                                       Collection JavaDoc children,
77                                       IBasicExpressionFactory factory)
78         throws BadQueryException
79     {
80         // this.factory = factory;
81
// Iterator it = children.iterator();
82
// BasicExpressionTxtContainsSample firstChild = (BasicExpressionTxtContainsSample)it.next();
83
//
84
// if (firstChild == null)
85
// throw new BadQueryException (mergeOperator + " needs at least one nested element");
86
//
87
// theExecutableCommand = firstChild.theExecutableCommand;
88
//
89
// // create the executable command
90
// while (it.hasNext()) {
91
// BasicExpressionTxtContainsSample exp = (BasicExpressionTxtContainsSample)it.next();
92
// theExecutableCommand += " " + mergeOperator + " " + exp.theExecutableCommand;
93
// }
94
}
95
96     /**
97      * Search the index for this expression using Lucene.
98      *
99      * @return an IBasicResultSet
100      *
101      * @throws org.apache.slide.search.SearchException
102      *
103      */

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