Author: bdelacretaz
Date: Mon Jun 16 08:41:50 2008
New Revision: 668200

URL: http://svn.apache.org/viewvc?rev=668200&view=rev
Log:
SLING-466 - render HTML and javascript separately, work in progress

Added:
    
incubator/sling/trunk/scripting/jst/src/main/java/org/apache/sling/scripting/jst/JsCodeGenerator.java
   (with props)
    
incubator/sling/trunk/scripting/jst/src/main/java/org/apache/sling/scripting/jst/JsCodeGeneratorServlet.java
   (with props)
Modified:
    incubator/sling/trunk/scripting/jst/pom.xml
    
incubator/sling/trunk/scripting/jst/src/main/java/org/apache/sling/scripting/jst/JstScriptEngine.java

Modified: incubator/sling/trunk/scripting/jst/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/jst/pom.xml?rev=668200&r1=668199&r2=668200&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/jst/pom.xml (original)
+++ incubator/sling/trunk/scripting/jst/pom.xml Mon Jun 16 08:41:50 2008
@@ -49,6 +49,10 @@
     <build>
         <plugins>
             <plugin>
+              <groupId>org.apache.felix</groupId>
+              <artifactId>maven-scr-plugin</artifactId>
+            </plugin>
+            <plugin>
                 <groupId>org.apache.felix</groupId>
                 <artifactId>maven-bundle-plugin</artifactId>
                 <extensions>true</extensions>

Added: 
incubator/sling/trunk/scripting/jst/src/main/java/org/apache/sling/scripting/jst/JsCodeGenerator.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/jst/src/main/java/org/apache/sling/scripting/jst/JsCodeGenerator.java?rev=668200&view=auto
==============================================================================
--- 
incubator/sling/trunk/scripting/jst/src/main/java/org/apache/sling/scripting/jst/JsCodeGenerator.java
 (added)
+++ 
incubator/sling/trunk/scripting/jst/src/main/java/org/apache/sling/scripting/jst/JsCodeGenerator.java
 Mon Jun 16 08:41:50 2008
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.scripting.jst;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Reader;
+
+import org.apache.sling.scripting.javascript.io.EspReader;
+
+/** Generates javascript code by converting a JST script to
+ *  client-side javascript code that renders the node. Meant
+ *  to be used with the JstScriptEngine.      
+ */
+class JsCodeGenerator {
+
+    /** Convert the script read on scriptReader to javascript code and 
+     *  write the result to output using a ScriptFilteredCopy
+     */
+    void generateCode(Reader scriptReader, PrintWriter output) throws 
IOException {
+        final EspReader er = new EspReader(scriptReader);
+        output.println("// Script generated by the Sling JST engine " + 
getClass().getSimpleName());
+        er.setOutInitStatement("out=document;\n");
+        new ScriptFilteredCopy().copy(er,output);
+    }
+}

Propchange: 
incubator/sling/trunk/scripting/jst/src/main/java/org/apache/sling/scripting/jst/JsCodeGenerator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/sling/trunk/scripting/jst/src/main/java/org/apache/sling/scripting/jst/JsCodeGenerator.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: 
incubator/sling/trunk/scripting/jst/src/main/java/org/apache/sling/scripting/jst/JsCodeGeneratorServlet.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/jst/src/main/java/org/apache/sling/scripting/jst/JsCodeGeneratorServlet.java?rev=668200&view=auto
==============================================================================
--- 
incubator/sling/trunk/scripting/jst/src/main/java/org/apache/sling/scripting/jst/JsCodeGeneratorServlet.java
 (added)
+++ 
incubator/sling/trunk/scripting/jst/src/main/java/org/apache/sling/scripting/jst/JsCodeGeneratorServlet.java
 Mon Jun 16 08:41:50 2008
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.scripting.jst;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.io.Reader;
+
+import javax.servlet.ServletException;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceMetadata;
+import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
+
+/**
+ * A SlingSafeMethodsServlet that renders JST templates as javascript code
+ *
+ * @scr.component immediate="true" metatype="no"
+ * @scr.service interface="javax.servlet.Servlet"
+ * 
+ * @scr.property name="service.description" value="JST code generator servlet"
+ * @scr.property name="service.vendor" value="The Apache Software Foundation"
+ * 
+ * @scr.property name="sling.servlet.resourceTypes"
+ *               value="sling/servlet/default"
+ * @scr.property name="sling.servlet.extensions" value="js"
+ * @scr.property name="sling.servlet.selectors" value="jst"
+ */
[EMAIL PROTECTED]("serial")
+public class JsCodeGeneratorServlet extends SlingSafeMethodsServlet {
+
+    private final JsCodeGenerator codeGenerator = new JsCodeGenerator();
+    
+    @Override
+    protected void doGet(SlingHttpServletRequest request, 
SlingHttpServletResponse response) 
+    throws ServletException, IOException {
+        final Reader scriptReader = getReader(request.getResource());
+        final PrintWriter output = new PrintWriter(response.getOutputStream());
+        response.setContentType(" application/x-javascript");
+        codeGenerator.generateCode(scriptReader, output);
+        output.flush();
+    }
+    
+    /** Return a Reader for the given Resource */
+    static Reader getReader(Resource resource) throws IOException {
+        if(resource == null) {
+            throw new IllegalArgumentException("Resource is null");
+        }
+        final InputStream ins = resource.adaptTo(InputStream.class);
+        if (ins == null) {
+            throw new IOException("Resource " + resource.getPath() + " cannot 
be adapted to an InputStream");
+        } 
+        
+        String enc = (String) 
resource.getResourceMetadata().get(ResourceMetadata.CHARACTER_ENCODING);
+        if (enc == null) {
+            enc = "UTF-8";
+        }
+        return new InputStreamReader(ins, enc);
+    }
+}

Propchange: 
incubator/sling/trunk/scripting/jst/src/main/java/org/apache/sling/scripting/jst/JsCodeGeneratorServlet.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/sling/trunk/scripting/jst/src/main/java/org/apache/sling/scripting/jst/JsCodeGeneratorServlet.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: 
incubator/sling/trunk/scripting/jst/src/main/java/org/apache/sling/scripting/jst/JstScriptEngine.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/jst/src/main/java/org/apache/sling/scripting/jst/JstScriptEngine.java?rev=668200&r1=668199&r2=668200&view=diff
==============================================================================
--- 
incubator/sling/trunk/scripting/jst/src/main/java/org/apache/sling/scripting/jst/JstScriptEngine.java
 (original)
+++ 
incubator/sling/trunk/scripting/jst/src/main/java/org/apache/sling/scripting/jst/JstScriptEngine.java
 Mon Jun 16 08:41:50 2008
@@ -19,8 +19,6 @@
 package org.apache.sling.scripting.jst;
 
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.io.PrintWriter;
 import java.io.Reader;
 import java.util.LinkedList;
@@ -35,15 +33,15 @@
 
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.resource.Resource;
-import org.apache.sling.api.resource.ResourceMetadata;
 import org.apache.sling.api.scripting.SlingBindings;
 import org.apache.sling.api.scripting.SlingScriptHelper;
 import org.apache.sling.api.wrappers.SlingRequestPaths;
 import org.apache.sling.commons.json.JSONException;
 import org.apache.sling.commons.json.jcr.JsonItemWriter;
 import org.apache.sling.scripting.api.AbstractSlingScriptEngine;
-import org.apache.sling.scripting.javascript.io.EspReader;
 import org.apache.sling.servlets.get.helpers.HtmlRendererServlet;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /** Experimental JST script engine: converts a JST template (using the
  *  same templating syntax as ESP) to client-side javascript code
@@ -55,10 +53,17 @@
 
     private final List<String> libraryScripts = new LinkedList<String>();
     private final HtmlRendererServlet htmlRenderer;
-    private final ScriptFilteredCopy copier = new ScriptFilteredCopy();
+    private final Logger log = LoggerFactory.getLogger(getClass());
     
     // TODO should be configurable or synced with the actual location
     public final static String SLING_JS_PATH = "/system/sling.js";
+    
+    /** Property names to use to build the page title, in order of preference 
*/
+    public final static String [] TITLE_PROPERTY_NAMES = {
+        "title",
+        "name",
+        "description"
+    };
 
     JstScriptEngine(ScriptEngineFactory scriptEngineFactory) {
         super(scriptEngineFactory);
@@ -73,25 +78,27 @@
         // to the client inside a skeleton HTML document, and let the
         // client run the script
         Bindings props = context.getBindings(ScriptContext.ENGINE_SCOPE);
-
         final SlingScriptHelper helper = (SlingScriptHelper) 
props.get(SlingBindings.SLING);
 
         try {
             final PrintWriter w = helper.getResponse().getWriter();
-            final Reader er = 
getReader(helper.getScript().getScriptResource());
 
             // access our data (need a Node)
             final Resource r = helper.getRequest().getResource();
-
-            // to render we must have either a Node or a SyntheticResourceData
             final Node n = r.adaptTo(Node.class);
 
             // output HEAD with javascript initializations
             // TODO we should instead parse (at least minimally) the template 
file, and inject our
             // stuff in the right places
             w.println("<html><head><title id=\"JstPageTitle\">");
-            w.println("JST rendering of " + r.getPath());
+            w.println(getTitle(r, n));
             w.println("</title>");
+            
+            // TODO get additional head stuff from the script?
+            // something like
+            //  <!-- jst:head
+            //      <link rel="stylesheet" href="/apps/foo/foo.css"/>
+            //  -->
 
             // library scripts
             final SlingHttpServletRequest request = helper.getRequest();
@@ -101,15 +108,10 @@
                     + SlingRequestPaths.getServletPath(request)
                     + lib
                 ;
-                w.println("<script src=\"" + fullScriptPath + "\"></script>");
+                w.println("<script type=\"text/javascript\" src=\"" + 
fullScriptPath + "\"></script>");
             }
 
-            // onLoad method
-            w.println("<script language=\"javascript\">");
-            w.println("function jstOnLoad() { if(typeof onLoad == 
\"function\") { onLoad(); } }");
-            w.println("</script>");
-
-            // data in JSON format
+            // Node data in JSON format
             final JsonItemWriter j = new JsonItemWriter(null);
             final int maxRecursionLevels = 1;
             w.println("<script language='javascript'>");
@@ -121,15 +123,10 @@
             }
             w.println(";");
             w.println("</script>");
-            w.println("</head><body onLoad=\"jstOnLoad()\">");
-
-            // output our parsed script, first in body
-            w.println("<div id=\"JstRenderingScript\">\n<script 
language='javascript'>");
-            copier.copy(er,w);
-            w.println("</script>\n</div>");
 
             // default rendering, turned off automatically from the javascript 
that
             // follows, if javascript is enabled
+            w.println("</head><body>");
             w.println("<div id=\"JstDefaultRendering\">");
             if(n!=null) {
                 htmlRenderer.render(w, r, n);
@@ -138,6 +135,10 @@
             w.println("<script language=\"javascript\">");
             
w.println("document.getElementById(\"JstDefaultRendering\").setAttribute(\"style\",\"display:none\");");
             w.println("</script>");
+            
+            // reference to script provided by the JstCodeGeneratorServlet
+            final String scriptUrl = 
helper.getScript().getScriptResource().getPath() + ".jst.js";
+            w.println("<script type=\"text/javascript\" src=\"" + scriptUrl + 
"\"></script>");
 
             // all done
             w.println("</body></html>");
@@ -150,27 +151,39 @@
 
         } catch(JSONException je) {
             throw new ScriptException(je);
-
         }
 
         return null;
     }
-
-    private Reader getReader(Resource resource) throws IOException {
-        InputStream ins = resource.adaptTo(InputStream.class);
-        if (ins != null) {
-            String enc = (String) 
resource.getResourceMetadata().get(ResourceMetadata.CHARACTER_ENCODING);
-            if (enc == null) {
-                enc = "UTF-8";
+    
+    /** Return the title to use for the generated page */ 
+    protected String getTitle(Resource r, Node n) {
+        String title = null;
+        
+        if(n != null) {
+            for(String name : TITLE_PROPERTY_NAMES) {
+                try {
+                    if(n.hasProperty(name)) {
+                        final String s = n.getProperty(name).getString();
+                        if(s.length() > 0) {
+                            title = s;
+                            break;
+                        }
+                    } 
+                } catch(RepositoryException re) {
+                    log.warn("RepositoryException in getTitle()", re);
+                }
             }
-
-            Reader r = new InputStreamReader(ins, enc);
-            EspReader er = new EspReader(r);
-            er.setOutInitStatement("out=document;\n");
-
-            return er;
         }
-
-        return null;
+        
+        if(title == null) {
+            title = r.getPath();
+            if(title.startsWith("/")) {
+                title = title.substring(1);
+            }
+        }
+        
+        return title;
     }
+
 }


Reply via email to