1 11 package org.eclipse.help.internal.search; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 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 29 public class SearchResults implements ISearchHitCollector { 30 private ArrayList scopes; 32 private int maxHits; 33 private String locale; 34 protected SearchHit[] searchHits = new SearchHit[0]; 35 41 public SearchResults(WorkingSet[] workingSets, int maxHits, String locale) { 42 this.maxHits = maxHits; 43 this.locale = locale; 44 this.scopes = getScopes(workingSets); 45 } 46 47 50 public void addHits(List hits, String highlightTerms) { 51 String urlEncodedWords = URLCoder.encode(highlightTerms); 52 List searchHitList = new ArrayList (); 53 float scoreScale = 1.0f; 54 boolean scoreScaleSet = false; 55 56 Iterator iter = hits.iterator(); 57 for (int i=0;i<maxHits && iter.hasNext();i++) { 58 SearchHit rawHit = (SearchHit)iter.next(); 59 String href = rawHit.getHref(); 60 IToc toc = null; AdaptableHelpResource scope = null; 62 if (scopes == null) { 64 toc = getTocForTopic(href, locale); 65 } else { 66 scope = getScopeForTopic(href); 67 if (scope == null) { 68 continue; 70 } else if (scope instanceof AdaptableToc) { 71 toc = (IToc) scope.getAdapter(IToc.class); 72 } else { toc = (IToc) scope.getParent().getAdapter(IToc.class); 74 } 75 } 76 77 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 String label = rawHit.getLabel(); 91 if ("".equals(label) && toc != null) { 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)) { label = href; 104 } 105 106 if (urlEncodedWords.length() > 0) { 108 href += "?resultof=" + urlEncodedWords; } 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 119 private AdaptableHelpResource getScopeForTopic(String 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 IToc tocRoot = getTocForScope(scope, locale); 127 if (tocRoot != null) { 128 IToc toc = HelpPlugin.getTocManager().getOwningToc(href); 129 if (toc != null) { 130 String owningTocHref = toc.getHref(); 131 if (owningTocHref == tocRoot.getHref()) { 132 return scope; 133 } 134 } 135 } 136 } 137 return null; 138 } 139 140 143 private IToc getTocForScope(AdaptableHelpResource scope, String locale) { 144 if (scope == null) { 145 return null; 146 } 147 String 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 IToc toc = getTocForScope(childrenScopes[i], locale); 162 if (toc != null) 163 return toc; 164 } 165 } 166 } 167 return null; 168 } 169 170 173 private IToc getTocForTopic(String href, String 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 188 public SearchHit[] getSearchHits() { 189 return searchHits; 190 } 191 192 198 private ArrayList getScopes(WorkingSet[] wSets) { 199 if (wSets == null) 200 return null; 201 202 scopes = new ArrayList (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 |