KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > jarpackager > JarPackagerUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.jarpackager;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Arrays JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.jar.JarEntry JavaDoc;
21 import java.util.zip.CRC32 JavaDoc;
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 /**
51  * Utility methods for JAR Import/Export.
52  */

53 public final class JarPackagerUtil {
54
55     static final String JavaDoc JAR_EXTENSION= "jar"; //$NON-NLS-1$
56
static final String JavaDoc DESCRIPTION_EXTENSION= "jardesc"; //$NON-NLS-1$
57

58     private static final String JavaDoc META_INF_ENTRY= "META-INF"; //$NON-NLS-1$
59
private static final String JavaDoc REFACTORINGS_ENTRY= META_INF_ENTRY + "/REFACTORINGS.XML"; //$NON-NLS-1$
60

61     private JarPackagerUtil() {
62         // Do nothing
63
}
64
65     public static boolean askToCreateDirectory(final Shell parent, File JavaDoc 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     /**
72      * Returns the name of the refactorings zip entry.
73      *
74      * @return the name of the refactorings zip entry
75      *
76      * @since 3.2
77      */

78     public static String JavaDoc getRefactoringsEntry() {
79         return REFACTORINGS_ENTRY;
80     }
81
82     /**
83      * Returns the name of the deprecations zip entry for the specified file.
84      *
85      * @param name
86      * the name of the file
87      * @return the name of the deprecations zip entry
88      *
89      * @since 3.2
90      */

91     public static String JavaDoc getDeprecationEntry(final String JavaDoc name) {
92         return META_INF_ENTRY + "/" + name; //$NON-NLS-1$
93
}
94
95     /**
96      * Returns the name of the meta entry.
97      *
98      * @return the name of the meta entry
99      *
100      * @since 3.2
101      */

102     public static String JavaDoc getMetaEntry() {
103         return META_INF_ENTRY;
104     }
105
106     /**
107      * Computes and returns the elements as resources.
108      * The underlying resource is used for Java elements.
109      *
110      * @return a List with the selected resources
111      */

112     public static List JavaDoc asResources(Object JavaDoc[] fSelectedElements) {
113         if (fSelectedElements == null)
114             return null;
115         List JavaDoc selectedResources= new ArrayList JavaDoc(fSelectedElements.length);
116         for (int i= 0; i < fSelectedElements.length; i++) {
117             Object JavaDoc 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 JavaDoc 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     /**
134      * Gets the name of the manifest's main class
135      *
136      * @return a string with the name
137      */

138     static String JavaDoc getMainClassName(JarPackageData jarPackage) {
139         if (jarPackage.getManifestMainClass() == null)
140             return ""; //$NON-NLS-1$
141
else
142             return jarPackage.getManifestMainClass().getFullyQualifiedName();
143     }
144
145
146     private static boolean queryDialog(final Shell parent, final String JavaDoc title, final String JavaDoc message) {
147         Display display= parent.getDisplay();
148         if (display == null || display.isDisposed())
149             return false;
150         final boolean[] returnValue= new boolean[1];
151         Runnable JavaDoc runnable= new Runnable JavaDoc() {
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     /**
161      * Creates a <code>CoreException</code> with the given parameters.
162      *
163      * @param message a string with the message
164      * @param ex the exception to be wrapped or <code>null</code> if none
165      * @return a CoreException
166      */

167     public static CoreException createCoreException(String JavaDoc message, Exception JavaDoc ex) {
168         if (message == null)
169             message= ""; //$NON-NLS-1$
170
return new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IJavaStatusConstants.INTERNAL_ERROR, message, ex));
171     }
172
173     /**
174      * Tells whether the specified manifest main class is valid.
175      *
176      * @return <code>true</code> if a main class is specified and valid
177      */

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             // no main class specified
185
return true;
186
187         try {
188             // Check if main method is in scope
189
IFile file= (IFile)mainClass.getResource();
190             if (file == null || !contains(asResources(data.getElements()), file))
191                 return false;
192
193             // Test if it has a main method
194
return JavaModelUtil.hasMainMethod(mainClass);
195         } catch (JavaModelException e) {
196             JavaPlugin.log(e.getStatus());
197         }
198         return false;
199     }
200     
201     static boolean contains(List JavaDoc resources, IFile file) {
202         if (resources == null || file == null)
203             return false;
204             
205         if (resources.contains(file))
206             return true;
207         
208         Iterator JavaDoc iter= resources.iterator();
209         while (iter.hasNext()) {
210             IResource resource= (IResource)iter.next();
211             if (resource != null && resource.getType() != IResource.FILE) {
212                 List JavaDoc children= null;
213                 try {
214                     children= Arrays.asList(((IContainer)resource).members());
215                 } catch (CoreException ex) {
216                     // ignore this folder
217
continue;
218                 }
219                 if (children != null && contains(children, file))
220                     return true;
221             }
222         }
223         return false;
224     }
225
226     /**
227      * Calculates the crc and size of the resource and updates the entry.
228      *
229      * @param entry
230      * the jar entry to update
231      * @param stream
232      * the input stream
233      * @param buffer
234      * a shared buffer to store temporary data
235      *
236      * @throws IOException
237      * if an input/output error occurs
238      */

239     public static void calculateCrcAndSize(final JarEntry JavaDoc entry, final InputStream JavaDoc stream, final byte[] buffer) throws IOException JavaDoc {
240         int size= 0;
241         final CRC32 JavaDoc crc= new CRC32 JavaDoc();
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 JavaDoc exception) {
253                     // Do nothing
254
}
255             }
256         }
257         entry.setSize(size);
258         entry.setCrc(crc.getValue());
259     }
260 }
Popular Tags