View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  /*
17   * $Header: /home/cvs/jakarta-slide/src/stores/org/apache/slide/index/BasicExpressionTxtContainsSample.java,v 1.3 2004/07/28 09:34:22 ib Exp $
18   * $Revision: 1.3 $
19   * $Date: 2004/07/28 09:34:22 $
20   *
21   * ====================================================================
22   *
23   * Copyright 1999-2004 The Apache Software Foundation
24   *
25   * Licensed under the Apache License, Version 2.0 (the "License");
26   * you may not use this file except in compliance with the License.
27   * You may obtain a copy of the License at
28   *
29   *     http://www.apache.org/licenses/LICENSE-2.0
30   *
31   * Unless required by applicable law or agreed to in writing, software
32   * distributed under the License is distributed on an "AS IS" BASIS,
33   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34   * See the License for the specific language governing permissions and
35   * limitations under the License.
36   *
37   */
38  
39  
40  
41  package com.isenshi.util.extlib;
42  
43  import java.util.Collection;
44  
45  import org.apache.lucene.analysis.Analyzer;
46  import org.apache.lucene.analysis.cjk.CJKAnalyzer;
47  import org.apache.lucene.document.Document;
48  import org.apache.lucene.queryParser.QueryParser;
49  import org.apache.lucene.search.Hits;
50  import org.apache.lucene.search.IndexSearcher;
51  import org.apache.lucene.search.Query;
52  import org.apache.lucene.search.Searcher;
53  import org.apache.slide.common.SlideException;
54  import org.apache.slide.search.BadQueryException;
55  import org.apache.slide.search.RequestedResource;
56  import org.apache.slide.search.SearchException;
57  import org.apache.slide.search.basic.BasicResultSetImpl;
58  import org.apache.slide.search.basic.ComparableResourceImpl;
59  import org.apache.slide.search.basic.IBasicExpression;
60  import org.apache.slide.search.basic.IBasicExpressionFactory;
61  import org.apache.slide.search.basic.IBasicQuery;
62  import org.apache.slide.search.basic.IBasicResultSet;
63  import org.apache.slide.structure.ObjectNode;
64  import org.apache.slide.structure.SubjectNode;
65  
66  /***
67   * A very basic sample for a store specific Expression. Depending on the
68   * complexity of the concrete store specific implementation, iut might be
69   * a good idea to have an Expression class for each DAV: expression
70   * (SQLEqExpression, SQLOrExpression, ...)
71   *
72   * @version $Revision: 1.3 $
73   */
74  public class BasicExpressionTxtContainsCJK implements IBasicExpression
75  {
76      /*** an example for an executable command */
77      String searchedText;
78      
79      String indexPath;
80      
81      /*** backptr to the factory */
82      IBasicExpressionFactory factory;
83      
84      /***
85       * constructor for a compare expression like gt, eq, ...
86       * For your concrete implementation you are free, which parameters have to
87       * be passed, let the factory give you everything you need.
88       */
89      BasicExpressionTxtContainsCJK (String searchedText, String rootPath)
90      {
91          this.searchedText = searchedText;
92          this.indexPath = rootPath;
93      }
94      
95      /***
96       * constructor for a merge expression
97       */
98      BasicExpressionTxtContainsCJK (String mergeOperator,
99                                        Collection children,
100                                       IBasicExpressionFactory factory)
101         throws BadQueryException
102     {
103         //        this.factory = factory;
104         //        Iterator it = children.iterator();
105         //        BasicExpressionTxtContainsSample firstChild = (BasicExpressionTxtContainsSample)it.next();
106         //
107         //        if (firstChild == null)
108         //            throw new BadQueryException (mergeOperator + " needs at least one nested element");
109         //
110         //        theExecutableCommand = firstChild.theExecutableCommand;
111         //
112         //        // create the executable command
113         //        while (it.hasNext()) {
114         //            BasicExpressionTxtContainsSample exp = (BasicExpressionTxtContainsSample)it.next();
115         //            theExecutableCommand += " " + mergeOperator + " " + exp.theExecutableCommand;
116         //        }
117     }
118     
119     /***
120      * fake executer. The executable command is printed and a fake result is created.
121      *
122      * @return   an IBasicResultSet
123      *
124      * @throws   SearchException
125      *
126      */
127     public IBasicResultSet execute() throws SearchException
128     {
129         IBasicResultSet result = new BasicResultSetImpl (false);
130         
131         try
132         {
133         	
134             Searcher searcher = new IndexSearcher(indexPath);
135             //searcher.
136             
137             Analyzer analyzer = new CJKAnalyzer();
138             
139             Query query = QueryParser.parse(searchedText, "contents", analyzer);
140             Hits hits = searcher.search (query);
141             int noOfHits = hits.length();
142             
143             for (int i = 0; i < noOfHits; i++)
144             {
145                 Document doc = hits.doc(i);
146                 String uri = doc.get("documentId");
147                 RequestedResource resource = createResource(uri);
148                 result.add (resource);
149             }
150         }
151         catch (Exception e)
152         {
153             throw new SearchException (e);
154         }
155         
156         return  result;
157     }
158     
159     private RequestedResource createResource(String uri) throws SearchException
160     {
161         ObjectNode node = new SubjectNode(uri); // this will return the root folder
162         RequestedResource resource = null;
163         IBasicQuery query = factory.getQuery();
164         
165         try
166         {
167             resource = new ComparableResourceImpl
168                 (node, query.getSearchToken(), query.getScope(),
169                  factory.getPropertyProvider());
170         }
171         catch (SlideException e)
172         {
173             throw new SearchException (e);
174         }
175         return resource;
176     }
177     
178     public void setFactory (IBasicExpressionFactory factory)
179     {
180         this.factory = factory;
181     }
182     
183     public IBasicExpressionFactory getFactory()
184     {
185         return this.factory;
186     }
187 }
188