KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > browser > CustomBrowser


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.help.internal.browser;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.StringTokenizer JavaDoc;
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 /**
23  *
24  */

25 public class CustomBrowser implements IBrowser {
26     public static final String JavaDoc CUSTOM_BROWSER_PATH_KEY = "custom_browser_path"; //$NON-NLS-1$
27

28     /**
29      * @see org.eclipse.help.browser.IBrowser#close()
30      */

31     public void close() {
32     }
33
34     /**
35      * @see org.eclipse.help.browser.IBrowser#isCloseSupported()
36      */

37     public boolean isCloseSupported() {
38         return false;
39     }
40
41     /**
42      * @see org.eclipse.help.browser.IBrowser#displayURL(java.lang.String)
43      */

44     public void displayURL(String JavaDoc url) throws Exception JavaDoc {
45         String JavaDoc path = HelpBasePlugin.getDefault().getPluginPreferences()
46                 .getString(CustomBrowser.CUSTOM_BROWSER_PATH_KEY);
47
48         String JavaDoc[] command = prepareCommand(path, url);
49
50         try {
51             Process JavaDoc pr = Runtime.getRuntime().exec(command);
52             Thread JavaDoc outConsumer = new StreamConsumer(pr.getInputStream());
53             outConsumer.setName("Custom browser adapter output reader"); //$NON-NLS-1$
54
outConsumer.start();
55             Thread JavaDoc errConsumer = new StreamConsumer(pr.getErrorStream());
56             errConsumer.setName("Custom browser adapter error reader"); //$NON-NLS-1$
57
errConsumer.start();
58         } catch (Exception JavaDoc e) {
59             HelpBasePlugin
60                     .logError(
61                             "Launching URL \"" //$NON-NLS-1$
62
+ url
63                                     + "\" using browser program \"" //$NON-NLS-1$
64
+ path
65                                     + "\" has failed. Specify another browser in help preferences.", //$NON-NLS-1$
66
e);
67             throw new Exception JavaDoc(NLS.bind(HelpBaseResources.CustomBrowser_errorLaunching, url, path));
68         }
69     }
70
71     /**
72      * @see org.eclipse.help.browser.IBrowser#isSetLocationSupported()
73      */

74     public boolean isSetLocationSupported() {
75         return false;
76     }
77
78     /**
79      * @see org.eclipse.help.browser.IBrowser#isSetSizeSupported()
80      */

81     public boolean isSetSizeSupported() {
82         return false;
83     }
84
85     /**
86      * @see org.eclipse.help.browser.IBrowser#setLocation(int, int)
87      */

88     public void setLocation(int x, int y) {
89     }
90
91     /**
92      * @see org.eclipse.help.browser.IBrowser#setSize(int, int)
93      */

94     public void setSize(int width, int height) {
95     }
96
97     /**
98      * Creates the final command to launch.
99      *
100      * @param path
101      * @param url
102      * @return String[]
103      */

104     private String JavaDoc[] prepareCommand(String JavaDoc path, String JavaDoc url) {
105         ArrayList JavaDoc tokenList = new ArrayList JavaDoc();
106         //Divide along quotation marks
107
StringTokenizer JavaDoc qTokenizer = new StringTokenizer JavaDoc(path.trim(),
108                 "\"", true); //$NON-NLS-1$
109
boolean withinQuotation = false;
110         String JavaDoc quotedString = ""; //$NON-NLS-1$
111
while (qTokenizer.hasMoreTokens()) {
112             String JavaDoc curToken = qTokenizer.nextToken();
113             if (curToken.equals("\"")) { //$NON-NLS-1$
114
if (withinQuotation) {
115                     if (Constants.OS_WIN32.equalsIgnoreCase(Platform.getOS())) {
116                         // need to quote URLs on Windows
117
tokenList.add("\"" + quotedString + "\""); //$NON-NLS-1$ //$NON-NLS-2$
118
} else {
119                         // qotes prevent launching on Unix 35673
120
tokenList.add(quotedString);
121                     }
122                 } else {
123                     quotedString = ""; //$NON-NLS-1$
124
}
125                 withinQuotation = !withinQuotation;
126                 continue;
127             } else if (withinQuotation) {
128                 quotedString = curToken;
129                 continue;
130             } else {
131                 //divide unquoted strings along white space
132
StringTokenizer JavaDoc parser = new StringTokenizer JavaDoc(curToken.trim());
133                 while (parser.hasMoreTokens()) {
134                     tokenList.add(parser.nextToken());
135                 }
136             }
137         }
138         // substitute %1 by url
139
boolean substituted = false;
140         for (int i = 0; i < tokenList.size(); i++) {
141             String JavaDoc token = (String JavaDoc) tokenList.get(i);
142             String JavaDoc newToken = doSubstitutions(token, url);
143             if (newToken != null) {
144                 tokenList.set(i, newToken);
145                 substituted = true;
146             }
147         }
148         // add the url if not substituted already
149
if (!substituted)
150             tokenList.add(url);
151
152         String JavaDoc[] command = new String JavaDoc[tokenList.size()];
153         tokenList.toArray(command);
154         return command;
155     }
156     
157     /**
158      * Replaces any occurrences of <code>"%1"</code> or <code>%1</code> with
159      * the URL.
160      *
161      * @param token
162      * The token in which the substitutions should be made; must not
163      * be <code>null</code>.
164      * @return The substituted string, if a substitution is made;
165      * <code>null</code> if no substitution is made.
166      */

167     private String JavaDoc doSubstitutions(String JavaDoc token, String JavaDoc url) {
168         boolean substituted = false;
169         StringBuffer JavaDoc newToken = new StringBuffer JavaDoc(token);
170         String JavaDoc substitutionMarker = "%1"; //$NON-NLS-1$
171
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