001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core;
011:
012: import java.io.File;
013: import java.net.MalformedURLException;
014: import java.net.URL;
015: import java.util.HashMap;
016: import java.util.HashSet;
017: import java.util.Iterator;
018: import java.util.Set;
019:
020: import org.eclipse.core.runtime.IConfigurationElement;
021: import org.eclipse.core.runtime.IExtension;
022: import org.eclipse.core.runtime.IPath;
023: import org.eclipse.core.runtime.Path;
024: import org.eclipse.osgi.service.resolver.BundleDescription;
025: import org.eclipse.osgi.service.resolver.HostSpecification;
026: import org.eclipse.pde.core.plugin.IPluginModelBase;
027: import org.eclipse.pde.core.plugin.PluginRegistry;
028: import org.eclipse.pde.internal.core.util.CoreUtility;
029:
030: public class JavadocLocationManager {
031:
032: public static final String JAVADOC_ID = "org.eclipse.pde.core.javadoc"; //$NON-NLS-1$
033:
034: private HashMap fLocations;
035:
036: public String getJavadocLocation(IPluginModelBase model) {
037: File file = new File(model.getInstallLocation());
038: if (file.isDirectory()) {
039: File doc = new File(file, "doc"); //$NON-NLS-1$
040: if (new File(doc, "package-list").exists()) //$NON-NLS-1$
041: return doc.getAbsolutePath();
042: } else if (CoreUtility.jarContainsResource(file,
043: "doc/package-list", false)) { //$NON-NLS-1$
044: return file.getAbsolutePath() + "!/doc"; //$NON-NLS-1$
045: }
046: return getEntry(model);
047: }
048:
049: private String getEntry(IPluginModelBase model) {
050: initialize();
051: BundleDescription desc = model.getBundleDescription();
052: if (desc != null) {
053: HostSpecification host = desc.getHost();
054: String id = host == null ? desc.getSymbolicName() : host
055: .getName();
056: if (id != null) {
057: Iterator iter = fLocations.keySet().iterator();
058: while (iter.hasNext()) {
059: String location = iter.next().toString();
060: Set set = (Set) fLocations.get(location);
061: if (set.contains(id))
062: return location;
063: }
064: }
065: }
066: return null;
067: }
068:
069: private synchronized void initialize() {
070: if (fLocations != null)
071: return;
072: fLocations = new HashMap();
073:
074: IExtension[] extensions = PDECore.getDefault()
075: .getExtensionsRegistry().findExtensions(JAVADOC_ID);
076: for (int i = 0; i < extensions.length; i++) {
077: IPluginModelBase base = PluginRegistry
078: .findModel(extensions[i].getContributor().getName());
079: // only search external models
080: if (base == null || base.getUnderlyingResource() != null)
081: continue;
082: processExtension(extensions[i], base);
083: }
084: }
085:
086: private void processExtension(IExtension extension,
087: IPluginModelBase base) {
088: IConfigurationElement[] children = extension
089: .getConfigurationElements();
090: for (int i = 0; i < children.length; i++) {
091: if (children[i].getName().equals("javadoc")) { //$NON-NLS-1$
092: String path = children[i].getAttribute("path"); //$NON-NLS-1$
093: if (path == null)
094: continue;
095: try {
096: new URL(path);
097: processPlugins(path, children[i].getChildren());
098: } catch (MalformedURLException e) {
099: String attr = children[i].getAttribute("archive"); //$NON-NLS-1$
100: boolean archive = attr == null ? false
101: : "true".equals(attr); //$NON-NLS-1$
102:
103: IPath modelPath = new Path(base
104: .getInstallLocation());
105: StringBuffer buffer = new StringBuffer();
106: File file = modelPath.toFile();
107: if (file.exists()) {
108: try {
109: buffer.append(file.toURI().toURL());
110: } catch (MalformedURLException e1) {
111: buffer.append("file:/"); //$NON-NLS-1$
112: buffer.append(modelPath.toPortableString());
113: }
114: if (file.isFile()) {
115: buffer.append("!/"); //$NON-NLS-1$
116: archive = true;
117: }
118: }
119: buffer.append(path);
120: if (archive)
121: buffer.insert(0, "jar:"); //$NON-NLS-1$
122: processPlugins(buffer.toString(), children[i]
123: .getChildren()); //$NON-NLS-1$
124: }
125: }
126: }
127: }
128:
129: private void processPlugins(String path,
130: IConfigurationElement[] plugins) {
131: for (int i = 0; i < plugins.length; i++) {
132: if (plugins[i].getName().equals("plugin")) { //$NON-NLS-1$
133: String id = plugins[i].getAttribute("id"); //$NON-NLS-1$
134: if (id == null)
135: continue;
136: Set set = (Set) fLocations.get(path);
137: if (set == null) {
138: set = new HashSet();
139: fLocations.put(path, set);
140: }
141: set.add(id);
142: }
143: }
144: }
145:
146: public void reset() {
147: fLocations = null;
148: }
149:
150: }
|