commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Henri Biestro (JIRA)" <j...@apache.org>
Subject [jira] [Comment Edited] (JEXL-177) Unified expressions to be used in String literals in Jexl scripts
Date Sun, 23 Aug 2015 07:38:45 GMT

    [ https://issues.apache.org/jira/browse/JEXL-177?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14708317#comment-14708317
] 

Henri Biestro edited comment on JEXL-177 at 8/23/15 7:37 AM:
-------------------------------------------------------------

Yes, this is what I meant. A crude version could be:
{code}
public static class fn177 {
        private final JexlContext context;
        public fn177(JexlContext context) {
            this.context = context;
        }
        public String print(String src) {
            JxltEngine jxlt = (JxltEngine) context.get("$jxlt");
            if (jxlt != null) {
                JxltEngine.Template template = jxlt.createTemplate(src);
                if (template != null) {
                    StringWriter strw = new StringWriter();
                    template.evaluate(context, strw);
                    return strw.toString();
                }
            }
            return "";
        }
}

 @Test
 public void test177() throws Exception {
        Map<String, Object> funcs = new HashMap<String, Object>();
        funcs.put("fn177", fn177.class);
        JexlEngine jexl = new JexlBuilder().namespaces(funcs).create();
        JxltEngine jxlt = jexl.createJxltEngine();
        MapContext context = new MapContext();
        context.set("$jxlt", jxlt);
        String expr =  "user = 'Dimitri'; fn177:print('Hello ${user}')";
        Object value = jexl.createScript(expr).execute(context);
        Assert.assertEquals(expr, "Hello Dimitri", value);
 }
{code}


was (Author: henrib):
Yes, this is what I meant. A crude version could be:
{code}
public static class fn177 {
        private final JexlContext context;
        public fn177(JexlContext context) {
            this.context = context;
        }
        public String print(String src) {
            JxltEngine jxlt = (JxltEngine) context.get("$jxlt");
            if (jxlt != null) {
                JxltEngine.Template template = jxlt.createTemplate(src);
                if (template != null) {
                    StringWriter strw = new StringWriter();
                    template.evaluate(context, strw);
                    return strw.toString();
                }
            }
            return "";
        }
    }

    @Test
    public void test177() throws Exception {
        Map<String, Object> funcs = new HashMap<String, Object>();
        funcs.put("fn177", fn177.class);
        JexlEngine jexl = new JexlBuilder().namespaces(funcs).create();
        JxltEngine jxlt = jexl.createJxltEngine();
        MapContext context = new MapContext();
        context.set("$jxlt", jxlt);
        String expr =  "user = 'Dimitri'; fn177:print('Hello ${user}')";
        Object value = jexl.createScript(expr).execute(context);
        Assert.assertEquals(expr, "Hello Dimitri", value);
    }
{code}

> Unified expressions to be used in String literals in Jexl scripts
> -----------------------------------------------------------------
>
>                 Key: JEXL-177
>                 URL: https://issues.apache.org/jira/browse/JEXL-177
>             Project: Commons JEXL
>          Issue Type: New Feature
>    Affects Versions: 3.0
>            Reporter: Dmitri Blinov
>            Priority: Minor
>
> Introduce new string literals to jexl script that allow unified expressions to be used,
string literals could use for example triple quoted (single or double) syntax for delimiters,
may include newlines and should not escape single quotes
> {code}
> user = "Bob"; server = "Test"; s = """Hello ${user}, welcome to ${server}"""; return
s;
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message