KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > build > packager > UnzipperGenerator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.build.packager;
12
13 import java.io.*;
14 import java.util.*;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.pde.internal.build.*;
18
19 public class UnzipperGenerator extends AbstractScriptGenerator {
20     private static final String JavaDoc DATA_SEPARATOR = "|"; //$NON-NLS-1$
21
private static final String JavaDoc ENTRY_SEPARATOR = "%"; //$NON-NLS-1$
22
private static final byte ARCHIVE_NAME = 0;
23     private static final byte FOLDER = 1;
24     private static final byte CONFIGS = 2;
25     
26     // The name the file containing the list of zips
27
private String JavaDoc directoryLocation = DEFAULT_PACKAGER_DIRECTORY_FILENAME_DESCRIPTOR;
28     // The list of zips. The key is the name of the zipfile, and the first property is the place to extract it
29
private Properties zipsList;
30     // The location of the packaging.properties file
31
private String JavaDoc packagingPropertiesLocation;
32
33     private String JavaDoc[] unzipOrder = new String JavaDoc[0];
34
35     public void generate() throws CoreException {
36         prepareGeneration();
37         openScript(workingDirectory, DEFAULT_UNZIPPER_FILENAME_DESCRIPTOR);
38         try {
39             generatePrologue();
40             generateUncompressionCommands();
41             generateEpilogue();
42         } finally {
43             closeScript();
44         }
45     }
46
47     /**
48      *
49      */

50     private void prepareGeneration() {
51         if (packagingPropertiesLocation == null)
52             return;
53
54         Properties packagingProperties = new Properties();
55         InputStream propertyStream = null;
56         try {
57             propertyStream = new BufferedInputStream(new FileInputStream(packagingPropertiesLocation));
58             try {
59                 packagingProperties.load(new BufferedInputStream(propertyStream));
60             } finally {
61                 propertyStream.close();
62             }
63         } catch (FileNotFoundException e) {
64             // String message = Policy.bind("exception.readingFile", packagingPropertiesLocation); //$NON-NLS-1$
65
//// Log.throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_READING_FILE, message, e));
66
} catch (IOException e) {
67             // String message = Policy.bind("exception.readingFile", packagingPropertiesLocation); //$NON-NLS-1$
68
// throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_READING_FILE, message, e));
69
}
70         unzipOrder = Utils.getArrayFromStringWithBlank(packagingProperties.getProperty("unzipOrder", ""), ","); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
71
}
72
73     private void generateEpilogue() {
74         script.printTargetEnd();
75         script.println();
76         script.printProjectEnd();
77     }
78
79     private void generatePrologue() {
80         script.println();
81         script.printComment("Unzip script"); //$NON-NLS-1$
82
script.println();
83         script.printProjectDeclaration("Unzipper", TARGET_MAIN, "."); //$NON-NLS-1$ //$NON-NLS-2$
84
script.printTargetDeclaration(TARGET_MAIN, null, null, null, null);
85     }
86
87     private void generateUncompressionCommands() throws CoreException {
88         zipsList = readProperties(workingDirectory, directoryLocation, IStatus.ERROR);
89
90         String JavaDoc zipEntries = zipsList.getProperty("toUnzip",""); //$NON-NLS-1$ //$NON-NLS-2$
91

92         List toUnzipWithOrder = new ArrayList(unzipOrder.length);
93         String JavaDoc[] allZipEntries = Utils.getArrayFromString(zipEntries, ENTRY_SEPARATOR);
94         for (int i = 0; i < allZipEntries.length; i++) {
95             String JavaDoc[] entryDetail = Utils.getArrayFromString(allZipEntries[i], DATA_SEPARATOR);
96             script.printComment("Uncompress " + entryDetail[ARCHIVE_NAME]); //$NON-NLS-1$
97

98             if (!entryDetail[FOLDER].equals(".")) //$NON-NLS-1$
99
script.printMkdirTask("${tempDirectory}/" + entryDetail[FOLDER]); //$NON-NLS-1$
100

101             if (delayed(entryDetail[ARCHIVE_NAME])) {
102                 toUnzipWithOrder.add(entryDetail);
103                 continue;
104             }
105             generateUncompress(entryDetail);
106             script.println();
107             script.println();
108         }
109
110         //Deal with the entries that have a specific order.
111
for (int i = 0; i < unzipOrder.length; i++) {
112             for (Iterator iter = toUnzipWithOrder.iterator(); iter.hasNext();) {
113                 String JavaDoc[] entry = (String JavaDoc[]) iter.next();
114                 if (entry[ARCHIVE_NAME].startsWith(unzipOrder[i])) {
115                     generateUncompress(entry);
116                     iter.remove();
117                 }
118             }
119         }
120     }
121
122     private void generateUncompress(String JavaDoc[] entryDetail) {
123         if (entryDetail[ARCHIVE_NAME].endsWith(".zip")) { //$NON-NLS-1$
124
generateUnzipArchive(entryDetail);
125             generateUnzipRootFiles(entryDetail);
126             return;
127         }
128
129         if (entryDetail[ARCHIVE_NAME].endsWith(".tar.gz") || entryDetail[ARCHIVE_NAME].endsWith(".tar")) { //$NON-NLS-1$ //$NON-NLS-2$
130
generateUntarArchice(entryDetail);
131             generateUntarRootFiles(entryDetail);
132         }
133     }
134
135     private boolean delayed(String JavaDoc fileName) {
136         for (int i = 0; i < unzipOrder.length; i++) {
137             if (fileName.startsWith(unzipOrder[i]))
138                 return true;
139         }
140         return false;
141     }
142
143     private List getMatchingConfig(String JavaDoc[] entryDetail) {
144         List applyingConfigs = null;
145         if (entryDetail.length == 2) {
146             applyingConfigs = getConfigInfos();
147         } else {
148             String JavaDoc[] configs = Utils.getArrayFromString(entryDetail[CONFIGS], "&");
149             applyingConfigs = new ArrayList(configs.length);
150             for (int i = 0; i < configs.length; i++) {
151                 applyingConfigs.add(new Config(configs[i]));
152             }
153         }
154         return applyingConfigs;
155     }
156
157     private void generateUnzipArchive(String JavaDoc[] entryDetail) {
158         List parameters = new ArrayList(1);
159         parameters.add("-o -X ${unzipArgs} "); //$NON-NLS-1$
160
parameters.add(Utils.getPropertyFormat("downloadDirectory") + '/' + entryDetail[ARCHIVE_NAME]); //$NON-NLS-1$
161
script.printExecTask("unzip", "${tempDirectory}/" + entryDetail[FOLDER], parameters, null); //$NON-NLS-1$//$NON-NLS-2$
162
}
163
164     //Uncompress the root files into a platform specific folder
165
private void generateUnzipRootFiles(String JavaDoc[] entryDetail) {
166         //Unzip the root files in a "config specific folder" for all the configurations that matched this entry
167
for (Iterator iter = getMatchingConfig(entryDetail).iterator(); iter.hasNext();) {
168             Config config = (Config) iter.next();
169             List parameters = new ArrayList(3);
170             String JavaDoc rootFilesFolder = "${tempDirectory}/" + config.toString(".") + '/' + entryDetail[FOLDER];
171             script.printMkdirTask(rootFilesFolder);
172             parameters.add("-o -X ${unzipArgs} "); //$NON-NLS-1$
173
parameters.add(Utils.getPropertyFormat("downloadDirectory") + '/' + entryDetail[ARCHIVE_NAME]); //$NON-NLS-1$
174
parameters.add("-x " + (entryDetail[FOLDER].equals(".") ? "eclipse/" : "") + "features/*" + " " + (entryDetail[FOLDER].equals(".") ? "eclipse/" : "") + "plugins/*");
175             script.printExecTask("unzip", rootFilesFolder, parameters, null); //$NON-NLS-1$
176
}
177     }
178     
179     
180     private void generateUntarArchice(String JavaDoc[] entryDetail) {
181         List parameters = new ArrayList(2);
182         parameters.add("-" + (entryDetail[ARCHIVE_NAME].endsWith(".gz") ? "z" : "") + "pxvf"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
183
parameters.add(Utils.getPropertyFormat("downloadDirectory") + '/' + entryDetail[ARCHIVE_NAME]); //$NON-NLS-1$
184
script.printExecTask("tar", "${tempDirectory}/" + entryDetail[FOLDER], parameters, null); //$NON-NLS-1$//$NON-NLS-2$
185
}
186     
187     private void generateUntarRootFiles(String JavaDoc[] entryDetail) {
188         //Unzip the root files in a "config specific folder" for all the configurations that matched this entry
189
for (Iterator iter = getMatchingConfig(entryDetail).iterator(); iter.hasNext();) {
190             Config config = (Config) iter.next();
191             List parameters = new ArrayList(4);
192             String JavaDoc rootFilesFolder = "${tempDirectory}/" + config.toString(".") + '/' + entryDetail[FOLDER]; //$NON-NLS-1$ //$NON-NLS-2$
193
script.printMkdirTask(rootFilesFolder);
194             parameters.add("-" + (entryDetail[ARCHIVE_NAME].endsWith(".gz") ? "z" : "") + "pxvf"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
195
parameters.add(Utils.getPropertyFormat("downloadDirectory") + '/' + entryDetail[ARCHIVE_NAME]); //$NON-NLS-1$
196
parameters.add("--exclude=" + (entryDetail[FOLDER].equals(".") ? "eclipse" : "") + "/features/*"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
197
parameters.add("--exclude=" + (entryDetail[FOLDER].equals(".") ? "eclipse" : "") + "/plugins/*"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
198
script.printExecTask("tar", rootFilesFolder, parameters, null); //$NON-NLS-1$
199
}
200     }
201
202     public void setDirectoryLocation(String JavaDoc filename) {
203         directoryLocation = filename;
204     }
205
206     /**
207      * Set the property file containing information about packaging
208      * @param propertyFile the path to a property file
209      */

210     public void setPropertyFile(String JavaDoc propertyFile) {
211         packagingPropertiesLocation = propertyFile;
212     }
213 }
214
Popular Tags