001: package org.xorm.tools;
002:
003: import java.io.IOException;
004:
005: /**
006: * Taken from http://www.javaworld.com/javaworld/javatips/jw-javatip66.html
007: * @author Steven Spencer
008: *
009: * A simple, static class to display a URL in the system browser.
010: *
011: * Under Unix, the system browser is hard-coded to be 'netscape'.
012: * Netscape must be in your PATH for this to work. This has been
013: * tested with the following platforms: AIX, HP-UX and Solaris.
014: *
015: * Under Windows, this will bring up the default browser under windows,
016: * usually either Netscape or Microsoft IE. The default browser is
017: * determined by the OS. This has been tested under Windows 95/98/NT.
018: *
019: * Examples:
020: * * BrowserControl.displayURL("http://www.javaworld.com")
021: *
022: * BrowserControl.displayURL("file://c:\\docs\\index.html")
023: *
024: * BrowserContorl.displayURL("file:///user/joe/index.html");
025: *
026: * Note - you must include the url type -- either "http://" or
027: * "file://".
028: */
029: public class BrowserControl {
030: /**
031: * Display a file in the system browser. If you want to display a
032: * file, you must include the absolute path name.
033: *
034: * @param url the file's url (the url must start with either "http://"
035: or
036: * "file://").
037: */
038: public static void displayURL(String url) {
039: boolean windows = isWindowsPlatform();
040: String cmd = null;
041: try {
042: if (windows) {
043: // cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
044: cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
045: Process p = Runtime.getRuntime().exec(cmd);
046: } else {
047: // Under Unix, Netscape has to be running for the "-remote"
048: // command to work. So, we try sending the command and
049: // check for an exit value. If the exit command is 0,
050: // it worked, otherwise we need to start the browser.
051: // cmd = 'netscape -remote openURL(http://www.javaworld.com)'
052: cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + url + ")";
053: Process p = Runtime.getRuntime().exec(cmd);
054: try {
055: // wait for exit code -- if it's 0, command worked,
056: // otherwise we need to start the browser up.
057: int exitCode = p.waitFor();
058: if (exitCode != 0) {
059: // Command failed, start up the browser
060: // cmd = 'netscape http://www.javaworld.com'
061: cmd = UNIX_PATH + " " + url;
062: p = Runtime.getRuntime().exec(cmd);
063: }
064: } catch (InterruptedException x) {
065: System.err
066: .println("Error bringing up browser, cmd='"
067: + cmd + "'");
068: System.err.println("Caught: " + x);
069: }
070: }
071: } catch (IOException x) {
072: // couldn't exec browser
073: System.err.println("Could not invoke browser, command="
074: + cmd);
075: System.err.println("Caught: " + x);
076: }
077: }
078:
079: /**
080: * Try to determine whether this application is running under Windows
081: * or some other platform by examing the "os.name" property.
082: *
083: * @return true if this application is running under a Windows OS
084: */
085: public static boolean isWindowsPlatform() {
086: String os = System.getProperty("os.name");
087: if (os != null && os.startsWith(WIN_ID))
088: return true;
089: else
090: return false;
091: }
092:
093: // Used to identify the windows platform.
094: private static final String WIN_ID = "Windows";
095: // The default system browser under windows.
096: //private static final String WIN_PATH = "rundll32";
097: private static final String WIN_PATH = "cmd /c start";
098: // The flag to display a url.
099: //private static final String WIN_FLAG = "url.dll,FileProtocolHandler";
100: private static final String WIN_FLAG = "";
101:
102: // The default browser under unix.
103: private static final String UNIX_PATH = "netscape";
104: // The flag to display a url.
105: private static final String UNIX_FLAG = "-remote openURL";
106: }
|