KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > expressions > DefinitionRegistry


1 /*******************************************************************************
2  * Copyright (c) 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.core.internal.expressions;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
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 /**
29  * This manages the extension point that allows core expression reuse.
30  *
31  * @since 3.3
32  */

33 public class DefinitionRegistry implements IRegistryChangeListener {
34     private Map JavaDoc cache= null;
35
36     private Map JavaDoc getCache() {
37         if (cache == null) {
38             cache= new HashMap JavaDoc();
39         }
40         return cache;
41     }
42
43     public DefinitionRegistry() {
44         Platform.getExtensionRegistry().addRegistryChangeListener(this, "org.eclipse.core.expressions"); //$NON-NLS-1$
45
}
46
47     /**
48      * Get the expression with the id defined by an extension. This class will
49      * cache the expressions when appropriate, so it's OK to always ask the
50      * registry.
51      *
52      * @param id The unique ID of the expression definition
53      * @return the expression
54      * @throws CoreException If the expression cannot be found.
55      */

56     public Expression getExpression(String JavaDoc 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"); //$NON-NLS-1$ //$NON-NLS-2$
64

65         Expression foundExpression= null;
66         for (int i= 0; i < ces.length; i++) {
67             String JavaDoc cid= ces[i].getAttribute("id"); //$NON-NLS-1$
68
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 JavaDoc 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"); //$NON-NLS-1$//$NON-NLS-2$
96
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 JavaDoc id= ces[j].getAttribute("id"); //$NON-NLS-1$
101
if (id != null) {
102                         getCache().remove(id);
103                     }
104                 }
105             }
106         }
107     }
108 }
Popular Tags