You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:56:46 UTC

[sling-org-apache-sling-pipes] 02/31: SLING-5361 Plain string expressions shouldn't be evaluated

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.pipes-0.0.10
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-pipes.git

commit 71f5ff4fd15e5c484bc541d07ae133ee220fd4ab
Author: Oliver Lietz <ol...@apache.org>
AuthorDate: Sat Jan 2 19:44:20 2016 +0000

    SLING-5361 Plain string expressions shouldn't be evaluated
    
    patch from Nicolas Peltier
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/sling-pipes@1722655 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  2 +-
 .../java/org/apache/sling/pipes/PipeBindings.java  | 48 +++++++++++++---------
 .../org/apache/sling/pipes/PipeBindingsTest.java   | 13 +++++-
 3 files changed, 41 insertions(+), 22 deletions(-)

diff --git a/pom.xml b/pom.xml
index c9c5648..558a57c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@
     <modelVersion>4.0.0</modelVersion>
     <artifactId>org.apache.sling.pipes</artifactId>
     <packaging>bundle</packaging>
-    <version>0.0.8-SNAPSHOT</version>
+    <version>0.0.9-SNAPSHOT</version>
 
     <name>Apache Sling Pipes</name>
     <description>bulk content changes tool</description>
diff --git a/src/main/java/org/apache/sling/pipes/PipeBindings.java b/src/main/java/org/apache/sling/pipes/PipeBindings.java
index 570ca0f..6cb4ac2 100644
--- a/src/main/java/org/apache/sling/pipes/PipeBindings.java
+++ b/src/main/java/org/apache/sling/pipes/PipeBindings.java
@@ -161,29 +161,32 @@ public class PipeBindings {
      * @return
      */
     protected String computeECMA5Expression(String expr){
-        StringBuilder expression = new StringBuilder();
         Matcher matcher = INJECTED_SCRIPT.matcher(expr);
-        int start = 0;
-        while (matcher.find()){
-            if (matcher.start() > start){
-                if (expression.length() == 0){
-                    expression.append("'");
+        if (INJECTED_SCRIPT.matcher(expr).find()) {
+            StringBuilder expression = new StringBuilder();
+            int start = 0;
+            while (matcher.find()) {
+                if (matcher.start() > start) {
+                    if (expression.length() == 0) {
+                        expression.append("'");
+                    }
+                    expression.append(expr.substring(start, matcher.start()));
+                }
+                if (expression.length() > 0) {
+                    expression.append("' + ");
+                }
+                expression.append(matcher.group(1));
+                start = matcher.end();
+                if (start < expr.length()) {
+                    expression.append(" + '");
                 }
-                expression.append(expr.substring(start,matcher.start()));
-            }
-            if (expression.length() > 0){
-                expression.append("' + ");
             }
-            expression.append(matcher.group(1));
-            start = matcher.end();
-            if (start < expr.length()){
-                expression.append(" + '");
+            if (start < expr.length()) {
+                expression.append(expr.substring(start) + "'");
             }
+            return expression.toString();
         }
-        if (start < expr.length()){
-             expression.append(expr.substring(start) + "'");
-        }
-        return expression.toString();
+        return null;
     }
 
     /**
@@ -192,8 +195,13 @@ public class PipeBindings {
      * @return
      * @throws ScriptException
      */
-    private Object evaluate(String expr) throws ScriptException {
-        return engine.eval(computeECMA5Expression(expr), scriptContext);
+    protected Object evaluate(String expr) throws ScriptException {
+        String computed = computeECMA5Expression(expr);
+        if (computed != null){
+            //computed is null in case expr is a simple string
+            return engine.eval(computed, scriptContext);
+        }
+        return expr;
     }
 
     /**
diff --git a/src/test/java/org/apache/sling/pipes/PipeBindingsTest.java b/src/test/java/org/apache/sling/pipes/PipeBindingsTest.java
index 3ae094d..5403e99 100644
--- a/src/test/java/org/apache/sling/pipes/PipeBindingsTest.java
+++ b/src/test/java/org/apache/sling/pipes/PipeBindingsTest.java
@@ -26,6 +26,8 @@ import java.util.Map;
 
 import static org.junit.Assert.*;
 
+import javax.script.ScriptException;
+
 /**
  * testing binding's expressions instanciations
  */
@@ -40,6 +42,15 @@ public class PipeBindingsTest extends AbstractPipeTest {
     }
 
     @Test
+    public void testEvaluateSimpleString() throws ScriptException {
+        Resource resource = context.resourceResolver().getResource(PATH_PIPE + "/" + ContainerPipeTest.NN_DUMMYTREE);
+        PipeBindings bindings = new PipeBindings(resource);
+        String simple = "simple string";
+        String evaluated = (String)bindings.evaluate(simple);
+        assertEquals("evaluated should be the same than input", evaluated, simple);
+    }
+
+    @Test
     public void computeEcma5Expression() {
         Resource resource = context.resourceResolver().getResource(PATH_PIPE + "/" + ContainerPipeTest.NN_DUMMYTREE);
         PipeBindings bindings = new PipeBindings(resource);
@@ -100,4 +111,4 @@ public class PipeBindingsTest extends AbstractPipeTest {
         Number expression = (Number)bindings.instantiateObject("${testSumFunction(1,2)}");
         assertEquals("computed expression have testSum script's functionavailable", 3, expression.intValue());
     }
-}
\ No newline at end of file
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.