1 8 package com.mountainminds.eclemma.internal.core.instr; 9 10 import java.io.FileInputStream ; 11 import java.io.FileOutputStream ; 12 import java.io.IOException ; 13 import java.io.OutputStream ; 14 import java.util.ArrayList ; 15 import java.util.List ; 16 import java.util.zip.ZipEntry ; 17 import java.util.zip.ZipInputStream ; 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 38 public class SourceLocation implements ISourceLocation { 39 40 private static final String JAVA_EXT = ".java"; 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 l = new ArrayList (); 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 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 prefix = rootpath == null ? "" : rootpath.toString(); byte[] buffer = new byte[0x1000]; 102 IPath srcfolder = EclEmmaCorePlugin.getInstance().getStateFiles().getSourceFolder(path); 103 if (!srcfolder.toFile().exists()) { 104 try { 105 ZipInputStream zip = new ZipInputStream (new FileInputStream (path.toFile())); 106 while (true) { 107 ZipEntry 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 out = new FileOutputStream (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 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 |