org.moremotion.evaluator
Interface MScriptFunction

All Known Implementing Classes:
MyFunc

public interface MScriptFunction

Defines an Interface for custom MScript Functions.

Custom MScript functions can be developed my implementing this interface.

When MScriptResolver encounters a function, i.e. @repeat() which is not built in, it first checks whether a function definition exist in the configuration files. For Example:

  <mscriptFunction name="repeat">
    <class>mypack.RepeaterMScriptFunction</class>
    <repeat-seperator>,</repeat-seperator>
  </mscriptFunction>
 
If it does, it loads the defined the class mypack.RepeaterMScriptFunction. If it does not then it assumes that the function name is a class name and tries to load it. It there is a success the resolver calls the execute(Expression[] params, MScriptFunctionContext msfc) method of the loaded class.

An Example Implementation

 package mypack;

 import org.moremotion.evaluator.*;
 import org.moremotion.servlet.*;
   
 public class RepeaterMScriptFunction implements MScriptFunction {
    
   public Object execute(Expression[] params, MScriptFunctionContext msfc) throws MScriptFunctionException {
     if (params.length < 2) throw new Exception("Less parameters than expected");
     MMSymbolResolver sr = msfc.getSymbolResolver();
     String seperator = msfc.getMScriptFunctionConfig().getParameter("repeat-seperator").stringValue(null);
     String s = params[0].evaluate(sr).toString(); 
     int times = ((Double)params[1].evaluate(sr)).intValue();
     StringBuffer result = new StringBuffer();
     for (int i = 0; i < times; i++) {
       if (result.length() > 0) result.append(seperator);
       result.append(s);
     } 
     return result.toString();
   } 
 }

 

Version:
$Id: MScriptFunction.java 51 2008-03-10 11:46:54Z erkan $

Method Summary
 java.lang.Object execute(Expression[] params, MScriptFunctionContext msfc)
          Executes the algorithm of the custom MScript function and returns the result.
 

Method Detail

execute

java.lang.Object execute(Expression[] params,
                         MScriptFunctionContext msfc)
                         throws MScriptFunctionException
Executes the algorithm of the custom MScript function and returns the result.

Parameters:
params - The function parameters as Expression object array.
msfc - MScriptFunctionContext object
Returns:
The result object. The expected objects are String, Double and Boolean.
Throws:
MScriptFunctionException - when something goes wrong with the execution of the function.


Copyright © 2002-2008 MOR YAZILIM. All Rights Reserved.