1 11 package org.eclipse.help.internal.browser; 12 13 import java.util.ArrayList ; 14 import java.util.StringTokenizer ; 15 16 import org.eclipse.core.runtime.*; 17 import org.eclipse.help.browser.*; 18 import org.eclipse.help.internal.base.*; 19 import org.eclipse.osgi.service.environment.*; 20 import org.eclipse.osgi.util.NLS; 21 22 25 public class CustomBrowser implements IBrowser { 26 public static final String CUSTOM_BROWSER_PATH_KEY = "custom_browser_path"; 28 31 public void close() { 32 } 33 34 37 public boolean isCloseSupported() { 38 return false; 39 } 40 41 44 public void displayURL(String url) throws Exception { 45 String path = HelpBasePlugin.getDefault().getPluginPreferences() 46 .getString(CustomBrowser.CUSTOM_BROWSER_PATH_KEY); 47 48 String [] command = prepareCommand(path, url); 49 50 try { 51 Process pr = Runtime.getRuntime().exec(command); 52 Thread outConsumer = new StreamConsumer(pr.getInputStream()); 53 outConsumer.setName("Custom browser adapter output reader"); outConsumer.start(); 55 Thread errConsumer = new StreamConsumer(pr.getErrorStream()); 56 errConsumer.setName("Custom browser adapter error reader"); errConsumer.start(); 58 } catch (Exception e) { 59 HelpBasePlugin 60 .logError( 61 "Launching URL \"" + url 63 + "\" using browser program \"" + path 65 + "\" has failed. Specify another browser in help preferences.", e); 67 throw new Exception (NLS.bind(HelpBaseResources.CustomBrowser_errorLaunching, url, path)); 68 } 69 } 70 71 74 public boolean isSetLocationSupported() { 75 return false; 76 } 77 78 81 public boolean isSetSizeSupported() { 82 return false; 83 } 84 85 88 public void setLocation(int x, int y) { 89 } 90 91 94 public void setSize(int width, int height) { 95 } 96 97 104 private String [] prepareCommand(String path, String url) { 105 ArrayList tokenList = new ArrayList (); 106 StringTokenizer qTokenizer = new StringTokenizer (path.trim(), 108 "\"", true); boolean withinQuotation = false; 110 String quotedString = ""; while (qTokenizer.hasMoreTokens()) { 112 String curToken = qTokenizer.nextToken(); 113 if (curToken.equals("\"")) { if (withinQuotation) { 115 if (Constants.OS_WIN32.equalsIgnoreCase(Platform.getOS())) { 116 tokenList.add("\"" + quotedString + "\""); } else { 119 tokenList.add(quotedString); 121 } 122 } else { 123 quotedString = ""; } 125 withinQuotation = !withinQuotation; 126 continue; 127 } else if (withinQuotation) { 128 quotedString = curToken; 129 continue; 130 } else { 131 StringTokenizer parser = new StringTokenizer (curToken.trim()); 133 while (parser.hasMoreTokens()) { 134 tokenList.add(parser.nextToken()); 135 } 136 } 137 } 138 boolean substituted = false; 140 for (int i = 0; i < tokenList.size(); i++) { 141 String token = (String ) tokenList.get(i); 142 String newToken = doSubstitutions(token, url); 143 if (newToken != null) { 144 tokenList.set(i, newToken); 145 substituted = true; 146 } 147 } 148 if (!substituted) 150 tokenList.add(url); 151 152 String [] command = new String [tokenList.size()]; 153 tokenList.toArray(command); 154 return command; 155 } 156 157 167 private String doSubstitutions(String token, String url) { 168 boolean substituted = false; 169 StringBuffer newToken = new StringBuffer (token); 170 String substitutionMarker = "%1"; int index = newToken.indexOf(substitutionMarker); 172 while (index != -1) { 173 newToken.replace(index, index + substitutionMarker.length(), url); 174 index = newToken.indexOf(substitutionMarker, index + url.length()); 175 substituted = true; 176 } 177 178 if (substituted) { 179 return newToken.toString(); 180 } 181 182 return null; 183 } 184 } 185 | Popular Tags |