incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r599378 - in /incubator/sling/trunk/scripting/javascript/src: main/java/org/apache/sling/scripting/javascript/EspReader.java test/java/org/apache/sling/scripting/javascript/EspReaderTest.java
Date Thu, 29 Nov 2007 11:05:38 GMT
Author: bdelacretaz
Date: Thu Nov 29 03:05:31 2007
New Revision: 599378

URL: http://svn.apache.org/viewvc?rev=599378&view=rev
Log:
SLING-114 - EspReader.setOutInitStatement() added

Modified:
    incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/EspReader.java
    incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/EspReaderTest.java

Modified: incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/EspReader.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/EspReader.java?rev=599378&r1=599377&r2=599378&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/EspReader.java
(original)
+++ incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/EspReader.java
Thu Nov 29 03:05:31 2007
@@ -178,6 +178,14 @@
      * @see #startWrite(String)
      */
     private boolean outUndefined = true;
+    
+    /**
+     * Javascript statement that sets the "out" variable that's used
+     * to output data. Automatically inserted by the reader in code,
+     * where needed.
+     */
+    public static final String DEFAULT_OUT_INIT_STATEMENT = "out=response.writer;"; 
+    private String outInitStatement = DEFAULT_OUT_INIT_STATEMENT;
 
     /**
      * Create an EspReader on top of the given <code>baseReader</code>. The
@@ -197,6 +205,11 @@
         // Start in ESP (template text) state
         pushState(PARSE_STATE_ESP);
     }
+    
+    /** Set the code fragment used to initialize the "out" variable */
+    public void setOutInitStatement(String statement) {
+        outInitStatement = statement;
+    }
 
     /**
      * Check whether we may block at the next read() operation. We may be ready
@@ -701,7 +714,7 @@
         // if out is not set yet, we also acquire it now setting it
         // globally
         if (outUndefined) {
-            doVerbatim("out=response.writer;");
+            doVerbatim(outInitStatement);
             outUndefined = false;
         }
     }

Modified: incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/EspReaderTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/EspReaderTest.java?rev=599378&r1=599377&r2=599378&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/EspReaderTest.java
(original)
+++ incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/EspReaderTest.java
Thu Nov 29 03:05:31 2007
@@ -88,6 +88,23 @@
         assertEquals("out=response.writer;out.write(\"test\");", parse("test"));
         assertEquals("out=response.writer;out.write(\"test\\n\");\nout.write(\"test2\");",
parse("test\ntest2"));
     }
+    
+    /** Test with a custom "out" initialization */
+    public void testOutInit() throws IOException {
+        final String input = "test";
+        final String expected = "out=getOut();out.write(\"test\");";
+            
+        StringBuffer buf = new StringBuffer();
+
+        EspReader r = new EspReader(new StringReader(input));
+        r.setOutInitStatement("out=getOut();");
+        int c;
+        while ( (c=r.read()) >= 0) {
+            buf.append( (char) c);
+        }
+
+        assertEquals(expected, buf.toString());
+    }
 
     /** Test plain JavaScript code */
     public void testCode() throws IOException {



Mime
View raw message