1 11 package org.eclipse.pde.internal.core; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 16 import org.eclipse.core.resources.IFile; 17 import org.eclipse.core.resources.IProject; 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IPath; 21 import org.eclipse.core.runtime.Path; 22 import org.eclipse.jdt.core.IClasspathEntry; 23 import org.eclipse.jdt.core.JavaCore; 24 import org.eclipse.osgi.service.resolver.BundleDescription; 25 import org.eclipse.pde.core.build.IBuild; 26 import org.eclipse.pde.core.build.IBuildModel; 27 import org.eclipse.pde.core.plugin.IFragment; 28 import org.eclipse.pde.core.plugin.IFragmentModel; 29 import org.eclipse.pde.core.plugin.IPlugin; 30 import org.eclipse.pde.core.plugin.IPluginBase; 31 import org.eclipse.pde.core.plugin.IPluginLibrary; 32 import org.eclipse.pde.core.plugin.IPluginModelBase; 33 import org.eclipse.pde.core.plugin.PluginRegistry; 34 import org.eclipse.pde.core.plugin.TargetPlatform; 35 import org.eclipse.pde.internal.core.build.WorkspaceBuildModel; 36 import org.eclipse.pde.internal.core.bundle.BundleFragment; 37 import org.eclipse.pde.internal.core.bundle.BundlePlugin; 38 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase; 39 import org.eclipse.pde.internal.core.plugin.Fragment; 40 import org.eclipse.pde.internal.core.plugin.Plugin; 41 import org.eclipse.pde.internal.core.plugin.PluginBase; 42 43 public class ClasspathUtilCore { 44 45 public static void addLibraries(IPluginModelBase model, ArrayList result) throws CoreException { 46 if (new File (model.getInstallLocation()).isFile()) { 47 addJARdPlugin(model, result); 48 } else { 49 IPluginLibrary[] libraries = model.getPluginBase().getLibraries(); 50 for (int i = 0; i < libraries.length; i++) { 51 if (IPluginLibrary.RESOURCE.equals(libraries[i].getType())) 52 continue; 53 IClasspathEntry entry = createLibraryEntry(libraries[i]); 54 if (entry != null && !result.contains(entry)) { 55 result.add(entry); 56 } 57 } 58 } 59 } 60 61 private static void addJARdPlugin(IPluginModelBase model, ArrayList result) 62 throws CoreException { 63 64 IPath sourcePath = getSourceAnnotation(model, "."); if (sourcePath == null) 66 sourcePath = new Path(model.getInstallLocation()); 67 68 IClasspathEntry entry = JavaCore.newLibraryEntry( 69 new Path(model.getInstallLocation()), 70 sourcePath, 71 null, 72 false); 73 if (entry != null && !result.contains(entry)) { 74 result.add(entry); 75 } 76 } 77 78 protected static IClasspathEntry createLibraryEntry(IPluginLibrary library) { 79 80 IClasspathEntry entry = null; 81 try { 82 String name = library.getName(); 83 String expandedName = expandLibraryName(name); 84 85 IPluginModelBase model = library.getPluginModel(); 86 IPath path = getPath(model, expandedName); 87 if (path == null) { 88 if (model.isFragmentModel() || !containsVariables(name)) 89 return null; 90 model = resolveLibraryInFragments(library, expandedName); 91 if (model == null) 92 return null; 93 path = getPath(model, expandedName); 94 } 95 96 if (model.getUnderlyingResource() == null && path.toFile().isDirectory()) 98 return null; 99 100 entry = JavaCore.newLibraryEntry( 101 path, 102 getSourceAnnotation(model, expandedName), 103 null, 104 false); 105 } catch (CoreException e) { 106 } 107 return entry; 108 } 109 110 public static boolean hasExtensibleAPI(IPluginModelBase model) { 111 IPluginBase pluginBase = model.getPluginBase(); 112 if (pluginBase instanceof IPlugin) 113 return hasExtensibleAPI((IPlugin)pluginBase); 114 return false; 115 } 116 117 public static boolean isPatchFragment(BundleDescription desc) { 118 IPluginModelBase model = PluginRegistry.findModel(desc); 119 return model instanceof IFragmentModel 120 ? isPatchFragment(((IFragmentModel)model).getFragment()) : false; 121 } 122 123 public static boolean isPatchFragment(IPluginModelBase model) { 124 IPluginBase pluginBase = model.getPluginBase(); 125 if (pluginBase instanceof IFragment) 126 return isPatchFragment((IFragment)pluginBase); 127 return false; 128 } 129 130 private static boolean hasExtensibleAPI(IPlugin plugin) { 131 if (plugin instanceof Plugin) 132 return ((Plugin) plugin).hasExtensibleAPI(); 133 if (plugin instanceof BundlePlugin) 134 return ((BundlePlugin) plugin).hasExtensibleAPI(); 135 return false; 136 } 137 138 private static boolean isPatchFragment(IFragment fragment) { 139 if (fragment instanceof Fragment) 140 return ((Fragment)fragment).isPatch(); 141 if (fragment instanceof BundleFragment) 142 return ((BundleFragment)fragment).isPatch(); 143 return false; 144 } 145 146 public static boolean hasBundleStructure(IPluginModelBase model) { 147 if (model.getUnderlyingResource() != null) 148 return model instanceof IBundlePluginModelBase; 149 150 IPluginBase plugin = model.getPluginBase(); 151 if (plugin instanceof PluginBase) 152 return ((PluginBase)plugin).hasBundleStructure(); 153 return false; 154 } 155 156 public static boolean containsVariables(String name) { 157 return name.indexOf("$os$") != -1 || name.indexOf("$ws$") != -1 || name.indexOf("$nl$") != -1 || name.indexOf("$arch$") != -1; } 162 163 public static String expandLibraryName(String source) { 164 if (source == null || source.length() == 0) 165 return ""; if (source.indexOf("$ws$") != -1) source = 168 source.replaceAll( 169 "\\$ws\\$", "ws" + IPath.SEPARATOR + TargetPlatform.getWS()); if (source.indexOf("$os$") != -1) source = 173 source.replaceAll( 174 "\\$os\\$", "os" + IPath.SEPARATOR + TargetPlatform.getOS()); if (source.indexOf("$nl$") != -1) source = 178 source.replaceAll( 179 "\\$nl\\$", "nl" + IPath.SEPARATOR + TargetPlatform.getNL()); if (source.indexOf("$arch$") != -1) source = 183 source.replaceAll( 184 "\\$arch\\$", "arch" + IPath.SEPARATOR + TargetPlatform.getOSArch()); return source; 187 } 188 189 public static IPath getSourceAnnotation(IPluginModelBase model, String libraryName) 190 throws CoreException { 191 String zipName = getSourceZipName(libraryName); 192 IPath path = getPath(model, zipName); 193 if (path == null) { 194 SourceLocationManager manager = PDECore.getDefault().getSourceLocationManager(); 195 path = manager.findSourcePath(model.getPluginBase(), new Path(zipName)); 196 } 197 return path; 198 } 199 200 public static String getSourceZipName(String libraryName) { 201 int dot = libraryName.lastIndexOf('.'); 202 return (dot != -1) ? libraryName.substring(0, dot) + "src.zip" : libraryName; } 204 205 private static IPluginModelBase resolveLibraryInFragments( 206 IPluginLibrary library, 207 String libraryName) { 208 IFragmentModel[] fragments = 209 PDEManager.findFragmentsFor(library.getPluginModel()); 210 211 for (int i = 0; i < fragments.length; i++) { 212 IPath path = getPath(fragments[i], libraryName); 213 if (path != null) 214 return fragments[i]; 215 } 216 return null; 217 } 218 219 private static IPath getPath(IPluginModelBase model, String libraryName) { 220 IResource resource = model.getUnderlyingResource(); 221 if (resource != null) { 222 IResource jarFile = resource.getProject().findMember(libraryName); 223 if (jarFile != null) 224 return jarFile.getFullPath(); 225 } else { 226 File file = new File (model.getInstallLocation(), libraryName); 227 if (file.exists()) 228 return new Path(file.getAbsolutePath()); 229 } 230 return null; 231 } 232 233 public static IBuild getBuild(IPluginModelBase model) 234 throws CoreException { 235 IBuildModel buildModel = model.getBuildModel(); 236 if (buildModel == null) { 237 IProject project = model.getUnderlyingResource().getProject(); 238 IFile buildFile = project.getFile("build.properties"); if (buildFile.exists()) { 240 buildModel = new WorkspaceBuildModel(buildFile); 241 buildModel.load(); 242 } 243 } 244 return (buildModel != null) ? buildModel.getBuild() : null; 245 } 246 247 public static String getFilename(IPluginModelBase model) { 248 return new Path(model.getInstallLocation()).lastSegment(); 249 } 250 251 } 252 | Popular Tags |