001    /////////////////////////////////////////////////
002    //This file is part of Sears project.
003    //Subtitle Editor And Re-Synch
004    //A tool to easily modify and resynch movies subtitles.
005    /////////////////////////////////////////////////
006    //This program is free software; 
007    //you can redistribute it and/or modify it under the terms 
008    //of the GNU General Public License 
009    //as published by the Free Software Foundation; 
010    //either version 2 of the License, or (at your option) any later version.
011    /////////////////////////////////////////////////
012    //Sears project is availbale under sourceforge
013    //at adress: http://sourceforge.net/projects/sears/
014    //Copyright (C) 2005 Booba Skaya
015    //Mail: booba.skaya@gmail.com
016    /////////////////////////////////////////////////
017    
018    
019    package sears.tools;
020    
021    import java.io.*;
022    
023    
024    /**
025     * This class gives methods for read information into an Info.plist XML file (without a parser)
026     * <br> on a Mac OS X system.
027     */
028    public class InfoPlist{
029    
030            private static final String infoPlist = "/Contents/Info";
031            private static final String executableParentSubPath = "/Contents/MacOS/";
032            private static final String separator = System.getProperty("file.separator");
033    
034            public InfoPlist(){
035                    //empty...
036            }
037    
038            /**
039             * this method returns the path of the executable file contains in a .app bundle
040             * <br> with the path of .app bundle given in parameter.
041             *
042             * @param pathOfApp the path of the .app bundle.
043             * @return                      a String, path of the executable contains in .app bundle,
044             * <br><b>null</b> if it isn't found or if the method was called out of Mac OS X system.
045             */
046            public static String getExecutablePath(String pathOfApp) {
047                    String result = getBundleExecutableName(pathOfApp);
048                    if(result != null) {
049                            result = pathOfApp + separator + executableParentSubPath + result;
050                    }
051                    return result;
052            }
053    
054            /**
055             * this method returns the name of the executable file contains in a .app bundle
056             * <br> with the path of .app bundle given in parameter.
057             *
058             * @param pathOfApp the path of the .app bundle.
059             * @return                      the name of the executable file,
060             * <br><b>null</b> if it isn't found or if the method was called out of Mac OS X system.
061             */
062            public static String getBundleExecutableName(String pathOfApp) {
063                    String result = null;
064                    // if method was called on a Mac OS X platform:
065                    if(Utils.isMacPlatform) {
066                            // unix command to read the key -CFBundleExecutable- into an Info.plist file
067                            String[] command = {"defaults", "read", pathOfApp + infoPlist, "CFBundleExecutable"};
068    
069                            try {
070                                    // we try to execute the unix command line:
071                                    Process process = Runtime.getRuntime().exec(command);                   
072    
073                                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
074                                    // we read the result:
075                                    result = bufferedReader.readLine();           
076                                    bufferedReader.close();
077    
078                            } catch (IOException e) {
079                                    e.printStackTrace();
080                            }               
081                    }
082                    return result;
083            }
084    }