2
* @(#)SearchTOCItemTag.java 1.3 06/10/30
4
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7
* This code is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU General Public License version 2 only, as
9
* published by the Free Software Foundation. Sun designates this
10
* particular file as subject to the "Classpath" exception as provided
11
* by Sun in the LICENSE file that accompanied this code.
13
* This code is distributed in the hope that it will be useful, but WITHOUT
14
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16
* version 2 for more details (a copy is included in the LICENSE file that
17
* accompanied this code).
19
* You should have received a copy of the GNU General Public License version
20
* 2 along with this work; if not, write to the Free Software Foundation,
21
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
24
* CA 95054 USA or visit www.sun.com if you need additional information or
28
package javax.help.tagext;
30
import javax.servlet.jsp.*;
31
import javax.servlet.jsp.tagext.*;
35
import java.net.MalformedURLException;
36
import javax.help.HelpBroker;
37
import javax.help.HelpSet;
38
import javax.help.Map;
39
import javax.help.Map.ID;
40
import javax.help.NavigatorView;
41
import javax.help.SearchTOCItem;
42
import javax.help.SearchView;
43
import javax.help.SearchHit;
44
import javax.help.search.MergingSearchEngine;
45
import javax.help.search.SearchQuery;
46
import javax.help.search.SearchListener;
47
import javax.help.search.SearchEvent;
48
import javax.help.search.SearchItem;
49
import javax.swing.tree.DefaultMutableTreeNode;
50
import javax.swing.tree.TreeNode;
53
* The JSP tag extra info class for an SearchTOCItem
55
* @author Roger D. Brinkley
56
* @version 1.3 10/30/06
57
* @see javax.help.SearchTOCItem
60
public class SearchTOCItemTag extends BodyTagSupport implements SearchListener{
61
private Enumeration treeEnum;
63
private SearchView view;
64
private HelpBroker hb;
66
private MergingSearchEngine helpsearch;
67
private SearchQuery searchquery;
68
private boolean searchFinished;
70
public void setSearchView(SearchView view) {
74
public void setHelpBroker(HelpBroker hb) {
78
public void setQuery(String query) {
83
public synchronized int doStartTag() {
84
if (helpsearch == null) {
85
helpsearch = new MergingSearchEngine(view);
86
searchquery = helpsearch.createQuery();
87
searchquery.addSearchListener(this);
89
// Make sure all the subhelpsets have a search engine
90
addSubHelpSets(view.getHelpSet());
93
if (searchquery.isActive()) {
97
searchquery.start(query, Locale.getDefault());
99
if (!searchFinished) {
102
} catch (InterruptedException e) {
108
if(treeEnum.hasMoreElements()) {
109
SearchTOCItem item = (SearchTOCItem) treeEnum.nextElement();
110
setNodeAttributes(item);
111
return EVAL_BODY_TAG;
119
* @param hs The HelpSet which subhelpsets will be added
121
private void addSubHelpSets(HelpSet hs){
122
for( Enumeration e = hs.getHelpSets(); e.hasMoreElements(); ) {
123
HelpSet ehs = (HelpSet) e.nextElement();
128
NavigatorView[] views = ehs.getNavigatorViews();
129
for(int i = 0; i < views.length; i++){
130
if (views[i] instanceof SearchView) {
131
helpsearch.merge(views[i]);
134
addSubHelpSets( ehs );
138
public int doAfterBody() throws JspException {
139
BodyContent body = getBodyContent();
141
body.writeOut(getPreviousOut());
142
} catch (IOException e) {
143
throw new JspTagException("SearchTOCItemTag: " + e.getMessage());
146
// clear up so the next time the body content is empty
148
if(treeEnum.hasMoreElements()) {
149
SearchTOCItem item = (SearchTOCItem) treeEnum.nextElement();
150
setNodeAttributes(item);
151
return EVAL_BODY_TAG;
157
private void setNodeAttributes(SearchTOCItem item) {
158
pageContext.setAttribute("name", item.getName());
159
pageContext.setAttribute("helpID", getMapID(item));
160
pageContext.setAttribute("confidence", Double.toString(item.getConfidence()));
161
pageContext.setAttribute("hits", Integer.toString(item.hitCount()));
162
pageContext.setAttribute("contentURL", item.getURL().toExternalForm());
163
pageContext.setAttribute("hitBoundries", getSearchHits(item));
167
* return an Map.ID if one exists for the content URL for a given SearchTOCItem
169
* returns an empty String if no content exists.
171
private String getMapID(SearchTOCItem item) {
172
URL url = item.getURL();
173
HelpSet hs = hb.getHelpSet();
174
Map map = hs.getCombinedMap();
175
ID id = map.getIDFromURL(url);
183
* Get a string representing the array of SearchHit's begin and end locations
186
* @return String a sring representation of the values
188
private String getSearchHits(SearchTOCItem item) {
189
String retval = "{ ";
190
for (Enumeration enum1 = item.getSearchHits();
191
enum1.hasMoreElements();) {
192
SearchHit info = (SearchHit) enum1.nextElement();
193
retval = retval + "{" + info.getBegin() + "," + info.getEnd() +
195
if (enum1.hasMoreElements()) {
196
retval = retval + ", ";
199
retval = retval + " }";
204
public synchronized void itemsFound(SearchEvent e) {
205
SearchTOCItem tocitem;
206
Enumeration itemEnum = e.getSearchItems();
207
// Iterate through each search item in the searchEvent
208
while (itemEnum.hasMoreElements()) {
209
SearchItem item = (SearchItem) itemEnum.nextElement();
212
url = new URL(item.getBase(), item.getFilename());
213
} catch (MalformedURLException me) {
214
debug ("Failed to create URL from " + item.getBase() + "|" +
218
boolean foundNode = false;
220
// see if this search item matches that of one we currently have
221
// if so just do an update
222
Enumeration nodesEnum = nodes.elements();
223
while (nodesEnum.hasMoreElements()) {
224
tocitem = (SearchTOCItem) nodesEnum.nextElement();
225
URL testURL = tocitem.getURL();
226
if (testURL != null && url != null && url.sameFile(testURL)) {
227
tocitem.addSearchHit(new SearchHit(item.getConfidence(),
236
// OK then add a new one.
238
tocitem = new SearchTOCItem(item);
239
nodes.addElement(tocitem);
244
public synchronized void searchStarted(SearchEvent e) {
245
nodes = new Vector();
246
searchFinished = false;
249
public synchronized void searchFinished(SearchEvent e) {
250
searchFinished = true;
251
treeEnum = nodes.elements();
255
private static final boolean debug = false;
256
private static void debug(String msg) {
258
System.err.println("SearchTOCItemTag: "+msg);