1 /**
2  Copyright (c) 2003, Technology Concepts & Design, Inc.
3  Copyright (c) 2006,2008, Olly Betts
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without modification, are permitted
7  provided that the following conditions are met:
8 
9  - Redistributions of source code must retain the above copyright notice, this list of conditions
10  and the following disclaimer.
11 
12  - Redistributions in binary form must reproduce the above copyright notice, this list of conditions
13  and the following disclaimer in the documentation and/or other materials provided with the distribution.
14 
15  - Neither the name of Technology Concepts & Design, Inc. nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
19  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
21  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25  THE POSSIBILITY OF SUCH DAMAGE.
26  **/
27 package org.xapian;
28 
29 import org.xapian.errors.XapianError;
30 import org.xapian.errors.XapianRuntimeError;
31 //import org.xapian.query_parser.ParseException;
32 //import org.xapian.query_parser.QueryParser;
33 
34 //import java.io.StringReader;
35 
36 public class Query {
37     public static final int OP_AND = 1;
38     public static final int OP_OR = 2;
39     public static final int OP_AND_NOT = 3;
40     public static final int OP_XOR = 4;
41     public static final int OP_AND_MAYBE = 5;
42     public static final int OP_FILTER = 6;
43     public static final int OP_NEAR = 7;
44     public static final int OP_PHRASE = 8;
45     public static final int OP_ELITE_SET = 10;
46 
47     private Query _left = null, _right = null;
48     long id = 0;
49 /*
50     private String _nativeStuff;
51     private String _nativeOperator;
52 
53     public static Query parse(String query) throws ParseException, XapianError {
54         if (query == null || query.trim().length() == 0)
55             throw new ParseException("Empty Queries are not allowed");
56 
57         QueryParser parser = new QueryParser(new StringReader(query));
58         Query q = parser.parse();
59         if (q == null)
60             throw new ParseException("syntax error: " + query);
61         q.setNativeStuff(parser.getNativeStuff());
62         q.setNativeOperator(parser.getNativeOperator());
63         return q;
64     }
65 */
validateOperator(int op)66     private static final void validateOperator(int op) throws IllegalArgumentException {
67         if (op < 1 || op > 10 || op == 9)
68             throw new IllegalArgumentException("operator " + op + " is not valid");
69     }
70 
Query()71     public Query() throws XapianError {
72         id = XapianJNI.query_new();
73     }
74 
Query(String term)75     public Query(String term) throws XapianError {
76         if (term == null)
77             throw new XapianError("Empty Queries are not allowed");
78         id = XapianJNI.query_new(term);
79     }
80 
Query(String term, int wqf)81     public Query(String term, int wqf) throws XapianError {
82         if (term == null)
83             throw new XapianError("Empty Queries are not allowed");
84         id = XapianJNI.query_new(term, wqf);
85     }
86 
Query(String term, int wqf, int pos)87     public Query(String term, int wqf, int pos) throws XapianError {
88         if (term == null)
89             throw new XapianError("Empty Queries are not allowed");
90         id = XapianJNI.query_new(term, wqf, pos);
91     }
92 
Query(int operator, Query left, Query right)93     public Query(int operator, Query left, Query right) throws XapianError {
94         validateOperator(operator);
95 
96         // must hold onto left and right Queries or the JVM might
97         // garbage-collect them on us!
98         _left = left;
99         _right = right;
100 
101         if (left == null)
102 	    throw new XapianError("Left side of query is null");
103         if (right == null)
104 	    throw new XapianError("Right side of query is null");
105 	id = XapianJNI.query_new(operator, left.id, right.id);
106     }
107 
Query(int operator, String left, String right)108     public Query(int operator, String left, String right) throws XapianError {
109         validateOperator(operator);
110         id = XapianJNI.query_new(operator, left, right);
111     }
112 
Query(int operator, Query[] queries)113     public Query(int operator, Query[] queries) throws XapianError {
114         validateOperator(operator);
115 	long[] query_ids = new long[queries.length];
116 	for (int i = 0; i < queries.length; ++i) {
117 	    query_ids[i] = queries[i].id;
118 	}
119 	id = XapianJNI.query_new(operator, query_ids);
120     }
121 
Query(int operator, String[] terms)122     public Query(int operator, String[] terms) throws XapianError {
123         validateOperator(operator);
124         id = XapianJNI.query_new(operator, terms);
125     }
126 
127 /*
128     public String getNativeStuff() {
129         return _nativeStuff;
130     }
131 
132     void setNativeStuff(String str) {
133         _nativeStuff = str;
134     }
135 
136     public String getNativeOperator() {
137         return _nativeOperator;
138     }
139 
140     void setNativeOperator(String str) {
141         _nativeOperator = str;
142     }
143 */
144 
getLength()145     public long getLength() throws XapianError {
146         return XapianJNI.query_get_length(id);
147     }
148 
getTerms()149     public TermIterator getTerms() throws XapianError {
150         return new TermIterator(id, XapianJNI.query_terms_begin(id), XapianJNI.query_terms_end(id));
151     }
152 
empty()153     public boolean empty() throws XapianError {
154         return XapianJNI.query_empty(id);
155     }
156 
isEmpty()157     public boolean isEmpty() throws XapianError {
158         return XapianJNI.query_empty(id);
159     }
160 
toString()161     public String toString() {
162         try {
163             return XapianJNI.query_get_description(id);
164         } catch (XapianError xe) {
165             throw new XapianRuntimeError(xe);
166         }
167     }
168 
finalize()169     protected void finalize() throws Throwable {
170         XapianJNI.query_finalize(id);
171         super.finalize();
172     }
173 
174 }
175