1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package net.sf.magicproject.tools;
28
29 /***
30 * Class used to launch a WebBrowser.
31 * <p>
32 * Extracted from method: EmbeddedKowariServer.LaunchBrowser()
33 * </p>
34 *
35 * @since 2004-07-23
36 * @author <a href="mailto:robert.turner@tucanatech.com">Robert Turner</a>
37 */
38 public final class WebBrowser {
39
40
41 private static final String ID_WIN = "Windows";
42
43 private static final String PATH_WIN = "rundll32";
44
45 private static final String FLAG_WIN = "url.dll,FileProtocolHandler";
46
47
48 private static final String ID_MACOSX = "Mac OS X";
49
50
51 private static final String FLAG_UNIX = "-remote openURL";
52
53 private static final String[] PATHES_UNIX = { "firefox", "opera",
54 "konqueror", "epiphany", "mozilla", "netscape" };
55
56 /***
57 * Constructor. This class uses static methods.
58 */
59 private WebBrowser() {
60 super();
61 }
62
63 /***
64 * Launch a browser to display the specified URL.
65 *
66 * @param url
67 * url to open.
68 * @throws Exception
69 */
70 public static void launchBrowser(String url) throws Exception {
71
72
73 if (url != null && !"".equals(url)) {
74
75
76 String os = System.getProperty("os.name");
77 boolean windows = os != null && os.startsWith(ID_WIN);
78 boolean macosx = os != null && os.startsWith(ID_MACOSX);
79
80
81 if (windows) {
82
83 WebBrowser.launchBrowserWindows(url);
84 } else if (macosx) {
85
86 WebBrowser.launchBrowserMac(url);
87 } else {
88
89
90 WebBrowser.launchBrowserUnix(url);
91 }
92 }
93 }
94
95 /***
96 * Executes a Windows command to launch the Browser.
97 *
98 * @param url
99 * String
100 * @throws Exception
101 */
102 private static void launchBrowserWindows(String url) throws Exception {
103
104
105 Runtime.getRuntime().exec(PATH_WIN + " " + FLAG_WIN + " " + url);
106 }
107
108 /***
109 * Executes a Mac command to launch the Browser.
110 *
111 * @param url
112 * String
113 * @throws Exception
114 */
115 private static void launchBrowserMac(String url) throws Exception {
116
117
118 Runtime.getRuntime().exec("open " + url);
119 }
120
121 /***
122 * Executes an Unix command to launch the Browser.
123 *
124 * @param url
125 * String
126 * @throws Exception
127 */
128 private static void launchBrowserUnix(String url) throws Exception {
129 for (String path : PATHES_UNIX) {
130 try {
131 WebBrowser.launchGeckoBrowserUnix(path, url);
132 return;
133 } catch (Exception ex) {
134
135 }
136 }
137 }
138
139 /***
140 * Executes an Unix command to launch the specified Gecko-based Browser.
141 *
142 * @param url
143 * String
144 * @throws Exception
145 */
146 private static void launchGeckoBrowserUnix(String browser, String url)
147 throws Exception {
148
149
150 String command = browser + " " + FLAG_UNIX + "(" + url + ")";
151
152
153 Process process = Runtime.getRuntime().exec(command);
154
155
156 boolean success = (process.waitFor()) == 0;
157
158
159 if (!success) {
160
161
162 command = browser + " " + url;
163 Runtime.getRuntime().exec(command);
164 }
165 }
166
167 }