1 11 package org.eclipse.help.internal.base; 12 13 import java.io.UnsupportedEncodingException ; 14 import java.net.URLEncoder ; 15 16 import org.eclipse.core.runtime.Platform; 17 import org.eclipse.help.IContext; 18 import org.eclipse.help.IHelpResource; 19 import org.eclipse.help.IToc; 20 import org.eclipse.help.internal.HelpPlugin; 21 import org.eclipse.help.internal.context.Context; 22 import org.eclipse.help.internal.server.WebappManager; 23 import org.eclipse.osgi.util.NLS; 24 25 29 public class HelpDisplay { 30 31 34 public HelpDisplay() { 35 super(); 36 } 37 38 41 public void displayHelp(boolean forceExternal) { 42 displayHelpURL(null, forceExternal); 43 } 44 45 57 public void displayHelpResource(String href, boolean forceExternal) { 58 IToc toc = HelpPlugin.getTocManager().getToc(href, Platform.getNL()); 60 if (toc != null) 61 try { 62 displayHelpURL( 63 "toc=" + URLEncoder.encode(toc.getHref(), "UTF-8"), forceExternal); } catch (UnsupportedEncodingException uee) { 65 } 66 else if (href != null && (href.startsWith("tab=") || href.startsWith("toc=") || href.startsWith("topic=") || href.startsWith("contextId="))) { displayHelpURL(href, forceExternal); 71 } else { if (getNoframesURL(href) == null) { 73 try { 74 displayHelpURL( 75 "topic=" + URLEncoder.encode(href, "UTF-8"), forceExternal); } catch (UnsupportedEncodingException uee) { 77 } 78 } else if (href.startsWith("jar:") || href.startsWith("platform:")) { displayHelpURL( 81 getBaseURL() + "nftopic/" + getNoframesURL(href), true); } else { 83 displayHelpURL(getNoframesURL(href), true); 84 } 85 } 86 } 87 88 96 public void displayHelp(IContext context, IHelpResource topic, 97 boolean forceExternal) { 98 if (context == null || topic == null || topic.getHref() == null) 99 return; 100 String topicURL = getTopicURL(topic.getHref()); 101 if (getNoframesURL(topicURL) == null) { 102 try { 103 String url = "tab=links" + "&contextId=" + URLEncoder.encode(getContextID(context), "UTF-8") + "&topic=" + URLEncoder.encode(topicURL, "UTF-8"); displayHelpURL(url, forceExternal); 109 } catch (UnsupportedEncodingException uee) { 110 } 111 112 } else if (topicURL.startsWith("jar:file:")) { displayHelpURL( 115 getBaseURL() + "nftopic/" + getNoframesURL(topicURL), true); } else { 117 displayHelpURL(getNoframesURL(topicURL), true); 118 } 119 } 120 121 129 public void displaySearch(String searchQuery, String topic, 130 boolean forceExternal) { 131 if (searchQuery == null || topic == null) 132 return; 133 if (getNoframesURL(topic) == null) { 134 try { 135 String url = "tab=search&" + searchQuery + "&topic=" + URLEncoder.encode(getTopicURL(topic), "UTF-8"); displayHelpURL(url, forceExternal); 139 } catch (UnsupportedEncodingException uee) { 140 } 141 142 } else { 143 displayHelpURL(getNoframesURL(topic), true); 144 } 145 } 146 147 151 private void displayHelpURL(String helpURL, boolean forceExternal) { 152 if (!BaseHelpSystem.ensureWebappRunning()) { 153 return; 154 } 155 if (BaseHelpSystem.getMode() == BaseHelpSystem.MODE_STANDALONE) { 156 DisplayUtils.waitForDisplay(); 158 } 159 160 try { 161 176 if (helpURL == null || helpURL.length() == 0) { 177 helpURL = getFramesetURL(); 178 } else if (helpURL.startsWith("tab=") || helpURL.startsWith("toc=") || helpURL.startsWith("topic=") || helpURL.startsWith("contextId=")) { helpURL = getFramesetURL() + "?" + helpURL; } 184 BaseHelpSystem.getHelpBrowser(forceExternal) 185 .displayURL(helpURL); 186 } catch (Exception e) { 187 HelpBasePlugin 188 .logError( 189 "An exception occurred while launching help. Check the log at " + Platform.getLogFileLocation().toOSString(), e); BaseHelpSystem.getDefaultErrorUtil() 191 .displayError( 192 NLS.bind(HelpBaseResources.HelpDisplay_exceptionMessage, Platform.getLogFileLocation().toOSString())); 193 } 194 } 195 196 private String getContextID(IContext context) { 197 if (context instanceof Context) { 198 return ((Context)context).getId(); 199 } 200 return HelpPlugin.getContextManager().addContext(context); 201 } 202 203 private String getBaseURL() { 204 return "http://" + WebappManager.getHost() + ":" + WebappManager.getPort() + "/help/"; } 208 209 private String getFramesetURL() { 210 return getBaseURL() + "index.jsp"; } 212 213 private String getTopicURL(String topic) { 214 if (topic == null) 215 return null; 216 if (topic.startsWith("../")) topic = topic.substring(2); 218 223 return topic; 224 } 225 226 233 private String getNoframesURL(String href) { 234 if (href == null) { 235 return null; 236 } 237 int ix = href.indexOf("?noframes=true&"); if (ix >= 0) { 239 return href.substring(0, ix + 1) 241 + href.substring(ix + "?noframes=true&".length()); 243 } 244 ix = href.indexOf("noframes=true"); if (ix > 0) { 246 return href.substring(0, ix - 1) 248 + href.substring(ix + "noframes=true".length()); } 250 return null; 252 } 253 254 } 255 | Popular Tags |