001    // This file is part of the program FRYSK.
002    //
003    // Copyright 2007, Red Hat Inc.
004    //
005    // FRYSK is free software; you can redistribute it and/or modify it
006    // under the terms of the GNU General Public License as published by
007    // the Free Software Foundation; version 2 of the License.
008    //
009    // FRYSK is distributed in the hope that it will be useful, but
010    // WITHOUT ANY WARRANTY; without even the implied warranty of
011    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012    // General Public License for more details.
013    // 
014    // You should have received a copy of the GNU General Public License
015    // along with FRYSK; if not, write to the Free Software Foundation,
016    // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
017    // 
018    // In addition, as a special exception, Red Hat, Inc. gives You the
019    // additional right to link the code of FRYSK with code not covered
020    // under the GNU General Public License ("Non-GPL Code") and to
021    // distribute linked combinations including the two, subject to the
022    // limitations in this paragraph. Non-GPL Code permitted under this
023    // exception must only link to the code of FRYSK through those well
024    // defined interfaces identified in the file named EXCEPTION found in
025    // the source code files (the "Approved Interfaces"). The files of
026    // Non-GPL Code may instantiate templates or use macros or inline
027    // functions from the Approved Interfaces without causing the
028    // resulting work to be covered by the GNU General Public
029    // License. Only Red Hat, Inc. may make changes or additions to the
030    // list of Approved Interfaces. You must obey the GNU General Public
031    // License in all respects for all of the FRYSK code and other code
032    // used in conjunction with FRYSK except the Non-GPL Code covered by
033    // this exception. If you modify this file, you may extend this
034    // exception to your version of the file, but you are not obligated to
035    // do so. If you do not wish to provide this exception without
036    // modification, you must delete this exception statement from your
037    // version and license this file solely under the GPL without
038    // exception.
039    
040    package frysk.sysroot;
041    
042    import java.io.File;
043    import java.io.IOException;
044    
045    public class SysRootFile {
046        private File sysRoot;
047        private File file;
048    
049        public SysRootFile (File sysRoot, File file) {
050            if (sysRoot.getPath().length() > 1 && file.getPath().startsWith(sysRoot.getPath()))
051                this.file = new File(file.getPath().substring(sysRoot.getPath().length()));
052            else
053                this.file = file;
054            this.sysRoot = sysRoot;
055        }
056        
057        /**
058         * Get the root directory for this SysRoot File.
059         * @return the root directory
060         */
061        public File getSysRoot () {
062            return sysRoot;
063        }
064    
065        /**
066         * Get the file within the SysRoot for this SysRoot File.
067         * @return the file.
068         */
069        public File getFile () {
070            return file;
071        }
072    
073        /**
074         * Get the file within the SysRoot for this SysRoot File.
075         * @return the file.
076         */
077        String getPath () {
078            try {
079                return file.getCanonicalPath();
080            } catch (IOException e) {
081                return null;
082            }
083        }
084        
085        /**
086         * Get the absolute file, including the SysRoot, for this SysRoot File.
087         * @return the absolute file.
088         */
089        public File getSysRootedFile () {
090            try {
091                if (file.getPath().startsWith("/"))
092                    return new File(sysRoot, file.getPath()).getCanonicalFile();
093                else
094                    return file;
095            } catch (IOException e) {
096                return null;
097            }
098        }
099    
100        /**
101         * Get the absolute path, including the SysRoot, for this SysRoot File.
102         * @return the absolute path.
103         */
104        public String getSysRootedPath () {
105            try {
106                if (file.getPath().startsWith("/"))
107                    return new File(sysRoot, file.getPath()).getCanonicalPath();
108                else
109                    return file.getCanonicalPath();
110            } catch (IOException e) {
111                return null;
112            }
113        }
114    }