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.net.URL;
013: import java.util.ArrayList;
014:
015: import org.eclipse.core.resources.IProject;
016: import org.eclipse.osgi.service.resolver.BundleDescription;
017: import org.eclipse.osgi.service.resolver.HostSpecification;
018: import org.eclipse.pde.core.plugin.IFragmentModel;
019: import org.eclipse.pde.core.plugin.IPluginModel;
020: import org.eclipse.pde.core.plugin.IPluginModelBase;
021: import org.eclipse.pde.core.plugin.PluginRegistry;
022: import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
023: import org.eclipse.pde.internal.core.plugin.ExternalPluginModelBase;
024:
025: public class PDEManager {
026:
027: public static IFragmentModel[] findFragmentsFor(
028: IPluginModelBase model) {
029: ArrayList result = new ArrayList();
030: BundleDescription desc = getBundleDescription(model);
031: if (desc != null) {
032: BundleDescription[] fragments = desc.getFragments();
033: for (int i = 0; i < fragments.length; i++) {
034: IPluginModelBase candidate = PluginRegistry
035: .findModel(fragments[i]);
036: if (candidate instanceof IFragmentModel) {
037: result.add(candidate);
038: }
039: }
040: }
041: return (IFragmentModel[]) result
042: .toArray(new IFragmentModel[result.size()]);
043: }
044:
045: public static IPluginModel findHostFor(IFragmentModel fragment) {
046: BundleDescription desc = getBundleDescription(fragment);
047: if (desc != null) {
048: HostSpecification spec = desc.getHost();
049: if (spec != null) {
050: IPluginModelBase host = PluginRegistry.findModel(spec
051: .getName());
052: if (host instanceof IPluginModel)
053: return (IPluginModel) host;
054: }
055: }
056: return null;
057: }
058:
059: private static BundleDescription getBundleDescription(
060: IPluginModelBase model) {
061: BundleDescription desc = model.getBundleDescription();
062:
063: if (desc == null && model.getUnderlyingResource() != null) {
064: // the model may be an editor model.
065: // editor models don't carry a bundle description
066: // get the core model counterpart.
067: IProject project = model.getUnderlyingResource()
068: .getProject();
069: IPluginModelBase coreModel = PluginRegistry
070: .findModel(project);
071: if (coreModel != null)
072: desc = coreModel.getBundleDescription();
073: }
074: return desc;
075: }
076:
077: public static URL[] getNLLookupLocations(IPluginModelBase model) {
078: ArrayList urls = new ArrayList();
079: addNLLocation(model, urls);
080: if (model instanceof IPluginModel) {
081: IFragmentModel[] fragments = findFragmentsFor(model);
082: for (int i = 0; i < fragments.length; i++) {
083: addNLLocation(fragments[i], urls);
084: }
085: } else if (model instanceof IFragmentModel) {
086: IPluginModel host = findHostFor((IFragmentModel) model);
087: if (host != null)
088: addNLLocation(host, urls);
089: }
090: return (URL[]) urls.toArray(new URL[urls.size()]);
091: }
092:
093: private static void addNLLocation(IPluginModelBase model,
094: ArrayList urls) {
095: URL location = model.getNLLookupLocation();
096: if (location != null)
097: urls.add(location);
098: }
099:
100: public static String getBundleLocalization(IPluginModelBase model) {
101: if (model instanceof IBundlePluginModelBase
102: && model.getUnderlyingResource() != null)
103: return ((IBundlePluginModelBase) model)
104: .getBundleLocalization();
105:
106: if (model instanceof ExternalPluginModelBase)
107: return ((ExternalPluginModelBase) model).getLocalization();
108:
109: return "plugin"; //$NON-NLS-1$
110: }
111:
112: }
|