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    package sears.tools;
018    
019    import java.io.*;
020    
021    
022    /**
023     * This class gives method for read information into an Info.plist XML file (without a parser)
024     * <br> on a Mac OS X system.
025     */
026    public class InfoPlist{
027            
028            public InfoPlist(){
029                    //empty...
030            }       
031            
032            /**
033             * this method allows to get back the path of the real executable file in .app bundle.
034             * <br> with the path of .app bundle given in parameter.
035             * <br> This method only works with an Info.plist file formatted like Mac OS X does.
036             *
037             * @param pathOfApp the path of the .app bundle.
038             * @return a String, path of the executable in .app bundle, <b>null</b> if it isn't found (Non conform .plist...)
039             *
040             * @throws java.io.FileNotFoundException Info.plist wasn't found.
041             * @throws java.io.IOException access error when reading file.
042             */
043            public static String getExecutablePath(String pathOfApp) throws FileNotFoundException, IOException{
044                    // the path of the executable file:
045            String result = null;
046            
047            // to stop the loop:
048                boolean stop=false;         
049                    // we create a buffer reader:
050                    BufferedReader buffer = null;
051            String line;
052            
053    
054                    // may cause a FileNotFoundException ->
055                // we get the Info.plist file:
056                    buffer = new BufferedReader(new FileReader(pathOfApp + "/Contents/Info.plist"));
057                        
058               
059                // we read line:
060                while (!stop && (line = buffer.readLine()) != null){
061                    int begin = line.indexOf("<key>");
062                    int end = line.indexOf("</key>");
063                            
064                    // if the line is a key:
065                    if(begin>=0 && end >0){
066                            // if the key is CFBundleExecutable:
067                                    if(line.substring(begin+5, end).equals("CFBundleExecutable")){
068                                            // we read the next line which may contain the name of the executable file:
069                                            line = (buffer.readLine());
070                                            
071                                            begin = line.indexOf("<string>");
072                                            end = line.indexOf("</string>");
073                                            // if the next line is really the string line:                                  
074                                            if(begin>=0 && end >0){
075                                                    result = pathOfApp + "/Contents/MacOS/" + line.substring(begin+8, end);
076                                            }
077                                            // we end the loop:
078                                            stop=true;
079                                    }                                       
080                    }
081            }
082                // we close the buffer      
083                buffer.close();        
084                
085                return result;
086            }
087    }