View Javadoc
1 /* 2 * ==================================================================== 3 * 4 * This code is subject to the freebxml License, Version 1.1 5 * 6 * Copyright (c) 2003 freebxml.org. All rights reserved. 7 * 8 * ==================================================================== 9 */ 10 package com.sun.xml.registry.client.browser; 11 12 import java.io.IOException; 13 14 import com.sun.xml.registry.ebxml.util.ProviderProperties; 15 16 /* 17 * Based upon code from: 18 * http://forum.java.sun.com/thread.jsp?thread=328882&forum=57&message=1337973 19 * 20 * Class is used to open a browser and show a defined URL/FILE 21 * if not running in an Applet. (Running 22 * in an Applet this will not work, use myApplet.getAppletContext 23 * ().showDocument(URL); 24 * If a browser window is opened, it will be reused to show the 25 * document (no new one will be opened) 26 */ 27 public class HyperLinker { 28 29 /*** 30 * Display a file in the system browser. If you want to 31 * display a 32 * file, you must include the absolute path name. 33 * 34 * @param url the file's url (the url must start with 35 * either "http://" or * "file://"). 36 * @return is true, if displaying was possible (at leas no 37 * error occures) 38 */ 39 public static boolean displayURL(String url) { 40 41 boolean result = true; 42 boolean windows = isWindowsPlatform(); 43 String cmd = ProviderProperties.getInstance().getProperty("jaxr-ebxml.registryBrowser.webBrowser.launch"); 44 45 46 try { 47 if (cmd == null) { 48 //Set cmd from hardwired platform specific defaults 49 if (windows) { 50 cmd = "rundll32 url.dll,FileProtocolHandler " + url; 51 } 52 else { 53 cmd = "netscape -remote openURL ( " + url + " )"; 54 } 55 56 } else { 57 //Replace url parameter with actual URL 58 int index = cmd.indexOf("$url"); 59 if (index != -1) { 60 cmd = cmd.substring(0, index) + url + cmd.substring(index+4); 61 } 62 } 63 64 if (windows) { 65 Process p = Runtime.getRuntime().exec(cmd); 66 } else { 67 // Under Unix, Netscape/Mozilla has to be running for the "- 68 //remote" 69 // command to work. So, we try sending the command and 70 // check for an exit value. If the exit command is 0, 71 // it worked, otherwise we need to start the browser. 72 // cmd = 'netscape -remote openURL 73 //(http://www.javaworld.com)' 74 75 Process p = Runtime.getRuntime().exec(cmd); 76 77 try { 78 // wait for exit code -- if it's 0, command worked, 79 // otherwise we need to start the browser up. 80 int exitCode = p.waitFor(); 81 82 if (exitCode != 0) { 83 // Command failed, start up the browser 84 // cmd = 'netscape http://www.javaworld.com' 85 cmd = UNIX_PATH + " " + url; 86 p = Runtime.getRuntime().exec(cmd); 87 } 88 } catch(InterruptedException x) { 89 result = false; 90 RegistryBrowser.displayError("Could not invoke browser with command=" + 91 cmd + "\nPlease set property ", x); 92 } 93 } 94 } catch(IOException ex) { 95 // couldn't exec browser 96 result = false; 97 RegistryBrowser.displayError("Could not invoke browser with command=" + 98 cmd + "\nPlease set property ", ex); 99 } 100 return result; 101 } 102 103 /*** 104 * Try to determine whether this application is running under 105 * Windows 106 * or some other platform by examing the "os.name" property. 107 * 108 * @return true if this application is running under a Windows 109 * OS 110 */ 111 public static boolean isWindowsPlatform() { 112 String os = System.getProperty("os.name"); 113 if ( os != null && os.startsWith(WIN_ID)) 114 return true; 115 else 116 return false; 117 } 118 119 /*** 120 * Simple example. Opens the url to "http://www.javaworld.com" 121 */ 122 public static void main(String[] args) { 123 displayURL("http://ebxmlrr.sourceforge.net"); 124 } 125 126 // Used to identify the windows platform. 127 private static final String WIN_ID = "Windows"; 128 129 // The default system browser under windows. 130 private static final String WIN_PATH = "rundll32"; 131 132 // The flag to display a url. 133 private static final String WIN_FLAG 134 = "url.dll,FileProtocolHandler"; 135 136 // The default browser under unix. 137 private static final String UNIX_PATH = "mozilla"; 138 139 // The flag to display a url. 140 private static final String UNIX_FLAG = "-remote openURL"; 141 }

This page was automatically generated by Maven