001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.console;
011:
012: import java.lang.reflect.Field;
013:
014: import org.eclipse.core.expressions.EvaluationContext;
015: import org.eclipse.core.expressions.EvaluationResult;
016: import org.eclipse.core.expressions.Expression;
017: import org.eclipse.core.expressions.ExpressionConverter;
018: import org.eclipse.core.expressions.ExpressionTagNames;
019: import org.eclipse.core.runtime.CoreException;
020: import org.eclipse.core.runtime.IConfigurationElement;
021: import org.eclipse.core.runtime.IStatus;
022: import org.eclipse.core.runtime.Status;
023: import org.eclipse.core.variables.VariablesPlugin;
024: import org.eclipse.ui.IPluginContribution;
025: import org.eclipse.ui.console.ConsolePlugin;
026: import org.eclipse.ui.console.IConsole;
027: import org.eclipse.ui.console.IPatternMatchListenerDelegate;
028:
029: import com.ibm.icu.text.MessageFormat;
030:
031: public class PatternMatchListenerExtension implements
032: IPluginContribution {
033:
034: private IConfigurationElement fConfig;
035: private Expression fEnablementExpression;
036: private String fPattern;
037: private String fQualifier;
038: private int fFlags = -1;
039:
040: public PatternMatchListenerExtension(IConfigurationElement extension) {
041: fConfig = extension;
042: }
043:
044: /*
045: * returns the integer value of the flags defined in java.util.regex.Pattern.
046: * Both <code>Pattern.MULTILINE</code> and <code>MULTILINE</code> will return
047: * the same value.
048: */
049: public int parseFlags(String flagsElement) {
050: int val = 0;
051: if (flagsElement == null) {
052: return val;
053: }
054:
055: try {
056: flagsElement = flagsElement.replaceAll("Pattern.", ""); //$NON-NLS-1$ //$NON-NLS-2$
057: String[] tokens = flagsElement.split("\\s\\|\\s"); //$NON-NLS-1$
058: Class clazz = Class.forName("java.util.regex.Pattern"); //$NON-NLS-1$
059:
060: for (int i = 0; i < tokens.length; i++) {
061: Field field = clazz.getDeclaredField(tokens[i]);
062: val |= field.getInt(null);
063: }
064: } catch (ClassNotFoundException e) {
065: ConsolePlugin.log(e);
066: } catch (NoSuchFieldException e) {
067: ConsolePlugin.log(e);
068: } catch (IllegalAccessException e) {
069: ConsolePlugin.log(e);
070: }
071: return val;
072: }
073:
074: public boolean isEnabledFor(IConsole console) throws CoreException {
075: EvaluationContext context = new EvaluationContext(null, console);
076: EvaluationResult evaluationResult = getEnablementExpression()
077: .evaluate(context);
078: return evaluationResult == EvaluationResult.TRUE;
079: }
080:
081: public IPatternMatchListenerDelegate createDelegate()
082: throws CoreException {
083: return (IPatternMatchListenerDelegate) fConfig
084: .createExecutableExtension("class"); //$NON-NLS-1$
085: }
086:
087: public Expression getEnablementExpression() throws CoreException {
088: if (fEnablementExpression == null) {
089: IConfigurationElement[] elements = fConfig
090: .getChildren(ExpressionTagNames.ENABLEMENT);
091: if (elements.length == 0) {
092: String message = MessageFormat
093: .format(
094: "{0} " + getLocalId() + " {1} " + getPluginId() + " {2}", new String[] { ConsoleMessages.PatternMatchListenerExtension_3, ConsoleMessages.PatternMatchListenerExtension_4, ConsoleMessages.PatternMatchListenerExtension_5 }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
095: ConsolePlugin.log(new Status(IStatus.WARNING,
096: ConsolePlugin.getUniqueIdentifier(),
097: IStatus.OK, message, null));
098: }
099: IConfigurationElement enablement = elements.length > 0 ? elements[0]
100: : null;
101:
102: if (enablement != null) {
103: fEnablementExpression = ExpressionConverter
104: .getDefault().perform(enablement);
105: }
106: }
107: return fEnablementExpression;
108: }
109:
110: /*
111: * returns the regular expression to be matched.
112: */
113: public String getPattern() {
114: if (fPattern == null) {
115: fPattern = fConfig.getAttribute("regex"); //$NON-NLS-1$
116: try {
117: fPattern = VariablesPlugin.getDefault()
118: .getStringVariableManager()
119: .performStringSubstitution(fPattern, false);
120: } catch (CoreException e) {
121: ConsolePlugin.log(e);
122: }
123: }
124: return fPattern;
125: }
126:
127: /*
128: * returns the flags to be used by <code>Pattern.compile(pattern, flags)</code>
129: */
130: public int getCompilerFlags() {
131: if (fFlags < 0) {
132: String flagsAttribute = fConfig.getAttribute("flags"); //$NON-NLS-1$
133: fFlags = parseFlags(flagsAttribute);
134: }
135: return fFlags;
136: }
137:
138: /* (non-Javadoc)
139: * @see org.eclipse.ui.IPluginContribution#getLocalId()
140: */
141: public String getLocalId() {
142: return fConfig.getAttribute("id"); //$NON-NLS-1$
143: }
144:
145: /* (non-Javadoc)
146: * @see org.eclipse.ui.IPluginContribution#getPluginId()
147: */
148: public String getPluginId() {
149: return fConfig.getContributor().getName();
150: }
151:
152: public String getQuickPattern() {
153: if (fQualifier == null) {
154: fQualifier = fConfig.getAttribute("qualifier"); //$NON-NLS-1$
155: try {
156: if (fQualifier != null) {
157: fQualifier = VariablesPlugin.getDefault()
158: .getStringVariableManager()
159: .performStringSubstitution(fQualifier,
160: false);
161: }
162: } catch (CoreException e) {
163: ConsolePlugin.log(e);
164: }
165: }
166: return fQualifier;
167: }
168: }
|