org.apache.avalon.framework.context.ContextException org.apache.avalon.framework.component.ComponentException org.apache.lucene.analysis.Analyzer org.apache.lucene.analysis.standard.StandardAnalyzer org.apache.lucene.document.Document org.apache.lucene.document.Field org.apache.lucene.queryParser.QueryParser org.apache.lucene.queryParser.MultiFieldQueryParser org.apache.lucene.search.Hits org.apache.lucene.search.IndexSearcher org.apache.lucene.search.Query org.apache.lucene.search.Searcher org.apache.lucene.search.Sort java.io.FileNotFoundException java.util.Enumeration java.util.StringTokenizer java.util.Vector org.apache.lenya.lucene.ReTokenizeFile org.apache.lenya.lucene.Publication File workDir = null; File indexDir=null; File excerptDir=null; String[] fields={"contents","title"}; String field = "contents"; /** * Contextualize this class */ public void contextualize(Context context) throws ContextException { super.contextualize( context ); workDir = (File) context.get(Constants.CONTEXT_WORK_DIR); } /** * Search index */ Hits search(String query_string, String publication_id, String sortField, boolean sortReverse) throws ProcessingException, IOException{ hits=null; try{ Searcher searcher=new IndexSearcher(indexDir.getAbsolutePath()); Analyzer l_analyzer=new StandardAnalyzer(); QueryParser l_queryParser = new QueryParser(field,l_analyzer); // Single field //MultiFieldQueryParser l_queryParser = new MultiFieldQueryParser(fields[0], l_analyzer); // Multiple fields l_queryParser.setOperator(QueryParser.DEFAULT_OPERATOR_AND); getLogger().debug(query_string); Query l_query = l_queryParser.parse(query_string); // Single field //Query l_query = l_queryParser.parse(query_string,fields,l_analyzer); // Multiple fields if (sortField != null) { Sort sort = new Sort(sortField, sortReverse); hits = searcher.search(l_query, sort); } else { hits = searcher.search(l_query); } if(hits != null){ return hits; } } catch(IOException e){ System.err.println(".search(): EXCEPTION: "+e); throw e; } catch(Exception e){ System.err.println(".search(): EXCEPTION: "+e); } return null; } /** * */ String getPercent(float score){ return ""+java.lang.Math.round(score*100.0); } Hits hits; int hits_length=-1; String[] words=new String[0]; int hitsPerPage; int maxPages; int excerptOffset; int start; int end; // Get sitemap path org.apache.excalibur.source.Source input_source=this.resolver.resolveURI("context://lenya"); String sitemapPath=input_source.getURI(); sitemapPath=sitemapPath.substring(5); // Remove "file:" protocol // Read parameters from sitemap String numberOfPubs = parameters.getParameter("number-of-pubs", "1"); Publication[] pubs = new Publication[Integer.parseInt(numberOfPubs)]; for(int i = 0;i < pubs.length;i++) { pubs[i] = new Publication(); pubs[i].id = parameters.getParameter("pub"+i+"-id","@ID@"); pubs[i].name = parameters.getParameter("pub"+i+"-name","@NAME@"); pubs[i].indexDir = parameters.getParameter("pub"+i+"-index-dir","@INDEX-DIR@"); pubs[i].searchFields = parameters.getParameter("pub"+i+"-search-fields","title,contents"); pubs[i].excerptDir = parameters.getParameter("pub"+i+"-excerpt-dir","@EXCERPT-DIR@"); pubs[i].prefix = parameters.getParameter("pub"+i+"-prefix","@PREFIX@"); } String param_hits_per_page = parameters.getParameter("max-hits-per-page","13"); hitsPerPage = Integer.parseInt(param_hits_per_page); String param_max_pages = parameters.getParameter("max-pages","5"); maxPages = Integer.parseInt(param_max_pages); String param_excerpt_offset = parameters.getParameter("excerpt-offset","100"); excerptOffset = Integer.parseInt(param_excerpt_offset); // Read parameters from query string String queryString = ; String publication_id = ; String sortBy = ; String sortReverse = ; String startString = ; String endString = ; start=new Integer(startString).intValue(); if(endString == null){ end=hitsPerPage; } else{ end=new Integer(endString).intValue(); } // Find the number of the selected publication int whichPublication=0; for (int i = 0;i < pubs.length;i++) { if (pubs[i].id.equals(publication_id)) { whichPublication = i; } } // Get all search fields Vector myFields = new Vector(); Enumeration parameterNames = request.getParameterNames(); while(parameterNames.hasMoreElements()){ String parameterName=(String)parameterNames.nextElement(); String value=request.getParameter(parameterName); if (parameterName.indexOf(".fields") > 0) { // looking for field parameters StringTokenizer st = new StringTokenizer(parameterName, "."); int length = st.countTokens(); String fieldPublicationId = st.nextToken(); if (fieldPublicationId.equals(publication_id) || fieldPublicationId.equals("dummy-index-id")) { st.nextToken(); // Ignore "fields" token if (length == 2) { // radio or select myFields.addElement(value); } else if (length == 3) { // checkbox myFields.addElement(st.nextToken()); } else { // something is wrong } } } } if (myFields.size() > 0) { field = (String)myFields.elementAt(0); fields = new String[myFields.size()]; for (int i = 0; i < myFields.size(); i++) { fields[i] = (String)myFields.elementAt(i); } } // Set index and excerpt dir String param_index_dir=pubs[whichPublication].indexDir; if(param_index_dir.charAt(0) == '/'){ indexDir=new File(param_index_dir); } else{ indexDir=new File(sitemapPath+File.separator+param_index_dir); } String param_excerpt_dir=pubs[whichPublication].excerptDir; if(param_excerpt_dir.charAt(0) == '/'){ excerptDir=new File(param_excerpt_dir); } else{ excerptDir=new File(sitemapPath+File.separator+param_excerpt_dir); } publication_id numberOfPubs for(int i = 0;i < pubs.length;i++) { pubs[i].id pubs[i].name pubs[i].indexDir String[] searchFields = pubs[i].getFields(); if (searchFields != null) { for (int k = 0; k < searchFields.length; k++) { searchFields[k] } } else { pubs[i].searchFields.getFields() returned null } pubs[i].excerptDir pubs[i].prefix } hitsPerPage maxPages excerptOffset Enumeration para_names = request.getParameterNames(); if(para_names.hasMoreElements()){ while(para_names.hasMoreElements()){ String para_name=(String)para_names.nextElement(); String para_value=request.getParameter(para_name); para_namepara_value para_namepara_value } } if(queryString != null && queryString.length() != 0 && publication_id != null && publication_id.length() > 0){ try { if (sortBy.equals("score")) { hits = search(queryString, publication_id, null, false); } else { if (sortReverse.equals("true")) { hits = search(queryString, publication_id, sortBy, true); } else { hits = search(queryString, publication_id, sortBy, false); } } } catch(Exception e) { e.toString() } if(hits != null){ hits_length=hits.length(); } else{ hits_length=-1; hits=null; } publication_id pubs[whichPublication].name pubs[whichPublication].prefix sortBy queryString if(queryString != null){ Vector twords=new Vector(); StringTokenizer st=new StringTokenizer(queryString," "); while(st.hasMoreTokens()){ String word=(String)st.nextElement(); if(!(word.equals("OR") || word.equals("AND"))){ word twords.addElement(word); } } words=new String[twords.size()]; for(int i=0;i<twords.size();i++){ words[i]=(String)twords.elementAt(i); } } start end for (int i = 0; i < fields.length; i++) { fields[i] } try{ Analyzer ll_analyzer=new StandardAnalyzer(); QueryParser queryParser = new QueryParser(field,ll_analyzer); //MultiFieldQueryParser queryParser = new MultiFieldQueryParser("contents",ll_analyzer); queryParser.setOperator(QueryParser.DEFAULT_OPERATOR_AND); Query ll_query = queryParser.parse(queryString); //Query ll_query = queryParser.parse(queryString,fields,ll_analyzer); ll_query.toString("contents") } catch(Exception e){ e.toString() } } else{ hits_length=-1; hits=null; } if(hits != null){ hits_length if(hits_length > 0){ for (int i=start-1;i <= end-1;i++) { if((i < 0) || (i >= hits.length())){ continue; } Document ldoc=hits.doc(i); Enumeration lfields = ldoc.fields(); String lpath=ldoc.get("path"); String lurl=ldoc.get("url"); String ltitle=ldoc.get("title"); String mime_type=ldoc.get("mime-type"); if(lpath != null){ getPercent(hits.score(i))hits.score(i) lpath } else if(lurl != null){ i+1 while (lfields.hasMoreElements()) { Field lfield = (Field)lfields.nextElement(); String slfield = lfield.toString(); slfield lfield.name()slfield.substring(0, slfield.indexOf("<"))lfield.stringValue() } getPercent(hits.score(i))hits.score(i) String parent = ""; String filename = ""; String querystring = ""; if(lurl.lastIndexOf("/") > -1) { parent = lurl.substring(0,lurl.lastIndexOf("/")); filename = lurl.substring(lurl.lastIndexOf("/")+1); } if(lurl.indexOf("?") > -1) { querystring = lurl.substring(lurl.indexOf("?")); } parent filename querystring lurl File excerptFile=new File(excerptDir+File.separator+lurl); if((ltitle != null) && (ltitle.length() > 0)){ <xsp:expr>ltitle</xsp:expr> } else{ <xsp:expr>excerptFile.getName()</xsp:expr> } if((mime_type != null) && (mime_type.length() > 0)){ mime_type } else{ } //String[] words={"funds","bonds"}; try{ ReTokenizeFile rtf=new ReTokenizeFile(); rtf.setOffset(excerptOffset); String excerpt=rtf.getExcerpt(excerptFile,words); if(excerpt != null){ excerpt=rtf.emphasizeAsXML(rtf.tidy(excerpt),words); excerpt // FIXME: Update Cocoon!!! //org.apache.cocoon.components.language.markup.xsp.XSPUtil.includeString(excerpt,this.manager,this.contentHandler); /* this.contentHandler.startElement("","excerpt","excerpt",xspAttr); this.lexicalHandler.startCDATA(); this.characters(excerpt); this.lexicalHandler.endCDATA(); this.contentHandler.endElement("","excerpt","excerpt"); */ } else{ throw new Exception("excerpt == null. Maybe file does not contain the words!"); } } catch(FileNotFoundException e){ excerptFile.getAbsolutePath()+" "+words[0]+" "+e } catch(Exception e){ ""+e } } } } else{ } int number_of_pages=(hits.length()/hitsPerPage); if(number_of_pages*hitsPerPage != hits.length()){ number_of_pages=number_of_pages+1; } if(number_of_pages > maxPages){ number_of_pages=maxPages; } if(hits.length() == 0){ number_of_pages=0; } else{ for(int i=0;i<number_of_pages;i++){ int pstart=i*hitsPerPage+1; int pend=(i+1)*hitsPerPage; if(hits_length < pend){ pend=hits_length; } String type="other"; if(pstart == start){ type="current"; } else if(pstart == start-hitsPerPage){ type="previous"; } else if(pstart == start+hitsPerPage){ type="next"; } pstart pend type } } }