1 11 package org.eclipse.core.internal.expressions; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IConfigurationElement; 18 import org.eclipse.core.runtime.IExtensionDelta; 19 import org.eclipse.core.runtime.IExtensionRegistry; 20 import org.eclipse.core.runtime.IRegistryChangeEvent; 21 import org.eclipse.core.runtime.IRegistryChangeListener; 22 import org.eclipse.core.runtime.InvalidRegistryObjectException; 23 import org.eclipse.core.runtime.Platform; 24 25 import org.eclipse.core.expressions.Expression; 26 import org.eclipse.core.expressions.ExpressionConverter; 27 28 33 public class DefinitionRegistry implements IRegistryChangeListener { 34 private Map cache= null; 35 36 private Map getCache() { 37 if (cache == null) { 38 cache= new HashMap (); 39 } 40 return cache; 41 } 42 43 public DefinitionRegistry() { 44 Platform.getExtensionRegistry().addRegistryChangeListener(this, "org.eclipse.core.expressions"); } 46 47 56 public Expression getExpression(String id) throws CoreException { 57 Expression cachedExpression= (Expression)getCache().get(id); 58 if (cachedExpression != null) { 59 return cachedExpression; 60 } 61 62 IExtensionRegistry registry= Platform.getExtensionRegistry(); 63 IConfigurationElement[] ces= registry.getConfigurationElementsFor("org.eclipse.core.expressions", "definitions"); 65 Expression foundExpression= null; 66 for (int i= 0; i < ces.length; i++) { 67 String cid= ces[i].getAttribute("id"); if (cid != null && cid.equals(id)) { 69 try { 70 foundExpression= getExpression(id, ces[i]); 71 break; 72 } catch (InvalidRegistryObjectException e) { 73 throw new CoreException(new ExpressionStatus(ExpressionStatus.MISSING_EXPRESSION, Messages.format( 74 ExpressionMessages.Missing_Expression, id))); 75 } 76 } 77 } 78 if (foundExpression == null) { 79 throw new CoreException(new ExpressionStatus(ExpressionStatus.MISSING_EXPRESSION, Messages.format( 80 ExpressionMessages.Missing_Expression, id))); 81 } 82 return foundExpression; 83 } 84 85 private Expression getExpression(String id, IConfigurationElement element) throws InvalidRegistryObjectException, 86 CoreException { 87 Expression expr= ExpressionConverter.getDefault().perform(element.getChildren()[0]); 88 if (expr != null) { 89 getCache().put(id, expr); 90 } 91 return expr; 92 } 93 94 public void registryChanged(IRegistryChangeEvent event) { 95 IExtensionDelta[] extensionDeltas= event.getExtensionDeltas("org.eclipse.core.expressions", "definitions"); for (int i= 0; i < extensionDeltas.length; i++) { 97 if (extensionDeltas[i].getKind() == IExtensionDelta.REMOVED) { 98 IConfigurationElement[] ces= extensionDeltas[i].getExtension().getConfigurationElements(); 99 for (int j= 0; j < ces.length; j++) { 100 String id= ces[j].getAttribute("id"); if (id != null) { 102 getCache().remove(id); 103 } 104 } 105 } 106 } 107 } 108 } | Popular Tags |