KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > tools > Executable


1 /*
2  * $Id: Executable.java 2352 2006-09-12 08:27:37Z blowagie $
3  * $Name$
4  *
5  * Copyright 2005 by Bruno Lowagie / Roger Mistelli
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50 package com.lowagie.tools;
51
52 import java.io.File JavaDoc;
53 import java.io.IOException JavaDoc;
54 import java.lang.reflect.Method JavaDoc;
55
56 /**
57  * This class enables you to call an executable that will show a PDF file.
58  */

59 public class Executable {
60     
61     /**
62      * The path to Acrobat Reader.
63      */

64     public static String JavaDoc acroread = null;
65
66     
67     /**
68      * Performs an action on a PDF document.
69      * @param fileName
70      * @param parameters
71      * @param waitForTermination
72      * @return a process
73      * @throws IOException
74      */

75     private static Process JavaDoc action(final String JavaDoc fileName,
76             String JavaDoc parameters, boolean waitForTermination) throws IOException JavaDoc {
77         Process JavaDoc 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 JavaDoc[] { "/usr/bin/open", fileName });
102             }
103             else {
104                 process = Runtime.getRuntime().exec(
105                         new String JavaDoc[] { "/usr/bin/open", parameters.trim(), fileName });
106             }
107         }
108         try {
109             if (process != null && waitForTermination)
110                 process.waitFor();
111         } catch (InterruptedException JavaDoc ie) {
112         }
113         return process;
114     }
115     
116     /**
117      * Opens a PDF document.
118      * @param fileName
119      * @param waitForTermination
120      * @return a process
121      * @throws IOException
122      */

123     public static final Process JavaDoc openDocument(String JavaDoc fileName,
124             boolean waitForTermination) throws IOException JavaDoc {
125         return action(fileName, "", waitForTermination);
126     }
127
128     /**
129      * Opens a PDF document.
130      * @param file
131      * @param waitForTermination
132      * @return a process
133      * @throws IOException
134      */

135     public static final Process JavaDoc openDocument(File JavaDoc file,
136             boolean waitForTermination) throws IOException JavaDoc {
137         return openDocument(file.getAbsolutePath(), waitForTermination);
138     }
139
140     /**
141      * Opens a PDF document.
142      * @param fileName
143      * @return a process
144      * @throws IOException
145      */

146     public static final Process JavaDoc openDocument(String JavaDoc fileName) throws IOException JavaDoc {
147         return openDocument(fileName, false);
148     }
149
150     /**
151      * Opens a PDF document.
152      * @param file
153      * @return a process
154      * @throws IOException
155      */

156     public static final Process JavaDoc openDocument(File JavaDoc file) throws IOException JavaDoc {
157         return openDocument(file, false);
158     }
159     
160     /**
161      * Prints a PDF document.
162      * @param fileName
163      * @param waitForTermination
164      * @return a process
165      * @throws IOException
166      */

167     public static final Process JavaDoc printDocument(String JavaDoc fileName,
168             boolean waitForTermination) throws IOException JavaDoc {
169         return action(fileName, "/p", waitForTermination);
170     }
171
172     /**
173      * Prints a PDF document.
174      * @param file
175      * @param waitForTermination
176      * @return a process
177      * @throws IOException
178      */

179     public static final Process JavaDoc printDocument(File JavaDoc file,
180             boolean waitForTermination) throws IOException JavaDoc {
181         return printDocument(file.getAbsolutePath(), waitForTermination);
182     }
183
184     /**
185      * Prints a PDF document.
186      * @param fileName
187      * @return a process
188      * @throws IOException
189      */

190     public static final Process JavaDoc printDocument(String JavaDoc fileName) throws IOException JavaDoc {
191         return printDocument(fileName, false);
192     }
193
194     /**
195      * Prints a PDF document.
196      * @param file
197      * @return a process
198      * @throws IOException
199      */

200     public static final Process JavaDoc printDocument(File JavaDoc file) throws IOException JavaDoc {
201         return printDocument(file, false);
202     }
203     
204     /**
205      * Prints a PDF document without opening a Dialog box.
206      * @param fileName
207      * @param waitForTermination
208      * @return a process
209      * @throws IOException
210      */

211     public static final Process JavaDoc printDocumentSilent(String JavaDoc fileName,
212             boolean waitForTermination) throws IOException JavaDoc {
213         return action(fileName, "/p /h", waitForTermination);
214     }
215
216     /**
217      * Prints a PDF document without opening a Dialog box.
218      * @param file
219      * @param waitForTermination
220      * @return a process
221      * @throws IOException
222      */

223     public static final Process JavaDoc printDocumentSilent(File JavaDoc file,
224             boolean waitForTermination) throws IOException JavaDoc {
225         return printDocumentSilent(file.getAbsolutePath(), waitForTermination);
226     }
227
228     /**
229      * Prints a PDF document without opening a Dialog box.
230      * @param fileName
231      * @return a process
232      * @throws IOException
233      */

234     public static final Process JavaDoc printDocumentSilent(String JavaDoc fileName) throws IOException JavaDoc {
235         return printDocumentSilent(fileName, false);
236     }
237
238     /**
239      * Prints a PDF document without opening a Dialog box.
240      * @param file
241      * @return a process
242      * @throws IOException
243      */

244     public static final Process JavaDoc printDocumentSilent(File JavaDoc file) throws IOException JavaDoc {
245         return printDocumentSilent(file, false);
246     }
247     
248     /**
249      * Launches a browser opening an URL.
250      *
251      * @param url the URL you want to open in the browser
252      * @throws IOException
253      */

254     public static final void launchBrowser(String JavaDoc url) throws IOException JavaDoc {
255         try {
256             if (isMac()) {
257                 Class JavaDoc macUtils = Class.forName("com.apple.mrj.MRJFileUtils");
258                 Method JavaDoc openURL = macUtils.getDeclaredMethod("openURL", new Class JavaDoc[] {String JavaDoc.class});
259                 openURL.invoke(null, new Object JavaDoc[] {url});
260             }
261             else if (isWindows())
262                 Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
263             else { //assume Unix or Linux
264
String JavaDoc[] browsers = {
265                    "firefox", "opera", "konqueror", "mozilla", "netscape" };
266                 String JavaDoc browser = null;
267                 for (int count = 0; count < browsers.length && browser == null; count++)
268                    if (Runtime.getRuntime().exec(new String JavaDoc[] {"which", browsers[count]}).waitFor() == 0)
269                       browser = browsers[count];
270                 if (browser == null)
271                    throw new Exception JavaDoc("Could not find web browser.");
272                 else
273                    Runtime.getRuntime().exec(new String JavaDoc[] {browser, url});
274                 }
275              }
276           catch (Exception JavaDoc e) {
277              throw new IOException JavaDoc("Error attempting to launch web browser");
278           }
279     }
280
281     /**
282      * Checks the Operating System.
283      *
284      * @return true if the current os is Windows
285      */

286     public static boolean isWindows() {
287         String JavaDoc os = System.getProperty("os.name").toLowerCase();
288         return os.indexOf("windows") != -1 || os.indexOf("nt") != -1;
289     }
290
291     /**
292      * Checks the Operating System.
293      *
294      * @return true if the current os is Windows
295      */

296     public static boolean isWindows9X() {
297         String JavaDoc os = System.getProperty("os.name").toLowerCase();
298         return os.equals("windows 95") || os.equals("windows 98");
299     }
300
301     /**
302      * Checks the Operating System.
303      *
304      * @return true if the current os is Apple
305      */

306     public static boolean isMac() {
307         String JavaDoc os = System.getProperty("os.name").toLowerCase();
308         return os.indexOf("mac") != -1;
309     }
310
311     /**
312      * Checks the Operating System.
313      *
314      * @return true if the current os is Linux
315      */

316     public static boolean isLinux() {
317         String JavaDoc os = System.getProperty("os.name").toLowerCase();
318         return os.indexOf("linux") != -1;
319     }
320 }
Popular Tags