1 | /*
|
---|
2 | * GS2MGSearch.java
|
---|
3 | * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
|
---|
4 | *
|
---|
5 | * This program is free software; you can redistribute it and/or modify
|
---|
6 | * the Free Software Foundation; either version 2 of the License, or
|
---|
7 | * (at your option) any later version.
|
---|
8 | *
|
---|
9 | * This program is distributed in the hope that it will be useful,
|
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | * GNU General Public License for more details.
|
---|
13 | *
|
---|
14 | * You should have received a copy of the GNU General Public License
|
---|
15 | * along with this program; if not, write to the Free Software
|
---|
16 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
17 | */
|
---|
18 | package org.greenstone.gsdl3.service;
|
---|
19 |
|
---|
20 |
|
---|
21 | // Greenstone classes
|
---|
22 | import org.greenstone.gsdl3.util.OID;
|
---|
23 | import org.greenstone.gsdl3.util.DBInfo;
|
---|
24 | import org.greenstone.gsdl3.util.GSXML;
|
---|
25 | import org.greenstone.gsdl3.util.SimpleCollectionDatabase;
|
---|
26 | import org.greenstone.gsdl3.util.GSFile;
|
---|
27 |
|
---|
28 | // XML classes
|
---|
29 | import org.w3c.dom.Document;
|
---|
30 | import org.w3c.dom.Element;
|
---|
31 | import org.w3c.dom.NodeList;
|
---|
32 |
|
---|
33 | // java
|
---|
34 | import java.util.Vector;
|
---|
35 | import java.util.ArrayList;
|
---|
36 | import java.util.HashMap;
|
---|
37 | import java.util.Map;
|
---|
38 | import java.util.Set;
|
---|
39 | import java.util.Iterator;
|
---|
40 | import java.io.File;
|
---|
41 |
|
---|
42 | import org.apache.log4j.*;
|
---|
43 |
|
---|
44 | public abstract class AbstractGS2Search
|
---|
45 | extends AbstractSearch
|
---|
46 | {
|
---|
47 |
|
---|
48 | protected static final String EQUIV_TERM_ELEM = "equivTerm";
|
---|
49 |
|
---|
50 | protected static final String STEM_ATT = "stem";
|
---|
51 | protected static final String NUM_DOCS_MATCH_ATT = "numDocsMatch";
|
---|
52 | protected static final String FREQ_ATT = "freq";
|
---|
53 |
|
---|
54 | // Elements used in the config file that are specific to this class
|
---|
55 | protected static final String DEFAULT_INDEX_ELEM = "defaultIndex";
|
---|
56 | protected static final String INDEX_STEM_ELEM = "indexStem";
|
---|
57 | protected static final String INDEX_ELEM = "index";
|
---|
58 | protected static final String DEFAULT_INDEX_SUBCOLLECTION_ELEM = "defaultIndexSubcollection";
|
---|
59 | protected static final String DEFAULT_INDEX_LANGUAGE_ELEM = "defaultIndexLanguage";
|
---|
60 | protected static final String INDEX_SUBCOLLECTION_ELEM = "indexSubcollection";
|
---|
61 | protected static final String INDEX_LANGUAGE_ELEM = "indexLanguage";
|
---|
62 |
|
---|
63 |
|
---|
64 | // Some indexing options
|
---|
65 | protected static final String STEMINDEX_OPTION = "stemIndexes";
|
---|
66 | protected static final String MAXNUMERIC_OPTION = "maxnumeric";
|
---|
67 |
|
---|
68 | /** the stem used for the index files */
|
---|
69 | protected String index_stem = null;
|
---|
70 |
|
---|
71 | // stem indexes available
|
---|
72 | protected boolean does_case=true;
|
---|
73 | protected boolean does_stem=true;
|
---|
74 | protected boolean does_accent=false;
|
---|
75 |
|
---|
76 | // maxnumeric -
|
---|
77 | protected int maxnumeric = 4;
|
---|
78 |
|
---|
79 | // collection database
|
---|
80 | protected SimpleCollectionDatabase coll_db = null;
|
---|
81 |
|
---|
82 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.AbstractGS2Search.class.getName());
|
---|
83 |
|
---|
84 |
|
---|
85 | /** constructor */
|
---|
86 | public AbstractGS2Search()
|
---|
87 | {
|
---|
88 |
|
---|
89 | }
|
---|
90 | public void cleanUp() {
|
---|
91 | super.cleanUp();
|
---|
92 | this.coll_db.closeDatabase();
|
---|
93 | }
|
---|
94 |
|
---|
95 | /** configure this service */
|
---|
96 | public boolean configure(Element info, Element extra_info)
|
---|
97 | {
|
---|
98 | if (!super.configure(info, extra_info)){
|
---|
99 | return false;
|
---|
100 | }
|
---|
101 |
|
---|
102 | // find out what kind of database we have
|
---|
103 | Element database_type_elem = (Element) GSXML.getChildByTagName(info, GSXML.DATABASE_TYPE_ELEM);
|
---|
104 | String database_type = null;
|
---|
105 | if (database_type_elem != null) {
|
---|
106 | database_type = database_type_elem.getAttribute(GSXML.NAME_ATT);
|
---|
107 | }
|
---|
108 | if (database_type == null || database_type.equals("")) {
|
---|
109 | database_type = "gdbm"; // the default
|
---|
110 | }
|
---|
111 | coll_db = new SimpleCollectionDatabase(database_type);
|
---|
112 | if (coll_db == null) {
|
---|
113 | logger.error("Couldn't create the collection database of type "+database_type);
|
---|
114 | return false;
|
---|
115 | }
|
---|
116 |
|
---|
117 | // the index stem is either the collection name or is specified in the config file
|
---|
118 | Element index_stem_elem = (Element) GSXML.getChildByTagName(info, INDEX_STEM_ELEM);
|
---|
119 | if (index_stem_elem != null) {
|
---|
120 | this.index_stem = index_stem_elem.getAttribute(GSXML.NAME_ATT);
|
---|
121 | }
|
---|
122 | if (this.index_stem == null || this.index_stem.equals("")) {
|
---|
123 | logger.warn("indexStem element not found, stem will default to collection name");
|
---|
124 | this.index_stem = this.cluster_name;
|
---|
125 | }
|
---|
126 |
|
---|
127 | // Open database for querying
|
---|
128 | String coll_db_file = GSFile.collectionDatabaseFile(this.site_home, this.cluster_name, this.index_stem, database_type);
|
---|
129 | if (!this.coll_db.openDatabase(coll_db_file, SimpleCollectionDatabase.READ)) {
|
---|
130 | logger.error("Could not open collection database!");
|
---|
131 | return false;
|
---|
132 | }
|
---|
133 |
|
---|
134 | // do we support any of the extended features?
|
---|
135 | does_chunking = true;
|
---|
136 |
|
---|
137 | // Get the default index out of <defaultIndex> (buildConfig.xml)
|
---|
138 | Element def = (Element) GSXML.getChildByTagName(info, DEFAULT_INDEX_ELEM);
|
---|
139 | if (def != null) {
|
---|
140 | this.default_index = def.getAttribute(GSXML.SHORTNAME_ATT);
|
---|
141 | } // otherwise will be "", and the first one will be the default
|
---|
142 |
|
---|
143 | //get the default indexSubcollection out of <defaultIndexSubcollection> (buildConfig.xml)
|
---|
144 | Element defSub = (Element) GSXML.getChildByTagName(info, DEFAULT_INDEX_SUBCOLLECTION_ELEM);
|
---|
145 | if (defSub != null) {
|
---|
146 | this.default_index_subcollection = defSub.getAttribute(GSXML.SHORTNAME_ATT);
|
---|
147 | }
|
---|
148 |
|
---|
149 | //get the default indexLanguage out of <defaultIndexLanguage> (buildConfig.xml)
|
---|
150 | Element defLang = (Element) GSXML.getChildByTagName(info, DEFAULT_INDEX_LANGUAGE_ELEM);
|
---|
151 | if (defLang != null) {
|
---|
152 | this.default_index_language = defLang.getAttribute(GSXML.SHORTNAME_ATT);
|
---|
153 | } //concate defaultIndex + defaultIndexSubcollection + defaultIndexLanguage
|
---|
154 |
|
---|
155 | // get index options
|
---|
156 | Element index_option_list = (Element) GSXML.getChildByTagName(info, GSXML.INDEX_OPTION_ELEM + GSXML.LIST_MODIFIER);
|
---|
157 | if (index_option_list != null) {
|
---|
158 | NodeList options = index_option_list.getElementsByTagName(GSXML.INDEX_OPTION_ELEM);
|
---|
159 | for (int i=0; i<options.getLength(); i++) {
|
---|
160 | Element opt = (Element)options.item(i);
|
---|
161 | String name = opt.getAttribute(GSXML.NAME_ATT);
|
---|
162 | String value = opt.getAttribute(GSXML.VALUE_ATT);
|
---|
163 | if (name.equals(MAXNUMERIC_OPTION)) {
|
---|
164 | int maxnum = Integer.parseInt(value);
|
---|
165 | if (4 <= maxnum && maxnum < 512) {
|
---|
166 | maxnumeric = maxnum;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | else if (name.equals(STEMINDEX_OPTION)) {
|
---|
170 | int stemindex = Integer.parseInt(value);
|
---|
171 | // stem and case are true by default, accent folding false by default
|
---|
172 | if ((stemindex & 1) == 0) {
|
---|
173 | does_case = false;
|
---|
174 | }
|
---|
175 | if ((stemindex & 2) == 0) {
|
---|
176 | does_stem = false;
|
---|
177 | }
|
---|
178 | if ((stemindex & 4) != 0) {
|
---|
179 | does_accent = true;
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | // get display info from extra info
|
---|
186 | if (extra_info !=null) {
|
---|
187 | Document owner = info.getOwnerDocument();
|
---|
188 | // so far we have index specific display elements, and global format elements
|
---|
189 | NodeList indexes = info.getElementsByTagName(GSXML.INDEX_ELEM);
|
---|
190 | Element config_search = (Element)GSXML.getChildByTagName(extra_info, GSXML.SEARCH_ELEM);
|
---|
191 |
|
---|
192 | for (int i=0; i<indexes.getLength();i++) {
|
---|
193 | Element ind = (Element)indexes.item(i);
|
---|
194 | String name = ind.getAttribute(GSXML.NAME_ATT);
|
---|
195 | Element node_extra = GSXML.getNamedElement(config_search,
|
---|
196 | GSXML.INDEX_ELEM,
|
---|
197 | GSXML.NAME_ATT,
|
---|
198 | name);
|
---|
199 | if (node_extra == null) {
|
---|
200 | logger.error("haven't found extra info for index named "+name);
|
---|
201 | continue;
|
---|
202 | }
|
---|
203 |
|
---|
204 | // get the display elements if any - displayName
|
---|
205 | NodeList display_names = node_extra.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
|
---|
206 | if (display_names !=null) {
|
---|
207 | for (int j=0; j<display_names.getLength(); j++) {
|
---|
208 | Element e = (Element)display_names.item(j);
|
---|
209 | ind.appendChild(owner.importNode(e, true));
|
---|
210 | }
|
---|
211 | }
|
---|
212 | } // for each index
|
---|
213 | }
|
---|
214 | return true;
|
---|
215 | }
|
---|
216 |
|
---|
217 | protected void getIndexData(ArrayList index_ids, ArrayList index_names, String lang) {
|
---|
218 | // the index info -
|
---|
219 | Element index_list = (Element)GSXML.getChildByTagName(this.config_info, INDEX_ELEM+GSXML.LIST_MODIFIER);
|
---|
220 | NodeList indexes = index_list.getElementsByTagName(INDEX_ELEM);
|
---|
221 | int len = indexes.getLength();
|
---|
222 | // now add even if there is only one
|
---|
223 | for (int i=0; i<len; i++) {
|
---|
224 | Element index = (Element)indexes.item(i);
|
---|
225 | String shortname = index.getAttribute(GSXML.SHORTNAME_ATT);
|
---|
226 | if (shortname.equals("")) {
|
---|
227 | continue;
|
---|
228 | }
|
---|
229 | index_ids.add(shortname);
|
---|
230 | String display_name = GSXML.getDisplayText(index, GSXML.DISPLAY_TEXT_NAME, lang, "en");
|
---|
231 | if (display_name.equals("")) {
|
---|
232 | display_name = index.getAttribute(GSXML.NAME_ATT);
|
---|
233 | if (display_name.equals("")) {
|
---|
234 | display_name = shortname;
|
---|
235 | }
|
---|
236 | }
|
---|
237 | index_names.add(display_name);
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | protected void getIndexSubcollectionData(ArrayList index_sub_ids, ArrayList index_sub_names, String lang){
|
---|
242 | // the index info -
|
---|
243 | Element index_sub_list = (Element)GSXML.getChildByTagName(this.config_info, INDEX_SUBCOLLECTION_ELEM+GSXML.LIST_MODIFIER);
|
---|
244 | NodeList index_subs = index_sub_list.getElementsByTagName(INDEX_SUBCOLLECTION_ELEM);
|
---|
245 | int len = index_subs.getLength();
|
---|
246 | // now add even if there is only one
|
---|
247 | for (int i=0; i<len; i++) {
|
---|
248 | Element indexsub = (Element)index_subs.item(i);
|
---|
249 | String shortname = indexsub.getAttribute(GSXML.SHORTNAME_ATT);
|
---|
250 | if (shortname.equals("")) {
|
---|
251 | continue;
|
---|
252 | }
|
---|
253 | index_sub_ids.add(shortname);
|
---|
254 | String display_name = GSXML.getDisplayText(indexsub, GSXML.DISPLAY_TEXT_NAME, lang, "en");
|
---|
255 | if (display_name.equals("")) {
|
---|
256 | display_name = indexsub.getAttribute(GSXML.NAME_ATT);
|
---|
257 | if (display_name.equals("")) {
|
---|
258 | display_name = shortname;
|
---|
259 | }
|
---|
260 | }
|
---|
261 | index_sub_names.add(display_name);
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|
265 | protected void getIndexLanguageData(ArrayList index_lang_ids, ArrayList index_lang_names, String lang){
|
---|
266 | // the index info -
|
---|
267 | Element index_lang_list = (Element)GSXML.getChildByTagName(this.config_info, INDEX_LANGUAGE_ELEM+GSXML.LIST_MODIFIER);
|
---|
268 | NodeList index_langs = index_lang_list.getElementsByTagName(INDEX_LANGUAGE_ELEM);
|
---|
269 | int len = index_langs.getLength();
|
---|
270 | // now add even if there is only one
|
---|
271 | for (int i=0; i<len; i++) {
|
---|
272 | Element indexlang = (Element)index_langs.item(i);
|
---|
273 | String shortname = indexlang.getAttribute(GSXML.SHORTNAME_ATT);
|
---|
274 | if (shortname.equals("")) {
|
---|
275 | continue;
|
---|
276 | }
|
---|
277 | index_lang_ids.add(shortname);
|
---|
278 | String display_name = GSXML.getDisplayText(indexlang, GSXML.DISPLAY_TEXT_NAME, lang, "en");
|
---|
279 | if (display_name.equals("")) {
|
---|
280 | display_name = indexlang.getAttribute(GSXML.NAME_ATT);
|
---|
281 | if (display_name.equals("")) {
|
---|
282 | display_name = shortname;
|
---|
283 | }
|
---|
284 | }
|
---|
285 | index_lang_names.add(display_name);
|
---|
286 | }
|
---|
287 |
|
---|
288 |
|
---|
289 | }
|
---|
290 |
|
---|
291 |
|
---|
292 | protected void addCustomQueryParams(Element param_list, String lang)
|
---|
293 | {
|
---|
294 | if (this.does_case){
|
---|
295 | createParameter(CASE_PARAM, param_list, lang);
|
---|
296 | }
|
---|
297 | if (this.does_stem){
|
---|
298 | createParameter(STEM_PARAM, param_list, lang);
|
---|
299 | }
|
---|
300 | if (this.does_accent){
|
---|
301 | createParameter(ACCENT_PARAM, param_list, lang);
|
---|
302 | }
|
---|
303 | createParameter(MATCH_PARAM, param_list, lang);
|
---|
304 | }
|
---|
305 |
|
---|
306 |
|
---|
307 |
|
---|
308 | /** returns the document type of the doc that the specified node
|
---|
309 | belongs to. should be one of
|
---|
310 | GSXML.DOC_TYPE_SIMPLE,
|
---|
311 | GSXML.DOC_TYPE_PAGED,
|
---|
312 | GSXML.DOC_TYPE_HIERARCHY
|
---|
313 | */
|
---|
314 | protected String getDocType(String node_id){
|
---|
315 | DBInfo info = this.coll_db.getInfo(node_id);
|
---|
316 | if (info == null) {
|
---|
317 | return GSXML.DOC_TYPE_SIMPLE;
|
---|
318 | }
|
---|
319 | String doc_type = info.getInfo("doctype");
|
---|
320 | if (!doc_type.equals("")&&!doc_type.equals("doc")) {
|
---|
321 | return doc_type;
|
---|
322 | }
|
---|
323 |
|
---|
324 | String top_id = OID.getTop(node_id);
|
---|
325 | boolean is_top = (top_id.equals(node_id) ? true : false);
|
---|
326 |
|
---|
327 | String children = info.getInfo("contains");
|
---|
328 | boolean is_leaf = (children.equals("") ? true : false);
|
---|
329 |
|
---|
330 | if (is_top && is_leaf) { // a single section document
|
---|
331 | return GSXML.DOC_TYPE_SIMPLE;
|
---|
332 | }
|
---|
333 |
|
---|
334 | // now we just check the top node
|
---|
335 | if (!is_top) { // we need to look at the top info
|
---|
336 | info = this.coll_db.getInfo(top_id);
|
---|
337 | }
|
---|
338 | if (info == null) {
|
---|
339 | return GSXML.DOC_TYPE_HIERARCHY;
|
---|
340 | }
|
---|
341 |
|
---|
342 | String childtype = info.getInfo("childtype");
|
---|
343 | if (childtype.equals("Paged")) {
|
---|
344 | return GSXML.DOC_TYPE_PAGED;
|
---|
345 | }
|
---|
346 | return GSXML.DOC_TYPE_HIERARCHY;
|
---|
347 |
|
---|
348 | }
|
---|
349 |
|
---|
350 | /** returns true if the node has child nodes */
|
---|
351 | protected boolean hasChildren(String node_id){
|
---|
352 | DBInfo info = this.coll_db.getInfo(node_id);
|
---|
353 | if (info == null) {
|
---|
354 | return false;
|
---|
355 | }
|
---|
356 | String contains = info.getInfo("contains");
|
---|
357 | if (contains.equals("")) {
|
---|
358 | return false;
|
---|
359 | }
|
---|
360 | return true;
|
---|
361 | }
|
---|
362 |
|
---|
363 | /** returns true if the node has a parent */
|
---|
364 | protected boolean hasParent(String node_id){
|
---|
365 | String parent = OID.getParent(node_id);
|
---|
366 | if (parent.equals(node_id)) {
|
---|
367 | return false;
|
---|
368 | }
|
---|
369 | return true;
|
---|
370 | }
|
---|
371 |
|
---|
372 | /** convert indexer internal id to Greenstone oid */
|
---|
373 | protected String internalNum2OID(long docnum)
|
---|
374 | {
|
---|
375 | return this.coll_db.docnum2OID(docnum);
|
---|
376 |
|
---|
377 | }
|
---|
378 | protected String internalNum2OID(String docnum)
|
---|
379 | {
|
---|
380 | return this.coll_db.docnum2OID(docnum);
|
---|
381 |
|
---|
382 | }
|
---|
383 |
|
---|
384 | }
|
---|
385 |
|
---|
386 |
|
---|