Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Sapphire » Substring with expression language
Substring with expression language [message #1061871] Tue, 04 June 2013 12:56 Go to next message
kon f is currently offline kon fFriend
Messages: 112
Registered: March 2012
Senior Member
Hey,

I'm trying to get a substring. I couldn't find anything in the documentation nor in the examples. For instance, how do I get the first 20 characters of Name ?

<label>${Name}</label>


Thank you!

Kon
Re: Substring with expression language [message #1061916 is a reply to message #1061871] Tue, 04 June 2013 17:07 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin KomissarchikFriend
Messages: 952
Registered: July 2009
Senior Member
There isn't a function for that yet. Please open an enhancement request.

https://bugs.eclipse.org/bugs/enter_bug.cgi?product=Sapphire

In the meantime, you can contribute a custom function on your own and then use it in EL. Create sapphire-extension.xml file in your plugin's META-INF folder and go from there. The extension editor that's in Sapphire SDK will help you define a function. There is an example of a function in the samples and many examples in the modeling plugin.

- Konstantin
Re: Substring with expression language [message #1062020 is a reply to message #1061916] Wed, 05 June 2013 12:39 Go to previous messageGo to next message
kon f is currently offline kon fFriend
Messages: 112
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

Re: Substring with expression language [message #1062066 is a reply to message #1062020] Wed, 05 June 2013 16:12 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin KomissarchikFriend
Messages: 952
Registered: July 2009
Senior Member
Thanks. Note that in Sapphire EL, a cast of a null to a String results in "" and a cast of a null to an integer results in 0.
Re: Substring with expression language [message #1062074 is a reply to message #1062066] Wed, 05 June 2013 17:10 Go to previous message
Konstantin Komissarchik is currently offline Konstantin KomissarchikFriend
Messages: 952
Registered: July 2009
Senior Member
Per the enhancement request, I have implemented Fragment, Head and Tail functions in 0.7 release code line. I chose more generic naming in order to facilitate possible future use with collections.

Please verify.
Previous Topic: Element ordering not according to XSD (with several files)
Next Topic:Index for list properties in sdef files
Goto Forum:
  


Current Time: Thu Nov 27 17:45:59 GMT 2014

Powered by FUDForum. Page generated in 0.08801 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software