KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > browser > browsers > DefaultBrowser


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.ui.internal.browser.browsers;
12
13 import java.net.URL JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.StringTokenizer JavaDoc;
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 /**
26  *
27  */

28 public class DefaultBrowser extends AbstractWebBrowser {
29     protected String JavaDoc location;
30     protected String JavaDoc parameters;
31     
32     public DefaultBrowser(String JavaDoc id, String JavaDoc location, String JavaDoc parameters) {
33         super(id);
34         this.location = location;
35         this.parameters = parameters;
36     }
37
38     /**
39      * @see org.eclipse.help.browser.IBrowser#displayURL(java.lang.String)
40      */

41     public void openURL(URL JavaDoc url2) throws PartInitException {
42         String JavaDoc url = url2.toExternalForm();
43         String JavaDoc path = location;
44
45         String JavaDoc[] command = prepareCommand(path, url);
46         Trace.trace(Trace.FINER, "Command: " + command); //$NON-NLS-1$
47

48         try {
49             Process JavaDoc pr = Runtime.getRuntime().exec(command);
50             Thread JavaDoc outConsumer = new StreamConsumer(pr.getInputStream());
51             outConsumer.setName("Custom browser adapter output reader"); //$NON-NLS-1$
52
outConsumer.start();
53             Thread JavaDoc errConsumer = new StreamConsumer(pr.getErrorStream());
54             errConsumer.setName("Custom browser adapter error reader"); //$NON-NLS-1$
55
errConsumer.start();
56         } catch (Exception JavaDoc e) {
57             WebBrowserUIPlugin.logError(
58                 "Launching URL \"" //$NON-NLS-1$
59
+ url
60                     + "\" using browser program \"" //$NON-NLS-1$
61
+ path
62                     + "\" has failed. Specify another browser in help preferences.", //$NON-NLS-1$
63
e);
64             throw new PartInitException(NLS.bind(Messages.errorCouldNotLaunchWebBrowser, path));
65         }
66     }
67
68     /**
69      * Creates the final command to launch.
70      *
71      * @param path
72      * @param url
73      * @return String[]
74      */

75     protected String JavaDoc[] prepareCommand(String JavaDoc path, String JavaDoc url) {
76         ArrayList JavaDoc tokenList = new ArrayList JavaDoc();
77         //Divide along quotation marks
78
StringTokenizer JavaDoc qTokenizer = new StringTokenizer JavaDoc(path.trim(),
79             "\"", true); //$NON-NLS-1$
80
boolean withinQuotation = false;
81         String JavaDoc quotedString = ""; //$NON-NLS-1$
82
while (qTokenizer.hasMoreTokens()) {
83             String JavaDoc curToken = qTokenizer.nextToken();
84             if (curToken.equals("\"")) { //$NON-NLS-1$
85
if (withinQuotation) {
86                     if (Constants.OS_WIN32.equalsIgnoreCase(Platform.getOS())) {
87                         // need to quote URLs on Windows
88
tokenList.add("\"" + quotedString + "\""); //$NON-NLS-1$ //$NON-NLS-2$
89
} else {
90                         // quotes prevent launching on Unix 35673
91
tokenList.add(quotedString);
92                     }
93                 } else {
94                     quotedString = ""; //$NON-NLS-1$
95
}
96                 withinQuotation = !withinQuotation;
97                 continue;
98             } else if (withinQuotation) {
99                 quotedString = curToken;
100                 continue;
101             } else {
102                 //divide unquoted strings along white space
103
StringTokenizer JavaDoc parser = new StringTokenizer JavaDoc(curToken.trim());
104                 while (parser.hasMoreTokens()) {
105                     tokenList.add(parser.nextToken());
106                 }
107             }
108         }
109         // substitute %1 by url
110
boolean substituted = false;
111         for (int i = 0; i < tokenList.size(); i++) {
112             String JavaDoc token = (String JavaDoc) tokenList.get(i);
113             String JavaDoc newToken = doSubstitutions(token, url);
114             if (newToken != null) {
115                 tokenList.set(i, newToken);
116                 substituted = true;
117             }
118         }
119         // add the url if not substituted already
120
if (!substituted)
121             tokenList.add(url);
122
123         String JavaDoc[] command = new String JavaDoc[tokenList.size()];
124         tokenList.toArray(command);
125         return command;
126     }
127     
128     /**
129      * Replaces any occurrences of <code>"%1"</code> or <code>%1</code> with
130      * the URL.
131      *
132      * @param token
133      * The token in which the substitutions should be made; must not
134      * be <code>null</code>.
135      * @return The substituted string, if a substitution is made;
136      * <code>null</code> if no substitution is made.
137      */

138     protected String JavaDoc doSubstitutions(String JavaDoc token, String JavaDoc url) {
139         boolean substituted = false;
140         StringBuffer JavaDoc newToken = new StringBuffer JavaDoc(token);
141         String JavaDoc substitutionMarker = "%1"; //$NON-NLS-1$
142
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