KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > JavadocLocationManager


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core;
12
13 import java.io.File JavaDoc;
14 import java.net.MalformedURLException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Set JavaDoc;
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 JAVADOC_ID = "org.eclipse.pde.core.javadoc"; //$NON-NLS-1$
36

37     private HashMap JavaDoc fLocations;
38     
39     public String JavaDoc getJavadocLocation(IPluginModelBase model) {
40         File JavaDoc file = new File JavaDoc(model.getInstallLocation());
41         if (file.isDirectory()) {
42             File JavaDoc doc = new File JavaDoc(file, "doc"); //$NON-NLS-1$
43
if (new File JavaDoc(doc, "package-list").exists()) //$NON-NLS-1$
44
return doc.getAbsolutePath();
45         } else if (CoreUtility.jarContainsResource(file, "doc/package-list", false)) { //$NON-NLS-1$
46
return file.getAbsolutePath() + "!/doc"; //$NON-NLS-1$
47
}
48         return getEntry(model);
49     }
50
51     private String JavaDoc getEntry(IPluginModelBase model) {
52         initialize();
53         BundleDescription desc = model.getBundleDescription();
54         if (desc != null) {
55             HostSpecification host = desc.getHost();
56             String JavaDoc id = host == null ? desc.getSymbolicName() : host.getName();
57             if (id != null) {
58                 Iterator JavaDoc iter = fLocations.keySet().iterator();
59                 while (iter.hasNext()) {
60                     String JavaDoc location = iter.next().toString();
61                     Set JavaDoc set = (Set JavaDoc)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 JavaDoc();
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")) { //$NON-NLS-1$
87
IPluginElement javadoc = (IPluginElement) children[i];
88                 IPluginAttribute attr = javadoc.getAttribute("path"); //$NON-NLS-1$
89
String JavaDoc path = (attr == null) ? null : attr.getValue();
90                 if (path == null)
91                     continue;
92                 try {
93                     new URL JavaDoc(path);
94                     processPlugins(path, javadoc.getChildren());
95                 } catch (MalformedURLException JavaDoc e) {
96                     attr = javadoc.getAttribute("archive"); //$NON-NLS-1$
97
boolean archive = attr == null ? false : "true".equals(attr.getValue()); //$NON-NLS-1$
98

99                     IPath modelPath = new Path(extension.getModel().getInstallLocation());
100                     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
101                     File JavaDoc file = modelPath.toFile();
102                     if (file.exists()) {
103                         try {
104                             buffer.append(file.toURI().toURL());
105                         } catch (MalformedURLException JavaDoc e1) {
106                             buffer.append("file:/"); //$NON-NLS-1$
107
buffer.append(modelPath.toPortableString());
108                         }
109                         if (file.isFile()) {
110                             buffer.append("!/"); //$NON-NLS-1$
111
archive = true;
112                         }
113                     }
114                     buffer.append(path);
115                     if (archive)
116                         buffer.insert(0, "jar:"); //$NON-NLS-1$
117
processPlugins(buffer.toString(), javadoc.getChildren()); //$NON-NLS-1$
118
}
119             }
120         }
121     }
122     
123     private void processPlugins(String JavaDoc path, IPluginObject[] plugins) {
124         for (int i = 0; i < plugins.length; i++) {
125             if (plugins[i].getName().equals("plugin")) { //$NON-NLS-1$
126
IPluginElement plugin = (IPluginElement)plugins[i];
127                 IPluginAttribute attr = plugin.getAttribute("id"); //$NON-NLS-1$
128
String JavaDoc id = attr == null ? null : attr.getValue();
129                 if (id == null)
130                     continue;
131                 Set JavaDoc set = (Set JavaDoc)fLocations.get(path);
132                 if (set == null) {
133                     set = new HashSet JavaDoc();
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