lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ehatc...@apache.org
Subject cvs commit: jakarta-lucene-sandbox/contributions/taglib/WEB-INF/classes/com/netwebapps/taglib/search CollectionTag.java SearchTag.java
Date Wed, 21 May 2003 01:22:23 GMT
ehatcher    2003/05/20 18:22:23

  Modified:    contributions/taglib index.jsp result.jsp
               contributions/taglib/WEB-INF lucene-taglib.tld
               contributions/taglib/WEB-INF/classes/com/netwebapps/taglib/search
                        SearchTag.java
  Added:       contributions/taglib index.html
               contributions/taglib/WEB-INF/classes/com/netwebapps/taglib/search
                        CollectionTag.java
  Log:
  Bugzilla #20081 - added Bryan LaPlante's patch
  
  Revision  Changes    Path
  1.2       +7 -0      jakarta-lucene-sandbox/contributions/taglib/index.jsp
  
  Index: index.jsp
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene-sandbox/contributions/taglib/index.jsp,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- index.jsp	12 May 2003 14:02:35 -0000	1.1
  +++ index.jsp	21 May 2003 01:22:22 -0000	1.2
  @@ -1,3 +1,10 @@
  +
  +<%
  +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) */ %>
   <center> 
  
  
  
  1.2       +15 -7     jakarta-lucene-sandbox/contributions/taglib/result.jsp
  
  Index: result.jsp
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene-sandbox/contributions/taglib/result.jsp,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- result.jsp	12 May 2003 14:02:35 -0000	1.1
  +++ result.jsp	21 May 2003 01:22:22 -0000	1.2
  @@ -1,5 +1,4 @@
   <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  -
   <%@ taglib uri="/WEB-INF/lucene-taglib.tld" prefix="JSP"%>
   <%@ include file="header.jsp"%>
   <%@ page import="java.util.*"%>
  @@ -15,34 +14,43 @@
   	catch(Exception e){
   	}
   %>
  -<table border=3>
   
  +<table border=3>
   
  -	<JSP:Search id="rs" collection="E:/opt/lucene/index" criteria="<%= query %>"
startRow="<%= startRow %>" maxRows="<%= maxRows %>">
  +	<JSP:Search throwOnException="false" id="rs" collection="E:/opt/lucene/index" criteria="<%=
query %>" startRow="<%= startRow %>" maxRows="<%= maxRows %>">
   	<%
  +		
   		Set allFields = rs.getFields();
   		int fieldSize = allFields.size();
   		Iterator fieldIter = allFields.iterator();
  +		
   		while(fieldIter.hasNext()){
   			String nextField = (String) fieldIter.next();
   			if(!nextField.equalsIgnoreCase("summary")){
  -			%>
  +	%>
   				<tr><td><b><%= nextField %></b></td><td><%=
rs.getField(nextField) %></td></tr>
   			<%
   			}else{
   			%>
  -				<tr><td colspan="2"><b><%= nextField %></b></td></tr>
  +				<tr><td colspan="2"><b><%= rs.hitCount %>|<%= nextField
%></b></td></tr>
   				<tr><td colspan="2"><%= rs.getField(nextField) %></td></tr>
   			<%
   			}
   		}
  +		
   	%>
   	</JSP:Search>
   <%
  -	if(new Integer(rs.hitCount).intValue() <= 0){
  +	int count = 0;
  +	try{
  +		count = new Integer(rs.hitCount).intValue();
  +	}catch(Exception e){
  +		out.print(e);
  +	}
  +	if(count <= 0){
   %>
   	<tr>
  -		<td colspan=2>No results were found</td>
  +		<td colspan=2>No results have been found</td>
   	</tr>
   <%
   	}
  
  
  
  1.1                  jakarta-lucene-sandbox/contributions/taglib/index.html
  
  Index: index.html
  ===================================================================
  <html>
      <head>
          <title>Hello World</title>
      </head>
      <body>
          Hello World
      </body>
  </html>
  
  
  
  1.2       +18 -1     jakarta-lucene-sandbox/contributions/taglib/WEB-INF/lucene-taglib.tld
  
  Index: lucene-taglib.tld
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene-sandbox/contributions/taglib/WEB-INF/lucene-taglib.tld,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- lucene-taglib.tld	12 May 2003 14:02:36 -0000	1.1
  +++ lucene-taglib.tld	21 May 2003 01:22:22 -0000	1.2
  @@ -23,13 +23,18 @@
   			<rtexprvalue>true</rtexprvalue>
   		</attribute>
   		<attribute>
  +			<name>throwOnException</name>
  +			<required>false</required>
  +			<rtexprvalue>true</rtexprvalue>
  +		</attribute>
  +		<attribute>
   			<name>criteria</name>
   			<required>true</required>
   			<rtexprvalue>true</rtexprvalue>
   		</attribute>
   		<attribute>
   			<name>collection</name>
  -			<required>true</required>
  +			<required>false</required>
   			<rtexprvalue>true</rtexprvalue>
   		</attribute>
   		<attribute>
  @@ -40,6 +45,18 @@
   		<attribute>
   			<name>maxRows</name>
   			<required>false</required>
  +			<rtexprvalue>true</rtexprvalue>
  +		</attribute>
  +	</tag>
  +	<tag>
  +		<name>Collection</name>
  +		<tagclass>com.netwebapps.taglib.search.CollectionTag</tagclass>
  +		<info>
  +			Adds an additional collection to the search tag.
  +		</info>
  +		<attribute>
  +			<name>directory</name>
  +			<required>true</required>
   			<rtexprvalue>true</rtexprvalue>
   		</attribute>
   	</tag>
  
  
  
  1.2       +107 -44   jakarta-lucene-sandbox/contributions/taglib/WEB-INF/classes/com/netwebapps/taglib/search/SearchTag.java
  
  Index: SearchTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene-sandbox/contributions/taglib/WEB-INF/classes/com/netwebapps/taglib/search/SearchTag.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SearchTag.java	12 May 2003 14:02:36 -0000	1.1
  +++ SearchTag.java	21 May 2003 01:22:22 -0000	1.2
  @@ -5,6 +5,7 @@
   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.*;
  @@ -29,26 +30,34 @@
   	private String criteria = ""; 
   	private Iterator searchItr = null;
   	private Enumeration fields = null;
  -	private HashMap aField = 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 = "";
  +	public String rowCount = "0";
   	public String pageCount = "1";
  -	public String hitCount = "";
  +	public String hitCount = "0";
   	public String firstPage = "";
   	public String nextPage = "";
   	public String previousPage = "";
   	public String lastPage = "";
  -	public LinkedList pageList = null;
  +	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();
  @@ -60,16 +69,25 @@
   	}
   	
   	public void doInitBody() throws JspException{
  -		doSearch();
  -		searchItr = hitArray.iterator();
  -		if(searchItr.hasNext()){
  -			aField = (HashMap) searchItr.next();
  -			rowCount = new Integer(startRow + ROWCOUNT).toString();
  -			pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE);
  +		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();
  @@ -88,6 +106,11 @@
   	}
   	
   	public int doEndTag() throws JspException{
  +
  +		if(abort){
  +			return EVAL_PAGE;
  +		}
  +		
   		try{
   			HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
   			String relativePath = req.getRequestURI();
  @@ -124,47 +147,83 @@
   	}
   	
   	public Set getFields(){
  -		if(aField != null){
  -			return aField.keySet();
  -		}
  -		return null;
  +		return aField.keySet();
   	}
   	
   	public void doSearch() throws JspException{
  -        try {
  -	        searcher = new IndexSearcher(IndexReader.open(collection));
  -			Analyzer analyzer = new StopAnalyzer();
  +
  +		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);
  -				hits = searcher.search(query);
  -				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;
  +				query = QueryParser.parse(criteria, "contents", analyzer);
  +			} catch (ParseException e) {
  +				if(throwOnException){
  +					throw new JspException("QueryParser.parse(criteria,contents,analyzer): " + e);
   				}
  -				hitArray = new ArrayList();
  -				for (int i = startRow; i < (thispage + startRow); i++) {
  -					hitMap = new HashMap();
  -					Document doc = hits.doc(i);
  -					hitMap.put("score",new Float(hits.score(i)).toString());
  -					fields = doc.fields();
  -					while(fields.hasMoreElements()){
  -						Field field = (Field) fields.nextElement();
  -						String fieldName = field.name();
  -						hitMap.put(fieldName,doc.get(fieldName));
  +				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);
  +							}
  +						}
   					}
  -					hitArray.add(hitMap);
   				}
   			}
  -			catch (Exception e){
  -				throw new JspException("An error occurred while parsing the index : " + e.toString());
  -			}
  -        }
  -		catch (Exception e) {
  -			throw new JspException("An error occurred while trying to open the search index: " +
e.toString());
   		}
   	}
   	
  @@ -204,6 +263,10 @@
   	
   	public void setCollection(String collection){
   		this.collection = collection;
  +	}
  +	
  +	public void setThrowOnException(String bool){
  +		this.throwOnException = new Boolean(bool).booleanValue();
   	}
   	
   	/* getters */
  
  
  
  1.1                  jakarta-lucene-sandbox/contributions/taglib/WEB-INF/classes/com/netwebapps/taglib/search/CollectionTag.java
  
  Index: CollectionTag.java
  ===================================================================
  /*
   * 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;
  	}
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-dev-help@jakarta.apache.org


Mime
View raw message