KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > correction > ContributedProcessorDescriptor


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.jdt.internal.ui.text.correction;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.core.runtime.IStatus;
20
21 import org.eclipse.core.expressions.EvaluationContext;
22 import org.eclipse.core.expressions.EvaluationResult;
23 import org.eclipse.core.expressions.Expression;
24 import org.eclipse.core.expressions.ExpressionConverter;
25 import org.eclipse.core.expressions.ExpressionTagNames;
26
27 import org.eclipse.jdt.core.ICompilationUnit;
28 import org.eclipse.jdt.core.IJavaModelMarker;
29 import org.eclipse.jdt.core.IJavaProject;
30 import org.eclipse.jdt.core.JavaCore;
31
32 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
33
34 import org.eclipse.jdt.internal.ui.JavaPlugin;
35 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
36
37 public final class ContributedProcessorDescriptor {
38
39     private final IConfigurationElement fConfigurationElement;
40     private Object JavaDoc fProcessorInstance;
41     private Boolean JavaDoc fStatus;
42     private boolean fLastResult;
43     private String JavaDoc fRequiredSourceLevel;
44     private final Set JavaDoc fHandledMarkerTypes;
45
46     private static final String JavaDoc ID= "id"; //$NON-NLS-1$
47
private static final String JavaDoc CLASS= "class"; //$NON-NLS-1$
48

49     private static final String JavaDoc REQUIRED_SOURCE_LEVEL= "requiredSourceLevel"; //$NON-NLS-1$
50

51     private static final String JavaDoc HANDLED_MARKER_TYPES= "handledMarkerTypes"; //$NON-NLS-1$
52
private static final String JavaDoc MARKER_TYPE= "markerType"; //$NON-NLS-1$
53

54     public ContributedProcessorDescriptor(IConfigurationElement element, boolean testMarkerTypes) {
55         fConfigurationElement= element;
56         fProcessorInstance= null;
57         fStatus= null; // undefined
58
if (fConfigurationElement.getChildren(ExpressionTagNames.ENABLEMENT).length == 0) {
59             fStatus= Boolean.TRUE;
60         }
61         fRequiredSourceLevel= element.getAttribute(REQUIRED_SOURCE_LEVEL);
62         fHandledMarkerTypes= testMarkerTypes ? getHandledMarkerTypes(element) : null;
63     }
64
65     private Set JavaDoc getHandledMarkerTypes(IConfigurationElement element) {
66         HashSet JavaDoc map= new HashSet JavaDoc(7);
67         IConfigurationElement[] children= element.getChildren(HANDLED_MARKER_TYPES);
68         for (int i= 0; i < children.length; i++) {
69             IConfigurationElement[] types= children[i].getChildren(MARKER_TYPE);
70             for (int k= 0; k < types.length; k++) {
71                 String JavaDoc attribute= types[k].getAttribute(ID);
72                 if (attribute != null) {
73                     map.add(attribute);
74                 }
75             }
76         }
77         if (map.isEmpty()) {
78             map.add(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER);
79             map.add(IJavaModelMarker.BUILDPATH_PROBLEM_MARKER);
80             map.add(IJavaModelMarker.TASK_MARKER);
81         }
82         return map;
83     }
84
85     public IStatus checkSyntax() {
86         IConfigurationElement[] children= fConfigurationElement.getChildren(ExpressionTagNames.ENABLEMENT);
87         if (children.length > 1) {
88             String JavaDoc id= fConfigurationElement.getAttribute(ID);
89             return new StatusInfo(IStatus.ERROR, "Only one < enablement > element allowed. Disabling " + id); //$NON-NLS-1$
90
}
91         return new StatusInfo(IStatus.OK, "Syntactically correct quick assist/fix processor"); //$NON-NLS-1$
92
}
93
94     private boolean matches(ICompilationUnit cunit) {
95         if (fRequiredSourceLevel != null) {
96             String JavaDoc current= cunit.getJavaProject().getOption(JavaCore.COMPILER_SOURCE, true);
97             if (JavaModelUtil.isVersionLessThan(current, fRequiredSourceLevel)) {
98                 return false;
99             }
100         }
101         
102         if (fStatus != null) {
103             return fStatus.booleanValue();
104         }
105
106         IConfigurationElement[] children= fConfigurationElement.getChildren(ExpressionTagNames.ENABLEMENT);
107         if (children.length == 1) {
108             try {
109                 ExpressionConverter parser= ExpressionConverter.getDefault();
110                 Expression expression= parser.perform(children[0]);
111                 EvaluationContext evalContext= new EvaluationContext(null, cunit);
112                 evalContext.addVariable("compilationUnit", cunit); //$NON-NLS-1$
113
IJavaProject javaProject= cunit.getJavaProject();
114                 String JavaDoc[] natures= javaProject.getProject().getDescription().getNatureIds();
115                 evalContext.addVariable("projectNatures", Arrays.asList(natures)); //$NON-NLS-1$
116
evalContext.addVariable("sourceLevel", javaProject.getOption(JavaCore.COMPILER_SOURCE, true)); //$NON-NLS-1$
117
fLastResult= !(expression.evaluate(evalContext) != EvaluationResult.TRUE);
118                 return fLastResult;
119             } catch (CoreException e) {
120                 JavaPlugin.log(e);
121             }
122         }
123         fStatus= Boolean.FALSE;
124         return false;
125     }
126     
127     public Object JavaDoc getProcessor(ICompilationUnit cunit) throws CoreException {
128         if (matches(cunit)) {
129             if (fProcessorInstance == null) {
130                 fProcessorInstance= fConfigurationElement.createExecutableExtension(CLASS);
131             }
132             return fProcessorInstance;
133         }
134         return null;
135     }
136     
137     public boolean canHandleMarkerType(String JavaDoc markerType) {
138         return fHandledMarkerTypes == null || fHandledMarkerTypes.contains(markerType);
139     }
140     
141 }
142
Popular Tags