1 11 package org.eclipse.pde.internal.core; 12 13 import java.io.File ; 14 import java.net.MalformedURLException ; 15 import java.net.URL ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.Set ; 20 21 import org.eclipse.core.runtime.IPath; 22 import org.eclipse.core.runtime.Path; 23 import org.eclipse.osgi.service.resolver.BundleDescription; 24 import org.eclipse.osgi.service.resolver.HostSpecification; 25 import org.eclipse.pde.core.plugin.IPluginAttribute; 26 import org.eclipse.pde.core.plugin.IPluginElement; 27 import org.eclipse.pde.core.plugin.IPluginExtension; 28 import org.eclipse.pde.core.plugin.IPluginModelBase; 29 import org.eclipse.pde.core.plugin.IPluginObject; 30 import org.eclipse.pde.core.plugin.PluginRegistry; 31 import org.eclipse.pde.internal.core.util.CoreUtility; 32 33 public class JavadocLocationManager { 34 35 public static final String JAVADOC_ID = "org.eclipse.pde.core.javadoc"; 37 private HashMap fLocations; 38 39 public String getJavadocLocation(IPluginModelBase model) { 40 File file = new File (model.getInstallLocation()); 41 if (file.isDirectory()) { 42 File doc = new File (file, "doc"); if (new File (doc, "package-list").exists()) return doc.getAbsolutePath(); 45 } else if (CoreUtility.jarContainsResource(file, "doc/package-list", false)) { return file.getAbsolutePath() + "!/doc"; } 48 return getEntry(model); 49 } 50 51 private String getEntry(IPluginModelBase model) { 52 initialize(); 53 BundleDescription desc = model.getBundleDescription(); 54 if (desc != null) { 55 HostSpecification host = desc.getHost(); 56 String id = host == null ? desc.getSymbolicName() : host.getName(); 57 if (id != null) { 58 Iterator iter = fLocations.keySet().iterator(); 59 while (iter.hasNext()) { 60 String location = iter.next().toString(); 61 Set set = (Set )fLocations.get(location); 62 if (set.contains(id)) 63 return location; 64 } 65 } 66 } 67 return null; 68 } 69 70 private synchronized void initialize() { 71 if (fLocations != null) return; 72 fLocations = new HashMap (); 73 IPluginModelBase[] models = PluginRegistry.getExternalModels(); 74 for (int i = 0; i < models.length; i++) { 75 IPluginExtension[] extensions = models[i].getPluginBase().getExtensions(); 76 for (int j = 0; j < extensions.length; j++) { 77 if (JAVADOC_ID.equals(extensions[j].getPoint())) 78 processExtension(extensions[j]); 79 } 80 } 81 } 82 83 private void processExtension(IPluginExtension extension) { 84 IPluginObject[] children = extension.getChildren(); 85 for (int i = 0; i < children.length; i++) { 86 if (children[i].getName().equals("javadoc")) { IPluginElement javadoc = (IPluginElement) children[i]; 88 IPluginAttribute attr = javadoc.getAttribute("path"); String path = (attr == null) ? null : attr.getValue(); 90 if (path == null) 91 continue; 92 try { 93 new URL (path); 94 processPlugins(path, javadoc.getChildren()); 95 } catch (MalformedURLException e) { 96 attr = javadoc.getAttribute("archive"); boolean archive = attr == null ? false : "true".equals(attr.getValue()); 99 IPath modelPath = new Path(extension.getModel().getInstallLocation()); 100 StringBuffer buffer = new StringBuffer (); 101 File file = modelPath.toFile(); 102 if (file.exists()) { 103 try { 104 buffer.append(file.toURI().toURL()); 105 } catch (MalformedURLException e1) { 106 buffer.append("file:/"); buffer.append(modelPath.toPortableString()); 108 } 109 if (file.isFile()) { 110 buffer.append("!/"); archive = true; 112 } 113 } 114 buffer.append(path); 115 if (archive) 116 buffer.insert(0, "jar:"); processPlugins(buffer.toString(), javadoc.getChildren()); } 119 } 120 } 121 } 122 123 private void processPlugins(String path, IPluginObject[] plugins) { 124 for (int i = 0; i < plugins.length; i++) { 125 if (plugins[i].getName().equals("plugin")) { IPluginElement plugin = (IPluginElement)plugins[i]; 127 IPluginAttribute attr = plugin.getAttribute("id"); String id = attr == null ? null : attr.getValue(); 129 if (id == null) 130 continue; 131 Set set = (Set )fLocations.get(path); 132 if (set == null) { 133 set = new HashSet (); 134 fLocations.put(path, set); 135 } 136 set.add(id); 137 } 138 } 139 } 140 141 public void reset() { 142 fLocations = null; 143 } 144 145 } 146 | Popular Tags |