1 11 package org.eclipse.core.internal.resources.mapping; 12 13 import java.util.*; 14 import org.eclipse.core.expressions.*; 15 import org.eclipse.core.internal.resources.ResourceException; 16 import org.eclipse.core.internal.utils.Messages; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.resources.ResourcesPlugin; 19 import org.eclipse.core.resources.mapping.*; 20 import org.eclipse.core.runtime.*; 21 import org.eclipse.osgi.util.NLS; 22 23 public class ModelProviderDescriptor implements IModelProviderDescriptor { 24 25 private String id; 26 private String [] extendedModels; 27 private String label; 28 private ModelProvider provider; 29 private Expression enablementRule; 30 31 private static EvaluationContext createEvaluationContext(Object element) { 32 EvaluationContext result = new EvaluationContext(null, element); 33 return result; 34 } 35 36 public ModelProviderDescriptor(IExtension extension) throws CoreException { 37 readExtension(extension); 38 } 39 40 private boolean convert(EvaluationResult eval) { 41 if (eval == EvaluationResult.FALSE) 42 return false; 43 return true; 44 } 45 46 protected void fail(String reason) throws CoreException { 47 throw new ResourceException(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, 1, reason, null)); 48 } 49 50 public String [] getExtendedModels() { 51 return extendedModels; 52 } 53 54 public String getId() { 55 return id; 56 } 57 58 public String getLabel() { 59 return label; 60 } 61 62 public IResource[] getMatchingResources(IResource[] resources) throws CoreException { 63 Set result = new HashSet(); 64 for (int i = 0; i < resources.length; i++) { 65 IResource resource = resources[i]; 66 EvaluationContext evalContext = createEvaluationContext(resource); 67 if (matches(evalContext)) { 68 result.add(resource); 69 } 70 } 71 return (IResource[]) result.toArray(new IResource[result.size()]); 72 } 73 74 public synchronized ModelProvider getModelProvider() throws CoreException { 75 if (provider == null) { 76 IExtension extension = Platform.getExtensionRegistry().getExtension(ResourcesPlugin.PI_RESOURCES, ResourcesPlugin.PT_MODEL_PROVIDERS, id); 77 IConfigurationElement[] elements = extension.getConfigurationElements(); 78 for (int i = 0; i < elements.length; i++) { 79 IConfigurationElement element = elements[i]; 80 if (element.getName().equalsIgnoreCase("modelProvider")) { try { 82 provider = (ModelProvider) element.createExecutableExtension("class"); provider.init(this); 84 } catch (ClassCastException e) { 85 String message = NLS.bind(Messages.mapping_wrongType, id); 86 throw new CoreException(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, Platform.PLUGIN_ERROR, message, e)); 87 } 88 } 89 } 90 } 91 return provider; 92 } 93 94 public boolean matches(IEvaluationContext context) throws CoreException { 95 if (enablementRule == null) 96 return false; 97 return convert(enablementRule.evaluate(context)); 98 } 99 100 103 protected void readExtension(IExtension extension) throws CoreException { 104 id = extension.getUniqueIdentifier(); 106 if (id == null) 107 fail(Messages.mapping_noIdentifier); 108 label = extension.getLabel(); 109 IConfigurationElement[] elements = extension.getConfigurationElements(); 110 int count = elements.length; 111 ArrayList extendsList = new ArrayList(count); 112 for (int i = 0; i < count; i++) { 113 IConfigurationElement element = elements[i]; 114 String name = element.getName(); 115 if (name.equalsIgnoreCase("extends-model")) { String attribute = element.getAttribute("id"); if (attribute == null) 118 fail(NLS.bind(Messages.mapping_invalidDef, id)); 119 extendsList.add(attribute); 120 } else if (name.equalsIgnoreCase(ExpressionTagNames.ENABLEMENT)) { 121 enablementRule = ExpressionConverter.getDefault().perform(element); 122 } 123 } 124 extendedModels = (String []) extendsList.toArray(new String [extendsList.size()]); 125 } 126 127 public ResourceTraversal[] getMatchingTraversals(ResourceTraversal[] traversals) throws CoreException { 128 List result = new ArrayList(); 129 for (int i = 0; i < traversals.length; i++) { 130 ResourceTraversal traversal = traversals[i]; 131 if (getMatchingResources(traversal.getResources()).length > 0) { 132 result.add(traversal); 133 } 134 } 135 return (ResourceTraversal[]) result.toArray(new ResourceTraversal[result.size()]); 136 } 137 138 } 139 | Popular Tags |