Index: index.html =================================================================== RCS file: /home/cvspublic/jakarta-lucene-sandbox/contributions/taglib/index.html,v retrieving revision 1.1 diff -u -r1.1 index.html --- index.html 21 May 2003 01:22:22 -0000 1.1 +++ index.html 28 May 2003 00:05:31 -0000 @@ -1,8 +1,114 @@ - - Hello World - - - Hello World - - + + Using Lucene Taglib + + + + +

Test page for Lucene-taglib

+ + + + + + + + + + + + + + + + + + + + + +
Ledgend
Required field  + + This page has been set up so that you can dynamically change the + parameters used by the taglib to produce different results. Hopefully + this tag library is abstract enough to address the majority of situations. + + The ledgend on the left shows which parameters are required and there + default values. If you set throwOnException to false the errors caused by + bad input parameter should not crash the page rather no results will be returned. + On the other hand setting it to true will show any errors encountered by the tag. + Some knowledge of the Lucene search engine is required to take advantage of the + the search tags advanced features such as changing the analyzer and using + fieldList's and flagList's. more documentation on each tag can be found in + the docs directory in the index.html file. + +
Default value 
String 
Object 
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
<LUCENE:Search id="rs"  
throwOnException  
collection"comma separated directory/path(s)"
criteria"String" 
startRow"0" 
maxRows"50" 
analyzerType + +   
search"contents" 
stopWords"comma,separated,list" 
fieldList"comma,separated,list" 
flagList"NORMAL,PROHIBITED,REQUIRED"> 
<%= rs.loopCount %>
int value of the current interation of the search tag
<%= rs.rowCount %>
int value of the current result in hitCount
<%= rs.hitCount %>
int value representing the total number of results
<%= rs.pageCount %>
int value of the offset between the maxrows and the total number of hits
<%= rs.firstPage %>
variable containing the query string needed by a navigation element to return you to the first page of the result
<%= rs.nextPage %>
variable containing the query string needed by a navigation element to move to the next page of the result
<%= rs.previousPage %>
variable containing the query string needed by a navigation element to return to the previous page of the result
<%= rs.lastPage %>
variable containing the query string needed by a navigation element to move to the last page of the result
<%= rs.score %>
returns a string representation of the float value stored in every lucene index as the value score.
if you want to translate it into something useful to a user you will need to create +
new Float(tagid.getField("score")).floatValue() and then you can use your own conversion method
+ as this will return you the original float value that was stored in the index.
<%= rs.pageList %>
returns a LinkedList of query strings needed to produce links to the number of page represented
by the offset of the current page and the maxrows being displayed on each page.
<%= rs.getField(String name) %> used by the Field tag
used to retreive the named field from the index for that iteration of the loop.
If no such name exists in the index such as author the method will return an empty string.
<%= rs.getFields() %> used by the Column tag
used to retrieve a Set view of all of the fields contained in the index for each iteration of the loop,
most generally would be used to write your table headers when the field names are not known until runtime
 <LUCENE:Column id="col" 
 runOnce 
 throwOnException> 
 <%= col.columnCount %> int value of the current iteration of the column tag
 <%= col.columnName %> String value of the field name at columnCount
  <LUCENE:Field id="fld"
  name"<%= col.columnName %>"
  throwOnException>
  <%= fld.value %> String value of the columnName
  </LUCENE:Field> 
 </LUCENE:Column>  
</LUCENE:Search>   
+
+ + \ No newline at end of file Index: index.jsp =================================================================== RCS file: /home/cvspublic/jakarta-lucene-sandbox/contributions/taglib/index.jsp,v retrieving revision 1.2 diff -u -r1.2 index.jsp --- index.jsp 21 May 2003 01:22:22 -0000 1.2 +++ index.jsp 28 May 2003 00:05:31 -0000 @@ -1,21 +1,3 @@ - -<% -response.setHeader("Pragma", "No-cache"); -response.setDateHeader("Expires", 0); -response.setHeader("Cache-Control", "no-cache"); -response.setHeader("Cache-Control", "private"); -%> -<%@include file="header.jsp"%> -<% /* Author: Andrew C. Oliver (acoliver2@users.sourceforge.net) */ %> -
-
-

-  Search Criteria -

-

-  Results Per Page  - -

-
-
-<%@include file="footer.jsp"%> \ No newline at end of file + \ No newline at end of file Index: readme.txt =================================================================== RCS file: /home/cvspublic/jakarta-lucene-sandbox/contributions/taglib/readme.txt,v retrieving revision 1.1 diff -u -r1.1 readme.txt --- readme.txt 12 May 2003 14:02:35 -0000 1.1 +++ readme.txt 28 May 2003 00:05:31 -0000 @@ -1,33 +1,71 @@ -INTRODUCTION -The lucene-taglib project provides a tag library for searching -a lucene-index. - -INSTRUCTIONS -1. download this project and create a context in your favorite - servelt container called lucene-taglib. -2. copy the file under the taglib directory into your new context. -3. open result.jsp and locate the collection attribute of the - tag. -4. change the collection attribute to point to a lucene-index - that you created using the system appropriate path. -5. open index.jsp in your browser and enter search criteria - and click the submit button. - -DOCUMENTATION: -you will find documentation and an over view of the tag library in -the docs folder of this project and the javadocs in the api folder -respectivley. - -BUGS: -I tried to create a .war file for this project but I am having trouble -getting it to deploy properly. - -PLANNED: -I am planning to document the result.jsp file line for line to explain -how to display a search result when you do not know what the names of -the search fields stored in the lucene-index. That is the way the result -page is currently written. - -Time permitting I want to write a couple of child tags for telling the search -tag that there are multiple index to be searched and to let it do the other -types of searches such as fuzzy and range queries. \ No newline at end of file +INTRODUCTION +The lucene-taglib project provides a tag library for searching +a lucene-index. + +INSTRUCTIONS +1. download this project and create a context in your favorite + servelt container called lucene-taglib. +2. copy the file under the taglib directory into your new context. +3. open result.jsp and locate the collection attribute of the + tag. +4. change the collection attribute to point to a lucene-index + that you created using the system appropriate path. +5. open index.jsp in your browser and enter search criteria + and click the submit button. + +DOCUMENTATION: +you will find documentation and an over view of the tag library in +the docs folder of this project and the javadocs in the api folder +respectively. + +BUGS: +I tried to create a .war file for this project but I am having trouble +getting it to deploy properly. + +More like a heads up than a bug I discovered that if you have tag pooling \ +turned on in Tomcat that values passed to a custom tag will not change in +the event that an exception is thrown. Catching the exception inside of +the tag or page does not help, to update the values at this point you +have to turn off tag pooling. Follow the instructions below to do this +in Tomcat. + +---------------------------------------------------------------------------- + + From: Bill Barker + Subject: Re: HELP:Tagpool sharing problems + Date: Mon, 28 Apr 2003 22:20:20 -0700 + +---------------------------------------------------------------------------- + +In $CATALINA_HOME/conf/web.xml, locate the jsp +servlet, and add: + + enablePooling + false + + +This will turn off tag-pooling. You'll also need to clear out +$CATALINA_HOME/work so that the JSP pages get re-compiled. + +If you just want it turned off for one context, then you can place the +definition of the jsp servlet in your own web.xml. + +If you are using: + + myJspPage + myJspPage.jsp + +then you also need to add the enablePooling init-param to your servlet's +definition. + + +HISTORY: +1. Added more robust error handling and the ability to turn it on and + off with a throwOnException attribute. (All tags) +2. Added a Column tag for outputting the field names found in a + Lucene index. +3. Added a Field tag for retrieving a value for a field in a search + result either produced by the Column tag or known in advance. +4. Added new example pages to illustrate how to use the new tags +5. The Collection tag has been deprecated, use the collection attribute + of the Search tag instead. \ No newline at end of file Index: result.jsp =================================================================== RCS file: /home/cvspublic/jakarta-lucene-sandbox/contributions/taglib/result.jsp,v retrieving revision 1.2 diff -u -r1.2 result.jsp --- result.jsp 21 May 2003 01:22:22 -0000 1.2 +++ result.jsp 28 May 2003 00:05:31 -0000 @@ -1,64 +1,139 @@ - -<%@ taglib uri="/WEB-INF/lucene-taglib.tld" prefix="JSP"%> -<%@ include file="header.jsp"%> -<%@ page import="java.util.*"%> - -<% - String startRow = "0"; - String maxRows = "10"; - String query = request.getParameter("query"); - try{ - startRow = request.getParameter("startRow"); - maxRows = request.getParameter("maxRows"); - } - catch(Exception e){ - } -%> - - - - - <% - - Set allFields = rs.getFields(); - int fieldSize = allFields.size(); - Iterator fieldIter = allFields.iterator(); - - while(fieldIter.hasNext()){ - String nextField = (String) fieldIter.next(); - if(!nextField.equalsIgnoreCase("summary")){ - %> - - <% - }else{ - %> - - - <% - } - } - - %> - -<% - int count = 0; - try{ - count = new Integer(rs.hitCount).intValue(); - }catch(Exception e){ - out.print(e); - } - if(count <= 0){ -%> - - - -<% - } -%> - - - -
<%= nextField %><%= rs.getField(nextField) %>
<%= rs.hitCount %>|<%= nextField %>
<%= rs.getField(nextField) %>
No results have been found
<%= rs.hitCount %>
- - -<%@include file="footer.jsp"%> + +<%@ taglib uri="/WEB-INF/lucene-taglib.tld" prefix="LUCENE"%> +<%@ page import="java.util.*"%> + + + Using Lucene Taglib + + + +<% + String startRow = ""; + String maxRows = ""; + String criteria = ""; + int colCount = 0; + String analyzerType = "STANDARD_ANALYZER"; + String search = "contents"; + String stopWords=""; + String fieldList=""; + String flagList=""; + String collection = "E:/search/lucene/index,E:/opt/lucene/index"; + String throwOnException = "false"; + String fieldthrowOnException = "false"; + String columnthrowOnException = "false"; + String runOnce = "false"; + String query = ""; + + try{ + query = request.getQueryString(); + criteria = request.getParameter("criteria"); + startRow = request.getParameter("startRow"); + maxRows = request.getParameter("maxRows"); + analyzerType = request.getParameter("analyzerType"); + search = request.getParameter("search"); + stopWords = request.getParameter("stopWords"); + fieldList = request.getParameter("fieldList"); + flagList = request.getParameter("flagList"); + throwOnException = request.getParameter("throwOnException"); + fieldthrowOnException = request.getParameter("fieldthrowOnException"); + columnthrowOnException = request.getParameter("columnthrowOnException"); + runOnce = request.getParameter("runOnce"); + } + catch(Exception e){ + } +%> +

Search results for "<%= criteria %>"

+ + + + + + <% colCount = header.columnCount; %> + + + + + + + + + + +<% + if(rs.hitCount <= 0){ +%> + + + +<% + } +%> + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
<%= header.columnName %>
[<%= rs.loopCount %>][<%= rs.rowCount %>]  + + + <% if(col.columnName.equalsIgnoreCase("url")){ %> + + <% } %> + + <%= fld.value %> + + <% if(col.columnName.equalsIgnoreCase("url")){ %> + + <% } %> + + +
No results have been found
hitCount<%= rs.hitCount %>
pageCount<%= rs.pageCount %>
firstPage<%= rs.firstPage %>
nextPage<%= rs.nextPage %>
previousPage<%= rs.previousPage %>
lastPage<%= rs.lastPage %>
+ +
+ + \ No newline at end of file Index: WEB-INF/classes/com/netwebapps/taglib/search/CollectionTag.java =================================================================== RCS file: WEB-INF/classes/com/netwebapps/taglib/search/CollectionTag.java diff -N WEB-INF/classes/com/netwebapps/taglib/search/CollectionTag.java --- WEB-INF/classes/com/netwebapps/taglib/search/CollectionTag.java 21 May 2003 01:22:22 -0000 1.1 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,49 +0,0 @@ -/* - * Created on May 18, 2003 - * - */ -package com.netwebapps.taglib.search; - -import java.lang.reflect.Method; - -import javax.servlet.jsp.JspException; -import javax.servlet.jsp.tagext.TagSupport; -; - -/** - * @author blaplante - * - */ -public class CollectionTag extends TagSupport{ - String directory = ""; - /* (non-Javadoc) - * @see javax.servlet.jsp.tagext.Tag#doStartTag() - */ - public int doStartTag() throws JspException { - Object parent = getParent(); - if(parent != null){ - try{ - Method call = parent.getClass().getMethod("addCollection", new Class[] {Class.forName("java.lang.String")}); - call.invoke(parent, new String[] {directory}); - } - catch(Exception e){ - throw new JspException("An error occured while trying to add a new collection path: " + e.getCause()); - } - } - return SKIP_BODY; - } - - /* (non-Javadoc) - * @see javax.servlet.jsp.tagext.Tag#release() - */ - public void release() { - directory = null; - } - /** - * @param string - */ - public void setDirectory(String dir) { - this.directory = dir; - } - -} Index: WEB-INF/classes/com/netwebapps/taglib/search/ColumnTag.java =================================================================== RCS file: WEB-INF/classes/com/netwebapps/taglib/search/ColumnTag.java diff -N WEB-INF/classes/com/netwebapps/taglib/search/ColumnTag.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ WEB-INF/classes/com/netwebapps/taglib/search/ColumnTag.java 28 May 2003 00:05:32 -0000 @@ -0,0 +1,192 @@ +/* + * Created on May 24, 2003 + */ +package com.netwebapps.taglib.search; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Set; + +import javax.servlet.jsp.JspException; +import javax.servlet.jsp.PageContext; +import javax.servlet.jsp.tagext.BodyTagSupport; + +/** + * @company Network Web Application + * @url http://www.netwebapps.com + * @author Bryan LaPlante + */ +public class ColumnTag extends BodyTagSupport{ + + private Object parent = null; + private Set fieldSet = null; + private ArrayList fieldArray = new ArrayList(); + private Iterator fieldNames = null; + private Iterator nextField = null; + private Method getFieldsMethod = null; + private boolean abort = false; + public boolean throwOnException = false; + public String columnName = ""; + public boolean runOnce = false; + public int columnCount = 0; + + public int doStartTag() throws JspException{ + parent = findAncestorWithClass(this,com.netwebapps.taglib.search.SearchTag.class); + if(runOnce && getLoopCount() > 1){ + abort = true; + return SKIP_BODY; + } + try { + getFieldsMethod = (Method) parent.getClass().getMethod("getFields",null); + fieldSet = (Set) getFieldsMethod.invoke(parent, null); + } catch (SecurityException e) { + if(throwOnException){ + throw new JspException("A security violation occurred: " + e); + } + abort = true; + } catch (IllegalArgumentException e) { + if(throwOnException){ + throw new JspException("IllegalArgumentException: " + e); + } + abort = true; + } catch (NoSuchMethodException e) { + if(throwOnException){ + throw new JspException("Unable to declair the getField method : " + e); + } + abort = true; + } catch (IllegalAccessException e) { + if(throwOnException){ + throw new JspException("Access denied: " + e); + } + abort = true; + } catch (InvocationTargetException e) { + if(throwOnException){ + throw new JspException("This tag must be nested in a Search tag in order to work: " + e); + } + abort = true; + }catch(NullPointerException e){ + if(throwOnException){ + throw new JspException(e); + } + abort = true; + } + + if(abort){ + return SKIP_BODY; + } + + if(fieldSet != null){ + nextField = fieldSet.iterator(); + while(nextField.hasNext()){ + fieldArray.add(nextField.next()); + } + columnCount = fieldSet.size(); + pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE); + return EVAL_BODY_AGAIN; + } + + return SKIP_BODY; + } + + public void doInitBody() throws JspException{ + if(!abort){ + if (fieldArray.size() > 0) { + fieldNames = fieldArray.iterator(); + if(fieldNames.hasNext()){ + columnName = (String) fieldNames.next(); + columnCount = fieldSet.size(); + pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE); + } + } + } + } + + public int doAfterBody() throws JspException{ + if(abort){ + return SKIP_BODY; + } + columnName = ""; + + try{ + getBodyContent().writeOut(getPreviousOut()); + getBodyContent().clearBody(); + } + catch(IOException e){ + throw new JspException(e.toString()); + } + if(fieldNames != null){ + if(fieldNames.hasNext()){ + columnName = (String) fieldNames.next(); + columnCount = fieldSet.size(); + pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE); + return EVAL_BODY_AGAIN; + } + } + return SKIP_BODY; + } + + public void release(){ + parent = null; + fieldSet = null; + fieldArray = null; + fieldNames = null; + nextField = null; + getFieldsMethod = null; + } + + private int getLoopCount() throws JspException{ + Field getLoopCountMember = null; + int rc = 0; + try { + getLoopCountMember = (Field) parent.getClass().getField("loopCount"); + rc = new Integer(getLoopCountMember.get(parent).toString()).intValue(); + } catch (SecurityException e) { + if(throwOnException){ + throw new JspException("A security violation occurred: " + e); + } + } catch (NoSuchFieldException e) { + if(throwOnException){ + throw new JspException("Unable to find the loopCount field : " + e); + } + }catch(IllegalAccessException e){ + if(throwOnException){ + throw new JspException("Access denied: " + e); + } + }catch(IllegalArgumentException e){ + if(throwOnException){ + throw new JspException("Bad argument: " + e); + } + } + return rc; + } + + /** + * @param string + */ + public void setcolumnName(String columnName) { + this.columnName = columnName; + } + + /** + * @param b + */ + public void setThrowOnException(String b) { + throwOnException = new Boolean(b).booleanValue(); + } + public void setThrowOnException(boolean b) { + throwOnException = b; + } + + public void setRunOnce(boolean b){ + runOnce = b; + } + + public void setRunOnce(String b){ + runOnce = new Boolean(b).booleanValue(); + } + +} Index: WEB-INF/classes/com/netwebapps/taglib/search/ColumnTagTei.java =================================================================== RCS file: WEB-INF/classes/com/netwebapps/taglib/search/ColumnTagTei.java diff -N WEB-INF/classes/com/netwebapps/taglib/search/ColumnTagTei.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ WEB-INF/classes/com/netwebapps/taglib/search/ColumnTagTei.java 28 May 2003 00:05:32 -0000 @@ -0,0 +1,29 @@ +/* + * Created on May 24, 2003 + */ +package com.netwebapps.taglib.search; + +/** + * @company Network Web Application + * @url http://www.netwebapps.com + * @author Bryan LaPlante + */ +import javax.servlet.jsp.tagext.*; + +public class ColumnTagTei extends TagExtraInfo +{ + + public ColumnTagTei(){ + } + /* + * VariableInfo is provided by the servlet container and allows the + * FieldTag class to output it's tag variables to the PageContext at runtime + * @see javax.servlet.jsp.tagext.TagExtraInfo#getVariableInfo(javax.servlet.jsp.tagext.TagData) + */ + public VariableInfo[] getVariableInfo(TagData tagdata) + { + VariableInfo avariableinfo[] = new VariableInfo[1]; + avariableinfo[0] = new VariableInfo(tagdata.getId(),"com.netwebapps.taglib.search.ColumnTag", true, VariableInfo.NESTED); + return avariableinfo; + } +} \ No newline at end of file Index: WEB-INF/classes/com/netwebapps/taglib/search/FieldTag.java =================================================================== RCS file: WEB-INF/classes/com/netwebapps/taglib/search/FieldTag.java diff -N WEB-INF/classes/com/netwebapps/taglib/search/FieldTag.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ WEB-INF/classes/com/netwebapps/taglib/search/FieldTag.java 28 May 2003 00:05:32 -0000 @@ -0,0 +1,96 @@ +/* + * Created on May 23, 2003 + * + */ +package com.netwebapps.taglib.search; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import javax.servlet.jsp.JspException; +import javax.servlet.jsp.PageContext; +import javax.servlet.jsp.tagext.TagSupport; + +/** + * @company Network Web Application + * @url http://www.netwebapps.com + * @author Bryan LaPlante + */ +public class FieldTag extends TagSupport{ + + public String name = ""; + public boolean throwOnException = false; + public String value = ""; + private boolean abort = false; + + /* (non-Javadoc) + * @see javax.servlet.jsp.tagext.BodyTagSupport#doEndTag() + */ + public int doStartTag() throws JspException { + Object parent = findAncestorWithClass(this,com.netwebapps.taglib.search.SearchTag.class); + try { + Method getFieldMethod = parent.getClass().getMethod("getField", new Class[] {Class.forName("java.lang.String")}); + value = getFieldMethod.invoke(parent, new String[] {name}).toString(); + } catch (SecurityException e) { + if(throwOnException){ + throw new JspException("A security violation occurred: " + e); + } + abort = true; + } catch (NoSuchMethodException e) { + if(throwOnException){ + throw new JspException("Unable to declair the getField method : " + e); + } + abort = true; + } catch (ClassNotFoundException e) { + if(throwOnException){ + throw new JspException("ClassNotFoundException: " + e); + } + }catch (IllegalAccessException e) { + if(throwOnException){ + throw new JspException("Access denied: " + e); + } + abort = true; + }catch (InvocationTargetException e) { + if(throwOnException){ + throw new JspException("This tag must be nested in a Search tag in order to work: " + e); + } + abort = true; + }catch(NullPointerException e){ + if(throwOnException){ + throw new JspException("This tag must be nested in a Search tag in order to work: " + e); + } + abort = true; + } + + if(abort){ + pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE); + return SKIP_BODY; + } + pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE); + return EVAL_BODY_INCLUDE; + } + + public void release(){ + name = ""; + throwOnException = false; + value = ""; + } + + /** + * @param string + */ + public void setName(String string) { + name = string; + } + + /** + * @param b + */ + public void setThrowOnException(String b) { + throwOnException = new Boolean(b).booleanValue(); + } + public void setThrowOnException(boolean b) { + throwOnException = b; + } + +} Index: WEB-INF/classes/com/netwebapps/taglib/search/FieldTagTei.java =================================================================== RCS file: WEB-INF/classes/com/netwebapps/taglib/search/FieldTagTei.java diff -N WEB-INF/classes/com/netwebapps/taglib/search/FieldTagTei.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ WEB-INF/classes/com/netwebapps/taglib/search/FieldTagTei.java 28 May 2003 00:05:32 -0000 @@ -0,0 +1,29 @@ +/* + * Created on May 24, 2003 + */ +package com.netwebapps.taglib.search; + +/** + * @company Network Web Application + * @url http://www.netwebapps.com + * @author Bryan LaPlante + */ +import javax.servlet.jsp.tagext.*; + +public class FieldTagTei extends TagExtraInfo +{ + + public FieldTagTei(){ + } + /* + * VariableInfo is provided by the servlet container and allows the + * FieldTag class to output it's tag variables to the PageContext at runtime + * @see javax.servlet.jsp.tagext.TagExtraInfo#getVariableInfo(javax.servlet.jsp.tagext.TagData) + */ + public VariableInfo[] getVariableInfo(TagData tagdata) + { + VariableInfo avariableinfo[] = new VariableInfo[1]; + avariableinfo[0] = new VariableInfo(tagdata.getId(),"com.netwebapps.taglib.search.FieldTag", true, VariableInfo.NESTED); + return avariableinfo; + } +} \ No newline at end of file Index: WEB-INF/classes/com/netwebapps/taglib/search/SearchTag.java =================================================================== RCS file: /home/cvspublic/jakarta-lucene-sandbox/contributions/taglib/WEB-INF/classes/com/netwebapps/taglib/search/SearchTag.java,v retrieving revision 1.2 diff -u -r1.2 SearchTag.java --- WEB-INF/classes/com/netwebapps/taglib/search/SearchTag.java 21 May 2003 01:22:22 -0000 1.2 +++ WEB-INF/classes/com/netwebapps/taglib/search/SearchTag.java 28 May 2003 00:05:32 -0000 @@ -1,281 +1,499 @@ -package com.netwebapps.taglib.search; - -import java.util.*; -import javax.servlet.jsp.*; -import javax.servlet.jsp.tagext.*; -import javax.servlet.http.*; -import java.io.*; - -import org.apache.lucene.analysis.*; -import org.apache.lucene.document.*; -import org.apache.lucene.index.*; -import org.apache.lucene.search.*; -import org.apache.lucene.queryParser.*; - -/* - * - * @author Bryan LaPlante - * @param - * - */ -public class SearchTag extends BodyTagSupport{ - - private HashMap hitMap = null; - private ArrayList hitArray = null; - private String collection = ""; - private IndexSearcher searcher = null; - private Query query = null; - private Hits hits = null; - private int thispage = 0; - private String criteria = ""; - private Iterator searchItr = null; - private Enumeration fields = null; - private HashMap aField = new HashMap(); - private int ROWCOUNT = 0; - private int PAGECOUNT = 1; - private int HITCOUNT = 0; - private boolean abort = false; - private Analyzer analyzer = null; - - public int startRow = 0; - public int maxRows = 50; - public String rowCount = "0"; - public String pageCount = "1"; - public String hitCount = "0"; - public String firstPage = ""; - public String nextPage = ""; - public String previousPage = ""; - public String lastPage = ""; - public LinkedList pageList = new LinkedList(); - public boolean throwOnException = false; - - - public int doStartTag() throws JspException{ - - doSearch(); - if(abort){ - rowCount = new Integer(startRow + ROWCOUNT).toString(); - pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE); - return SKIP_BODY; - } - searchItr = hitArray.iterator(); - if(searchItr.hasNext()){ - aField = (HashMap) searchItr.next(); - rowCount = new Integer(startRow + ROWCOUNT++).toString(); - pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE); - return EVAL_BODY_AGAIN; - } - return SKIP_BODY; - } - - public void doInitBody() throws JspException{ - if(!abort){ - doSearch(); - searchItr = hitArray.iterator(); - if(searchItr.hasNext()){ - aField = (HashMap) searchItr.next(); - rowCount = new Integer(startRow + ROWCOUNT).toString(); - pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE); - } - } - } - - public int doAfterBody() throws JspException{ - - if(abort){ - rowCount = new Integer(startRow + ROWCOUNT).toString(); - pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE); - return SKIP_BODY; - } - - try{ - getBodyContent().writeOut(getPreviousOut()); - getBodyContent().clearBody(); - } - catch(IOException e){ - throw new JspException(e.toString()); - } - - if(searchItr.hasNext()){ - aField = (HashMap) searchItr.next(); - rowCount = new Integer(startRow + ROWCOUNT++).toString(); - pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE); - return EVAL_BODY_AGAIN; - } - return SKIP_BODY; - } - - public int doEndTag() throws JspException{ - - if(abort){ - return EVAL_PAGE; - } - - try{ - HttpServletRequest req = (HttpServletRequest) pageContext.getRequest(); - String relativePath = req.getRequestURI(); - firstPage = relativePath + "?startRow=0&maxRows=" + maxRows; - nextPage = relativePath + "?startRow=" + ((startRow + maxRows <= HITCOUNT)? startRow + maxRows : startRow) + "&maxRows=" + maxRows; - previousPage = relativePath + "?startRow=" + ((startRow - maxRows >=0)? startRow - maxRows : 0 ) + "&maxRows=" + maxRows; - lastPage = relativePath + "?startRow=" + (((HITCOUNT - maxRows) >= 0)? HITCOUNT - maxRows : 0) + "&maxRows=" + maxRows; - if(HITCOUNT > 0){ - pageList = new LinkedList(); - for(int i=0; i < (HITCOUNT / maxRows); i++){ - String tempURL = relativePath + "?startRow=" + (maxRows * i) + "&maxRows=" + maxRows; - pageList.add(tempURL); - } - } - } - catch(Exception e){ - throw new JspException("A problem occured durring doEndTag: " + e.toString()); - } - - pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE); - return EVAL_PAGE; - } - - public void release(){ - } - - public String getField(String name){ - if(aField != null){ - if(aField.containsKey(name)){ - return aField.get((String) name).toString(); - } - } - return ""; - } - - public Set getFields(){ - return aField.keySet(); - } - - public void doSearch() throws JspException{ - - try { - searcher = new IndexSearcher(IndexReader.open(collection)); - } catch (IOException e) { - if(throwOnException){ - throw new JspException("IndexSearcher(IndexReader.open(collection)): " + e); - } - abort = true; - } - if(!abort){ - analyzer = new StopAnalyzer(); - - try { - query = QueryParser.parse(criteria, "contents", analyzer); - } catch (ParseException e) { - if(throwOnException){ - throw new JspException("QueryParser.parse(criteria,contents,analyzer): " + e); - } - abort = true; - } - if(!abort){ - try { - hits = searcher.search(query); - } catch (IOException e) { - if(throwOnException){ - throw new JspException("searcher.search(query): " + e); - } - abort = true; - } - - if(!abort){ - hitCount = new Integer(hits.length()).toString(); - HITCOUNT = hits.length(); - PAGECOUNT = PAGECOUNT = (int) (( (double) startRow) / maxRows ); - pageCount = new Integer(PAGECOUNT).toString(); - thispage = maxRows; - if ((startRow + maxRows) > hits.length()) { - thispage = hits.length() - startRow; - } - hitArray = new ArrayList(); - for (int i = startRow; i < (thispage + startRow); i++) { - hitMap = new HashMap(); - Document doc = null; - try { - doc = hits.doc(i); - } catch (IOException e) { - if(throwOnException){ - throw new JspException("hits.doc(i) : " + e); - } - abort = true; - } - if(!abort){ - try { - hitMap.put("score",new Float(hits.score(i)).toString()); - } catch (IOException e) { - if(throwOnException){ - throw new JspException("hitMap.put(score,new Float(hits.score(i)).toString()); : " + e); - } - abort = true; - } - if(!abort){ - fields = doc.fields(); - while(fields.hasMoreElements()){ - Field field = (Field) fields.nextElement(); - String fieldName = field.name(); - hitMap.put(fieldName,doc.get(fieldName)); - } - hitArray.add(hitMap); - } - } - } - } - } - } - } - - /* setters */ - - - - public void setCriteria(String criteria){ - this.criteria = criteria; - } - - public void setStartRow(String startRow){ - try{ - this.startRow = Integer.parseInt(startRow); - } - catch(Exception e){ - this.startRow = 0; - } - } - - public void setStartRow(int startRow){ - this.startRow = startRow; - } - - public void setMaxRows(String maxRows){ - try{ - this.maxRows = Integer.parseInt(maxRows); - } - catch(Exception e){ - this.maxRows = 10; - } - } - - public void setMaxRows(int maxRows){ - this.maxRows = maxRows; - } - - public void setCollection(String collection){ - this.collection = collection; - } - - public void setThrowOnException(String bool){ - this.throwOnException = new Boolean(bool).booleanValue(); - } - - /* getters */ - - public int getStartRow(){ - return startRow; - } - - public int getMaxRows(){ - return maxRows; - } -} +package com.netwebapps.taglib.search; + +import java.io.File; +import java.io.IOException; +import java.util.*; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.jsp.JspException; +import javax.servlet.jsp.PageContext; +import javax.servlet.jsp.tagext.BodyTagSupport; + +import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.analysis.SimpleAnalyzer; +import org.apache.lucene.analysis.StopAnalyzer; +import org.apache.lucene.analysis.WhitespaceAnalyzer; +import org.apache.lucene.analysis.de.GermanAnalyzer; +import org.apache.lucene.analysis.de.WordlistLoader; +import org.apache.lucene.analysis.standard.StandardAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.queryParser.ParseException; +import org.apache.lucene.queryParser.QueryParser; +import org.apache.lucene.queryParser.MultiFieldQueryParser; +import org.apache.lucene.search.Hits; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.MultiSearcher; +import org.apache.lucene.search.Query; + +/* + * + * @company Network Web Application + * @url http://www.netwebapps.com + * @author Bryan LaPlante + * + */ +public class SearchTag extends BodyTagSupport{ + + private HashMap hitMap = null; + private ArrayList hitArray = null; + private String collection = ""; + private IndexSearcher searcher = null; + private Query query = null; + private Hits hits = null; + private int thispage = 0; + private String criteria = ""; + private Iterator searchItr = null; + private Enumeration fields = null; + private HashMap aField = new HashMap(); + private int ROWCOUNT = 0; + private int PAGECOUNT = 0; + private int HITCOUNT = 0; + private boolean abort = false; + private Analyzer analyzer = null; + private Document doc = null; + private ArrayList idxArray = new ArrayList(); + private MultiSearcher msearcher = null; + private final int GERMANAN_ALYZER = 0; + private final int SIMPLE_ANALYZER = 1; + private final int STANDARD_ANALYZER = 2; + private final int STOP_ANALYZER = 3; + private final int WHITESPACE_ANALYZER = 4; + + public int startRow = 0; + public int maxRows = 50; + public int rowCount = 0; + public int pageCount = 1; + public int hitCount = 0; + public int loopCount = 0; + public String firstPage = ""; + public String nextPage = ""; + public String previousPage = ""; + public String lastPage = ""; + public LinkedList pageList = new LinkedList(); + public boolean throwOnException = false; + public String[] stopWords = new String[0]; + public String[] fieldList = new String[0]; + public int[] flagList = new int[0]; + public String search = "contents"; + public int analyzerType = STANDARD_ANALYZER; + + + public int doStartTag() throws JspException{ + rowCount = startRow + ROWCOUNT++; + loopCount++; + pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE); + return EVAL_BODY_AGAIN; + } + + public void doInitBody() throws JspException{ + doSearch(); + if(!abort){ + searchItr = hitArray.iterator(); + if(searchItr.hasNext()){ + aField = (HashMap) searchItr.next(); + rowCount = startRow + ROWCOUNT++; + pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE); + } + } + } + + public int doAfterBody() throws JspException{ + + if(abort){ + hitCount = 0; + loopCount = 0; + rowCount = startRow + ROWCOUNT; + pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE); + return SKIP_BODY; + } + + try{ + getBodyContent().writeOut(getPreviousOut()); + getBodyContent().clearBody(); + } + catch(IOException e){ + throw new JspException(e.toString()); + } + + if(searchItr.hasNext()){ + aField = (HashMap) searchItr.next(); + rowCount = startRow + ROWCOUNT++; + loopCount++; + pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE); + return EVAL_BODY_AGAIN; + } + return SKIP_BODY; + } + + public int doEndTag() throws JspException{ + + if(abort){ + hitCount = 0; + pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE); + return EVAL_PAGE; + } + + try{ + HttpServletRequest req = (HttpServletRequest) pageContext.getRequest(); + String relativePath = req.getRequestURI(); + firstPage = relativePath + "?startRow=0&maxRows=" + maxRows; + nextPage = relativePath + "?startRow=" + ((startRow + maxRows <= HITCOUNT)? startRow + maxRows : startRow) + "&maxRows=" + maxRows; + previousPage = relativePath + "?startRow=" + ((startRow - maxRows >=0)? startRow - maxRows : 0 ) + "&maxRows=" + maxRows; + lastPage = relativePath + "?startRow=" + (((HITCOUNT - maxRows) >= 0)? HITCOUNT - maxRows : 0) + "&maxRows=" + maxRows; + if(HITCOUNT > 0){ + pageList = new LinkedList(); + for(int i=0; i < (HITCOUNT / maxRows); i++){ + String tempURL = relativePath + "?startRow=" + (maxRows * i) + "&maxRows=" + maxRows; + pageList.add(tempURL); + } + } + } + catch(Exception e){ + throw new JspException("A problem occured durring doEndTag: " + e.toString()); + } + + pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE); + return EVAL_PAGE; + } + + public void release(){ + hitMap = null; + hitArray = null; + collection = ""; + searcher = null; + query = null; + hits = null; + thispage = 0; + criteria = ""; + searchItr = null; + fields = null; + aField = new HashMap(); + ROWCOUNT = 0; + PAGECOUNT = 1; + HITCOUNT = 0; + abort = false; + analyzer = null; + doc = null; + idxArray = null; + msearcher = null; + } + + public String getField(String name){ + if(aField != null){ + if(aField.containsKey(name)){ + return aField.get((String) name).toString(); + } + } + return ""; + } + + public Set getFields(){ + return aField.keySet(); + } + + + public void addCollection(String name) throws JspException{ + try { + searcher = new IndexSearcher(IndexReader.open(name)); + idxArray.add(searcher); + } catch (IOException e) { + if(throwOnException){ + throw new JspException("Error occured while opening " + name + " ]: " + e); + } + } + } + + public void doSearch() throws JspException{ + + try { + if(idxArray.size() > 0){ + IndexSearcher[] idxToArray = new IndexSearcher[idxArray.size()]; + Iterator idxIter = idxArray.iterator(); + int arrayCount = 0; + while(idxIter.hasNext()){ + idxToArray[arrayCount++] = (IndexSearcher) idxIter.next(); + } + msearcher = new MultiSearcher(idxToArray); + }else{ + throw new JspException("No collection has been specified"); + } + } catch (IOException e) { + if(throwOnException){ + throw new JspException("IndexSearcher(IndexReader.open(collection)): " + e); + } + abort = true; + } + if(!abort){ + // choosing the type of analyzer to use in this search + switch (analyzerType) { + case GERMANAN_ALYZER: + if(stopWords.length > 0){ + analyzer = new GermanAnalyzer(stopWords); + }else{ + if(throwOnException){ + throw new JspException("In order to use a GermanAnalyzer you must provide a list of stop words"); + } + abort = true; + } + break; + case SIMPLE_ANALYZER: + analyzer = new SimpleAnalyzer(); + break; + case STANDARD_ANALYZER: + if(stopWords.length > 0){ + analyzer = new StandardAnalyzer(stopWords); + }else{ + analyzer = new StandardAnalyzer(); + } + break; + case STOP_ANALYZER: + if(stopWords.length > 0){ + analyzer = new StopAnalyzer(stopWords); + }else{ + analyzer = new StopAnalyzer(); + } + break; + case WHITESPACE_ANALYZER: + analyzer = new WhitespaceAnalyzer(); + break; + + default : + if(stopWords.length > 0){ + analyzer = new StandardAnalyzer(stopWords); + }else{ + analyzer = new StandardAnalyzer(); + } + break; + } + + try { + // choose a query parser + if(fieldList.length > 0){ + if(flagList.length > 0){ + query = MultiFieldQueryParser.parse(criteria,fieldList,flagList,analyzer); + }else{ + query = MultiFieldQueryParser.parse(criteria,fieldList,analyzer); + } + }else{ + query = QueryParser.parse(criteria, search, analyzer); + } + } catch (ParseException e) { + if(throwOnException){ + throw new JspException("If using fieldList and or flagList check to see you have the same number of items in each: " + e); + } + abort = true; + } + if(!abort){ + try { + hits = msearcher.search(query); + } catch (IOException e) { + if(throwOnException){ + throw new JspException("msearcher.search(query): " + e); + } + abort = true; + } + + if(!abort){ + hitCount = hits.length(); + HITCOUNT = hits.length(); + PAGECOUNT = (int) (( (double) startRow) / maxRows ); + pageCount = PAGECOUNT; + thispage = maxRows; + if ((startRow + maxRows) > hits.length()) { + thispage = hits.length() - startRow; + } + hitArray = new ArrayList(); + for (int i = startRow; i < (thispage + startRow); i++) { + hitMap = new HashMap(); + try { + doc = hits.doc(i); + } catch (IOException e) { + if(throwOnException){ + throw new JspException("hits.doc(i) : " + e); + } + abort = true; + } + if(!abort){ + try { + hitMap.put("score",new Float(hits.score(i)).toString()); + } catch (IOException e) { + if(throwOnException){ + throw new JspException("hitMap.put(score,new Float(hits.score(i)).toString()); : " + e); + } + abort = true; + } + if(!abort){ + fields = doc.fields(); + while(fields.hasMoreElements()){ + Field field = (Field) fields.nextElement(); + String fieldName = field.name(); + hitMap.put(fieldName,doc.get(fieldName)); + } + hitArray.add(hitMap); + } + } + } + } + } + } + if(msearcher != null){ + try { + msearcher.close(); + } catch (IOException e) { + if(throwOnException){ + throw new JspException("A problem occured trying to close the searcher : " + e); + } + } + } + } + + public void setCriteria(String criteria){ + this.criteria = criteria; + } + + public void setStartRow(String startRow){ + try{ + this.startRow = Integer.parseInt(startRow); + } + catch(Exception e){ + this.startRow = 0; + } + } + + public void setStartRow(int startRow){ + this.startRow = startRow; + } + + public void setMaxRows(String maxRows){ + try{ + this.maxRows = Integer.parseInt(maxRows); + } + catch(Exception e){ + this.maxRows = 10; + } + } + + public void setMaxRows(int maxRows){ + this.maxRows = maxRows; + } + + public void setCollection(String collection) throws JspException{ + idxArray = new ArrayList(); + String[] collectionArray = collection.split(","); + for(int i=0; i 0){ + String[] words = swords.split(","); + for (int i = 0; i < words.length; i++) { + if(new File(words[i]).isFile()){ + wordTable.putAll(WordlistLoader.getWordtable(words[i])); + }else{ + wordTable.put(words[i], words[i]); + } + } + temp = new String[wordTable.size()]; + + int count = 0; + if(wordTable.size() > 0){ + Iterator wtIter = wordTable.keySet().iterator(); + while (wtIter.hasNext()){ + temp[count++] = (String) wtIter.next(); + } + } + } + stopWords = temp; + } + +// public void setStopWords(String[] swords) throws JspException{ +// stopWords = swords; +// } + + /** + * @param string + */ + public void setFlagList(String fg) { + int[] list = new int[0]; + if(fg.split(",").length > 0){ + String[] ssplit = fg.split(","); + Integer fsplit = new Integer(fg.split(",").length); + list = new int[fsplit.intValue()]; + for(int i=0; i < fsplit.intValue(); i++){ + if(ssplit[i].equalsIgnoreCase("NORMAL")){ + list[i] = MultiFieldQueryParser.NORMAL_FIELD; + }else if(ssplit[i].equalsIgnoreCase("PROHIBITED")){ + list[i] = MultiFieldQueryParser.PROHIBITED_FIELD; + }else if(ssplit[i].equalsIgnoreCase("REQUIRED")){ + list[i] = MultiFieldQueryParser.REQUIRED_FIELD; + } + } + } + flagList = list; + } + + /** + * @param string + */ + public void setFieldList(String fl) { + if(fl.split(",").length > 0){ + fieldList = fl.split(","); + } + } + /** + * @param string + */ + public void setFieldList(String[] fl) { + fieldList = fl; + } + + /** + * @param string + */ + public void setSearch(String string) { + search = string; + } + + /** + * @param string + */ + public void setAnalyzerType(String atype) { + if(atype.equalsIgnoreCase("GERMANAN_ALYZER")){ + analyzerType = 0; + }else if(atype.equalsIgnoreCase("SIMPLE_ANALYZER")){ + analyzerType = 1; + }else if(atype.equalsIgnoreCase("STANDARD_ANALYZER")){ + analyzerType = 2; + }else if(atype.equalsIgnoreCase("STOP_ANALYZER")){ + analyzerType = 3; + }else if(atype.equalsIgnoreCase("WHITESPACE_ANALYZER")){ + analyzerType = 4; + }else{ + analyzerType = 2; + } + } + +} Index: WEB-INF/classes/com/netwebapps/taglib/search/SearchTagTei.java =================================================================== RCS file: /home/cvspublic/jakarta-lucene-sandbox/contributions/taglib/WEB-INF/classes/com/netwebapps/taglib/search/SearchTagTei.java,v retrieving revision 1.1 diff -u -r1.1 SearchTagTei.java --- WEB-INF/classes/com/netwebapps/taglib/search/SearchTagTei.java 12 May 2003 14:02:36 -0000 1.1 +++ WEB-INF/classes/com/netwebapps/taglib/search/SearchTagTei.java 28 May 2003 00:05:32 -0000 @@ -1,26 +1,27 @@ -package com.netwebapps.taglib.search; - -/** - * @author Network Web Application - * @author Bryan LaPlante - * - */ -import javax.servlet.jsp.tagext.*; - -public class SearchTagTei extends TagExtraInfo -{ - - public SearchTagTei(){ - } - /* - * VariableInfo is provided by the servlet container and allows the - * SearchTag class to output it's tag variables to the PageContext at runtime - * @see javax.servlet.jsp.tagext.TagExtraInfo#getVariableInfo(javax.servlet.jsp.tagext.TagData) - */ - public VariableInfo[] getVariableInfo(TagData tagdata) - { - VariableInfo avariableinfo[] = new VariableInfo[1]; - avariableinfo[0] = new VariableInfo(tagdata.getId(),"com.netwebapps.taglib.search.SearchTag", true, VariableInfo.AT_BEGIN); - return avariableinfo; - } +package com.netwebapps.taglib.search; + +/** + * @company Network Web Application + * @url http://www.netwebapps.com + * @author Bryan LaPlante + * + */ +import javax.servlet.jsp.tagext.*; + +public class SearchTagTei extends TagExtraInfo +{ + + public SearchTagTei(){ + } + /* + * VariableInfo is provided by the servlet container and allows the + * SearchTag class to output it's tag variables to the PageContext at runtime + * @see javax.servlet.jsp.tagext.TagExtraInfo#getVariableInfo(javax.servlet.jsp.tagext.TagData) + */ + public VariableInfo[] getVariableInfo(TagData tagdata) + { + VariableInfo avariableinfo[] = new VariableInfo[1]; + avariableinfo[0] = new VariableInfo(tagdata.getId(),"com.netwebapps.taglib.search.SearchTag", true, VariableInfo.AT_BEGIN); + return avariableinfo; + } }