|
|
Re: Substring with expression language [message #1062020 is a reply to message #1061916] |
Wed, 05 June 2013 12:39   |
kon f Messages: 152 Registered: March 2012 |
Senior Member |
|
|
Hey Konstantin,
thank you for the info. I created a feature request. I also created the custom function as suggested.
sapphire-extension.xml
<?xml version="1.0" encoding="UTF-8"?>
<extension xmlns="http://www.eclipse.org/sapphire/xmlns/extension" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<function>
<name>x:Substring</name>
<impl>MY.COMPANY.PATH.el.SubstringFunction</impl>
</function>
</extension>
Replace the x in the name of the function with your namespace.
SubstringFunction.java
import org.eclipse.sapphire.modeling.el.Function;
import org.eclipse.sapphire.modeling.el.FunctionContext;
import org.eclipse.sapphire.modeling.el.FunctionResult;
public final class SubstringFunction extends Function {
@Override
public String name() {
return "x:Substring";
}
@Override
public FunctionResult evaluate(final FunctionContext context) {
return new FunctionResult(this, context) {
@Override
protected Object evaluate() {
String value = cast(operand(0).value(), String.class);
Integer startIndex = cast(operand(1).value(), Integer.class);
Integer endIndex = cast(operand(2).value(), Integer.class);
if (value == null || value.isEmpty() || startIndex.equals(endIndex)) {
return "";
}
if (value.length() <= (startIndex + endIndex)) {
return value;
}
return value.substring(startIndex, endIndex);
}
};
}
}
As mentioned above, the x needs to be replaced by your own namespace since "no namespace" is reserved for Sapphire.
<label>${x:Substring(Name,0,20)}</label>
Thank you!
Kon
Edit: I updated the "empty" comparison on the value due to Konstantin's comment.
[Updated on: Fri, 07 June 2013 07:40] Report message to a moderator
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02589 seconds