incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r586450 - /incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/helpers/EspReader.java
Date Fri, 19 Oct 2007 13:43:37 GMT
Author: fmeschbe
Date: Fri Oct 19 06:43:37 2007
New Revision: 586450

URL: http://svn.apache.org/viewvc?rev=586450&view=rev
Log:
Create the out variable on demand when the first out.write statement is written. This way
an ESP script might theoretically also be used to write to an OutputStream (which is not recommended
IMHO)

Modified:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/helpers/EspReader.java

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/helpers/EspReader.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/helpers/EspReader.java?rev=586450&r1=586449&r2=586450&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/helpers/EspReader.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/helpers/EspReader.java
Fri Oct 19 06:43:37 2007
@@ -171,6 +171,15 @@
     private boolean escape;
 
     /**
+     * Whether the definition of the out variable has already been written or not.
+     * The initial value is <code>true</code> indicating it has still to be
+     * defined.
+     *
+     * @see #startWrite(String)
+     */
+    private boolean outUndefined = true;
+
+    /**
      * Create an EspReader on top of the given <code>baseReader</code>. The
      * constructor wraps the input reader with a <code>PushbackReader</code>,
      * so that input stream modifications may be handled transparently by our
@@ -407,7 +416,7 @@
 
                                 // ECMA expression <%= ... %>
                                 pushState(PARSE_STATE_ECMA_EXPR);
-                                doVerbatim("out.write(");
+                                startWrite(null);
                                 if (!lineStart) {
                                     doVerbatim("\");");
                                 }
@@ -468,7 +477,7 @@
                             // no matter what line ending we have, make it LF
                             doVerbatim("\");\n");
                             doVerbatim(lineEnd);
-                            doVerbatim("out.write(\"");
+                            startWrite("\"");
                         }
 
                         continue;
@@ -486,7 +495,7 @@
                     // If in template text at the beginning of a line
                     if (lineStart) {
                         lineStart = false;
-                        doVerbatim("out.write(\"" + (char) c);
+                        startWrite("\"" + (char) c);
                         continue;
                     }
 
@@ -666,6 +675,33 @@
     private void ensureOpen() throws IOException {
         if (input == null) {
             throw new IOException("Reader is closed");
+        }
+    }
+
+    /**
+     * Injects the call to write template text and checks whether the global
+     * <em>out</em> variable has also to be defined such that the writer is
+     * acquired on demand.
+     *
+     * @param startString Additional data to be injected as initial argument
+     *      to the <em>out.write</em> call written. If <code>null</code>
just
+     *      the method call is injected.
+     *
+     * @throws IOException if the 'unreading' throws
+     */
+    private void startWrite(String startString) throws IOException {
+
+        // inject the out.write( part and the initial string
+        if (startString != null && startString.length() > 0) {
+            doVerbatim(startString);
+        }
+        doVerbatim("out.write(");
+
+        // if out is not set yet, we also acquire it now setting it
+        // globally
+        if (outUndefined) {
+            doVerbatim("out=response.writer;");
+            outUndefined = false;
         }
     }
 



Mime
View raw message