KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > doc > javadoc > Query


1 /*
2  * Copyright (c) 1998-2003 Caucho Technology -- all rights reserved
3  *
4  * Caucho Technology permits redistribution, modification and use
5  * of this file in source and binary form ("the Software") under the
6  * Caucho Developer Source License ("the License"). The following
7  * conditions must be met:
8  *
9  * 1. Each copy or derived work of the Software must preserve the copyright
10  * notice and this notice unmodified.
11  *
12  * 2. Redistributions of the Software in source or binary form must include
13  * an unmodified copy of the License, normally in a plain ASCII text
14  *
15  * 3. The names "Resin" or "Caucho" are trademarks of Caucho Technology and
16  * may not be used to endorse products derived from this software.
17  * "Resin" or "Caucho" may not appear in the names of products derived
18  * from this software.
19  *
20  * This Software is provided "AS IS," without a warranty of any kind.
21  * ALL EXPRESS OR IMPLIED REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
22  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
24  *
25  * CAUCHO TECHNOLOGY AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
26  * SUFFERED BY LICENSEE OR ANY THIRD PARTY AS A RESULT OF USING OR
27  * DISTRIBUTING SOFTWARE. IN NO EVENT WILL CAUCHO OR ITS LICENSORS BE LIABLE
28  * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
29  * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
30  * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
31  * INABILITY TO USE SOFTWARE, EVEN IF HE HAS BEEN ADVISED OF THE POSSIBILITY
32  * OF SUCH DAMAGES.
33  *
34  * @author Sam
35  */

36
37 package com.caucho.doc.javadoc;
38
39 import com.caucho.log.Log;
40 import com.caucho.util.CharBuffer;
41 import com.caucho.util.L10N;
42
43 import java.sql.SQLException JavaDoc;
44
45 import java.util.LinkedList JavaDoc;
46 import java.util.logging.Logger JavaDoc;
47
48 import javax.naming.NamingException JavaDoc;
49
50 /**
51  * A query to be created and used from jsp.
52  */

53 public class Query {
54   static protected final Logger JavaDoc log = Log.open(Query.class);
55   static final L10N L = new L10N(Query.class);
56
57   private final static int LIMIT_DEFAULT = 15;
58   private final static int LIMIT_MAX = 250;
59
60   private Store _store;
61
62   private String JavaDoc _query;
63   private int _offset;
64   private int _limit = LIMIT_DEFAULT;
65
66   private LinkedList JavaDoc<JavadocItem> _results;
67
68   public Query()
69     throws NamingException JavaDoc
70   {
71     _store = Store.getInstance();
72   }
73
74   protected void reset()
75   {
76     _results = null;
77   }
78
79   /**
80    * The store that this Query should use.
81    */

82   public void setStore(Store store)
83   {
84     _store = store;
85   }
86
87   /**
88    * The query string submitted by the user.
89    */

90   public void setQuery(String JavaDoc query)
91   {
92     _query = safeString(query);
93
94     if (_query != null) {
95       // turn com/caucho/vfs/Path.html#method() style submissions
96
// into appropriate ones
97

98       CharBuffer cb = new CharBuffer(_query);
99
100       // take .html out
101
int di = cb.indexOf(".html");
102       if (di > -1)
103         cb.delete(di,di+5);
104
105
106       // take all (...) out
107
while ( (di = cb.indexOf('(')) > -1) {
108         int di2 = cb.indexOf(')',di);
109         if (di2 > di)
110           cb.delete(di,di2+1);
111       }
112
113       for (int i = 0; i < cb.length(); i++) {
114         char ch = cb.charAt(i);
115         if (i > 0 && ch == '/' || ch == '\\' || ch == '#')
116           cb.setCharAt(i,'.');
117       }
118
119       _query = cb.toString();
120     }
121
122     reset();
123   }
124
125   /**
126    * The query string submitted by the user.
127    */

128   public String JavaDoc getQuery()
129   {
130     return _query;
131   }
132
133   /**
134    * An offset into the search results, for previous/next page
135    * functionality.
136    */

137   public void setOffset(String JavaDoc offset)
138   {
139     _offset = safeParseInt(offset,_offset);
140     reset();
141   }
142
143   /**
144    * An offset into the search results, for previous/next page
145    * functionality.
146    */

147   public int getOffset()
148   {
149     return _offset;
150   }
151
152   /**
153    * A limit on the number of search results to return for one page.
154    */

155   public void setLimit(String JavaDoc limit)
156   {
157     _limit = safeParseInt(limit,_limit);
158     if (_limit > LIMIT_MAX)
159       _limit = LIMIT_MAX;
160     reset();
161   }
162
163   /**
164    * A limit on the number of search results to return for one page.
165    */

166   public int getLimit()
167   {
168     return _limit;
169   }
170
171   /**
172    * Do the query and return the results.
173    */

174   public LinkedList JavaDoc<JavadocItem> getResults()
175     throws SQLException JavaDoc
176   {
177     if (_results != null)
178       return _results;
179
180     if (_query == null)
181       _results = new LinkedList JavaDoc<JavadocItem>();
182     else
183       _results = _store.query(_query,_offset,_limit);
184
185     return _results;
186   }
187
188   /**
189    * True if there is a next page.
190    */

191   public boolean getIsNextPage()
192   {
193     return _results != null && _results.size() == _limit;
194   }
195
196   /**
197    * The offset to be used for a link to the next set of results.
198    */

199   public int getNextPageOffset()
200   {
201     return _offset + _limit;
202   }
203
204   /**
205    * True if there is a previous page
206    */

207   public boolean getIsPreviousPage()
208   {
209     return _offset > 0;
210   }
211
212   /**
213    * The offset to be used for a link to the previous set of results, null
214    * if there is no next page.
215    */

216   public int getPreviousPageOffset()
217   {
218     int r = _offset - _limit;
219     if (r < 0)
220       r = 0;
221     return r;
222   }
223
224   public String JavaDoc toString()
225   {
226     return _query;
227   }
228
229   private static String JavaDoc safeString(String JavaDoc s, String JavaDoc deflt)
230   {
231     if (s == null || s.length() == 0)
232       return deflt;
233     else
234       return s;
235   }
236
237   private static String JavaDoc safeString(String JavaDoc s)
238   {
239     return safeString(s,null);
240   }
241
242   private static int safeParseInt(String JavaDoc s, int deflt)
243   {
244     if (s == null || s.length() == 0)
245       return deflt;
246     else {
247       try {
248         return Integer.parseInt(s);
249       } catch (Exception JavaDoc ex) {
250         return deflt;
251       }
252     }
253   }
254
255 }
256
257
Popular Tags