ExperSHOP enables Java objects inside scripting language


Why ?
1. New Keywords
  1.1. $NewObject
  1.2. $Invoke
2. How does it work ?
3. Multiple pages objects
4. Extra Possibilities
5. Examples


Why ?

ExperSHOP is able to provide scripting tools, with a set of keywords. To extend the current language we need a small set of new keywords to integrate Java objects. We need to integrate Java objects because it's a good way to open the scripting language to an unlimited set of functionnalities.

1. New Keywords

Two new keywords are now available to create and manage Java objects: NewObject and $Invoke, the syntax is the following:

$NewObject

$NewObject is able to call a constructor to create new Java object, you can pass parameter to the constructor method. This keyword returns a object which is stored for the current template.
$NewObject object $classname(param*)$
with:
    object: is the name of the object locally created, the type of the object is 'classname'
    classname: is the name of the class of the object (e.g.: java.lang.String)
    param: is a parameter of the class constructor, if there is no parameter for the current constructor, you only have to open and close brackets. In case, there are multiple parameters, you have to separate parameters with pipe '|'. param is composed by:
(type)paramName
with:
     type: is the type of the parameter (e.g. int or java.lang.String, etc...)
     paramName: is the name of the parameter or its value

$Invoke

$Invoke invoques a method on a specific Java object. It returns something when it is possible.
$Invoke result $object.method(param*)$
with:
    result: is the result of method call
    object: is the name of the object previously created
    method: is the name of the method that must be called
    param: is a parameter of the method, if there is no parameter for the current method, you only have to open and close brackets. In case, there are multiple parameters, you have to separate parameters with pipe '|'. param is composed by:
(type)paramName
with:
     type: is the type of the parameter (e.g. int or java.lang.String, etc...)
     paramName: is the name of the parameter or its value

2. How does it work ?

The $NewObject keyword calls the constructor of the object with its parameters if needed. The user can then use this object in his current template. The object is stored in memory for the template life time, just an exception for the static object which are keept for life. If an error occurs when the object is created, you can use the existing $IfError keyword to manage the problem.
The $Invoke keyword allows you to invoke Java methods on Java objects. A Java method can return a result (primitive type or an other object). You only can invoke Java methods on existing Java objects. If you try to use a method on a object that does not exist or try to use an invalid method, you get an error.

The $Invoke keyword is able to return :

When $Invoke returns a Collection or ResultSet objects, you can use the $LoopOnResults keyword to loop on set.


3. Multiple pages objects

You can create objects which live into multiple tmpl pages. To use this possbility, you need to use a special word on $NewObject or $Invoke keywords.
  $NewObject isPersist test $String((String)$myVar$)$
or
  $Invoke isPersist res $test.length()$
Then, test or res objects could be used in others pages, but in the same client session.
When you want to stop the life of an object, you need to use a special keyword:

$CloseObject

$CloseObject closes a Java object. After this call, you could not use anymore the object.
  $CloseObject object
with:
    object: is the name of the object you want to delete and close

4. Extra Possibilities

o Created objects are only used in the current template. If you need to use objects through multiple templates, you have to define a Singleton pattern on your created object to use static objects. In this case, second time you try to create this object, you work on the object previously created.
o It is possible to use variables which have been defined previously by a $DefVar keyword. To use this possibility, you need to use $myVariable$ with the properly type:
  $DefVar myVar thisIsATest
  $NewObject test $String((String)$myVar$)$
o When a variable or object is not defined, and you try to use it, the String representation of this String is used to build the object.


5. Examples

The following example creates a String object with a simple String, and calls the length() method. In this example we can directly display the length without any other operation:
  $NewObject test $java.lang.String((java.lang.String)"this is a string")$
  $Invoke res $test.length()$
  The length is: $res$
The following example creates a String object with a short name object. Only for String object, you can use this possibility:
  $NewObject test2 $String((String)"this is a string")$
  $Invoke res2 $test2.length()$
  The length is: $res2$
The following example creates a String object with a simple String, without any cast (you can do that only with String object):
  $NewObject test3 $String("tototer")$
  $Invoke res3 $test3.length()$
  The length is: $res3$
The following example creates a String object with a previous created object (test2). Java Objects use do not use $$ syntax. $$ syntax is only used for scripting variables:
  $NewObject test4 $String(test2)$
  $Invoke res4 $test4.length()$
  The length is: $res4$
The following example creates a String object with a previous create object (test3) with a cast:
  $NewObject test5 $String((String)test3)$
  $Invoke res5 $test5.length()$
  The length is: $res5$
The following example creates a TesterCollection object with a single int parameter (3). In this case, we need to cast the value, to be sure to use the properly Java type. This object has 2 methods, a getVector method which returns a Vector object (Collection), and a getArrayList method which returns a ArrayList object (Collection):
  $NewObject tester $TesterCollection((int)3)$
  $Invoke col $tester.getVector()$
  $Invoke col2 $tester.getArrayList()$
Now we can loop on results on col and col2 objects:
$LoopOnResults col cc
  $cc:value$.<br>
$EndLoop

$LoopOnResults col2 cc2
  $cc2:value$.<br>
$EndLoop
The following example uses the previous created object (tester object) to get a ResultSet from a database. A rset object is used with $LoopOnResults keyword to loop on datas.
  $Invoke rset $tester.getresultSet()$
  $LoopOnResults rset rr
    $rr:id$ - $rr:foo$.<br>
  $EndLoop
The following example creates a String object with a previous defined variable (and with a cast (mandatory)):
  $DefVar myVar thisIsATest
  $NewObject atest $String((String)$myVar$)$
  $Invoke resATest $atest.length()$
  The length is: $resATest$



Experlog 1999-2005