001: /*
002: * regain - A file search engine providing plenty of formats
003: * Copyright (C) 2004 Til Schneider
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: Til Schneider, info@murfman.de
020: *
021: * CVS information:
022: * $RCSfile$
023: * $Source$
024: * $Date: 2005-11-02 22:58:52 +0100 (Mi, 02 Nov 2005) $
025: * $Author: til132 $
026: * $Revision: 177 $
027: */
028: package net.sf.regain.search.sharedlib;
029:
030: import net.sf.regain.RegainException;
031: import net.sf.regain.RegainToolkit;
032: import net.sf.regain.search.SearchConstants;
033: import net.sf.regain.search.SearchToolkit;
034: import net.sf.regain.search.results.SearchResults;
035: import net.sf.regain.util.sharedtag.PageRequest;
036: import net.sf.regain.util.sharedtag.PageResponse;
037: import net.sf.regain.util.sharedtag.SharedTag;
038:
039: /**
040: * This Tag creates hyperlinks to navigate through the search result pages.
041: * <p>
042: * Tag Parameters:
043: * <ul>
044: * <li><code>msgBack</code>: The message to use for labeling the back link.</li>
045: * <li><code>msgForward</code>: The message to use for labeling the forward link.</li>
046: * <li><code>targetPage</code>: The URL of the page where the links should point to.</li>
047: * <li><code>class</code>: The style sheet class to use for the link tags.</li>
048: * </ul>
049: *
050: * @author Til Schneider, www.murfman.de
051: */
052: public class NavigationTag extends SharedTag implements SearchConstants {
053:
054: /** The maximum number of links to create. */
055: private static final int MAX_BUTTONS = 15;
056:
057: /** Die Default-Seite, auf die die Weiter-Links zeigen sollen. */
058: private static final String DEFAULT_TARGET_PAGE = "SearchOutput.jsp";
059:
060: /**
061: * Called when the parser reaches the end tag.
062: *
063: * @param request The page request.
064: * @param response The page response.
065: * @throws RegainException If there was an exception.
066: */
067: public void printEndTag(PageRequest request, PageResponse response)
068: throws RegainException {
069: String query = SearchToolkit.getSearchQuery(request);
070: SearchResults results = SearchToolkit.getSearchResults(request);
071:
072: int fromResult = request
073: .getParameterAsInt(PARAM_FROM_RESULT, 0);
074: int maxResults = request.getParameterAsInt(PARAM_MAX_RESULTS,
075: SearchConstants.DEFAULT_MAX_RESULTS);
076: int totalResults = results.getHitCount();
077:
078: int buttonCount = (int) Math.ceil((double) totalResults
079: / (double) maxResults);
080: int currButton = fromResult / maxResults;
081:
082: // The first and the last button to show
083: int fromButton = 0;
084: int toButton = buttonCount - 1;
085:
086: if (buttonCount > MAX_BUTTONS) {
087: if (currButton < (MAX_BUTTONS / 2)) {
088: // The button range starts at the first button (---X------.....)
089: toButton = fromButton + MAX_BUTTONS - 1;
090: } else if (currButton > (buttonCount - ((MAX_BUTTONS + 1) / 2))) {
091: // The button range ends at the last button (.....-------X--)
092: fromButton = toButton - MAX_BUTTONS + 1;
093: } else {
094: // The button range is somewhere in the middle (...----X-----..)
095: toButton = currButton + (MAX_BUTTONS / 2);
096: fromButton = toButton - MAX_BUTTONS + 1;
097: }
098: }
099:
100: String[] indexNameArr = request.getParameters("index");
101: if (currButton > 0) {
102: String msgBack = getParameter("msgBack", true);
103: msgBack = RegainToolkit.replace(msgBack, """, "\"");
104: printLink(response, currButton - 1, query, maxResults,
105: indexNameArr, msgBack);
106: }
107: for (int i = fromButton; i <= toButton; i++) {
108: if (i == currButton) {
109: // This is the current button
110: response.print("<b>" + (i + 1) + "</b> ");
111: } else {
112: String linkText = Integer.toString(i + 1);
113: printLink(response, i, query, maxResults, indexNameArr,
114: linkText);
115: }
116: }
117: if (currButton < (buttonCount - 1)) {
118: String msgForward = getParameter("msgForward", true);
119: msgForward = RegainToolkit.replace(msgForward, "'", "\"");
120: printLink(response, currButton + 1, query, maxResults,
121: indexNameArr, msgForward);
122: }
123: }
124:
125: /**
126: * Prints the HTML for a hyperlink.
127: *
128: * @param response The page response.
129: * @param button The index of the button to create the HTML for.
130: * @param query The search query.
131: * @param maxResults The maximum results.
132: * @param indexNameArr The names of the search indexes.
133: * @param linkText The link text.
134: * @throws RegainException If printing failed.
135: */
136: private void printLink(PageResponse response, int button,
137: String query, int maxResults, String[] indexNameArr,
138: String linkText) throws RegainException {
139: String targetPage = getParameter("targetPage",
140: DEFAULT_TARGET_PAGE);
141:
142: String encoding = response.getEncoding();
143: String encodedQuery = RegainToolkit.urlEncode(query, encoding);
144:
145: response.print("<a href=\"" + targetPage + "?query="
146: + encodedQuery);
147: if (indexNameArr != null) {
148: for (int i = 0; i < indexNameArr.length; i++) {
149: String encodedIndexName = RegainToolkit.urlEncode(
150: indexNameArr[i], encoding);
151: response.print("&index=" + encodedIndexName);
152: }
153: }
154: if (maxResults != SearchConstants.DEFAULT_MAX_RESULTS) {
155: response.print("&maxresults=" + maxResults);
156: }
157: if (button != 0) {
158: response.print("&fromresult=" + (button * maxResults));
159: }
160: response.print("\"");
161: String styleSheetClass = getParameter("class");
162: if (styleSheetClass != null) {
163: response.print(" class=\"" + styleSheetClass + "\"");
164: }
165: response.print(">" + linkText + "</a> ");
166: }
167:
168: }
|