1 11 package org.eclipse.ui.internal.browser.browsers; 12 13 import java.net.URL ; 14 import java.util.ArrayList ; 15 import java.util.StringTokenizer ; 16 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.osgi.service.environment.Constants; 19 import org.eclipse.osgi.util.NLS; 20 import org.eclipse.ui.PartInitException; 21 import org.eclipse.ui.browser.AbstractWebBrowser; 22 import org.eclipse.ui.internal.browser.Messages; 23 import org.eclipse.ui.internal.browser.Trace; 24 import org.eclipse.ui.internal.browser.WebBrowserUIPlugin; 25 28 public class DefaultBrowser extends AbstractWebBrowser { 29 protected String location; 30 protected String parameters; 31 32 public DefaultBrowser(String id, String location, String parameters) { 33 super(id); 34 this.location = location; 35 this.parameters = parameters; 36 } 37 38 41 public void openURL(URL url2) throws PartInitException { 42 String url = url2.toExternalForm(); 43 String path = location; 44 45 String [] command = prepareCommand(path, url); 46 Trace.trace(Trace.FINER, "Command: " + command); 48 try { 49 Process pr = Runtime.getRuntime().exec(command); 50 Thread outConsumer = new StreamConsumer(pr.getInputStream()); 51 outConsumer.setName("Custom browser adapter output reader"); outConsumer.start(); 53 Thread errConsumer = new StreamConsumer(pr.getErrorStream()); 54 errConsumer.setName("Custom browser adapter error reader"); errConsumer.start(); 56 } catch (Exception e) { 57 WebBrowserUIPlugin.logError( 58 "Launching URL \"" + url 60 + "\" using browser program \"" + path 62 + "\" has failed. Specify another browser in help preferences.", e); 64 throw new PartInitException(NLS.bind(Messages.errorCouldNotLaunchWebBrowser, path)); 65 } 66 } 67 68 75 protected String [] prepareCommand(String path, String url) { 76 ArrayList tokenList = new ArrayList (); 77 StringTokenizer qTokenizer = new StringTokenizer (path.trim(), 79 "\"", true); boolean withinQuotation = false; 81 String quotedString = ""; while (qTokenizer.hasMoreTokens()) { 83 String curToken = qTokenizer.nextToken(); 84 if (curToken.equals("\"")) { if (withinQuotation) { 86 if (Constants.OS_WIN32.equalsIgnoreCase(Platform.getOS())) { 87 tokenList.add("\"" + quotedString + "\""); } else { 90 tokenList.add(quotedString); 92 } 93 } else { 94 quotedString = ""; } 96 withinQuotation = !withinQuotation; 97 continue; 98 } else if (withinQuotation) { 99 quotedString = curToken; 100 continue; 101 } else { 102 StringTokenizer parser = new StringTokenizer (curToken.trim()); 104 while (parser.hasMoreTokens()) { 105 tokenList.add(parser.nextToken()); 106 } 107 } 108 } 109 boolean substituted = false; 111 for (int i = 0; i < tokenList.size(); i++) { 112 String token = (String ) tokenList.get(i); 113 String newToken = doSubstitutions(token, url); 114 if (newToken != null) { 115 tokenList.set(i, newToken); 116 substituted = true; 117 } 118 } 119 if (!substituted) 121 tokenList.add(url); 122 123 String [] command = new String [tokenList.size()]; 124 tokenList.toArray(command); 125 return command; 126 } 127 128 138 protected String doSubstitutions(String token, String url) { 139 boolean substituted = false; 140 StringBuffer newToken = new StringBuffer (token); 141 String substitutionMarker = "%1"; int index = newToken.indexOf(substitutionMarker); 143 while (index != -1) { 144 newToken.replace(index, index + substitutionMarker.length(), url); 145 index = newToken.indexOf(substitutionMarker, index + url.length()); 146 substituted = true; 147 } 148 149 if (substituted) { 150 return newToken.toString(); 151 } 152 153 return null; 154 } 155 } 156 | Popular Tags |