KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > console > PatternMatchListenerExtension


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.ui.internal.console;
12
13 import java.lang.reflect.Field JavaDoc;
14
15 import org.eclipse.core.expressions.EvaluationContext;
16 import org.eclipse.core.expressions.EvaluationResult;
17 import org.eclipse.core.expressions.Expression;
18 import org.eclipse.core.expressions.ExpressionConverter;
19 import org.eclipse.core.expressions.ExpressionTagNames;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IConfigurationElement;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.core.variables.VariablesPlugin;
25 import org.eclipse.ui.IPluginContribution;
26 import org.eclipse.ui.console.ConsolePlugin;
27 import org.eclipse.ui.console.IConsole;
28 import org.eclipse.ui.console.IPatternMatchListenerDelegate;
29
30 import com.ibm.icu.text.MessageFormat;
31
32 public class PatternMatchListenerExtension implements IPluginContribution {
33
34     private IConfigurationElement fConfig;
35     private Expression fEnablementExpression;
36     private String JavaDoc fPattern;
37     private String JavaDoc fQualifier;
38     private int fFlags = -1;
39    
40     public PatternMatchListenerExtension(IConfigurationElement extension) {
41         fConfig = extension;
42     }
43
44     /*
45      * returns the integer value of the flags defined in java.util.regex.Pattern.
46      * Both <code>Pattern.MULTILINE</code> and <code>MULTILINE</code> will return
47      * the same value.
48      */

49     public int parseFlags(String JavaDoc flagsElement) {
50         int val = 0;
51         if (flagsElement == null) {
52             return val;
53         }
54             
55         try {
56             flagsElement = flagsElement.replaceAll("Pattern.", ""); //$NON-NLS-1$ //$NON-NLS-2$
57
String JavaDoc[] tokens = flagsElement.split("\\s\\|\\s"); //$NON-NLS-1$
58
Class JavaDoc clazz = Class.forName("java.util.regex.Pattern"); //$NON-NLS-1$
59

60             for (int i = 0; i < tokens.length; i++) {
61                 Field JavaDoc field = clazz.getDeclaredField(tokens[i]);
62                 val |= field.getInt(null);
63             }
64         } catch (ClassNotFoundException JavaDoc e) {
65             ConsolePlugin.log(e);
66         } catch (NoSuchFieldException JavaDoc e) {
67             ConsolePlugin.log(e);
68         } catch (IllegalAccessException JavaDoc e) {
69             ConsolePlugin.log(e);
70         }
71         return val;
72     }
73     
74     public boolean isEnabledFor(IConsole console) throws CoreException {
75         EvaluationContext context = new EvaluationContext(null, console);
76         EvaluationResult evaluationResult = getEnablementExpression().evaluate(context);
77         return evaluationResult == EvaluationResult.TRUE;
78     }
79     
80     public IPatternMatchListenerDelegate createDelegate() throws CoreException {
81         return (IPatternMatchListenerDelegate) fConfig.createExecutableExtension("class"); //$NON-NLS-1$
82
}
83     
84     public Expression getEnablementExpression() throws CoreException {
85         if (fEnablementExpression == null) {
86             IConfigurationElement[] elements = fConfig.getChildren(ExpressionTagNames.ENABLEMENT);
87             if (elements.length == 0) {
88                 String JavaDoc message = MessageFormat.format("{0} " +getLocalId() + " {1} " + getPluginId() + " {2}", new String JavaDoc[] {ConsoleMessages.PatternMatchListenerExtension_3,ConsoleMessages.PatternMatchListenerExtension_4,ConsoleMessages.PatternMatchListenerExtension_5}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
89
ConsolePlugin.log(new Status(IStatus.WARNING, ConsolePlugin.getUniqueIdentifier(), IStatus.OK, message, null));
90             }
91             IConfigurationElement enablement = elements.length > 0 ? elements[0] : null;
92
93             if (enablement != null) {
94                 fEnablementExpression = ExpressionConverter.getDefault().perform(enablement);
95             }
96         }
97         return fEnablementExpression;
98     }
99     
100     /*
101      * returns the regular expression to be matched.
102      */

103     public String JavaDoc getPattern() {
104         if (fPattern == null) {
105             fPattern = fConfig.getAttribute("regex"); //$NON-NLS-1$
106
try {
107                 fPattern = VariablesPlugin.getDefault().getStringVariableManager().performStringSubstitution(fPattern, false);
108             } catch (CoreException e) {
109                 ConsolePlugin.log(e);
110             }
111         }
112         return fPattern;
113     }
114
115     /*
116      * returns the flags to be used by <code>Pattern.compile(pattern, flags)</code>
117      */

118     public int getCompilerFlags() {
119         if(fFlags < 0) {
120             String JavaDoc flagsAttribute = fConfig.getAttribute("flags"); //$NON-NLS-1$
121
fFlags = parseFlags(flagsAttribute);
122         }
123         return fFlags;
124     }
125     
126     /* (non-Javadoc)
127      * @see org.eclipse.ui.IPluginContribution#getLocalId()
128      */

129     public String JavaDoc getLocalId() {
130         return fConfig.getAttribute("id"); //$NON-NLS-1$
131
}
132
133     /* (non-Javadoc)
134      * @see org.eclipse.ui.IPluginContribution#getPluginId()
135      */

136     public String JavaDoc getPluginId() {
137         return fConfig.getContributor().getName();
138     }
139
140     public String JavaDoc getQuickPattern() {
141         if (fQualifier == null) {
142             fQualifier = fConfig.getAttribute("qualifier"); //$NON-NLS-1$
143
try {
144                 if (fQualifier != null) {
145                     fQualifier = VariablesPlugin.getDefault().getStringVariableManager().performStringSubstitution(fQualifier, false);
146                 }
147             } catch (CoreException e) {
148                 ConsolePlugin.log(e);
149             }
150         }
151         return fQualifier;
152     }
153 }
154
Popular Tags