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.schema;
011:
012: import java.io.File;
013: import java.net.MalformedURLException;
014: import java.net.URL;
015: import java.util.HashMap;
016:
017: import org.eclipse.core.runtime.IPath;
018: import org.eclipse.core.runtime.Path;
019: import org.eclipse.pde.core.plugin.IFragment;
020: import org.eclipse.pde.core.plugin.IFragmentModel;
021: import org.eclipse.pde.core.plugin.IPluginBase;
022: import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
023: import org.eclipse.pde.core.plugin.IPluginModelBase;
024: import org.eclipse.pde.core.plugin.ModelEntry;
025: import org.eclipse.pde.core.plugin.PluginRegistry;
026: import org.eclipse.pde.internal.core.PDECore;
027: import org.eclipse.pde.internal.core.SourceLocationManager;
028: import org.eclipse.pde.internal.core.ischema.ISchema;
029: import org.eclipse.pde.internal.core.ischema.ISchemaDescriptor;
030: import org.eclipse.pde.internal.core.text.plugin.PluginExtensionPointNode;
031: import org.eclipse.pde.internal.core.util.CoreUtility;
032:
033: public class SchemaRegistry {
034:
035: private HashMap fRegistry = new HashMap();
036:
037: public ISchema getSchema(String extPointID) {
038: IPluginExtensionPoint point = PDECore.getDefault()
039: .getExtensionsRegistry().findExtensionPoint(extPointID);
040: if (point == null) {
041: // if there is an old schema associated with this extension point, release it.
042: if (fRegistry.containsKey(extPointID))
043: fRegistry.remove(extPointID);
044: return null;
045: }
046:
047: URL url = getSchemaURL(point);
048: if (url == null)
049: return null;
050:
051: ISchemaDescriptor desc = getExistingDescriptor(extPointID, url);
052: if (desc == null) {
053: desc = new SchemaDescriptor(extPointID, url);
054: fRegistry.put(extPointID, desc);
055: }
056:
057: return (desc == null) ? null : desc.getSchema(true);
058: }
059:
060: public ISchema getIncludedSchema(ISchemaDescriptor parent,
061: String schemaLocation) {
062: try {
063: URL url = IncludedSchemaDescriptor.computeURL(parent,
064: schemaLocation);
065: if (url == null)
066: return null;
067:
068: ISchemaDescriptor desc = getExistingDescriptor(url
069: .toString(), url);
070: if (desc == null) {
071: desc = new IncludedSchemaDescriptor(url);
072: fRegistry.put(url.toString(), desc);
073: }
074: return desc.getSchema(true);
075: } catch (MalformedURLException e) {
076: }
077: return null;
078: }
079:
080: private ISchemaDescriptor getExistingDescriptor(String key, URL url) {
081: ISchemaDescriptor desc = null;
082: if (fRegistry.containsKey(key)) {
083: desc = (ISchemaDescriptor) fRegistry.get(key);
084: if (hasSchemaChanged(desc, url))
085: desc = null;
086: }
087: return desc;
088: }
089:
090: public static URL getSchemaURL(IPluginExtensionPoint point,
091: IPluginModelBase base) {
092: URL url = getSchemaURL(point);
093: if (url != null) {
094: return url;
095: }
096: String schema = point.getSchema();
097: if ((schema == null) || (schema.trim().length() == 0)) {
098: return null;
099: }
100: return getSchemaURL(getId(point, base), schema);
101: }
102:
103: public static URL getSchemaURL(IPluginExtensionPoint point) {
104: String schema = point.getSchema();
105: if (schema == null || schema.trim().length() == 0)
106: return null;
107:
108: IPluginModelBase model = point.getPluginModel();
109: URL url = getSchemaURL(model.getPluginBase().getId(), schema);
110: if (url == null)
111: url = getSchemaFromSourceExtension(point.getPluginBase(),
112: new Path(schema));
113: return url;
114: }
115:
116: public static URL getSchemaFromSourceExtension(IPluginBase plugin,
117: IPath path) {
118: SourceLocationManager mgr = PDECore.getDefault()
119: .getSourceLocationManager();
120: File file = mgr.findSourceFile(plugin, path);
121: try {
122: if (file != null && file.exists() && file.isFile())
123: return file.toURL();
124: } catch (MalformedURLException e) {
125: }
126: return null;
127: }
128:
129: public static URL getSchemaURL(String pluginID, String schema) {
130: if (pluginID == null)
131: return null;
132:
133: URL url = null;
134: ModelEntry entry = PluginRegistry.findEntry(pluginID);
135: if (entry != null) {
136: IPluginModelBase[] models = entry.getWorkspaceModels();
137: for (int i = 0; i < models.length; i++) {
138: url = getSchemaURL(models[i], schema);
139: if (url != null)
140: break;
141: }
142: if (url == null) {
143: models = entry.getExternalModels();
144: for (int i = 0; i < models.length; i++) {
145: url = getSchemaURL(models[i], schema);
146: if (url != null)
147: break;
148: }
149: }
150: }
151: return url;
152: }
153:
154: private static URL getSchemaURL(IPluginModelBase model,
155: String schema) {
156: try {
157: if (model == null)
158: return null;
159:
160: String location = model.getInstallLocation();
161: if (location == null)
162: return null;
163:
164: File file = new File(location);
165: if (file.isDirectory()) {
166: File schemaFile = new File(file, schema);
167: if (schemaFile.exists())
168: return schemaFile.toURL();
169: } else if (CoreUtility.jarContainsResource(file, schema,
170: false)) { //$NON-NLS-1$
171: return new URL(
172: "jar:file:" + file.getAbsolutePath() + "!/" + schema); //$NON-NLS-1$ //$NON-NLS-2$
173: }
174: } catch (MalformedURLException e) {
175: }
176: return null;
177: }
178:
179: private boolean hasSchemaChanged(ISchemaDescriptor desc, URL url) {
180: if (!desc.getSchemaURL().equals(url))
181: return true;
182: File file = new File(url.getFile());
183: return (desc.getLastModified() != file.lastModified());
184: }
185:
186: public void shutdown() {
187: fRegistry.clear();
188: }
189:
190: private static String getId(IPluginExtensionPoint point,
191: IPluginModelBase base) {
192: String id = null;
193: if (point instanceof PluginExtensionPointNode) {
194: if (base instanceof IFragmentModel) {
195: IFragment fragment = ((IFragmentModel) base)
196: .getFragment();
197: if (fragment != null) {
198: id = fragment.getPluginId();
199: }
200: }
201: if (id == null) {
202: id = base.getPluginBase().getId();
203: }
204: }
205: return id;
206: }
207:
208: }
|