Expression Language

Expression Language is based on Tomcat Expression Language .

Functions

EL supports defining own functions in expressions. There is a FunctionHelper in the Expressions object. Some Functions are predefined.

Predefined Functions

Functions can have a prefix. The Sapphire Core Functions has the prefix: sf.

Expressions.register("testBoolean", false)
Expressions.getValue("${sf:boolString(testBoolean,'Y', 'N')}").get must be equalTo "N"
function sample info
frameworkName ${sf:frameworkName()}
frameworkVersion ${sf:frameworkVersion()}
dateString ${sf:dateString(testDate)} Default Pattern: YYYY-MM-dd
now ${sf:frameworkName()}
nowAsString ${sf:nowAsString()}").toString Default Pattern: YYYY-MM-dd
boolString ${sf:boolString(testBoolean,‘Y’, ‘N’)}
configString ${sf:configString(‘test.string’)}
i18n ${sf:i18n(‘personText’)}

Custom Functions

"add custom function" in {

  val clazz: Class[_] = Class.forName("com.sfxcode.sapphire.core.el.CustomFunctionMapper")
  Expressions.functionHelper.addFunction("custom", "myCoolMethod", clazz, "coolMethod", classOf[String])
  Expressions.getValue("${custom:myCoolMethod('123')}").get must be equalTo "test-123"

}

Base Usage

Expression Language Processing is defined in the Expressions object.

There is also an expressions trait:

trait Expressions {

  def getExpressionValue(expression: String, clazz: Class[AnyRef] = classOf[Object]): Option[Any] =
    Expressions.getValue(expression, clazz)

  def setExpressionValue(expression: String, value: Any, clazz: Class[AnyRef] = classOf[Object]): Unit =
    Expressions.setValue(expression, value, clazz)

  def register(name: String, obj: Any): Unit = Expressions.register(name: String, obj: Any)

  def unregister(name: String): Unit = Expressions.unregister(name)

  def registerBean(bean: AnyRef): Unit = Expressions.registerBean(bean)

  def unregisterBean(bean: AnyRef): Unit = Expressions.unregisterBean(bean)

  def registeredBean[T <: AnyRef]()(implicit ct: ClassTag[T]): Option[T] =
    Expressions.registeredBean[T]()

  def evaluateExpressionOnObject[T <: Any](source: AnyRef, expression: String): Option[T] =
    Expressions.evaluateExpressionOnObject[T](source, expression)
}

EL in FXBean

val testBean = FXBean[TestBean](TestBean())
testBean.getValue("result ${2*4}") must be equalTo "result 8"
testBean.getValue("${_self.description().get()}") must be equalTo "desc"
testBean.getValue("!{_self.description().get()}") must be equalTo "desc"
testBean.getValue("zip.value") must be equalTo 12345
testBean.getValue("${_self.age() / 2}") must be equalTo 21.0
testBean.getValue("${_self.multiply(2,3)}") must be equalTo 6
testBean.getValue("!{_self.multiply(2,3)}") must be equalTo 6

EL in WindowController / ViewController

WindowController- and ViewController-Beans are automatically registered by name