KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > PackFile


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2001 Johannes Lehtinen
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 /*
23  * $Id: PackFile.java 1708 2007-01-13 18:31:26Z jponge $
24  * IzPack
25  * Copyright (C) 2001 Johannes Lehtinen
26  *
27  * File : Pack.java
28  * Description : Contains informations about a pack file.
29  * Author's email : johannes.lehtinen@iki.fi
30  * Author's Website : http://www.iki.fi/jle/
31  *
32  * This program is free software; you can redistribute it and/or
33  * modify it under the terms of the GNU General Public License
34  * as published by the Free Software Foundation; either version 2
35  * of the License, or any later version.
36  *
37  * This program is distributed in the hope that it will be useful,
38  * but WITHOUT ANY WARRANTY; without even the implied warranty of
39  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40  * GNU General Public License for more details.
41  *
42  * You should have received a copy of the GNU General Public License
43  * along with this program; if not, write to the Free Software
44  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
45  */

46 package com.izforge.izpack;
47
48 import java.io.File JavaDoc;
49 import java.io.FileNotFoundException JavaDoc;
50 import java.io.Serializable JavaDoc;
51 import java.util.List JavaDoc;
52 import java.util.Map JavaDoc;
53
54 /**
55  * Encloses information about a packed file. This class abstracts the way file data is stored to
56  * package.
57  *
58  * @author Johannes Lehtinen <johannes.lehtinen@iki.fi>
59  */

60 public class PackFile implements Serializable JavaDoc
61 {
62
63     static final long serialVersionUID = -834377078706854909L;
64
65     public static final int OVERRIDE_FALSE = 0;
66
67     public static final int OVERRIDE_TRUE = 1;
68
69     public static final int OVERRIDE_ASK_FALSE = 2;
70
71     public static final int OVERRIDE_ASK_TRUE = 3;
72
73     public static final int OVERRIDE_UPDATE = 4;
74
75     /** Only available when compiling. Makes no sense when installing, use relativePath instead. */
76     public transient String JavaDoc sourcePath = null;//should not be used anymore - may deprecate it.
77
/** The Path of the file relative to the given (compiletime's) basedirectory.
78      * Can be resolved while installing with either current working directory or directory of "installer.jar". */

79     protected String JavaDoc relativePath = null;
80
81     /** The full path name of the target file */
82     private String JavaDoc targetPath = null;
83
84     /** The target operating system constraints of this file */
85     private List JavaDoc osConstraints = null;
86
87     /** The length of the file in bytes */
88     private long length = 0;
89
90     /** The last-modification time of the file. */
91     private long mtime = -1;
92
93     /** True if file is a directory (length should be 0 or ignored) */
94     private boolean isDirectory = false;
95
96     /** Whether or not this file is going to override any existing ones */
97     private int override = OVERRIDE_FALSE;
98
99     /** Additional attributes or any else for customisation */
100     private Map JavaDoc additionals = null;
101
102     public int previousPackNumber = -1;
103
104     public long offsetInPreviousPack = -1;
105
106     /**
107      * Constructs and initializes from a source file.
108      *
109      * @param baseDir the baseDirectory of the Fileselection/compilation or null
110      * @param src file which this PackFile describes
111      * @param target the path to install the file to
112      * @param osList OS constraints
113      * @param override what to do when the file already exists
114      * @throws FileNotFoundException if the specified file does not exist.
115      */

116     public PackFile(File JavaDoc baseDir, File JavaDoc src, String JavaDoc target, List JavaDoc osList, int override)
117             throws FileNotFoundException JavaDoc
118     {
119         this(src, computeRelativePathFrom(baseDir, src), target, osList, override, null);
120     }
121     
122     /**
123      * Constructs and initializes from a source file.
124      *
125      * @param src file which this PackFile describes
126      * @param relativeSourcePath the path relative to the compiletime's basedirectory, use computeRelativePathFrom(File, File) to compute this.
127      * @param target the path to install the file to
128      * @param osList OS constraints
129      * @param override what to do when the file already exists
130      * @param additionals additional attributes
131      * @throws FileNotFoundException if the specified file does not exist.
132      */

133     public PackFile(File JavaDoc src, String JavaDoc relativeSourcePath, String JavaDoc target, List JavaDoc osList, int override, Map JavaDoc additionals)
134     throws FileNotFoundException JavaDoc
135     {
136         if (!src.exists()) // allows cleaner client co
137
throw new FileNotFoundException JavaDoc("No such file: " + src);
138
139         if ('/' != File.separatorChar) target = target.replace(File.separatorChar, '/');
140         if (target.endsWith("/")) target = target.substring(0, target.length() - 1);
141
142         this.sourcePath = src.getPath();
143         this.relativePath = relativeSourcePath;
144
145         this.targetPath = target;
146         this.osConstraints = osList;
147         this.override = override;
148
149         this.length = src.length();
150         this.mtime = src.lastModified();
151         this.isDirectory = src.isDirectory();
152         this.additionals = additionals;
153     }
154
155     /**
156      * Constructs and initializes from a source file.
157      *
158      * @param baseDir The Base directory that is used to search for the files. This is used to build the relative path's
159      * @param src file which this PackFile describes
160      * @param target the path to install the file to
161      * @param osList OS constraints
162      * @param override what to do when the file already exists
163      * @param additionals additional attributes
164      * @throws FileNotFoundException if the specified file does not exist.
165      */

166     public PackFile(File JavaDoc baseDir, File JavaDoc src, String JavaDoc target, List JavaDoc osList, int override, Map JavaDoc additionals)
167             throws FileNotFoundException JavaDoc
168     {
169         this(src, computeRelativePathFrom(baseDir, src), target, osList, override, additionals);
170     }
171
172     /**
173      * Builds the relative path of file to the baseDir.
174      * @param baseDir The Base Directory to build the relative path from
175      * @param file the file inside basDir
176      * @return null if file is not a inside baseDir
177      */

178     public static String JavaDoc computeRelativePathFrom(File JavaDoc baseDir, File JavaDoc file) {
179         if (baseDir==null || file == null) return null;
180         try{ //extract relative path...
181
if (file.getCanonicalPath().startsWith(baseDir.getCanonicalPath()))
182             {
183               return file.getCanonicalPath().substring(baseDir.getCanonicalPath().length());
184             }
185         }
186         catch(Exception JavaDoc x)//don't throw an exception here. return null instead!
187
{
188             //if we cannot build the relative path because of an error, the developer should be informed about.
189
x.printStackTrace();
190         }
191         
192         //we can not build a relative path for whatever reason
193
return null;
194     }
195
196     public void setPreviousPackFileRef(int previousPackNumber, long offsetInPreviousPack)
197     {
198         this.previousPackNumber = previousPackNumber;
199         this.offsetInPreviousPack = offsetInPreviousPack;
200     }
201
202     /** The target operating system constraints of this file */
203     public final List JavaDoc osConstraints()
204     {
205         return osConstraints;
206     }
207
208     /** The length of the file in bytes */
209     public final long length()
210     {
211         return length;
212     }
213
214     /** The last-modification time of the file. */
215     public final long lastModified()
216     {
217         return mtime;
218     }
219
220     /** Whether or not this file is going to override any existing ones */
221     public final int override()
222     {
223         return override;
224     }
225
226     public final boolean isDirectory()
227     {
228         return isDirectory;
229     }
230
231     public final boolean isBackReference()
232     {
233         return (previousPackNumber >= 0);
234     }
235
236     /** The full path name of the target file, using '/' as fileseparator. */
237     public final String JavaDoc getTargetPath()
238     {
239         return targetPath;
240     }
241     
242     /** The Path of the file relative to the given (compiletime's) basedirectory.
243      * Can be resolved while installing with either current working directory or directory of "installer.jar" */

244     public String JavaDoc getRelativeSourcePath()
245     {
246         return relativePath;
247     }
248
249     /**
250      * Returns the additionals map.
251      *
252      * @return additionals
253      */

254     public Map JavaDoc getAdditionals()
255     {
256         return additionals;
257     }
258
259 }
260
Popular Tags