KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mountainminds > eclemma > internal > core > instr > SourceLocation


1 /*******************************************************************************
2  * Copyright (c) 2006 Mountainminds GmbH & Co. KG
3  * This software is provided under the terms of the Eclipse Public License v1.0
4  * See http://www.eclipse.org/legal/epl-v10.html.
5  *
6  * $Id: SourceLocation.java 84 2006-09-14 20:00:01Z mho $
7  ******************************************************************************/

8 package com.mountainminds.eclemma.internal.core.instr;
9
10 import java.io.FileInputStream JavaDoc;
11 import java.io.FileOutputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.OutputStream JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.zip.ZipEntry JavaDoc;
17 import java.util.zip.ZipInputStream JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.jdt.core.IPackageFragmentRoot;
24 import org.eclipse.jdt.core.JavaModelException;
25 import org.eclipse.osgi.util.NLS;
26
27 import com.mountainminds.eclemma.core.EclEmmaStatus;
28 import com.mountainminds.eclemma.core.ISourceLocation;
29 import com.mountainminds.eclemma.internal.core.CoreMessages;
30 import com.mountainminds.eclemma.internal.core.EclEmmaCorePlugin;
31
32 /**
33  * Implementation of {@link ISourceLocation}.
34  *
35  * @author Marc R. Hoffmann
36  * @version $Revision: 84 $
37  */

38 public class SourceLocation implements ISourceLocation {
39   
40   private static final String JavaDoc JAVA_EXT = ".java"; //$NON-NLS-1$
41

42   public static ISourceLocation findLocation(IPackageFragmentRoot root)
43       throws JavaModelException {
44     if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
45       IPath path = EclEmmaCorePlugin.getAbsolutePath(root.getPath());
46       return new SourceLocation(path, new Path(
47           IPackageFragmentRoot.DEFAULT_PACKAGEROOT_PATH));
48     } else {
49       IPath path = root.getSourceAttachmentPath();
50       if (path != null) {
51         path = EclEmmaCorePlugin.getAbsolutePath(path);
52         return new SourceLocation(path, root.getSourceAttachmentRootPath());
53       } else {
54         return null;
55       }
56     }
57   }
58
59   public static ISourceLocation[] findLocations(IPackageFragmentRoot[] roots)
60       throws JavaModelException {
61     List JavaDoc l = new ArrayList JavaDoc();
62     for (int i = 0; i < roots.length; i++) {
63       ISourceLocation loc = findLocation(roots[i]);
64       if (loc != null) {
65         l.add(loc);
66       }
67     }
68     return (ISourceLocation[]) l.toArray(new ISourceLocation[l.size()]);
69   }
70
71   private IPath path;
72   private IPath rootpath;
73
74   /**
75    * Creates a source location for the given path and root path.
76    *
77    * @param path
78    * @param rootpath
79    */

80   public SourceLocation(IPath path, IPath rootpath) {
81     this.path = path;
82     this.rootpath = rootpath;
83   }
84
85   public IPath getPath() {
86     return path;
87   }
88
89   public IPath getRootPath() {
90     return rootpath;
91   }
92
93   public boolean isArchive() {
94     return path.toFile().isFile();
95   }
96
97   public void extract(IProgressMonitor monitor) throws CoreException {
98     if (isArchive()) {
99       monitor.beginTask(NLS.bind(CoreMessages.ExtractingSourceArchive_task, path), 1);
100       String JavaDoc prefix = rootpath == null ? "" : rootpath.toString(); //$NON-NLS-1$
101
byte[] buffer = new byte[0x1000];
102       IPath srcfolder = EclEmmaCorePlugin.getInstance().getStateFiles().getSourceFolder(path);
103       if (!srcfolder.toFile().exists()) {
104         try {
105           ZipInputStream JavaDoc zip = new ZipInputStream JavaDoc(new FileInputStream JavaDoc(path.toFile()));
106           while (true) {
107             ZipEntry JavaDoc entry = zip.getNextEntry();
108             if (entry == null) break;
109             if (!entry.isDirectory() && entry.getName().startsWith(prefix) && entry.getName().endsWith(JAVA_EXT)) {
110               IPath path = srcfolder.append(entry.getName().substring(prefix.length()));
111               path.removeLastSegments(1).toFile().mkdirs();
112               OutputStream JavaDoc out = new FileOutputStream JavaDoc(path.toFile());
113               int len;
114               while ((len = zip.read(buffer)) != -1) {
115                 out.write(buffer, 0, len);
116               }
117               out.close();
118             }
119             zip.closeEntry();
120           }
121           zip.close();
122         } catch (IOException JavaDoc e) {
123           throw new CoreException(EclEmmaStatus.SOURCE_EXTRACTION_ERROR.getStatus(path, e));
124         }
125       }
126       path = srcfolder;
127       rootpath = null;
128    }
129     monitor.done();
130   }
131
132 }
133
Popular Tags