1 11 package org.eclipse.ui.internal.console; 12 13 import java.lang.reflect.Field ; 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 fPattern; 37 private String fQualifier; 38 private int fFlags = -1; 39 40 public PatternMatchListenerExtension(IConfigurationElement extension) { 41 fConfig = extension; 42 } 43 44 49 public int parseFlags(String flagsElement) { 50 int val = 0; 51 if (flagsElement == null) { 52 return val; 53 } 54 55 try { 56 flagsElement = flagsElement.replaceAll("Pattern.", ""); String [] tokens = flagsElement.split("\\s\\|\\s"); Class clazz = Class.forName("java.util.regex.Pattern"); 60 for (int i = 0; i < tokens.length; i++) { 61 Field field = clazz.getDeclaredField(tokens[i]); 62 val |= field.getInt(null); 63 } 64 } catch (ClassNotFoundException e) { 65 ConsolePlugin.log(e); 66 } catch (NoSuchFieldException e) { 67 ConsolePlugin.log(e); 68 } catch (IllegalAccessException 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"); } 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 message = MessageFormat.format("{0} " +getLocalId() + " {1} " + getPluginId() + " {2}", new String [] {ConsoleMessages.PatternMatchListenerExtension_3,ConsoleMessages.PatternMatchListenerExtension_4,ConsoleMessages.PatternMatchListenerExtension_5}); 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 103 public String getPattern() { 104 if (fPattern == null) { 105 fPattern = fConfig.getAttribute("regex"); 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 118 public int getCompilerFlags() { 119 if(fFlags < 0) { 120 String flagsAttribute = fConfig.getAttribute("flags"); fFlags = parseFlags(flagsAttribute); 122 } 123 return fFlags; 124 } 125 126 129 public String getLocalId() { 130 return fConfig.getAttribute("id"); } 132 133 136 public String getPluginId() { 137 return fConfig.getContributor().getName(); 138 } 139 140 public String getQuickPattern() { 141 if (fQualifier == null) { 142 fQualifier = fConfig.getAttribute("qualifier"); 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 |