|
|||||
|
|||||
Macros
Macro support in MVEL is a basic facility to allow the replacement of a token with an expanded source output. The functionality was introduced as a way of creating specialized keywords that enclose an Interceptor. A macro can consist of of any legal identifier for example: modify Consider the following code: modify (obj) { value = 'foo' };
This is not a legal MVEL expression. There is, after all, no modify keyword in MVEL. However, in JBoss Drools this functionality is used to implement a form of change listener by replacing the modify identifier with the string: @Modify with. The org.mvel.Macro Interfacepublic interface Macro { public String doMacro(); } The interface is very simple. Upon execution, the doMacro() method returns a String to replace the representative token. For example: Macro modifyMacro = new Macro() { public String doMacro() { return "@Modify with"; } } Using the MacroProcessorThe MacroProcessor is a lightweight and fast text parser that uses the provided macros to replace all the matching identifiers. The defined macros are passed via a Map with the key representing the identifier to match, and the value being an instance of the Macro. For example: Map<String, Macro> myMacros = new HashMap<String, Macro>(); // Add modifyMacro to the Map myMacros.put("modify", modifyMacro); // Create the macro processor MacroProcessor macroProcessor = new MacroProcessor(); // Add the macro map to the macro processor macroProcessor.setMacros(myMacros); // Now we pre-parse our expression String parsedExpression = macroProcessor.parse(expression); The returned String can be simply passed to the MVEL compiler.
|
|||||
|
Copyright 2003-2006 - The Codehaus. All rights reserved unless otherwise noted.
Powered by Atlassian Confluence
|
|||||