incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r587848 - in /incubator/sling/whiteboard/microsling/src/main: java/org/apache/sling/microsling/scripting/engines/ webapp/
Date Wed, 24 Oct 2007 10:08:14 GMT
Author: bdelacretaz
Date: Wed Oct 24 03:08:12 2007
New Revision: 587848

URL: http://svn.apache.org/viewvc?rev=587848&view=rev
Log:
SLING-77 - javascript-friendly wrappers for JCR Node and microsling Resource. Contributed
by Lars Trieloff, thanks!

Added:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/ScriptableNode.java
  (with props)
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/ScriptableResource.java
  (with props)
Modified:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/RhinoJavasSriptEngine.java
    incubator/sling/whiteboard/microsling/src/main/webapp/server-side-javascript.html

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/RhinoJavasSriptEngine.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/RhinoJavasSriptEngine.java?rev=587848&r1=587847&r2=587848&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/RhinoJavasSriptEngine.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/RhinoJavasSriptEngine.java
Wed Oct 24 03:08:12 2007
@@ -21,6 +21,7 @@
 import java.util.Map;
 import java.util.Map.Entry;
 
+import org.apache.sling.microsling.api.Resource;
 import org.apache.sling.microsling.api.exceptions.SlingException;
 import org.apache.sling.microsling.scripting.ScriptEngine;
 import org.apache.sling.microsling.scripting.helpers.EspReader;
@@ -61,7 +62,7 @@
 
             // add initial properties to the scope
             for (Entry<String, Object> entry : props.entrySet()) {
-                Object wrapped = Context.javaToJS(entry.getValue(), scope);
+                Object wrapped = wrap(scope, entry.getValue(), rhinoContext);
                 ScriptableObject.putProperty(scope, entry.getKey(), wrapped);
             }
 
@@ -79,4 +80,24 @@
             Context.exit();
         }
     }
+    /**
+     * Wraps Java objects in Rhino host objects.
+     * @param scope the current scope
+     * @param entry the object to wrap
+     * @param context the current context, if needed for class definitions
+     * @return a rhino hostobject
+     */
+	protected Object wrap(final ScriptableObject scope,
+			Object entry, Context context) {
+		//wrap the resource to make it friendlier for javascript
+		if (entry instanceof Resource) {
+			try {
+				ScriptableObject.defineClass(scope, ScriptableResource.class);
+				return context.newObject(scope, "Resource", new Object[] {entry});
+			} catch (Exception e) {
+				return Context.javaToJS(entry, scope);
+			}
+		}
+		return Context.javaToJS(entry, scope);
+	}
 }

Added: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/ScriptableNode.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/ScriptableNode.java?rev=587848&view=auto
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/ScriptableNode.java
(added)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/ScriptableNode.java
Wed Oct 24 03:08:12 2007
@@ -0,0 +1,134 @@
+package org.apache.sling.microsling.scripting.engines;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import javax.jcr.Node;
+import javax.jcr.NodeIterator;
+import javax.jcr.Property;
+import javax.jcr.PropertyIterator;
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.Value;
+import javax.jcr.ValueFormatException;
+
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.NativeArray;
+import org.mozilla.javascript.ScriptRuntime;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.ScriptableObject;
+/**
+ * A wrapper for JCR nodes that exposes all properties and child nodes
+ * as properties of a Javascript object.
+ */
+public class ScriptableNode extends ScriptableObject {
+	private Node node;
+	
+	public ScriptableNode(Node item) {
+		super();
+		this.node = item;
+	}
+	
+	public String getClassName() {
+		return "Node";
+	}
+	/**
+	 * Gets the value of a (Javascript) property. If there is a single single-value
+	 * JCR property of this node, return its string value. If there are multiple properties
+	 * of the same name or child nodes of the same name, return an array.
+	 */
+	@Override
+	public Object get(String name, Scriptable start) {
+		Set items = new HashSet();
+		try {
+			Iterator it = node.getNodes(name);
+			while (it.hasNext()) {
+				items.add(new ScriptableNode((Node) it.next()));
+			}
+		} catch (RepositoryException e) {}
+		
+		try {
+			Iterator it = node.getProperties(name);
+			while (it.hasNext()) {
+				Property prop = (Property) it.next();
+				int type = prop.getType();
+				if (prop.getDefinition().isMultiple()) {
+					Value[] values = prop.getValues();
+					for (int i=0;i<values.length;i++) {
+						items.add(wrap(values[i], type));
+					}
+				} else {
+					if (type==PropertyType.REFERENCE) {
+						items.add(new ScriptableNode(prop.getNode()));
+					} else {
+						items.add(wrap(prop.getValue(), type));
+					}
+				}
+			}
+		} catch (RepositoryException e) {}
+		
+		if (items.size()==0) {
+			return null;
+		} else if (items.size()==1) {
+			return items.iterator().next();
+		} else {
+			//TODO: add write support
+			NativeArray result = new NativeArray(items.toArray());
+	        ScriptRuntime.setObjectProtoAndParent(result, this);
+	        return result;
+		}
+	}
+	private Object wrap(Value value, int type) throws ValueFormatException, IllegalStateException,
RepositoryException {
+		if (type==PropertyType.BINARY) {
+			return Context.toBoolean(value.getBoolean());
+		} else if (type==PropertyType.DOUBLE) {
+			return Context.toNumber(value.getDouble());
+		} else if (type==PropertyType.LONG) {
+			return Context.toNumber(value.getLong());
+		}
+		return value.getString();
+	}
+
+	@Override
+	public Object[] getIds() {
+		Collection<String> ids = new HashSet<String>();
+		try {
+			PropertyIterator pit = node.getProperties();
+			while (pit.hasNext()) {
+				ids.add(pit.nextProperty().getName());
+			}
+		} catch (RepositoryException e) {
+			//do nothing, just do not list properties
+		}
+		try {
+			NodeIterator nit = node.getNodes();
+			while (nit.hasNext()) {
+				ids.add(nit.nextNode().getName());
+			}
+		} catch (RepositoryException e) {
+			//do nothing, just do not list child nodes
+		}
+		return ids.toArray();
+	}
+
+	@Override
+	public boolean has(String name, Scriptable start) {
+		try {
+			return node.hasProperty(name) || node.hasNode(name);
+		} catch (RepositoryException e) {
+			return false;
+		}
+	}
+
+	@Override
+	public Object getDefaultValue(Class typeHint) {
+		try {
+			return node.getPath();
+		} catch (RepositoryException e) {
+			return null;
+		}
+	}
+
+}

Propchange: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/ScriptableNode.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/ScriptableNode.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/ScriptableResource.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/ScriptableResource.java?rev=587848&view=auto
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/ScriptableResource.java
(added)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/ScriptableResource.java
Wed Oct 24 03:08:12 2007
@@ -0,0 +1,88 @@
+package org.apache.sling.microsling.scripting.engines;
+
+import javax.jcr.Node;
+
+import org.apache.sling.microsling.api.Resource;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.ScriptableObject;
+/**
+ * Resource in JavaScript has following signature:
+ * [Object] getData();
+ * [Object] data
+ * [Item] getItem();
+ * [Item] item
+ * [String] getResourceType();
+ * [String] type
+ * [String] getURI();
+ * [String] uri
+ * 
+ */
+public class ScriptableResource extends ScriptableObject {
+	private Resource resource;
+	
+	public ScriptableResource() {
+	}
+	
+	public void jsConstructor(Object res) {
+		this.resource = (Resource) res;
+	}
+
+	@Override
+	public String getClassName() {
+		return "Resource";
+	}
+
+	public Object jsFunction_getData() {
+		return resource.getData();
+	}
+	
+	public Object jsGet_data() {
+		return this.jsFunction_getData();
+	}
+
+	public Object jsFunction_getItem() {
+		return resource.getItem();
+	}
+	
+	public Object jsGet_item() {
+		if (resource.getItem() instanceof Node) {
+			return new ScriptableNode((Node) resource.getItem());
+		}
+		return Context.javaToJS(resource.getItem(), this);
+	}
+
+	public String jsFunction_getResourceType() {
+		return resource.getResourceType();
+	}
+	
+	public String jsGet_type() {
+		return this.jsFunction_getResourceType();
+	}
+
+	public Object jsFunction_getURI() {
+		return Context.javaToJS(resource.getURI(), this);
+	}
+	
+	public Object jsGet_uri() {
+		return this.jsFunction_getURI();
+	}
+	
+	public Object jsFunction_getMetadata() {
+		return resource.getMetadata();
+	}
+	
+	public Object jsGet_meta() {
+		return resource.getMetadata();
+	}
+
+	@Override
+	public Object getDefaultValue(Class typeHint) {
+		return resource.getURI();
+	}
+
+	public void setResource(Resource entry) {
+		this.resource = entry;
+	}
+	
+	
+}

Propchange: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/ScriptableResource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/ScriptableResource.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: incubator/sling/whiteboard/microsling/src/main/webapp/server-side-javascript.html
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/webapp/server-side-javascript.html?rev=587848&r1=587847&r2=587848&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/webapp/server-side-javascript.html (original)
+++ incubator/sling/whiteboard/microsling/src/main/webapp/server-side-javascript.html Wed
Oct 24 03:08:12 2007
@@ -75,22 +75,17 @@
 &lt;body>
 &lt;p>This page is generated from an ESP template!&lt;/p>
 &lt;h1>&lt;%= resource.getURI() %>&lt;/h1>
-&lt;% 
-  var it = resource.item.properties;
-  while(it.hasNext()) {
-    var p = it.next();
+&lt;%
+  for (var prop in resource.item) {
     %>
       &lt;p>
-        &lt;%= p.name %>
-        =
-        &lt;strong>&lt;%= p.string %>&lt;/strong>
+        &lt;%= resource.item[prop] %>
       &lt;/p>
     &lt;%
   }
-%> 
+%>
 &lt;/body>
 &lt;/html></pre>
-    
   <h2>Raw javascript</h2>
   <p>
   	Raw javascript is probably more useful to handle the POST, PUT or DELETE methods.
@@ -109,5 +104,34 @@
   </p>
   </div>
   
+  <h2>Directory listing example</h2>
+  <p>
+    Store the following template under 
+    <code>sling/scripts/NODETYPES/nt/unstructured/html.esp</code> to render a
simple
+    directory listing for <em>nt:unstructured</em> nodes which do not have a

+    <code>slingComponentId</code> property.
+  </p>
+  <pre>
+&lt;%-- microsling ESP directory listing example -%>
+&lt;html>
+&lt;body>
+&lt;p>This HTML directory listing is generated from an ESP template!&lt;/p>
+&lt;h1>&lt;%= resource.uri %>&lt;/h1>
+&lt;ol>
+&lt;%
+for (var prop in resource.item) {
+    if (resource.item[prop]["text"]) {
+        %>&lt;li>&lt;a href="/microsling&lt;%= resource.item[prop] %>.html">&lt;%=
resource.item[prop] %>&lt;/a>&lt;/li>&lt;%
+    }
+}
+%>
+&lt;/ol>
+&lt;/body>
+&lt;/html></pre>
+  <p>
+    If you have created some content with the test form, and activated the above template,

+    <a href="microsling/content/testing.sling.html">microsling/content/testing.sling.html</a>
should
+    display an HTML directory listing. 
+  </p>
 </body>
 </html>



Mime
View raw message