001: package org.apache.lucene.benchmark.byTask.feeds;
002:
003: import org.apache.lucene.analysis.Analyzer;
004: import org.apache.lucene.queryParser.QueryParser;
005: import org.apache.lucene.queryParser.ParseException;
006: import org.apache.lucene.search.Query;
007:
008: import java.io.*;
009: import java.util.ArrayList;
010: import java.util.List;
011:
012: /**
013: * Copyright 2004 The Apache Software Foundation
014: * <p/>
015: * Licensed under the Apache License, Version 2.0 (the "License");
016: * you may not use this file except in compliance with the License.
017: * You may obtain a copy of the License at
018: * <p/>
019: * http://www.apache.org/licenses/LICENSE-2.0
020: * <p/>
021: * Unless required by applicable law or agreed to in writing, software
022: * distributed under the License is distributed on an "AS IS" BASIS,
023: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
024: * See the License for the specific language governing permissions and
025: * limitations under the License.
026: */
027:
028: /**
029: * Create queries from a FileReader. One per line, pass them through the
030: * QueryParser. Lines beginning with # are treated as comments
031: *
032: * File can be specified as a absolute, relative or resource.
033: * Two properties can be set:
034: * file.query.maker.file=<Full path to file containing queries>
035: * <br/>
036: * file.query.maker.default.field=<Name of default field - Default value is "body">
037: *
038: * Example:
039: * file.query.maker.file=c:/myqueries.txt
040: * file.query.maker.default.field=body
041: */
042: public class FileBasedQueryMaker extends AbstractQueryMaker implements
043: QueryMaker {
044:
045: protected Query[] prepareQueries() throws Exception {
046:
047: Analyzer anlzr = (Analyzer) Class
048: .forName(
049: config
050: .get("analyzer",
051: "org.apache.lucene.analysis.standard.StandardAnalyzer"))
052: .newInstance();
053: String defaultField = config.get(
054: "file.query.maker.default.field",
055: BasicDocMaker.BODY_FIELD);
056: QueryParser qp = new QueryParser(defaultField, anlzr);
057:
058: List qq = new ArrayList();
059: String fileName = config.get("file.query.maker.file", null);
060: if (fileName != null) {
061: File file = new File(fileName);
062: Reader reader = null;
063: if (file != null && file.exists()) {
064: reader = new FileReader(file);
065: } else {
066: //see if we can find it as a resource
067: InputStream asStream = FileBasedQueryMaker.class
068: .getClassLoader().getResourceAsStream(fileName);
069: if (asStream != null) {
070: reader = new InputStreamReader(asStream);
071: }
072: }
073: if (reader != null) {
074: BufferedReader buffered = new BufferedReader(reader);
075: String line = null;
076: int lineNum = 0;
077: while ((line = buffered.readLine()) != null) {
078: line = line.trim();
079: if (!line.equals("") && !line.startsWith("#")) {
080: Query query = null;
081: try {
082: query = qp.parse(line);
083: } catch (ParseException e) {
084: System.err.println("Exception: "
085: + e.getMessage()
086: + " occurred while parsing line: "
087: + lineNum + " Text: " + line);
088: }
089: qq.add(query);
090: }
091: lineNum++;
092: }
093: } else {
094: System.err.println("No Reader available for: "
095: + fileName);
096: }
097: }
098: Query[] result = (Query[]) qq.toArray(new Query[qq.size()]);
099: return result;
100: }
101: }
|