1 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 DATA_SEPARATOR = "|"; private static final String ENTRY_SEPARATOR = "%"; private static final byte ARCHIVE_NAME = 0; 23 private static final byte FOLDER = 1; 24 private static final byte CONFIGS = 2; 25 26 private String directoryLocation = DEFAULT_PACKAGER_DIRECTORY_FILENAME_DESCRIPTOR; 28 private Properties zipsList; 30 private String packagingPropertiesLocation; 32 33 private String [] unzipOrder = new String [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 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 } catch (IOException e) { 67 } 70 unzipOrder = Utils.getArrayFromStringWithBlank(packagingProperties.getProperty("unzipOrder", ""), ","); } 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"); script.println(); 83 script.printProjectDeclaration("Unzipper", TARGET_MAIN, "."); 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 zipEntries = zipsList.getProperty("toUnzip",""); 92 List toUnzipWithOrder = new ArrayList(unzipOrder.length); 93 String [] allZipEntries = Utils.getArrayFromString(zipEntries, ENTRY_SEPARATOR); 94 for (int i = 0; i < allZipEntries.length; i++) { 95 String [] entryDetail = Utils.getArrayFromString(allZipEntries[i], DATA_SEPARATOR); 96 script.printComment("Uncompress " + entryDetail[ARCHIVE_NAME]); 98 if (!entryDetail[FOLDER].equals(".")) script.printMkdirTask("${tempDirectory}/" + entryDetail[FOLDER]); 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 for (int i = 0; i < unzipOrder.length; i++) { 112 for (Iterator iter = toUnzipWithOrder.iterator(); iter.hasNext();) { 113 String [] entry = (String []) 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 [] entryDetail) { 123 if (entryDetail[ARCHIVE_NAME].endsWith(".zip")) { generateUnzipArchive(entryDetail); 125 generateUnzipRootFiles(entryDetail); 126 return; 127 } 128 129 if (entryDetail[ARCHIVE_NAME].endsWith(".tar.gz") || entryDetail[ARCHIVE_NAME].endsWith(".tar")) { generateUntarArchice(entryDetail); 131 generateUntarRootFiles(entryDetail); 132 } 133 } 134 135 private boolean delayed(String 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 [] entryDetail) { 144 List applyingConfigs = null; 145 if (entryDetail.length == 2) { 146 applyingConfigs = getConfigInfos(); 147 } else { 148 String [] 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 [] entryDetail) { 158 List parameters = new ArrayList(1); 159 parameters.add("-o -X ${unzipArgs} "); parameters.add(Utils.getPropertyFormat("downloadDirectory") + '/' + entryDetail[ARCHIVE_NAME]); script.printExecTask("unzip", "${tempDirectory}/" + entryDetail[FOLDER], parameters, null); } 163 164 private void generateUnzipRootFiles(String [] entryDetail) { 166 for (Iterator iter = getMatchingConfig(entryDetail).iterator(); iter.hasNext();) { 168 Config config = (Config) iter.next(); 169 List parameters = new ArrayList(3); 170 String rootFilesFolder = "${tempDirectory}/" + config.toString(".") + '/' + entryDetail[FOLDER]; 171 script.printMkdirTask(rootFilesFolder); 172 parameters.add("-o -X ${unzipArgs} "); parameters.add(Utils.getPropertyFormat("downloadDirectory") + '/' + entryDetail[ARCHIVE_NAME]); parameters.add("-x " + (entryDetail[FOLDER].equals(".") ? "eclipse/" : "") + "features/*" + " " + (entryDetail[FOLDER].equals(".") ? "eclipse/" : "") + "plugins/*"); 175 script.printExecTask("unzip", rootFilesFolder, parameters, null); } 177 } 178 179 180 private void generateUntarArchice(String [] entryDetail) { 181 List parameters = new ArrayList(2); 182 parameters.add("-" + (entryDetail[ARCHIVE_NAME].endsWith(".gz") ? "z" : "") + "pxvf"); parameters.add(Utils.getPropertyFormat("downloadDirectory") + '/' + entryDetail[ARCHIVE_NAME]); script.printExecTask("tar", "${tempDirectory}/" + entryDetail[FOLDER], parameters, null); } 186 187 private void generateUntarRootFiles(String [] entryDetail) { 188 for (Iterator iter = getMatchingConfig(entryDetail).iterator(); iter.hasNext();) { 190 Config config = (Config) iter.next(); 191 List parameters = new ArrayList(4); 192 String rootFilesFolder = "${tempDirectory}/" + config.toString(".") + '/' + entryDetail[FOLDER]; script.printMkdirTask(rootFilesFolder); 194 parameters.add("-" + (entryDetail[ARCHIVE_NAME].endsWith(".gz") ? "z" : "") + "pxvf"); parameters.add(Utils.getPropertyFormat("downloadDirectory") + '/' + entryDetail[ARCHIVE_NAME]); parameters.add("--exclude=" + (entryDetail[FOLDER].equals(".") ? "eclipse" : "") + "/features/*"); parameters.add("--exclude=" + (entryDetail[FOLDER].equals(".") ? "eclipse" : "") + "/plugins/*"); script.printExecTask("tar", rootFilesFolder, parameters, null); } 200 } 201 202 public void setDirectoryLocation(String filename) { 203 directoryLocation = filename; 204 } 205 206 210 public void setPropertyFile(String propertyFile) { 211 packagingPropertiesLocation = propertyFile; 212 } 213 } 214 | Popular Tags |