KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > search > SearchResults


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.search;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.help.IToc;
18 import org.eclipse.help.ITopic;
19 import org.eclipse.help.internal.HelpPlugin;
20 import org.eclipse.help.internal.util.URLCoder;
21 import org.eclipse.help.internal.workingset.AdaptableHelpResource;
22 import org.eclipse.help.internal.workingset.AdaptableToc;
23 import org.eclipse.help.internal.workingset.WorkingSet;
24
25 /**
26  * Search result collector. Performs filtering and collects hits into an array
27  * of SearchHit
28  */

29 public class SearchResults implements ISearchHitCollector {
30     // Collection of AdaptableHelpResource[]
31
private ArrayList JavaDoc scopes;
32     private int maxHits;
33     private String JavaDoc locale;
34     protected SearchHit[] searchHits = new SearchHit[0];
35     /**
36      * Constructor
37      *
38      * @param workingSets
39      * working sets or null if no filtering
40      */

41     public SearchResults(WorkingSet[] workingSets, int maxHits, String JavaDoc locale) {
42         this.maxHits = maxHits;
43         this.locale = locale;
44         this.scopes = getScopes(workingSets);
45     }
46
47     /* (non-Javadoc)
48      * @see org.eclipse.help.internal.search.ISearchHitCollector#addHits(List, String)
49      */

50     public void addHits(List JavaDoc hits, String JavaDoc highlightTerms) {
51         String JavaDoc urlEncodedWords = URLCoder.encode(highlightTerms);
52         List JavaDoc searchHitList = new ArrayList JavaDoc();
53         float scoreScale = 1.0f;
54         boolean scoreScaleSet = false;
55         
56         Iterator JavaDoc iter = hits.iterator();
57         for (int i=0;i<maxHits && iter.hasNext();i++) {
58             SearchHit rawHit = (SearchHit)iter.next();
59             String JavaDoc href = rawHit.getHref();
60             IToc toc = null; // the TOC containing the topic
61
AdaptableHelpResource scope = null;
62             // the scope for the topic, if any
63
if (scopes == null) {
64                 toc = getTocForTopic(href, locale);
65             } else {
66                 scope = getScopeForTopic(href);
67                 if (scope == null) {
68                     // topic outside of scope
69
continue;
70                 } else if (scope instanceof AdaptableToc) {
71                     toc = (IToc) scope.getAdapter(IToc.class);
72                 } else { // scope is AdaptableTopic
73
toc = (IToc) scope.getParent().getAdapter(IToc.class);
74                 }
75             }
76
77             // adjust score
78
float score = rawHit.getScore();
79             if (!scoreScaleSet) {
80                 if (score > 0) {
81                     scoreScale = 0.99f / score;
82                     score = 1;
83                 }
84                 scoreScaleSet = true;
85             } else {
86                 score = score * scoreScale + 0.01f;
87             }
88
89             // Set the document label
90
String JavaDoc label = rawHit.getLabel();
91             if ("".equals(label) && toc != null) { //$NON-NLS-1$
92
ITopic t;
93                 if (scope != null) {
94                     t = scope.getTopic(href);
95                 } else {
96                     t = toc.getTopic(href);
97                 }
98                 if (t != null) {
99                     label = t.getLabel();
100                 }
101             }
102             if (label == null || "".equals(label)) { //$NON-NLS-1$
103
label = href;
104             }
105             
106             // Set document href
107
if (urlEncodedWords.length() > 0) {
108                 href += "?resultof=" + urlEncodedWords; //$NON-NLS-1$
109
}
110             searchHitList.add(new SearchHit(href, label, rawHit.getSummary(), score, toc, rawHit.getRawId(), rawHit.getParticipantId(), rawHit.isPotentialHit()));
111         }
112         searchHits = (SearchHit[]) searchHitList
113                 .toArray(new SearchHit[searchHitList.size()]);
114
115     }
116     /**
117      * Finds a topic within a scope
118      */

119     private AdaptableHelpResource getScopeForTopic(String JavaDoc href) {
120         for (int i = 0; i < scopes.size(); i++) {
121             AdaptableHelpResource scope = (AdaptableHelpResource) scopes.get(i);
122             if (scope.getTopic(href) != null)
123                 return scope;
124         
125             // add root toc's extradir topics to search scope
126
IToc tocRoot = getTocForScope(scope, locale);
127             if (tocRoot != null) {
128                 IToc toc = HelpPlugin.getTocManager().getOwningToc(href);
129                 if (toc != null) {
130                     String JavaDoc owningTocHref = toc.getHref();
131                     if (owningTocHref == tocRoot.getHref()) {
132                         return scope;
133                     }
134                 }
135             }
136         }
137         return null;
138     }
139
140     /**
141      * Finds a scope in a toc
142      */

143     private IToc getTocForScope(AdaptableHelpResource scope, String JavaDoc locale) {
144         if (scope == null) {
145             return null;
146         }
147         String JavaDoc href = scope.getHref();
148         if(scope.getAdapter(IToc.class) instanceof IToc){
149             IToc toc=(IToc)scope.getAdapter(IToc.class);
150             href=toc.getTopic(null).getHref();
151         }
152         
153         if (href != null && href.length() > 0) {
154             return getTocForTopic(href, locale);
155         } else {
156             AdaptableHelpResource[] childrenScopes = scope.getChildren();
157             if (childrenScopes != null) {
158                 for (int i = 0; i < childrenScopes.length; i++) {
159                     // To find the target toc recursively because scope.getHref
160
// may be null.
161
IToc toc = getTocForScope(childrenScopes[i], locale);
162                     if (toc != null)
163                         return toc;
164                 }
165             }
166         }
167         return null;
168     }
169
170     /**
171      * Finds a topic in a toc or within a scope if specified
172      */

173     private IToc getTocForTopic(String JavaDoc href, String JavaDoc locale) {
174         IToc[] tocs = HelpPlugin.getTocManager().getTocs(locale);
175         for (int i = 0; i < tocs.length; i++) {
176             ITopic topic = tocs[i].getTopic(href);
177             if (topic != null)
178                 return tocs[i];
179         }
180         return null;
181     }
182
183     /**
184      * Gets the searchHits.
185      *
186      * @return Returns a SearchHit[]
187      */

188     public SearchHit[] getSearchHits() {
189         return searchHits;
190     }
191
192     /**
193      * Returns a collection of adaptable help resources that are roots for
194      * filtering.
195      *
196      * @return Collection
197      */

198     private ArrayList JavaDoc getScopes(WorkingSet[] wSets) {
199         if (wSets == null)
200             return null;
201
202         scopes = new ArrayList JavaDoc(wSets.length);
203         for (int w = 0; w < wSets.length; w++) {
204             AdaptableHelpResource[] elements = wSets[w].getElements();
205             for (int i = 0; i < elements.length; i++)
206                 scopes.add(elements[i]);
207         }
208         return scopes;
209     }
210 }
211
Popular Tags