PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/joggplayer-1.1.4s/src/kiwi/ui/WebBrowser.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 248 lines | 93 code | 30 blank | 125 comment | 13 complexity | 3c95f94a5cbabc8c2ad02d0a0afd8cf6 MD5 | raw file
  1. /* ----------------------------------------------------------------------------
  2. The Kiwi Toolkit
  3. Copyright (C) 1998-2003 Mark A. Lindner
  4. This file is part of Kiwi.
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this library; if not, write to the Free
  15. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. The author may be contacted at:
  17. mark_a_lindner@yahoo.com
  18. ----------------------------------------------------------------------------
  19. $Log: WebBrowser.java,v $
  20. Revision 1.3 2003/01/19 09:50:54 markl
  21. Javadoc & comment header updates.
  22. Revision 1.2 2002/08/11 09:57:21 markl
  23. Version number change.
  24. Revision 1.1 2002/08/11 09:50:49 markl
  25. New class.
  26. ----------------------------------------------------------------------------
  27. */
  28. package kiwi.ui;
  29. import java.io.IOException;
  30. import java.text.MessageFormat;
  31. import java.util.*;
  32. import java.net.URL;
  33. /** A class that represents a web browser and provides URL opening capability.
  34. * This class allows a Java program to open a URL in a web browser. It
  35. * currently works on Windows and UNIX systems.
  36. * <p>
  37. * On Windows, the URL is opened in the default browser by invoking the
  38. * command:
  39. * <pre>
  40. * rundll32 url.dll,FileProtocolHandler <i>url</i>
  41. * </pre>
  42. * This calling convention works on most variants of Windows, from Windows 98
  43. * to Windows 2000. It has not been tested on Windows XP.
  44. * <p>
  45. * On UNIX, this class can launch (or send a remote command to) any
  46. * Netscape-compatible browser which supports the ``-remote'' switch; this
  47. * includes Netscape itself, Mozilla, and possibly other browsers. An attempt
  48. * is first made to open the URL in a running browser, via a command like:
  49. * <pre>
  50. * netscape -remote openURL(<i>url</i>)
  51. * </pre>
  52. * and if that fails, an attempt is then made to launch the browser, via a
  53. * command like:
  54. * <pre>
  55. * netscape <i>url</i>
  56. * </pre>
  57. * In the case of an error, an exception is thrown.
  58. * <p>
  59. * On MacOS, the equivalent result can be achieved by using the method:
  60. *
  61. * <pre>
  62. * com.apple.mrj.MRJUtils.openURL(String url) throws IOException
  63. * </pre>
  64. *
  65. * Since this call relies on an OS-specific class library, MacOS support has
  66. * not been added to this class, as doing so would break the compilation on
  67. * non-MacOS systems.
  68. *
  69. * @author Mark Lindner
  70. *
  71. * @since Kiwi 1.3.4
  72. */
  73. public class WebBrowser
  74. {
  75. private boolean windows = false;
  76. private String browserPath = null;
  77. private static final String[] unixCommands
  78. = { "{0} -remote openURL({1})", "{0} {1} &" };
  79. private static final String[] unixBrowsers = { "mozilla", "netscape" };
  80. private static final String[] windowsCommands
  81. = { "rundll32 url.dll,FileProtocolHandler {1}" };
  82. private static final String[] windowsBrowsers = { "" };
  83. /**
  84. * Construct a new <code>WebBrowser</code>, with no browser path specified.
  85. * An attempt will be made to use mozilla or netscape, in that order. It
  86. * will be assumed that a browser binary can be found in the command
  87. * search path.
  88. */
  89. public WebBrowser()
  90. {
  91. windows = isWindows();
  92. }
  93. /** Specify the path to a Netscape-compatible browser (such as Netscape,
  94. * Mozilla, or any other browser that supports the ``-remote'' switch). It
  95. * is assumed (but not required) that <i>path</i> is an absolute path to
  96. * a browser executable.
  97. * <p>
  98. * This method has no effect on Windows systems.
  99. *
  100. * @param path The path to the browser.
  101. */
  102. public void setBrowserPath(String path)
  103. {
  104. if(! windows)
  105. browserPath = path;
  106. }
  107. /** Open the specified URL in the web browser.
  108. *
  109. * @param url The URL to open.
  110. * @throws java.io.IOException If the browser could not be launched.
  111. */
  112. public void openURL(URL url) throws IOException
  113. {
  114. openURL(url.toString());
  115. }
  116. /** Open the specified URL in the web browser.
  117. *
  118. * @param url The URL to open.
  119. * @throws java.io.IOException If the browser could not be launched.
  120. */
  121. public void openURL(String url) throws IOException
  122. {
  123. String commands[] = (windows ? windowsCommands : unixCommands);
  124. String browsers[] = (windows ? windowsBrowsers : unixBrowsers);
  125. boolean ok = false;
  126. if(!windows && (browserPath != null))
  127. {
  128. openURL(url, browserPath, commands);
  129. return;
  130. }
  131. else
  132. {
  133. for(int i = 0; i < browsers.length; i++)
  134. {
  135. try
  136. {
  137. openURL(url, browsers[i], commands);
  138. browserPath = browsers[i]; // save for next time
  139. return;
  140. }
  141. catch(IOException ex) { /* ignore */ }
  142. }
  143. throw(new IOException("Unable to launch browser."));
  144. }
  145. }
  146. /*
  147. */
  148. private void openURL(String url, String browser,
  149. String commandList[]) throws IOException
  150. {
  151. String cmd;
  152. int exitCode = 0;
  153. Process p;
  154. boolean wait = true;
  155. for(int i = 0; i < commandList.length; i++)
  156. {
  157. MessageFormat mf = new MessageFormat(commandList[i]);
  158. cmd = mf.format(new Object[] { browser, url });
  159. if(cmd.endsWith("&"))
  160. {
  161. wait = false;
  162. cmd = cmd.substring(0, cmd.length() - 1);
  163. }
  164. System.err.println("browser command: " + cmd);
  165. p = Runtime.getRuntime().exec(cmd);
  166. if(wait)
  167. {
  168. for(;;)
  169. {
  170. try
  171. {
  172. exitCode = p.waitFor();
  173. break;
  174. }
  175. catch(InterruptedException ex) { /* ignore */ }
  176. }
  177. if(exitCode == 0)
  178. break;
  179. }
  180. }
  181. }
  182. /*
  183. */
  184. private boolean isWindows()
  185. {
  186. String os = System.getProperty("os.name");
  187. return(os != null && os.startsWith("Windows"));
  188. }
  189. /*
  190. * For testing...
  191. */
  192. /*
  193. public static void main(String args[])
  194. {
  195. try
  196. {
  197. WebBrowser browser = new WebBrowser();
  198. browser.setBrowserPath("/usr/local/bin/netscape");
  199. browser.openURL("http://www.slashdot.org/");
  200. }
  201. catch(Exception ex)
  202. {
  203. ex.printStackTrace();
  204. }
  205. }
  206. */
  207. }
  208. /* end of source file */