1 20 21 package org.jdesktop.jdic.desktop.internal.impl; 22 23 import java.io.IOException ; 24 import java.net.URL ; 25 26 import org.jdesktop.jdic.desktop.internal.BrowserService; 27 import org.jdesktop.jdic.desktop.internal.LaunchFailedException; 28 29 32 public class GnomeBrowserService implements BrowserService { 33 static { 34 System.loadLibrary("jdic"); 35 } 36 37 private static final String MOZILLA_NAME_PATTERN = "mozilla"; 39 40 43 private static final String [] RESERVED_TARGET_NAMES = {"_blank", "new-window", "new-tab"}; 44 45 53 private String convertTargetName(String target) { 54 boolean isTargetNameReserved = false; 55 for (int index = 0; index < RESERVED_TARGET_NAMES.length; index++) { 56 if (target.equals(RESERVED_TARGET_NAMES[index])) { 57 isTargetNameReserved = true; 58 break; 59 } 60 } 61 62 if (!isTargetNameReserved) { 63 return target; 64 } else { 65 if (target.equals("_blank")) { 66 return "new-window"; 67 } else { 68 char reversedChars[] = new char[target.length()]; 71 for (int i = 0; i < target.length(); i++) 72 reversedChars[i] = target.charAt(target.length() - i - 1); 73 74 String reversedTarget = new String (reversedChars); 75 76 String convertedTarget = reversedTarget + "?" 78 + reversedTarget + "?" 79 + reversedTarget; 80 return convertedTarget; 81 } 82 } 83 } 84 85 92 public void show(URL url) throws LaunchFailedException { 93 if (!nativeBrowseURL(url.toString())) { 94 throw new LaunchFailedException("Failed to launch the default browser."); 95 } 96 } 97 98 106 public void show(URL url, String target) throws LaunchFailedException { 107 111 String mozillaPath = MOZILLA_NAME_PATTERN; 112 113 boolean result = browseURLInMozilla(mozillaPath, url, target); 114 if (!result) { 115 throw new LaunchFailedException("Failed to launch mozilla."); 116 } 117 } 118 119 126 public boolean browseURLInMozilla(String browserPath, URL url, String target) { 127 String MOZILLA_REQUIRED_VERSION_NUMBER = "1.4"; 128 String urlStr = url.toString(); 129 try { 130 String verNum = GnomeUtility.getMozillaVersionNumber(browserPath); 131 if (verNum == null || !(verNum.compareToIgnoreCase(MOZILLA_REQUIRED_VERSION_NUMBER) > 0)) { 132 return false; 134 } else { 135 if (!GnomeUtility.isMozillaRunning(browserPath)) { 137 return false; 138 } 139 140 String convertedTargetName = convertTargetName(target); 143 Runtime.getRuntime().exec(new String [] { 144 browserPath, "-remote", "openurl(" + urlStr + "," + convertedTargetName + ")" }); 145 146 return true; 147 } 148 } catch (IOException e) { 149 return false; 150 } 151 } 152 153 private native boolean nativeBrowseURL(String urlStr); 154 } 155 | Popular Tags |