]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/ExternalTools.java
Version 16, February 2014
[GpsPrune.git] / tim / prune / ExternalTools.java
1 package tim.prune;
2
3 import java.io.IOException;
4
5 import tim.prune.config.Config;
6
7
8 /**
9  * Class to manage interfaces to external tools, like exiftool
10  */
11 public abstract class ExternalTools
12 {
13         /** Constant for Exiftool */
14         public static final int TOOL_EXIFTOOL = 0;
15         /** Constant for Gpsbabel */
16         public static final int TOOL_GPSBABEL = 1;
17         /** Constant for Gnuplot */
18         public static final int TOOL_GNUPLOT  = 2;
19         /** Constant for Xerces xml library */
20         public static final int TOOL_XERCES   = 3;
21         /** Config keys in order that the tools are defined above */
22         private static final String[] CONFIG_KEYS = {Config.KEY_EXIFTOOL_PATH, Config.KEY_GPSBABEL_PATH, Config.KEY_GNUPLOT_PATH};
23         /** Verification flags for the tools in the order defined above */
24         private static final String[] VERIFY_FLAGS = {"-v", "-V", "-V"};
25
26
27         /**
28          * Check if the selected tool is installed
29          * @param inToolNum number of tool, from constants
30          * @return true if selected tool is installed
31          */
32         public static boolean isToolInstalled(int inToolNum)
33         {
34                 switch (inToolNum) {
35                         case TOOL_EXIFTOOL:
36                         case TOOL_GPSBABEL:
37                         case TOOL_GNUPLOT:
38                                 String toolPath = Config.getConfigString(CONFIG_KEYS[inToolNum]);
39                                 if (toolPath != null && toolPath.length() > 0) {
40                                         return check(toolPath + " " + VERIFY_FLAGS[inToolNum]);
41                                 }
42                                 break;
43                         case TOOL_XERCES:
44                                 try {
45                                         return Class.forName("org.apache.xerces.parsers.SAXParser").getClassLoader() != null;
46                                 }
47                                 catch (ClassNotFoundException e) {
48                                         // System.err.println(e.getClass().getName() + " : " + e.getMessage());
49                                 }
50                                 break;
51                 }
52                 // Not found
53                 return false;
54         }
55
56         /**
57          * Check if the selected tool is installed using the given path
58          * @param inToolNum number of tool, from constants
59          * @param inPath selected path to use instead of configured one
60          * @return true if selected tool is installed
61          */
62         public static boolean isToolInstalled(int inToolNum, String inPath)
63         {
64                 if (inPath == null || inPath.equals("")) {return false;}
65                 switch (inToolNum) {
66                         case TOOL_EXIFTOOL:
67                         case TOOL_GPSBABEL:
68                         case TOOL_GNUPLOT:
69                                 return check(inPath + " " + VERIFY_FLAGS[inToolNum]);
70                 }
71                 // Not found
72                 return false;
73         }
74
75         /**
76          * Attempt to call the specified command
77          * @return true if found, false otherwise
78          */
79         private static boolean check(String inCommand)
80         {
81                 try
82                 {
83                         Runtime.getRuntime().exec(inCommand);
84                         return true;
85                 }
86                 catch (IOException ioe)
87                 {
88                         // exception thrown, command not found
89                         return false;
90                 }
91         }
92 }