1 11 package org.eclipse.pde.internal.core.schema; 12 13 import java.io.File ; 14 import java.net.MalformedURLException ; 15 import java.net.URL ; 16 import java.util.HashMap ; 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 fRegistry = new HashMap (); 39 40 public ISchema getSchema(String extPointID) { 41 IPluginExtensionPoint point = PDEStateHelper.findExtensionPoint(extPointID); 42 if (point == null) { 43 if (fRegistry.containsKey(extPointID)) 45 fRegistry.remove(extPointID); 46 return null; 47 } 48 49 URL 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 schemaLocation) { 63 try { 64 URL 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 e) { 75 } 76 return null; 77 } 78 79 private ISchemaDescriptor getExistingDescriptor(String key, URL 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 getSchemaURL(IPluginExtensionPoint point, 90 IPluginModelBase base) { 91 URL url = getSchemaURL(point); 92 if (url != null) { 93 return url; 94 } 95 String 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 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(), new Path(schema)); 112 return url; 113 } 114 115 public static URL getSchemaFromSourceExtension(IPluginBase plugin, IPath path) { 116 SourceLocationManager mgr = PDECore.getDefault().getSourceLocationManager(); 117 File file = mgr.findSourceFile(plugin, path); 118 try { 119 if (file != null && file.exists() && file.isFile()) 120 return file.toURL(); 121 } catch (MalformedURLException e) { 122 } 123 return null; 124 } 125 126 public static URL getSchemaURL(String pluginID, String schema) { 127 if (pluginID == null) 128 return null; 129 130 URL 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 getSchemaURL(IPluginModelBase model, String schema) { 152 try { 153 if (model == null) 154 return null; 155 156 String location = model.getInstallLocation(); 157 if (location == null) 158 return null; 159 160 File file = new File (location); 161 if (file.isDirectory()) { 162 File schemaFile = new File (file, schema); 163 if (schemaFile.exists()) 164 return schemaFile.toURL(); 165 } else if (CoreUtility.jarContainsResource(file, schema, false)) { return new URL ("jar:file:" + file.getAbsolutePath() + "!/" + schema); } 168 } catch (MalformedURLException e) { 169 } 170 return null; 171 } 172 173 private boolean hasSchemaChanged(ISchemaDescriptor desc, URL url) { 174 if (!desc.getSchemaURL().equals(url)) 175 return true; 176 File file = new File (url.getFile()); 177 return (desc.getLastModified() != file.lastModified()); 178 } 179 180 public void shutdown() { 181 fRegistry.clear(); 182 } 183 184 private static String getId(IPluginExtensionPoint point, IPluginModelBase base) { 185 String 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 |