5555 * methods.
5656 *
5757 * We therefore decided to copy the code of the inner class here (see first commit) and then made
58- * some adjustments (the following commits).
58+ * some adjustments (the following commits, see "adjustment" comments ).
5959 */
6060public class StreamingTemplateEngine extends groovy .text .StreamingTemplateEngine {
6161 @ Override
@@ -86,7 +86,11 @@ private static class StreamingTemplate implements Template {
8686 + "return { _p, _s, _b, out -> "
8787 + "int _i = 0;"
8888 + "try {"
89- + "delegate = new Binding(_b);" ;
89+ // adjustment: using the GroovyBuiltInFunctions binding implementation
90+ // provided from the outside (via the 'fn' map entry) allows the script
91+ // to directly call Membrane functions. This enables templating syntax like
92+ // ${xpath('/root/element')} .
93+ + "delegate = _b.get('fn');" ;
9094
9195 /**
9296 * The 'footer' we use for the resulting groovy script source
@@ -514,7 +518,7 @@ private int parseDollarIdentifier(int c,
514518 final StringBuilder target ,
515519 final Position sourcePosition ,
516520 final Position targetPosition ) throws IOException , FinishedReadingException {
517- append (target , targetPosition , "out<<" );
521+ append (target , targetPosition , "out<<escape( " ); // adjustment: escape the output
518522 append (target , targetPosition , (char ) c );
519523
520524 while (true ) {
@@ -525,7 +529,7 @@ private int parseDollarIdentifier(int c,
525529 append (target , targetPosition , (char ) c );
526530 }
527531
528- append (target , targetPosition , ";" );
532+ append (target , targetPosition , ") ;" );
529533
530534 return c ;
531535 }
@@ -562,10 +566,13 @@ private void parseDollarCurlyIdentifier(final Reader reader,
562566 final StringBuilder target ,
563567 final Position sourcePosition ,
564568 final Position targetPosition ) throws IOException , FinishedReadingException {
565- append (target , targetPosition , "out<<\" \" \" ${" );
569+ append (target , targetPosition , "out<<\" \" \" ${escape( " ); // adjustment: escape the output
566570
567571 while (true ) {
568572 int c = read (reader , sourcePosition );
573+ // adjustment: escape the output
574+ if (c == '}' )
575+ append (target , targetPosition , ')' );
569576 append (target , targetPosition , (char ) c );
570577 if (c == '}' ) break ;
571578 }
@@ -612,11 +619,11 @@ private void parseExpression(final Reader reader,
612619 final StringBuilder target ,
613620 final Position sourcePosition ,
614621 final Position targetPosition ) throws IOException , FinishedReadingException {
615- append (target , targetPosition , "out<<\" \" \" ${" );
622+ append (target , targetPosition , "out<<\" \" \" ${escape( " ); // adjustment: escape the output
616623
617624 readAndAppend (reader , target , sourcePosition , targetPosition );
618625
619- append (target , targetPosition , "}\" \" \" ;" );
626+ append (target , targetPosition , ") }\" \" \" ;" );
620627 }
621628
622629 @ Override
0 commit comments