1 11 package org.eclipse.jdt.internal.ui.jarpackager; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.util.ArrayList ; 17 import java.util.Arrays ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.jar.JarEntry ; 21 import java.util.zip.CRC32 ; 22 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IStatus; 25 import org.eclipse.core.runtime.Status; 26 27 import org.eclipse.core.resources.IContainer; 28 import org.eclipse.core.resources.IFile; 29 import org.eclipse.core.resources.IResource; 30 31 import org.eclipse.swt.widgets.Display; 32 import org.eclipse.swt.widgets.Shell; 33 34 import org.eclipse.jface.dialogs.MessageDialog; 35 import org.eclipse.jface.operation.IRunnableContext; 36 37 import org.eclipse.jdt.core.IJavaElement; 38 import org.eclipse.jdt.core.IType; 39 import org.eclipse.jdt.core.JavaModelException; 40 41 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 42 import org.eclipse.jdt.internal.corext.util.Messages; 43 44 import org.eclipse.jdt.ui.JavaUI; 45 import org.eclipse.jdt.ui.jarpackager.JarPackageData; 46 47 import org.eclipse.jdt.internal.ui.IJavaStatusConstants; 48 import org.eclipse.jdt.internal.ui.JavaPlugin; 49 50 53 public final class JarPackagerUtil { 54 55 static final String JAR_EXTENSION= "jar"; static final String DESCRIPTION_EXTENSION= "jardesc"; 58 private static final String META_INF_ENTRY= "META-INF"; private static final String REFACTORINGS_ENTRY= META_INF_ENTRY + "/REFACTORINGS.XML"; 61 private JarPackagerUtil() { 62 } 64 65 public static boolean askToCreateDirectory(final Shell parent, File directory) { 66 if (parent == null) 67 return false; 68 return queryDialog(parent, JarPackagerMessages.JarPackage_confirmCreate_title, Messages.format(JarPackagerMessages.JarPackage_confirmCreate_message, directory.toString())); 69 } 70 71 78 public static String getRefactoringsEntry() { 79 return REFACTORINGS_ENTRY; 80 } 81 82 91 public static String getDeprecationEntry(final String name) { 92 return META_INF_ENTRY + "/" + name; } 94 95 102 public static String getMetaEntry() { 103 return META_INF_ENTRY; 104 } 105 106 112 public static List asResources(Object [] fSelectedElements) { 113 if (fSelectedElements == null) 114 return null; 115 List selectedResources= new ArrayList (fSelectedElements.length); 116 for (int i= 0; i < fSelectedElements.length; i++) { 117 Object element= fSelectedElements[i]; 118 if (element instanceof IJavaElement) { 119 selectedResources.add(((IJavaElement)element).getResource()); 120 } 121 else if (element instanceof IResource) 122 selectedResources.add(element); 123 } 124 return selectedResources; 125 } 126 127 public static boolean askForOverwritePermission(final Shell parent, String filePath) { 128 if (parent == null) 129 return false; 130 return queryDialog(parent, JarPackagerMessages.JarPackage_confirmReplace_title, Messages.format(JarPackagerMessages.JarPackage_confirmReplace_message, filePath)); 131 } 132 133 138 static String getMainClassName(JarPackageData jarPackage) { 139 if (jarPackage.getManifestMainClass() == null) 140 return ""; else 142 return jarPackage.getManifestMainClass().getFullyQualifiedName(); 143 } 144 145 146 private static boolean queryDialog(final Shell parent, final String title, final String message) { 147 Display display= parent.getDisplay(); 148 if (display == null || display.isDisposed()) 149 return false; 150 final boolean[] returnValue= new boolean[1]; 151 Runnable runnable= new Runnable () { 152 public void run() { 153 returnValue[0]= MessageDialog.openQuestion(parent, title, message); 154 } 155 }; 156 display.syncExec(runnable); 157 return returnValue[0]; 158 } 159 160 167 public static CoreException createCoreException(String message, Exception ex) { 168 if (message == null) 169 message= ""; return new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IJavaStatusConstants.INTERNAL_ERROR, message, ex)); 171 } 172 173 178 public static boolean isMainClassValid(JarPackageData data, IRunnableContext context) { 179 if (data == null) 180 return false; 181 182 IType mainClass= data.getManifestMainClass(); 183 if (mainClass == null) 184 return true; 186 187 try { 188 IFile file= (IFile)mainClass.getResource(); 190 if (file == null || !contains(asResources(data.getElements()), file)) 191 return false; 192 193 return JavaModelUtil.hasMainMethod(mainClass); 195 } catch (JavaModelException e) { 196 JavaPlugin.log(e.getStatus()); 197 } 198 return false; 199 } 200 201 static boolean contains(List resources, IFile file) { 202 if (resources == null || file == null) 203 return false; 204 205 if (resources.contains(file)) 206 return true; 207 208 Iterator iter= resources.iterator(); 209 while (iter.hasNext()) { 210 IResource resource= (IResource)iter.next(); 211 if (resource != null && resource.getType() != IResource.FILE) { 212 List children= null; 213 try { 214 children= Arrays.asList(((IContainer)resource).members()); 215 } catch (CoreException ex) { 216 continue; 218 } 219 if (children != null && contains(children, file)) 220 return true; 221 } 222 } 223 return false; 224 } 225 226 239 public static void calculateCrcAndSize(final JarEntry entry, final InputStream stream, final byte[] buffer) throws IOException { 240 int size= 0; 241 final CRC32 crc= new CRC32 (); 242 int count; 243 try { 244 while ((count= stream.read(buffer, 0, buffer.length)) != -1) { 245 crc.update(buffer, 0, count); 246 size+= count; 247 } 248 } finally { 249 if (stream != null) { 250 try { 251 stream.close(); 252 } catch (IOException exception) { 253 } 255 } 256 } 257 entry.setSize(size); 258 entry.setCrc(crc.getValue()); 259 } 260 } | Popular Tags |