~vcs-imports/xena/trunk

« back to all changes in this revision

Viewing changes to ext/src/javahelp/jhMaster/JavaHelp/src/new/javax/help/tagext/SearchTOCItemTag.java

  • Committer: matthewoliver
  • Date: 2009-12-10 03:18:07 UTC
  • Revision ID: vcs-imports@canonical.com-20091210031807-l086qguzdlljtkl9
Merged Xena Testing into Xena Stable for the Xena 5 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * @(#)SearchTOCItemTag.java    1.3 06/10/30
 
3
 * 
 
4
 * Copyright (c) 2006 Sun Microsystems, Inc.  All Rights Reserved.
 
5
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
6
 * 
 
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.
 
12
 * 
 
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).
 
18
 * 
 
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.
 
22
 * 
 
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
 
25
 * have any questions.
 
26
 */
 
27
 
 
28
package javax.help.tagext;
 
29
 
 
30
import javax.servlet.jsp.*;
 
31
import javax.servlet.jsp.tagext.*;
 
32
import java.util.*;
 
33
import java.io.*;
 
34
import java.net.URL;
 
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;
 
51
 
 
52
/**
 
53
 * The JSP tag extra info class for an SearchTOCItem
 
54
 *
 
55
 * @author Roger D. Brinkley
 
56
 * @version     1.3     10/30/06
 
57
 * @see javax.help.SearchTOCItem
 
58
 */
 
59
 
 
60
public class SearchTOCItemTag extends BodyTagSupport implements SearchListener{
 
61
    private Enumeration treeEnum;
 
62
    private Vector nodes;
 
63
    private SearchView view;
 
64
    private HelpBroker hb;
 
65
    private String query;
 
66
    private MergingSearchEngine helpsearch;
 
67
    private SearchQuery searchquery;
 
68
    private boolean searchFinished;
 
69
 
 
70
    public void setSearchView(SearchView view) {
 
71
        this.view = view;
 
72
    }
 
73
 
 
74
    public void setHelpBroker(HelpBroker hb) {
 
75
        this.hb = hb;
 
76
    }
 
77
 
 
78
    public void setQuery(String query) {
 
79
        this.query = query;
 
80
    }
 
81
 
 
82
 
 
83
    public synchronized int doStartTag() {
 
84
        if (helpsearch == null) {
 
85
            helpsearch = new MergingSearchEngine(view);
 
86
            searchquery = helpsearch.createQuery();
 
87
            searchquery.addSearchListener(this);
 
88
 
 
89
            // Make sure all the subhelpsets have a search engine
 
90
            addSubHelpSets(view.getHelpSet());
 
91
        }
 
92
 
 
93
        if (searchquery.isActive()) {
 
94
            searchquery.stop();
 
95
        }
 
96
 
 
97
        searchquery.start(query, Locale.getDefault());
 
98
 
 
99
        if (!searchFinished) {
 
100
            try {
 
101
                wait();
 
102
            } catch (InterruptedException e) {
 
103
                // ignore
 
104
            }
 
105
        }
 
106
 
 
107
 
 
108
        if(treeEnum.hasMoreElements()) {
 
109
            SearchTOCItem item = (SearchTOCItem) treeEnum.nextElement();
 
110
            setNodeAttributes(item);
 
111
            return EVAL_BODY_TAG;
 
112
        } else {
 
113
            return SKIP_BODY;
 
114
        }
 
115
    }
 
116
 
 
117
    /** Adds subhelpsets
 
118
     *
 
119
     * @param hs The HelpSet which subhelpsets will be added
 
120
     */
 
121
    private void addSubHelpSets(HelpSet hs){
 
122
        for( Enumeration e = hs.getHelpSets(); e.hasMoreElements(); ) {
 
123
            HelpSet ehs = (HelpSet) e.nextElement();
 
124
            if (ehs == null) {
 
125
                continue;
 
126
            }
 
127
            // merge views
 
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]);
 
132
                }
 
133
            }
 
134
            addSubHelpSets( ehs );
 
135
        }
 
136
    }    
 
137
 
 
138
    public int doAfterBody() throws JspException {
 
139
        BodyContent body = getBodyContent();
 
140
        try {
 
141
            body.writeOut(getPreviousOut());
 
142
        } catch (IOException e) {
 
143
            throw new JspTagException("SearchTOCItemTag: " + e.getMessage());
 
144
        }
 
145
 
 
146
        // clear up so the next time the body content is empty
 
147
        body.clearBody();
 
148
        if(treeEnum.hasMoreElements()) {
 
149
            SearchTOCItem item = (SearchTOCItem) treeEnum.nextElement();
 
150
            setNodeAttributes(item);
 
151
            return EVAL_BODY_TAG;
 
152
        } else {
 
153
            return SKIP_BODY;
 
154
        }
 
155
    }
 
156
 
 
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));
 
164
    }
 
165
 
 
166
    /**
 
167
     * return an Map.ID if one exists for the content URL for a given SearchTOCItem
 
168
     * 
 
169
     * returns an empty String if no content exists.
 
170
     */
 
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);
 
176
        if (id == null) {
 
177
            return "";
 
178
        }
 
179
        return id.id;
 
180
    }
 
181
 
 
182
    /**
 
183
     * Get a string representing the array of SearchHit's begin and end locations
 
184
     * for each hit
 
185
     * 
 
186
     * @return String a sring representation of the values
 
187
     */
 
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() + 
 
194
                "}";
 
195
            if (enum1.hasMoreElements()) {
 
196
                retval = retval + ", ";
 
197
            }
 
198
        }
 
199
        retval = retval + " }";
 
200
        return retval;
 
201
    }
 
202
        
 
203
 
 
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();
 
210
            URL url;
 
211
            try {
 
212
                url = new URL(item.getBase(), item.getFilename());
 
213
            } catch (MalformedURLException me) {
 
214
                debug ("Failed to create URL from " + item.getBase() + "|" +
 
215
                       item.getFilename());
 
216
                continue;
 
217
            }
 
218
            boolean foundNode = false;
 
219
 
 
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(),
 
228
                                                       item.getBegin(),
 
229
                                                       item.getEnd()));
 
230
                    foundNode = true;
 
231
                    break;
 
232
                }
 
233
            }
 
234
 
 
235
            // No match. 
 
236
            // OK then add a new one.
 
237
            if (!foundNode) {
 
238
                tocitem = new SearchTOCItem(item);
 
239
                nodes.addElement(tocitem);
 
240
            }
 
241
        }
 
242
    }
 
243
 
 
244
    public synchronized void searchStarted(SearchEvent e) {
 
245
        nodes = new Vector();
 
246
        searchFinished = false;
 
247
    }
 
248
 
 
249
    public synchronized void searchFinished(SearchEvent e) {
 
250
        searchFinished = true;
 
251
        treeEnum = nodes.elements();
 
252
        notifyAll();
 
253
    }
 
254
 
 
255
    private static final boolean debug = false;
 
256
    private static void debug(String msg) {
 
257
        if (debug) {
 
258
            System.err.println("SearchTOCItemTag: "+msg);
 
259
        }
 
260
    }
 
261
}
 
262