001: /*******************************************************************************
002: * Copyright (c) 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.wizards.imports;
011:
012: import java.io.File;
013: import java.io.FileInputStream;
014: import java.io.IOException;
015: import java.lang.reflect.InvocationTargetException;
016: import java.util.ArrayList;
017: import java.util.List;
018: import java.util.zip.ZipFile;
019:
020: import org.eclipse.core.resources.IFile;
021: import org.eclipse.core.resources.IProject;
022: import org.eclipse.core.resources.IResource;
023: import org.eclipse.core.resources.IWorkspaceRunnable;
024: import org.eclipse.core.runtime.CoreException;
025: import org.eclipse.core.runtime.IPath;
026: import org.eclipse.core.runtime.IProgressMonitor;
027: import org.eclipse.core.runtime.IStatus;
028: import org.eclipse.core.runtime.OperationCanceledException;
029: import org.eclipse.core.runtime.Status;
030: import org.eclipse.pde.internal.core.util.CoreUtility;
031: import org.eclipse.pde.internal.ui.PDEPlugin;
032: import org.eclipse.ui.dialogs.IOverwriteQuery;
033: import org.eclipse.ui.wizards.datatransfer.IImportStructureProvider;
034: import org.eclipse.ui.wizards.datatransfer.ImportOperation;
035: import org.eclipse.ui.wizards.datatransfer.ZipFileStructureProvider;
036:
037: public abstract class JarImportOperation implements IWorkspaceRunnable {
038:
039: protected void extractZipFile(File file, IPath destPath,
040: IProgressMonitor monitor) throws CoreException {
041: ZipFile zipFile = null;
042: try {
043: zipFile = new ZipFile(file);
044: ZipFileStructureProvider provider = new ZipFileStructureProvider(
045: zipFile);
046: importContent(provider.getRoot(), destPath, provider, null,
047: monitor);
048: } catch (IOException e) {
049: IStatus status = new Status(IStatus.ERROR, PDEPlugin
050: .getPluginId(), IStatus.ERROR, e.getMessage(), e);
051: throw new CoreException(status);
052: } finally {
053: if (zipFile != null) {
054: try {
055: zipFile.close();
056: } catch (IOException e) {
057: }
058: }
059: }
060: }
061:
062: protected void importContent(Object source, IPath destPath,
063: IImportStructureProvider provider, List filesToImport,
064: IProgressMonitor monitor) throws CoreException {
065: IOverwriteQuery query = new IOverwriteQuery() {
066: public String queryOverwrite(String file) {
067: return ALL;
068: }
069: };
070: try {
071: ImportOperation op = new ImportOperation(destPath, source,
072: provider, query);
073: op.setCreateContainerStructure(false);
074: if (filesToImport != null) {
075: op.setFilesToImport(filesToImport);
076: }
077: op.run(monitor);
078: } catch (InvocationTargetException e) {
079: IStatus status = new Status(IStatus.ERROR, PDEPlugin
080: .getPluginId(), IStatus.ERROR, e.getMessage(), e);
081: throw new CoreException(status);
082: } catch (InterruptedException e) {
083: throw new OperationCanceledException(e.getMessage());
084: }
085: }
086:
087: protected void extractResources(File file, IResource dest,
088: IProgressMonitor monitor) throws CoreException {
089: ZipFile zipFile = null;
090: try {
091: zipFile = new ZipFile(file);
092: ZipFileStructureProvider provider = new ZipFileStructureProvider(
093: zipFile);
094: ArrayList collected = new ArrayList();
095: collectResources(provider, provider.getRoot(), true,
096: collected);
097: importContent(provider.getRoot(), dest.getFullPath(),
098: provider, collected, monitor);
099: } catch (IOException e) {
100: IStatus status = new Status(IStatus.ERROR, PDEPlugin
101: .getPluginId(), IStatus.ERROR, e.getMessage(), e);
102: throw new CoreException(status);
103: } finally {
104: if (zipFile != null) {
105: try {
106: zipFile.close();
107: } catch (IOException e) {
108: }
109: }
110: }
111: }
112:
113: protected void extractJavaResources(File file, IResource dest,
114: IProgressMonitor monitor) throws CoreException {
115: ZipFile zipFile = null;
116: try {
117: zipFile = new ZipFile(file);
118: ZipFileStructureProvider provider = new ZipFileStructureProvider(
119: zipFile);
120: ArrayList collected = new ArrayList();
121: collectJavaResources(provider, provider.getRoot(),
122: collected);
123: importContent(provider.getRoot(), dest.getFullPath(),
124: provider, collected, monitor);
125: } catch (IOException e) {
126: IStatus status = new Status(IStatus.ERROR, PDEPlugin
127: .getPluginId(), IStatus.ERROR, e.getMessage(), e);
128: throw new CoreException(status);
129: } finally {
130: if (zipFile != null) {
131: try {
132: zipFile.close();
133: } catch (IOException e) {
134: }
135: }
136: }
137: }
138:
139: protected void importArchive(IProject project, File archive,
140: IPath destPath) throws CoreException {
141: try {
142: if (destPath.segmentCount() > 2)
143: CoreUtility.createFolder(project.getFolder(destPath
144: .removeLastSegments(1)));
145: IFile file = project.getFile(destPath);
146: FileInputStream fstream = new FileInputStream(archive);
147: if (file.exists())
148: file.setContents(fstream, true, false, null);
149: else
150: file.create(fstream, true, null);
151: fstream.close();
152: } catch (IOException e) {
153: IStatus status = new Status(IStatus.ERROR, PDEPlugin
154: .getPluginId(), IStatus.OK, e.getMessage(), e);
155: throw new CoreException(status);
156: }
157: }
158:
159: private void collectResources(ZipFileStructureProvider provider,
160: Object element, boolean excludeMeta, ArrayList collected) {
161: List children = provider.getChildren(element);
162: if (children != null && !children.isEmpty()) {
163: for (int i = 0; i < children.size(); i++) {
164: Object curr = children.get(i);
165: if (provider.isFolder(curr)) {
166: if (!excludeMeta
167: || !provider.getLabel(curr).equals(
168: "META-INF")) { //$NON-NLS-1$
169: collectResources(provider, curr, excludeMeta,
170: collected);
171: }
172: } else if (!provider.getLabel(curr).endsWith(".class")) { //$NON-NLS-1$
173: collected.add(curr);
174: }
175: }
176: }
177: }
178:
179: protected void collectNonJavaResources(
180: ZipFileStructureProvider provider, Object element,
181: ArrayList collected) {
182: List children = provider.getChildren(element);
183: if (children != null && !children.isEmpty()) {
184: for (int i = 0; i < children.size(); i++) {
185: Object curr = children.get(i);
186: if (provider.isFolder(curr)) {
187: if (!provider.getLabel(curr).equals("src") && !isClassFolder(provider, curr)) { //$NON-NLS-1$
188: ArrayList list = new ArrayList();
189: collectResources(provider, curr, false, list);
190: collected.addAll(list);
191: }
192: } else if (!provider.getLabel(curr).endsWith(".class")) { //$NON-NLS-1$
193: collected.add(curr);
194: }
195: }
196: }
197: }
198:
199: protected void collectJavaFiles(ZipFileStructureProvider provider,
200: Object element, ArrayList collected) {
201: List children = provider.getChildren(element);
202: if (children != null && !children.isEmpty()) {
203: for (int i = 0; i < children.size(); i++) {
204: Object curr = children.get(i);
205: if (provider.isFolder(curr)) {
206: if (provider.getLabel(curr).equals("src")) { //$NON-NLS-1$
207: ArrayList list = new ArrayList();
208: collectResources(provider, curr, false, list);
209: collected.addAll(list);
210: }
211: }
212: }
213: }
214: }
215:
216: protected void collectJavaResources(
217: ZipFileStructureProvider provider, Object element,
218: ArrayList collected) {
219: List children = provider.getChildren(element);
220: if (children != null && !children.isEmpty()) {
221: for (int i = 0; i < children.size(); i++) {
222: Object curr = children.get(i);
223: if (provider.isFolder(curr)) {
224: if (isClassFolder(provider, curr)) {
225: ArrayList list = new ArrayList();
226: collectResources(provider, curr, false, list);
227: collected.addAll(list);
228: }
229: }
230: }
231: }
232: }
233:
234: private boolean isClassFolder(ZipFileStructureProvider provider,
235: Object element) {
236: List children = provider.getChildren(element);
237: if (children != null && !children.isEmpty()) {
238: for (int i = 0; i < children.size(); i++) {
239: Object curr = children.get(i);
240: if (provider.isFolder(curr)) {
241: if (isClassFolder(provider, curr)) {
242: return true;
243: }
244: } else if (provider.getLabel(curr).endsWith(".class")) { //$NON-NLS-1$
245: return true;
246: }
247: }
248: }
249: return false;
250: }
251:
252: protected boolean hasEmbeddedSource(
253: ZipFileStructureProvider provider) {
254: List children = provider.getChildren(provider.getRoot());
255: if (children != null && !children.isEmpty()) {
256: for (int i = 0; i < children.size(); i++) {
257: Object curr = children.get(i);
258: if (provider.isFolder(curr)
259: && provider.getLabel(curr).equals("src")) { //$NON-NLS-1$
260: return true;
261: }
262: }
263: }
264: return false;
265: }
266:
267: protected boolean containsCode(ZipFileStructureProvider provider) {
268: List children = provider.getChildren(provider.getRoot());
269: if (children != null && !children.isEmpty()) {
270: for (int i = 0; i < children.size(); i++) {
271: Object curr = children.get(i);
272: if (provider.isFolder(curr)
273: && isClassFolder(provider, curr)) {
274: return true;
275: }
276: }
277: }
278: return false;
279: }
280:
281: protected boolean containsCode(File file) {
282: ZipFile zipFile = null;
283: try {
284: zipFile = new ZipFile(file);
285: return containsCode(new ZipFileStructureProvider(zipFile));
286: } catch (IOException e) {
287: } finally {
288: if (zipFile != null) {
289: try {
290: zipFile.close();
291: } catch (IOException e) {
292: }
293: }
294: }
295: return true;
296: }
297:
298: protected String[] getTopLevelResources(File file) {
299: ArrayList result = new ArrayList();
300: ZipFile zipFile = null;
301: try {
302: zipFile = new ZipFile(file);
303: ZipFileStructureProvider provider = new ZipFileStructureProvider(
304: zipFile);
305: List children = provider.getChildren(provider.getRoot());
306: if (children != null && !children.isEmpty()) {
307: for (int i = 0; i < children.size(); i++) {
308: Object curr = children.get(i);
309: if (provider.isFolder(curr)) {
310: if (!isClassFolder(provider, curr))
311: result.add(provider.getLabel(curr) + "/"); //$NON-NLS-1$
312: else {
313: if (!result.contains(".")) //$NON-NLS-1$
314: result.add("."); //$NON-NLS-1$
315: }
316: } else {
317: result.add(provider.getLabel(curr));
318: }
319: }
320: }
321: } catch (IOException e) {
322: } finally {
323: if (zipFile != null) {
324: try {
325: zipFile.close();
326: } catch (IOException e) {
327: }
328: }
329: }
330: return (String[]) result.toArray(new String[result.size()]);
331: }
332:
333: }
|