22-Dec-07 (Created: 22-Dec-07) | More in 'Howto'

How to take advantage of header substitutions (using function expressions)

What you need

1. You need AITransform7
2. You need build 18.6

You need the following entries in the config file


request.aspire.expressionevaluator.classname=com.ai.htmlgen.DefaultExpressionEvaluator1

What can you do on the page with this


(some html segment)
{{substitute(my_header)}}
(The rest of the html segment)

Where "my_header" is a key name in the properties file. Ex:


my_header=some_html {somekey} some more html {anotherkey}

Where "somekey" is substituted with a value from the input page data. The input pagedata contains values from all of the following:

1. URL arguments
2. Any maindata selects
3. session values for that user
4. Global other config values

You can also do


(some html segment)
{{substituteKey(my_header_key)}}
(The rest of the html segment)

Here the "my_header_key" is a double key. First the key is looked up in the page data. The value of it is considered a key and looked up again.

Available functions with this default class

1. substitute
2. substituteKey
3. substituteFile (not tested)

Here is what they do

substitute(somekey)

Look for the value of "somekey" in the config file and expand it with the values from the page data. The resulting string is returned to be placed on the input html screen.

substituteKey (key's key)

Same as above. Except, that the input is the key of a key. So there is a double look up. This allows for dynamically choosing your headers.

substituteFile (soemfilename)

The input is considered a filename. The contents of the filename are expanded the same way. In both cases we are not using loops to expand. Only the main data values are used.

How can I write my own function in java

1. Implement IExpressionEvaluator
2. Use com.ai.htmlgen.DefaultExpressionEvaluator1 as an example
3. Assuming your class implements two functions "myfunc1" and "myfunc2"
create property file entries like the following

request.Aspire.ExpressionEvaluatorDelegate.myfunc1.classname=(mypackage.myclassname)
request.Aspire.ExpressionEvaluatorDelegate.myfunc2.classname=(mypackage.myclassname)

Here is an uncompiled sample code for com.ai.htmlgen.DefaultExpressionEvaluator1


package com.ai.htmlgen;
import com.ai.common.*;
import com.ai.application.interfaces.*;
import com.ai.application.utils.*;
import java.io.*;
import java.util.*;

public class DefaultExpressionEvaluator1 implements IExpressionEvaluator,
ICreator
{
   public Object executeRequest(String requestName, Object args)
      throws RequestExecutionException
   {
      return this;
   }

  public String evaluate(String expression, IDictionary args)
  {
     Vector v = com.ai.common.Tokenizer.tokenize(expression,"(,)");
     String functionName = (String)v.get(0);
     if (functionName.equals("substitute"))
     {
        return substituteForKey((String)v.get(1),args);
     }
     else if (functionName.equals("substituteFile"))
     {
        return substituteRelativeFile((String)v.get(1),args);
     }
     else
     {
      return "";
     }
  }

  public void evaluate(String expression, IDictionary args, PrintWriter
out)
    throws IOException, TransformException
  {
     AppObjects.log("Trace:Processing expression:" + expression);
     Vector v =
com.ai.common.Tokenizer.tokenize(expression.toLowerCase(),"(,)");
     String functionName = (String)v.get(0);
     if (functionName.equals("substitute"))
     {
        out.print(substituteForKey((String)v.get(1),args));
     }
     else if (functionName.equals("substitutefile"))
     {
        out.print(substituteRelativeFile((String)v.get(1),args));
     }
     else if (functionName.equals("substitutekey"))
     {
        out.print(substituteUsingAKey((String)v.get(1),args));
     }
     else
     {
            AppObjects.log("Info:Function " + functionName + " not found.
Using a delegate instead");
            this.delegateFunction(functionName,expression,args,out);
     }
  }

  //substituteUsingAKey
  private String substituteUsingAKey(String key, IDictionary args)
  {
     String realKey = (String)args.get(key);
     if (realKey == null)
     {
       AppObjects.log("Error:Could not find key for key:" + key);
       return "";
     }
      return substituteForKey(realKey,args);
  }

  //substitute(key)
  private String substituteForKey(String key, IDictionary args)
  {
     String value = (String)args.get(key);
     if (value == null)
     {
       AppObjects.log("Error:Could not find value for key:" + key);
       return "";
     }
     return SubstitutorUtils.generalSubstitute(value,args);
  }

  //substitute(relative_filename)
  private String substituteRelativeFile(String filename, IDictionary args)
  {
    try
    {
      String s = FileUtils.readFile(FileUtils.translateFileName(filename));
       return SubstitutorUtils.generalSubstitute(s,args);
    }
    catch(java.io.IOException x)
    {
      AppObjects.log("Error: Could not read file for substitution",x);
      return "";
    }
  }//eof-function

  /**
  * Call an external functor
  */
  private void delegateFunction(final String functionName
    ,final String caseSensitiveExpression
    ,IDictionary args
    ,PrintWriter out) throws TransformException, IOException
  {
   try
   {
    IExpressionEvaluator delegate =
    (IExpressionEvaluator)

AppObjects.getIFactory().getObject("Aspire.ExpressionEvaluatorDelegate." +
functionName,null);
    delegate.evaluate(caseSensitiveExpression,args,out);
   }
   catch(RequestExecutionException x)
   {
      throw new TransformException("Error:Invoking expression
evaluator",x);
   }
  }//eof-function


}//eof-class