001: /*
002: * $Id: Executable.java 2352 2006-09-12 08:27:37Z blowagie $
003: * $Name$
004: *
005: * Copyright 2005 by Bruno Lowagie / Roger Mistelli
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050: package com.lowagie.tools;
051:
052: import java.io.File;
053: import java.io.IOException;
054: import java.lang.reflect.Method;
055:
056: /**
057: * This class enables you to call an executable that will show a PDF file.
058: */
059: public class Executable {
060:
061: /**
062: * The path to Acrobat Reader.
063: */
064: public static String acroread = null;
065:
066: /**
067: * Performs an action on a PDF document.
068: * @param fileName
069: * @param parameters
070: * @param waitForTermination
071: * @return a process
072: * @throws IOException
073: */
074: private static Process action(final String fileName,
075: String parameters, boolean waitForTermination)
076: throws IOException {
077: Process process = null;
078: if (parameters.trim().length() > 0) {
079: parameters = " " + parameters.trim();
080: } else {
081: parameters = "";
082: }
083: if (acroread != null) {
084: process = Runtime.getRuntime().exec(
085: acroread + parameters + " \"" + fileName + "\"");
086: } else if (isWindows()) {
087: if (isWindows9X()) {
088: process = Runtime.getRuntime().exec(
089: "command.com /C start acrord32" + parameters
090: + " \"" + fileName + "\"");
091: } else {
092: process = Runtime.getRuntime().exec(
093: "cmd /c start acrord32" + parameters + " \""
094: + fileName + "\"");
095: }
096: } else if (isMac()) {
097: if (parameters.trim().length() == 0) {
098: process = Runtime.getRuntime().exec(
099: new String[] { "/usr/bin/open", fileName });
100: } else {
101: process = Runtime.getRuntime().exec(
102: new String[] { "/usr/bin/open",
103: parameters.trim(), fileName });
104: }
105: }
106: try {
107: if (process != null && waitForTermination)
108: process.waitFor();
109: } catch (InterruptedException ie) {
110: }
111: return process;
112: }
113:
114: /**
115: * Opens a PDF document.
116: * @param fileName
117: * @param waitForTermination
118: * @return a process
119: * @throws IOException
120: */
121: public static final Process openDocument(String fileName,
122: boolean waitForTermination) throws IOException {
123: return action(fileName, "", waitForTermination);
124: }
125:
126: /**
127: * Opens a PDF document.
128: * @param file
129: * @param waitForTermination
130: * @return a process
131: * @throws IOException
132: */
133: public static final Process openDocument(File file,
134: boolean waitForTermination) throws IOException {
135: return openDocument(file.getAbsolutePath(), waitForTermination);
136: }
137:
138: /**
139: * Opens a PDF document.
140: * @param fileName
141: * @return a process
142: * @throws IOException
143: */
144: public static final Process openDocument(String fileName)
145: throws IOException {
146: return openDocument(fileName, false);
147: }
148:
149: /**
150: * Opens a PDF document.
151: * @param file
152: * @return a process
153: * @throws IOException
154: */
155: public static final Process openDocument(File file)
156: throws IOException {
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 printDocument(String fileName,
168: boolean waitForTermination) throws IOException {
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 printDocument(File file,
180: boolean waitForTermination) throws IOException {
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 printDocument(String fileName)
191: throws IOException {
192: return printDocument(fileName, false);
193: }
194:
195: /**
196: * Prints a PDF document.
197: * @param file
198: * @return a process
199: * @throws IOException
200: */
201: public static final Process printDocument(File file)
202: throws IOException {
203: return printDocument(file, false);
204: }
205:
206: /**
207: * Prints a PDF document without opening a Dialog box.
208: * @param fileName
209: * @param waitForTermination
210: * @return a process
211: * @throws IOException
212: */
213: public static final Process printDocumentSilent(String fileName,
214: boolean waitForTermination) throws IOException {
215: return action(fileName, "/p /h", waitForTermination);
216: }
217:
218: /**
219: * Prints a PDF document without opening a Dialog box.
220: * @param file
221: * @param waitForTermination
222: * @return a process
223: * @throws IOException
224: */
225: public static final Process printDocumentSilent(File file,
226: boolean waitForTermination) throws IOException {
227: return printDocumentSilent(file.getAbsolutePath(),
228: waitForTermination);
229: }
230:
231: /**
232: * Prints a PDF document without opening a Dialog box.
233: * @param fileName
234: * @return a process
235: * @throws IOException
236: */
237: public static final Process printDocumentSilent(String fileName)
238: throws IOException {
239: return printDocumentSilent(fileName, false);
240: }
241:
242: /**
243: * Prints a PDF document without opening a Dialog box.
244: * @param file
245: * @return a process
246: * @throws IOException
247: */
248: public static final Process printDocumentSilent(File file)
249: throws IOException {
250: return printDocumentSilent(file, false);
251: }
252:
253: /**
254: * Launches a browser opening an URL.
255: *
256: * @param url the URL you want to open in the browser
257: * @throws IOException
258: */
259: public static final void launchBrowser(String url)
260: throws IOException {
261: try {
262: if (isMac()) {
263: Class macUtils = Class
264: .forName("com.apple.mrj.MRJFileUtils");
265: Method openURL = macUtils.getDeclaredMethod("openURL",
266: new Class[] { String.class });
267: openURL.invoke(null, new Object[] { url });
268: } else if (isWindows())
269: Runtime.getRuntime().exec(
270: "rundll32 url.dll,FileProtocolHandler " + url);
271: else { //assume Unix or Linux
272: String[] browsers = { "firefox", "opera", "konqueror",
273: "mozilla", "netscape" };
274: String browser = null;
275: for (int count = 0; count < browsers.length
276: && browser == null; count++)
277: if (Runtime.getRuntime().exec(
278: new String[] { "which", browsers[count] })
279: .waitFor() == 0)
280: browser = browsers[count];
281: if (browser == null)
282: throw new Exception("Could not find web browser.");
283: else
284: Runtime.getRuntime().exec(
285: new String[] { browser, url });
286: }
287: } catch (Exception e) {
288: throw new IOException(
289: "Error attempting to launch web browser");
290: }
291: }
292:
293: /**
294: * Checks the Operating System.
295: *
296: * @return true if the current os is Windows
297: */
298: public static boolean isWindows() {
299: String os = System.getProperty("os.name").toLowerCase();
300: return os.indexOf("windows") != -1 || os.indexOf("nt") != -1;
301: }
302:
303: /**
304: * Checks the Operating System.
305: *
306: * @return true if the current os is Windows
307: */
308: public static boolean isWindows9X() {
309: String os = System.getProperty("os.name").toLowerCase();
310: return os.equals("windows 95") || os.equals("windows 98");
311: }
312:
313: /**
314: * Checks the Operating System.
315: *
316: * @return true if the current os is Apple
317: */
318: public static boolean isMac() {
319: String os = System.getProperty("os.name").toLowerCase();
320: return os.indexOf("mac") != -1;
321: }
322:
323: /**
324: * Checks the Operating System.
325: *
326: * @return true if the current os is Linux
327: */
328: public static boolean isLinux() {
329: String os = System.getProperty("os.name").toLowerCase();
330: return os.indexOf("linux") != -1;
331: }
332: }
|