1 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 ; 44 45 import java.util.LinkedList ; 46 import java.util.logging.Logger ; 47 48 import javax.naming.NamingException ; 49 50 53 public class Query { 54 static protected final Logger 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 _query; 63 private int _offset; 64 private int _limit = LIMIT_DEFAULT; 65 66 private LinkedList <JavadocItem> _results; 67 68 public Query() 69 throws NamingException 70 { 71 _store = Store.getInstance(); 72 } 73 74 protected void reset() 75 { 76 _results = null; 77 } 78 79 82 public void setStore(Store store) 83 { 84 _store = store; 85 } 86 87 90 public void setQuery(String query) 91 { 92 _query = safeString(query); 93 94 if (_query != null) { 95 98 CharBuffer cb = new CharBuffer(_query); 99 100 int di = cb.indexOf(".html"); 102 if (di > -1) 103 cb.delete(di,di+5); 104 105 106 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 128 public String getQuery() 129 { 130 return _query; 131 } 132 133 137 public void setOffset(String offset) 138 { 139 _offset = safeParseInt(offset,_offset); 140 reset(); 141 } 142 143 147 public int getOffset() 148 { 149 return _offset; 150 } 151 152 155 public void setLimit(String limit) 156 { 157 _limit = safeParseInt(limit,_limit); 158 if (_limit > LIMIT_MAX) 159 _limit = LIMIT_MAX; 160 reset(); 161 } 162 163 166 public int getLimit() 167 { 168 return _limit; 169 } 170 171 174 public LinkedList <JavadocItem> getResults() 175 throws SQLException 176 { 177 if (_results != null) 178 return _results; 179 180 if (_query == null) 181 _results = new LinkedList <JavadocItem>(); 182 else 183 _results = _store.query(_query,_offset,_limit); 184 185 return _results; 186 } 187 188 191 public boolean getIsNextPage() 192 { 193 return _results != null && _results.size() == _limit; 194 } 195 196 199 public int getNextPageOffset() 200 { 201 return _offset + _limit; 202 } 203 204 207 public boolean getIsPreviousPage() 208 { 209 return _offset > 0; 210 } 211 212 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 toString() 225 { 226 return _query; 227 } 228 229 private static String safeString(String s, String deflt) 230 { 231 if (s == null || s.length() == 0) 232 return deflt; 233 else 234 return s; 235 } 236 237 private static String safeString(String s) 238 { 239 return safeString(s,null); 240 } 241 242 private static int safeParseInt(String 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 ex) { 250 return deflt; 251 } 252 } 253 } 254 255 } 256 257 | Popular Tags |