KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > resources > mapping > ModelProviderDescriptor


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.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 JavaDoc id;
26     private String JavaDoc[] extendedModels;
27     private String JavaDoc label;
28     private ModelProvider provider;
29     private Expression enablementRule;
30
31     private static EvaluationContext createEvaluationContext(Object JavaDoc 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 JavaDoc reason) throws CoreException {
47         throw new ResourceException(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, 1, reason, null));
48     }
49
50     public String JavaDoc[] getExtendedModels() {
51         return extendedModels;
52     }
53
54     public String JavaDoc getId() {
55         return id;
56     }
57
58     public String JavaDoc 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")) { //$NON-NLS-1$
81
try {
82                         provider = (ModelProvider) element.createExecutableExtension("class"); //$NON-NLS-1$
83
provider.init(this);
84                     } catch (ClassCastException JavaDoc e) {
85                         String JavaDoc 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     /**
101      * Initialize this descriptor based on the provided extension point.
102      */

103     protected void readExtension(IExtension extension) throws CoreException {
104         //read the extension
105
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 JavaDoc name = element.getName();
115             if (name.equalsIgnoreCase("extends-model")) { //$NON-NLS-1$
116
String JavaDoc attribute = element.getAttribute("id"); //$NON-NLS-1$
117
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 JavaDoc[]) extendsList.toArray(new String JavaDoc[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