add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / feeds / FileBasedQueryMaker.java
1 package org.apache.lucene.benchmark.byTask.feeds;
2
3 import org.apache.lucene.analysis.Analyzer;
4 import org.apache.lucene.queryParser.QueryParser;
5 import org.apache.lucene.queryParser.ParseException;
6 import org.apache.lucene.search.Query;
7 import org.apache.lucene.benchmark.byTask.tasks.NewAnalyzerTask;
8 import org.apache.lucene.util.Version;
9
10 import java.io.*;
11 import java.util.ArrayList;
12 import java.util.List;
13
14 /**
15  * Copyright 2004 The Apache Software Foundation
16  * <p/>
17  * Licensed under the Apache License, Version 2.0 (the "License");
18  * you may not use this file except in compliance with the License.
19  * You may obtain a copy of the License at
20  * <p/>
21  * http://www.apache.org/licenses/LICENSE-2.0
22  * <p/>
23  * Unless required by applicable law or agreed to in writing, software
24  * distributed under the License is distributed on an "AS IS" BASIS,
25  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26  * See the License for the specific language governing permissions and
27  * limitations under the License.
28  */
29
30 /**
31  * Create queries from a FileReader.  One per line, pass them through the
32  * QueryParser.  Lines beginning with # are treated as comments
33  *
34  * File can be specified as a absolute, relative or resource.
35  * Two properties can be set:
36  * file.query.maker.file=&lt;Full path to file containing queries&gt;
37  * <br/>
38  * file.query.maker.default.field=&lt;Name of default field - Default value is "body"&gt;
39  *
40  * Example:
41  * file.query.maker.file=c:/myqueries.txt
42  * file.query.maker.default.field=body
43  */
44 public class FileBasedQueryMaker extends AbstractQueryMaker implements QueryMaker{
45
46
47   @Override
48   protected Query[] prepareQueries() throws Exception {
49
50     Analyzer anlzr = NewAnalyzerTask.createAnalyzer(config.get("analyzer",
51             "org.apache.lucene.analysis.standard.StandardAnalyzer"));
52     String defaultField = config.get("file.query.maker.default.field", DocMaker.BODY_FIELD);
53     QueryParser qp = new QueryParser(Version.LUCENE_CURRENT, defaultField, anlzr);
54     qp.setAllowLeadingWildcard(true);
55
56     List<Query> qq = new ArrayList<Query>();
57     String fileName = config.get("file.query.maker.file", null);
58     if (fileName != null)
59     {
60       File file = new File(fileName);
61       Reader reader = null;
62       if (file.exists()) {
63         reader = new FileReader(file);
64       } else {
65         //see if we can find it as a resource
66         InputStream asStream = FileBasedQueryMaker.class.getClassLoader().getResourceAsStream(fileName);
67         if (asStream != null) {
68           reader = new InputStreamReader(asStream);
69         }
70       }
71       if (reader != null) {
72         try {
73           BufferedReader buffered = new BufferedReader(reader);
74           String line = null;
75           int lineNum = 0;
76           while ((line = buffered.readLine()) != null) {
77             line = line.trim();
78             if (line.length() != 0 && !line.startsWith("#")) {
79               try {
80                 qq.add(qp.parse(line));
81               } catch (ParseException e) {
82                 System.err.println("Exception: " + e.getMessage() + " occurred while parsing line: " + lineNum + " Text: " + line);
83               }
84             }
85             lineNum++;
86           }
87         } finally {
88           reader.close();
89         }
90       } else {
91         System.err.println("No Reader available for: " + fileName);
92       }
93       
94     }
95     return qq.toArray(new Query[qq.size()]) ;
96   }
97 }