Added support for specifying a file type extension in the search tag.

This commit is contained in:
Erik C. Thauvin 2003-10-13 23:43:20 +00:00
parent acb262f5d7
commit bed16e985b
4 changed files with 156 additions and 113 deletions

View file

@ -95,6 +95,11 @@ public class GoogleSearchBean
*/
public static final int DEFAULT_START = 0;
/**
* The default filetype.
*/
public static final String DEFAULT_TYPE = "";
/**
* The <em>next</em> keyword.
*/

View file

@ -147,6 +147,11 @@ public class TagUtility
*/
public static final String START_PARAM = "start";
/**
* The name of the (file) type request parameter.
*/
public static final String TYPE_PARAM = "type";
/**
* Protected constructor to disable instantiation.
*/

View file

@ -42,7 +42,6 @@ import net.thauvin.google.TagUtility;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
/**
* A custom tag used to perform Google searches.
*
@ -54,14 +53,15 @@ import javax.servlet.jsp.PageContext;
public class Search extends QuerySupport
{
private GoogleSearchBean bean = null;
private String lr = GoogleSearchBean.DEFAULT_LR;
private String restrict = GoogleSearchBean.DEFAULT_RESTRICT;
private String site = GoogleSearchBean.DEFAULT_SITE;
private boolean cache = GoogleSearchBean.DEFAULT_CACHE;
private boolean filter = GoogleSearchBean.DEFAULT_FILTER;
private boolean safeSearch = GoogleSearchBean.DEFAULT_SAFE_SEARCH;
private String lr = GoogleSearchBean.DEFAULT_LR;
private int maxResults = GoogleSearchBean.DEFAULT_MAX_RESULTS;
private String restrict = GoogleSearchBean.DEFAULT_RESTRICT;
private boolean safeSearch = GoogleSearchBean.DEFAULT_SAFE_SEARCH;
private String site = GoogleSearchBean.DEFAULT_SITE;
private int start = GoogleSearchBean.DEFAULT_START;
private String type = GoogleSearchBean.DEFAULT_TYPE;
/**
* Sets the cache attribute.
@ -106,7 +106,7 @@ public class Search extends QuerySupport
}
catch (NumberFormatException e)
{
; // Do nothing
;// Do nothing
}
}
@ -153,116 +153,18 @@ public class Search extends QuerySupport
}
catch (NumberFormatException e)
{
; // Do nothing
;// Do nothing
}
}
/**
* doEndTag method.
* Sets the (file) type attribute
*
* @return EVAL_PAGE
* @exception JspException
* @param type The new attribute value.
*/
public int doEndTag()
throws JspException
public final void setType(String type)
{
final String query = getQuery();
if (TagUtility.isValidString(query, true))
{
try
{
bean.setProxyServer(pageContext.getServletContext()
.getInitParameter(TagUtility.GOOGLE_PROXY_HOST),
pageContext.getServletContext()
.getInitParameter(TagUtility.GOOGLE_PROXY_PORT),
pageContext.getServletContext()
.getInitParameter(TagUtility.GOOGLE_PROXY_USERNAME),
pageContext.getServletContext()
.getInitParameter(TagUtility.GOOGLE_PROXY_PASSWORD));
bean.getGoogleSearch(getKey(), getSite() + getQuery(),
getStart(), getMaxResults(), getFilter(),
getRestrict(), getSafeSearch(), getLr());
}
catch (Exception e)
{
throw TagUtility.outputError("search", e);
}
}
else if (!getCache())
{
bean.reset();
}
// Reset the values
reset();
return EVAL_PAGE;
}
/**
* doStartTag method.
*
* @return EVAL_BODY_TAG.
* @exception JspException
*/
public int doStartTag()
throws JspException
{
// Get the Google bean
bean = TagUtility.getGoogleSearchBean(pageContext);
// Create a new bean if it doesn't exists
if (bean == null)
{
try
{
bean = new GoogleSearchBean();
// Set the bean as named session attribute
pageContext.setAttribute(TagUtility.GOOGLE_SEARCH_BEAN, bean,
PageContext.SESSION_SCOPE);
}
catch (Exception e)
{
throw new JspException("An unknown error ocurred while creating the Google search bean.");
}
}
return EVAL_BODY_TAG;
}
/**
* Release method.
*/
public void release()
{
super.release();
// Reset all attributes
start = GoogleSearchBean.DEFAULT_START;
maxResults = GoogleSearchBean.DEFAULT_MAX_RESULTS;
filter = GoogleSearchBean.DEFAULT_FILTER;
safeSearch = GoogleSearchBean.DEFAULT_SAFE_SEARCH;
restrict = GoogleSearchBean.DEFAULT_RESTRICT;
lr = GoogleSearchBean.DEFAULT_LR;
site = GoogleSearchBean.DEFAULT_SITE;
cache = GoogleSearchBean.DEFAULT_CACHE;
// Reset the bean
bean = null;
// Reset the values
reset();
}
/**
* Reset the values.
*/
protected void reset()
{
super.reset();
this.type = type;
}
/**
@ -352,6 +254,23 @@ public class Search extends QuerySupport
return getIntParam(TagUtility.START_PARAM, start);
}
/**
* Returns the (file) type attribute.
*
* @return The attribute value
*/
private final String getType()
{
String type = getStringParam(TagUtility.TYPE_PARAM, this.type);
if (type.length() > 0)
{
return (" filetype:" + type);
}
return "";
}
/**
* Converts a request parameter to a boolean.
*
@ -362,7 +281,7 @@ public class Search extends QuerySupport
private boolean getBoolParam(String paramName, boolean defaultValue)
{
String param =
TagUtility.getParameter(pageContext.getRequest(), paramName);
TagUtility.getParameter(pageContext.getRequest(), paramName);
if (TagUtility.isValidString(param, true))
{
@ -382,7 +301,7 @@ public class Search extends QuerySupport
private int getIntParam(String paramName, int defaultValue)
{
String param =
TagUtility.getParameter(pageContext.getRequest(), paramName);
TagUtility.getParameter(pageContext.getRequest(), paramName);
if (TagUtility.isValidString(param, true))
{
@ -392,7 +311,7 @@ public class Search extends QuerySupport
}
catch (NumberFormatException e)
{
; // Do nothing
;// Do nothing
}
}
@ -409,7 +328,7 @@ public class Search extends QuerySupport
private String getStringParam(String paramName, String defaultValue)
{
String param =
TagUtility.getParameter(pageContext.getRequest(), paramName);
TagUtility.getParameter(pageContext.getRequest(), paramName);
if (TagUtility.isValidString(param, true))
{
@ -418,4 +337,113 @@ public class Search extends QuerySupport
return defaultValue;
}
/**
* doEndTag method.
*
* @return EVAL_PAGE
* @exception JspException
*/
public int doEndTag()
throws JspException
{
final String query = getQuery();
if (TagUtility.isValidString(query, true))
{
try
{
bean.setProxyServer(pageContext.getServletContext()
.getInitParameter(TagUtility.GOOGLE_PROXY_HOST),
pageContext.getServletContext()
.getInitParameter(TagUtility.GOOGLE_PROXY_PORT),
pageContext.getServletContext()
.getInitParameter(TagUtility.GOOGLE_PROXY_USERNAME),
pageContext.getServletContext()
.getInitParameter(TagUtility.GOOGLE_PROXY_PASSWORD));
bean.getGoogleSearch(getKey(), getSite() + getQuery() + getType(),
getStart(), getMaxResults(), getFilter(),
getRestrict(), getSafeSearch(), getLr());
}
catch (Exception e)
{
throw TagUtility.outputError("search", e);
}
}
else if (!getCache())
{
bean.reset();
}
// Reset the values
reset();
return EVAL_PAGE;
}
/**
* doStartTag method.
*
* @return EVAL_BODY_TAG.
* @exception JspException
*/
public int doStartTag()
throws JspException
{
// Get the Google bean
bean = TagUtility.getGoogleSearchBean(pageContext);
// Create a new bean if it doesn't exists
if (bean == null)
{
try
{
bean = new GoogleSearchBean();
// Set the bean as named session attribute
pageContext.setAttribute(TagUtility.GOOGLE_SEARCH_BEAN, bean,
PageContext.SESSION_SCOPE);
}
catch (Exception e)
{
throw new JspException("An unknown error ocurred while creating the Google search bean.");
}
}
return EVAL_BODY_TAG;
}
/**
* Release method.
*/
public void release()
{
super.release();
// Reset all attributes
start = GoogleSearchBean.DEFAULT_START;
maxResults = GoogleSearchBean.DEFAULT_MAX_RESULTS;
filter = GoogleSearchBean.DEFAULT_FILTER;
safeSearch = GoogleSearchBean.DEFAULT_SAFE_SEARCH;
restrict = GoogleSearchBean.DEFAULT_RESTRICT;
lr = GoogleSearchBean.DEFAULT_LR;
site = GoogleSearchBean.DEFAULT_SITE;
cache = GoogleSearchBean.DEFAULT_CACHE;
type = GoogleSearchBean.DEFAULT_TYPE;
// Reset the bean
bean = null;
// Reset the values
reset();
}
/**
* Reset the values.
*/
protected void reset()
{
super.reset();
}
}

View file

@ -162,6 +162,11 @@
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>type</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<!-- SearchComments Tag -->
<tag>