KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > cbutil > CBLauncher


1 package com.ca.commons.cbutil;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5
6 /**
7  * This class attempts to launch a program (depending on its file extension) and attempts
8  * to open the specified file. For example: launch MS word (winword.exe) with file 'a.doc'.
9  *
10  * @author Trudi.
11  */

12
13 public class CBLauncher
14 {
15
16
17     /**
18      * This class attempts to launch a program (depending on its file extension) and attempts
19      * to open the specified file. For example: launch MS word (winword.exe) with file 'a.doc'.
20      *
21      * @param extension the file extension that determines what program to use (e.g. '.mp3').
22      * @param fileName the name of the file that needs to be opened (e.g. 'a.mp3').
23      */

24
25     public static void launchProgram(String JavaDoc extension, String JavaDoc fileName)
26     {
27         String JavaDoc command = null;
28         StringBuffer JavaDoc fileType = new StringBuffer JavaDoc(20);
29         StringBuffer JavaDoc program = new StringBuffer JavaDoc(50);
30         Process JavaDoc p;
31         InputStream JavaDoc stdOut = null;
32         boolean collecting;
33         int b;
34
35         if (command == null)
36         {
37             /*
38              * Find out the file type associated with .xls files
39              *
40              * assoc determines the association between a file extention
41              * and the 'file type'. It will return something like:
42              *
43              * .xls=Excel.Sheet.8
44              *
45              * We strip off everything upto (and including) the equals
46              */

47
48             try
49             {
50                 p = Runtime.getRuntime().exec("cmd /c assoc " + extension);
51                 stdOut = p.getInputStream();
52                 collecting = false;
53
54                 while ((b = stdOut.read()) != -1)
55                 {
56                     char c = (char) b;
57                     if (c == '\r' || c == '\n')
58                     {
59                         //Do nothing
60
}
61                     else if (collecting)
62                     {
63                         fileType.append(c);
64                     }
65                     else if (c == '=')
66                     {
67                         collecting = true;
68                     }
69                 }
70             }
71             catch (IOException JavaDoc e)
72             {
73                 CBUtility.error(CBIntText.get("Error trying to associate file extension: {0} with program", new String JavaDoc[]{extension}) + "\n" + e);
74             }
75
76             /*
77              * Find the program associated with the file type
78              *
79              * ftype determines the command used to open files of a
80              * given type. It will return something like:
81              *
82              * Excel.Sheet.8="h:\MSOffice.97\Office\excel.exe" /e
83              *
84              * We strip off everything upto (and including) the equals
85              */

86
87             try
88             {
89                 p = Runtime.getRuntime().exec("cmd /c ftype " + fileType.toString());
90                 stdOut = p.getInputStream();
91                 collecting = false;
92
93                 while ((b = stdOut.read()) != -1)
94                 {
95                     char c = (char) b;
96
97                     if (c == '\r' || c == '\n')
98                     {
99                         //Do nothing
100
}
101                     else if (collecting)
102                     {
103                         program.append(c);
104                     }
105                     else if (c == '=')
106                     {
107                         collecting = true;
108                     }
109                 }
110             }
111             catch (IOException JavaDoc e)
112             {
113                 CBUtility.error(CBIntText.get("Error trying to associate file extension: {0} with program", new String JavaDoc[]{extension}) + "\n" + e);
114             }
115             command = program.toString();
116         }
117
118         String JavaDoc fullProgramName = program.toString();
119         String JavaDoc runProgramName;
120
121         if (program.toString().endsWith("%1")) //TE: for some reason, to get mplayer2 & realplayer working, remove the %1 from the end of the name (they don't write it like the others i.e. "%1").
122
runProgramName = fullProgramName.substring(0, fullProgramName.lastIndexOf("%1") - 1);
123         else if (program.toString().endsWith("\"%L\"")) //TE: Midi format.
124
runProgramName = fullProgramName.substring(0, fullProgramName.lastIndexOf("\"%L\"") - 1);
125         else
126             runProgramName = program.toString();
127
128         Runtime JavaDoc r = Runtime.getRuntime();
129
130         try
131         {
132             r.exec(runProgramName + " \"" + fileName + "\"");
133         }
134         catch (IOException JavaDoc e)
135         {
136             CBUtility.error(CBIntText.get("Error occured when trying to launch program: {0} with the file {1}. Either the program found does not support the file type, or the file name was incorrect.",
137                     new String JavaDoc[]{runProgramName, fileName}) + "\n\n\n" + e);
138         }
139     }
140 }
Popular Tags