KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > schema > SchemaRegistry


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.schema;
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
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.Path;
20 import org.eclipse.pde.core.plugin.IFragment;
21 import org.eclipse.pde.core.plugin.IFragmentModel;
22 import org.eclipse.pde.core.plugin.IPluginBase;
23 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
24 import org.eclipse.pde.core.plugin.IPluginModelBase;
25 import org.eclipse.pde.core.plugin.ModelEntry;
26 import org.eclipse.pde.core.plugin.PluginRegistry;
27 import org.eclipse.pde.internal.core.PDECore;
28 import org.eclipse.pde.internal.core.PDEStateHelper;
29 import org.eclipse.pde.internal.core.SourceLocationManager;
30 import org.eclipse.pde.internal.core.ischema.ISchema;
31 import org.eclipse.pde.internal.core.ischema.ISchemaDescriptor;
32 import org.eclipse.pde.internal.core.text.plugin.PluginExtensionPointNode;
33 import org.eclipse.pde.internal.core.util.CoreUtility;
34
35
36 public class SchemaRegistry {
37     
38     private HashMap JavaDoc fRegistry = new HashMap JavaDoc();
39     
40     public ISchema getSchema(String JavaDoc extPointID) {
41         IPluginExtensionPoint point = PDEStateHelper.findExtensionPoint(extPointID);
42         if (point == null) {
43             // if there is an old schema associated with this extension point, release it.
44
if (fRegistry.containsKey(extPointID))
45                 fRegistry.remove(extPointID);
46             return null;
47         }
48         
49         URL JavaDoc url = getSchemaURL(point);
50         if (url == null)
51             return null;
52         
53         ISchemaDescriptor desc = getExistingDescriptor(extPointID, url);
54         if (desc == null) {
55             desc = new SchemaDescriptor(extPointID, url);
56             fRegistry.put(extPointID, desc);
57         }
58         
59         return (desc == null) ? null : desc.getSchema(true);
60     }
61     
62     public ISchema getIncludedSchema(ISchemaDescriptor parent, String JavaDoc schemaLocation) {
63         try {
64             URL JavaDoc url = IncludedSchemaDescriptor.computeURL(parent, schemaLocation);
65             if (url == null)
66                 return null;
67             
68             ISchemaDescriptor desc = getExistingDescriptor(url.toString(), url);
69             if (desc == null) {
70                 desc = new IncludedSchemaDescriptor(url);
71                 fRegistry.put(url.toString(), desc);
72             }
73             return desc.getSchema(true);
74         } catch (MalformedURLException JavaDoc e) {
75         }
76         return null;
77     }
78     
79     private ISchemaDescriptor getExistingDescriptor(String JavaDoc key, URL JavaDoc url) {
80         ISchemaDescriptor desc = null;
81         if (fRegistry.containsKey(key)) {
82             desc = (ISchemaDescriptor)fRegistry.get(key);
83             if (hasSchemaChanged(desc, url))
84                 desc = null;
85         }
86         return desc;
87     }
88
89     public static URL JavaDoc getSchemaURL(IPluginExtensionPoint point,
90             IPluginModelBase base) {
91         URL JavaDoc url = getSchemaURL(point);
92         if (url != null) {
93             return url;
94         }
95         String JavaDoc schema = point.getSchema();
96         if ((schema == null) ||
97                 (schema.trim().length() == 0)) {
98             return null;
99         }
100         return getSchemaURL(getId(point, base), schema);
101     }
102     
103     public static URL JavaDoc getSchemaURL(IPluginExtensionPoint point) {
104         String JavaDoc schema = point.getSchema();
105         if (schema == null || schema.trim().length() == 0)
106             return null;
107         
108         IPluginModelBase model = point.getPluginModel();
109         URL JavaDoc url = getSchemaURL(model.getPluginBase().getId(), schema);
110         if (url == null)
111             url = getSchemaFromSourceExtension(point.getPluginBase(), new Path(schema));
112         return url;
113     }
114     
115     public static URL JavaDoc getSchemaFromSourceExtension(IPluginBase plugin, IPath path) {
116         SourceLocationManager mgr = PDECore.getDefault().getSourceLocationManager();
117         File JavaDoc file = mgr.findSourceFile(plugin, path);
118         try {
119             if (file != null && file.exists() && file.isFile())
120                 return file.toURL();
121         } catch (MalformedURLException JavaDoc e) {
122         }
123         return null;
124     }
125     
126     public static URL JavaDoc getSchemaURL(String JavaDoc pluginID, String JavaDoc schema) {
127         if (pluginID == null)
128             return null;
129         
130         URL JavaDoc url = null;
131         ModelEntry entry = PluginRegistry.findEntry(pluginID);
132         if (entry != null) {
133             IPluginModelBase[] models = entry.getWorkspaceModels();
134             for (int i = 0; i < models.length; i++) {
135                 url = getSchemaURL(models[i], schema);
136                 if (url != null)
137                     break;
138             }
139             if (url == null) {
140                 models = entry.getExternalModels();
141                 for (int i = 0; i < models.length; i++) {
142                     url = getSchemaURL(models[i], schema);
143                     if (url != null)
144                         break;
145                 }
146             }
147         }
148         return url;
149     }
150     
151     private static URL JavaDoc getSchemaURL(IPluginModelBase model, String JavaDoc schema) {
152         try {
153             if (model == null)
154                 return null;
155             
156             String JavaDoc location = model.getInstallLocation();
157             if (location == null)
158                 return null;
159             
160             File JavaDoc file = new File JavaDoc(location);
161             if (file.isDirectory()) {
162                 File JavaDoc schemaFile = new File JavaDoc(file, schema);
163                 if (schemaFile.exists())
164                     return schemaFile.toURL();
165             } else if (CoreUtility.jarContainsResource(file, schema, false)) { //$NON-NLS-1$
166
return new URL JavaDoc("jar:file:" + file.getAbsolutePath() + "!/" + schema); //$NON-NLS-1$ //$NON-NLS-2$
167
}
168         } catch (MalformedURLException JavaDoc e) {
169         }
170         return null;
171     }
172     
173     private boolean hasSchemaChanged(ISchemaDescriptor desc, URL JavaDoc url) {
174         if (!desc.getSchemaURL().equals(url))
175             return true;
176         File JavaDoc file = new File JavaDoc(url.getFile());
177         return (desc.getLastModified() != file.lastModified());
178     }
179     
180     public void shutdown() {
181         fRegistry.clear();
182     }
183
184     private static String JavaDoc getId(IPluginExtensionPoint point, IPluginModelBase base) {
185         String JavaDoc id = null;
186         if (point instanceof PluginExtensionPointNode) {
187             if (base instanceof IFragmentModel) {
188                 IFragment fragment = ((IFragmentModel)base).getFragment();
189                 if (fragment != null) {
190                     id = fragment.getPluginId();
191                 }
192             }
193             if (id == null) {
194                 id = base.getPluginBase().getId();
195             }
196         }
197         return id;
198     }
199     
200 }
201
Popular Tags