1 50 package com.lowagie.tools; 51 52 import java.io.File ; 53 import java.io.IOException ; 54 import java.lang.reflect.Method ; 55 56 59 public class Executable { 60 61 64 public static String acroread = null; 65 66 67 75 private static Process action(final String fileName, 76 String parameters, boolean waitForTermination) throws IOException { 77 Process process = null; 78 if (parameters.trim().length() > 0) { 79 parameters = " " + parameters.trim(); 80 } 81 else { 82 parameters = ""; 83 } 84 if (acroread != null) { 85 process = Runtime.getRuntime().exec( 86 acroread + parameters + " \"" + fileName + "\""); 87 } 88 else if (isWindows()) { 89 if (isWindows9X()) { 90 process = Runtime.getRuntime().exec( 91 "command.com /C start acrord32" + parameters + " \"" + fileName + "\""); 92 } 93 else { 94 process = Runtime.getRuntime().exec( 95 "cmd /c start acrord32" + parameters + " \"" + fileName + "\""); 96 } 97 } 98 else if (isMac()) { 99 if (parameters.trim().length() == 0) { 100 process = Runtime.getRuntime().exec( 101 new String [] { "/usr/bin/open", fileName }); 102 } 103 else { 104 process = Runtime.getRuntime().exec( 105 new String [] { "/usr/bin/open", parameters.trim(), fileName }); 106 } 107 } 108 try { 109 if (process != null && waitForTermination) 110 process.waitFor(); 111 } catch (InterruptedException ie) { 112 } 113 return process; 114 } 115 116 123 public static final Process openDocument(String fileName, 124 boolean waitForTermination) throws IOException { 125 return action(fileName, "", waitForTermination); 126 } 127 128 135 public static final Process openDocument(File file, 136 boolean waitForTermination) throws IOException { 137 return openDocument(file.getAbsolutePath(), waitForTermination); 138 } 139 140 146 public static final Process openDocument(String fileName) throws IOException { 147 return openDocument(fileName, false); 148 } 149 150 156 public static final Process openDocument(File file) throws IOException { 157 return openDocument(file, false); 158 } 159 160 167 public static final Process printDocument(String fileName, 168 boolean waitForTermination) throws IOException { 169 return action(fileName, "/p", waitForTermination); 170 } 171 172 179 public static final Process printDocument(File file, 180 boolean waitForTermination) throws IOException { 181 return printDocument(file.getAbsolutePath(), waitForTermination); 182 } 183 184 190 public static final Process printDocument(String fileName) throws IOException { 191 return printDocument(fileName, false); 192 } 193 194 200 public static final Process printDocument(File file) throws IOException { 201 return printDocument(file, false); 202 } 203 204 211 public static final Process printDocumentSilent(String fileName, 212 boolean waitForTermination) throws IOException { 213 return action(fileName, "/p /h", waitForTermination); 214 } 215 216 223 public static final Process printDocumentSilent(File file, 224 boolean waitForTermination) throws IOException { 225 return printDocumentSilent(file.getAbsolutePath(), waitForTermination); 226 } 227 228 234 public static final Process printDocumentSilent(String fileName) throws IOException { 235 return printDocumentSilent(fileName, false); 236 } 237 238 244 public static final Process printDocumentSilent(File file) throws IOException { 245 return printDocumentSilent(file, false); 246 } 247 248 254 public static final void launchBrowser(String url) throws IOException { 255 try { 256 if (isMac()) { 257 Class macUtils = Class.forName("com.apple.mrj.MRJFileUtils"); 258 Method openURL = macUtils.getDeclaredMethod("openURL", new Class [] {String .class}); 259 openURL.invoke(null, new Object [] {url}); 260 } 261 else if (isWindows()) 262 Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url); 263 else { String [] browsers = { 265 "firefox", "opera", "konqueror", "mozilla", "netscape" }; 266 String browser = null; 267 for (int count = 0; count < browsers.length && browser == null; count++) 268 if (Runtime.getRuntime().exec(new String [] {"which", browsers[count]}).waitFor() == 0) 269 browser = browsers[count]; 270 if (browser == null) 271 throw new Exception ("Could not find web browser."); 272 else 273 Runtime.getRuntime().exec(new String [] {browser, url}); 274 } 275 } 276 catch (Exception e) { 277 throw new IOException ("Error attempting to launch web browser"); 278 } 279 } 280 281 286 public static boolean isWindows() { 287 String os = System.getProperty("os.name").toLowerCase(); 288 return os.indexOf("windows") != -1 || os.indexOf("nt") != -1; 289 } 290 291 296 public static boolean isWindows9X() { 297 String os = System.getProperty("os.name").toLowerCase(); 298 return os.equals("windows 95") || os.equals("windows 98"); 299 } 300 301 306 public static boolean isMac() { 307 String os = System.getProperty("os.name").toLowerCase(); 308 return os.indexOf("mac") != -1; 309 } 310 311 316 public static boolean isLinux() { 317 String os = System.getProperty("os.name").toLowerCase(); 318 return os.indexOf("linux") != -1; 319 } 320 } | Popular Tags |