/* * MapSearch.java * Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ package org.greenstone.gsdl3.service; // Greenstone classes import org.greenstone.gsdl3.util.*; // XML classes import org.w3c.dom.Element; import org.w3c.dom.Document; import org.w3c.dom.NodeList; // General java classes import java.util.Hashtable; import java.util.HashMap; import java.util.Arrays; import java.util.ArrayList; import java.util.LinkedList; import java.lang.reflect.Array; import java.util.Comparator; import java.io.BufferedReader; import java.io.FileReader; import java.io.File; import java.io.IOException; import java.io.ObjectInputStream; import java.io.FileInputStream; import java.io.Serializable; import java.lang.ClassNotFoundException; import org.apache.log4j.*; /** * * */ public class MapSearch extends AbstractTextSearch { static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.MapSearch.class.getName()); protected String files_home_dir = null; protected static final String FREQ_ATT = "freq"; // use the following if we add back in index searching //protected String default_index = null; //protected static final String DEFAULT_INDEX_ELEM = "defaultIndex"; //protected static final String INDEX_ELEM = "index"; /** Hashtable containing the place names database, indexed on the place name in lower case. */ public static Hashtable hashNames; /** Constructor */ public MapSearch() {} /** */ public boolean configure(Element info, Element extra_info) { if (!super.configure(info, extra_info)) { return false; } logger.info("Configuring MapSearch..."); // set the files_home_dir variable for this collection this.files_home_dir = GSFile.collectionIndexDir(site_home, cluster_name)+File.separator+"assoc"+File.separator; //load the hashtable hashNames = new Hashtable(); try{ ObjectInputStream in = new ObjectInputStream(new FileInputStream(this.files_home_dir+"hashnames.dat")); hashNames = (Hashtable)in.readObject(); in.close(); }catch(IOException ioe){ ioe.printStackTrace(); return false; }catch(ClassNotFoundException cnf){ cnf.printStackTrace(); return false; } // just in case we get the index stuff going, here is the code // get the display stuff for the indexes // NodeList indexes = info.getElementsByTagName(GSXML.INDEX_ELEM); // if (extra_info != null) { // Document owner = info.getOwnerDocument(); // Element config_search = (Element)GSXML.getChildByTagName(extra_info, GSXML.SEARCH_ELEM); // if (config_search != null) { // for (int i=0; i (buildConfig.xml) // Element def = (Element) GSXML.getChildByTagName(info, DEFAULT_INDEX_ELEM); // if (def != null) { // default_index = def.getAttribute(GSXML.NAME_ATT); // } // if (default_index == null || default_index.equals("")) { // // use the first one from the index list // default_index = ((Element)indexes.item(0)).getAttribute(GSXML.NAME_ATT); // } return true; } protected void getIndexData(ArrayList index_ids, ArrayList index_names, String lang) { // for now, we just have one dummy index index_ids.add("idx"); index_names.add("maps"); // get from properties // use the following if we ever get the index search working // Element index_list = (Element)GSXML.getChildByTagName(this.config_info, INDEX_ELEM+GSXML.LIST_MODIFIER); // NodeList indexes = index_list.getElementsByTagName(INDEX_ELEM); // for (int i=0; i params = GSXML.extractParams(param_list, false); // Make sure a query has been specified String searchTerm = (String) params.get(QUERY_PARAM); if (searchTerm == null || searchTerm.equals("")) { return result; // Return the empty result } // If an index hasn't been specified, use the default // String index = (String) params.get(INDEX_PARAM); // if (index == null) { // index = default_index; // } //convert the query string from the form '"Te Awamutu" Hamilton' to 'Te Awamutu+Hamilton' //replace ' ' with '+' searchTerm = searchTerm.replace(' ','+'); //replace '"' with '"' int place = -1; while(searchTerm.indexOf(""",place+1) != -1){ place = searchTerm.indexOf(""",place+1); searchTerm = (searchTerm.substring(0,place) + "\"" + searchTerm.substring(place+6,searchTerm.length())); } //replace spaces in string enclosed in quotes place = -1; while(searchTerm.indexOf('"',place+1) != -1){ place = searchTerm.indexOf('"',place+1); if(searchTerm.indexOf('"',place+1) != -1) searchTerm = (searchTerm.substring(0,place) + searchTerm.substring(place, searchTerm.indexOf('"',place+1)).replace('+',' ') + searchTerm.substring(searchTerm.indexOf('"',place+1), searchTerm.length())); place = searchTerm.indexOf('"',place); } //remove speech marks place = 0; while(place != -1){ place = searchTerm.indexOf('"', place); if(place != -1){ searchTerm = searchTerm.substring(0, place) + searchTerm.substring(place+1, searchTerm.length()); place=0; } } //find the number of search terms (number of '+' plus 1) int words = 1; place = 0; while (place != -1){ place = searchTerm.indexOf('+', place+1); if(place != -1) words++; } place = 0; //store each search term in a string array String terms[] = new String[words]; String terms_freq[] = new String[words]; for (int i=0; i<(words-1); i++){ terms[i]=searchTerm.substring(place, searchTerm.indexOf('+', place)); place = searchTerm.indexOf('+', place)+1; } terms[words-1] = searchTerm.substring(place, searchTerm.length()); Object nameArray[] = new Object[1]; int nameArraySize; String nameData; double xco, yco; LinkedList mapList = new LinkedList(); LinkedList placeList = new LinkedList(); String readString = ""; //capitalise the search terms for(int i=0;i= xtop && xco < xbot && yco >= ytop && yco < ybot) mapList.add(readString+"```"+nameData); } }catch(Exception e){e.printStackTrace();} } } // use an array object to sort the linked list by map number Object[] mapListArray = mapList.toArray(); Arrays.sort(mapListArray); mapList.clear(); for(int mla=0; mla tempList = new LinkedList(); String mapNumber = ""; String currentMap[] = {"",""}; int mapFreq = 0; while(mapList.size()>0){ readString = (String)mapList.removeFirst(); if(mapNumber.equals(readString.substring(0,readString.indexOf('.')))){ currentMap[1] = currentMap[1]+readString.substring(readString.indexOf("```"),readString.length()); mapFreq++; } else{ if(!currentMap[0].equals("")) tempList.add(currentMap[0]+"`"+mapFreq+currentMap[1]); currentMap[0] = readString.substring(0,readString.indexOf("```")); currentMap[1] = readString.substring(readString.indexOf("```"),readString.length()); mapNumber = readString.substring(0,readString.indexOf('.')); mapFreq=1; } } if(!currentMap[0].equals("")) tempList.add(currentMap[0]+"`"+mapFreq+currentMap[1]); int totalDocs = tempList.size(); // use an array object to sort the linked list by number of matches on each map Object[] tempListArray = tempList.toArray(); Arrays.sort(tempListArray, new MapResultSorter()); tempList.clear(); for(int tla=0; tla secondInt) return -1; else if(firstInt == secondInt) return 0; else return 1; } } }