You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2008/02/08 15:56:18 UTC
svn commit: r619901 - in /incubator/sling/trunk/scripting/javascript/src:
main/java/org/apache/sling/scripting/javascript/wrapper/
test/java/org/apache/sling/scripting/wrapper/
Author: bdelacretaz
Date: Fri Feb 8 06:56:17 2008
New Revision: 619901
URL: http://svn.apache.org/viewvc?rev=619901&view=rev
Log:
SLING-154 - Node methods accessible using their real names like node.getPath() - see ScriptableNodeTest for details
Modified:
incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableNode.java
incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableProperty.java
incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/wrapper/ScriptableNodeTest.java
Modified: incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableNode.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableNode.java?rev=619901&r1=619900&r2=619901&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableNode.java (original)
+++ incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableNode.java Fri Feb 8 06:56:17 2008
@@ -54,7 +54,7 @@
private final Logger log = LoggerFactory.getLogger(getClass());
private Node node;
-
+
public ScriptableNode() {
}
@@ -74,26 +74,26 @@
public Class<?> [] getWrappedClasses() {
return WRAPPED_CLASSES;
}
-
- public ScriptableItemMap jsGet_children() {
+
+ public ScriptableItemMap jsFunction_getChildren() {
try {
return new ScriptableItemMap(node.getNodes());
} catch (RepositoryException re) {
- log.warn("Cannot get children of " + jsGet_path(), re);
+ log.warn("Cannot get children of " + jsFunction_getPath(), re);
return new ScriptableItemMap();
}
}
- public ScriptableItemMap jsGet_properties() {
+ public ScriptableItemMap jsFunction_getProperties() {
try {
return new ScriptableItemMap(node.getProperties());
} catch (RepositoryException re) {
- log.warn("Cannot get children of " + jsGet_path(), re);
+ log.warn("Cannot get children of " + jsFunction_getPath(), re);
return new ScriptableItemMap();
}
}
- public Object jsGet_primaryItem() {
+ public Object jsFunction_getPrimaryItem() {
try {
return ScriptRuntime.toObject(this, node.getPrimaryItem());
} catch (RepositoryException re) {
@@ -101,7 +101,7 @@
}
}
- public String jsGet_UUID() {
+ public String jsFunction_getUUID() {
try {
return node.getUUID();
} catch (RepositoryException re) {
@@ -109,7 +109,7 @@
}
}
- public int jsGet_index() {
+ public int jsFunction_getIndex() {
try {
return node.getIndex();
} catch (RepositoryException re) {
@@ -117,7 +117,7 @@
}
}
- public Iterator<?> jsGet_references() {
+ public Iterator<?> jsFunction_getReferences() {
try {
return node.getReferences();
} catch (RepositoryException re) {
@@ -125,7 +125,7 @@
}
}
- public Object jsGet_primaryNodeType() {
+ public Object jsFunction_getPrimaryNodeType() {
try {
return node.getPrimaryNodeType();
} catch (RepositoryException re) {
@@ -133,7 +133,7 @@
}
}
- public NodeType[] jsGet_mixinNodeTypes() {
+ public NodeType[] jsFunction_getMixinNodeTypes() {
try {
return node.getMixinNodeTypes();
} catch (RepositoryException re) {
@@ -141,7 +141,7 @@
}
}
- public Object jsGet_definition() {
+ public Object jsFunction_getDefinition() {
try {
return node.getDefinition();
} catch (RepositoryException re) {
@@ -149,7 +149,7 @@
}
}
- public boolean jsGet_checkedOut() {
+ public boolean jsFunction_getCheckedOut() {
try {
return node.isCheckedOut();
} catch (RepositoryException re) {
@@ -157,7 +157,7 @@
}
}
- public Object jsGet_versionHistory() {
+ public Object jsFunction_getVersionHistory() {
try {
return node.getVersionHistory();
} catch (RepositoryException re) {
@@ -165,7 +165,7 @@
}
}
- public Object jsGet_baseVersion() {
+ public Object jsFunction_getBaseVersion() {
try {
return node.getBaseVersion();
} catch (RepositoryException re) {
@@ -173,7 +173,7 @@
}
}
- public Object jsGet_lock() {
+ public Object jsFunction_getLock() {
try {
return node.getLock();
} catch (RepositoryException re) {
@@ -181,7 +181,7 @@
}
}
- public boolean jsGet_locked() {
+ public boolean jsFunction_getLocked() {
try {
return node.isLocked();
} catch (RepositoryException re) {
@@ -189,7 +189,7 @@
}
}
- public Object jsGet_session() {
+ public Object jsFunction_getSession() {
try {
return node.getSession();
} catch (RepositoryException re) {
@@ -197,7 +197,7 @@
}
}
- public String jsGet_path() {
+ public String jsFunction_getPath() {
try {
return node.getPath();
} catch (RepositoryException e) {
@@ -205,7 +205,7 @@
}
}
- public String jsGet_name() {
+ public String jsFunction_getName() {
try {
return node.getName();
} catch (RepositoryException e) {
@@ -213,7 +213,7 @@
}
}
- public Object jsGet_parent() {
+ public Object jsFunction_getParent() {
try {
return ScriptRuntime.toObject(this, node.getParent());
} catch (RepositoryException re) {
@@ -221,7 +221,7 @@
}
}
- public int jsGet_depth() {
+ public int jsFunction_getDepth() {
try {
return node.getDepth();
} catch (RepositoryException re) {
@@ -229,11 +229,11 @@
}
}
- public boolean jsGet_new() {
+ public boolean jsFunction_getNew() {
return node.isNew();
}
- public boolean jsGet_modified() {
+ public boolean jsFunction_getModified() {
return node.isModified();
}
@@ -244,17 +244,30 @@
*/
@Override
public Object get(String name, Scriptable start) {
- List<Scriptable> items = new ArrayList<Scriptable>();
- // add all matching nodes
+ // builtin javascript properties (jsFunction_ etc.) have priority
+ final Object fromSuperclass = super.get(name, start);
+ if(fromSuperclass != Scriptable.NOT_FOUND) {
+ return fromSuperclass;
+ }
+
+ if(node == null) {
+ return Undefined.instance;
+ }
+
+ final List<Scriptable> items = new ArrayList<Scriptable>();
+
+ // Add all matching nodes to result
try {
NodeIterator it = node.getNodes(name);
while (it.hasNext()) {
items.add(new ScriptableNode(it.nextNode()));
}
- } catch (RepositoryException e) {}
+ } catch (RepositoryException e) {
+ log.debug("RepositoryException while collecting Node children",e);
+ }
- // add all matching properies
+ // Add all matching properties to result
try {
PropertyIterator it = node.getProperties(name);
while (it.hasNext()) {
@@ -273,10 +286,12 @@
}
}
}
- } catch (RepositoryException e) {}
+ } catch (RepositoryException e) {
+ log.debug("RepositoryException while collecting Node properties",e);
+ }
if (items.size()==0) {
- return Undefined.instance;
+ return Scriptable.NOT_FOUND;
} else if (items.size()==1) {
return items.iterator().next();
} else {
@@ -326,8 +341,18 @@
}
@Override
+ public Object getDefaultValue(Class typeHint) {
+ try {
+ return node.getPath();
+ } catch(Exception e) {
+ return null;
+ }
+ }
+
+ @Override
public boolean has(String name, Scriptable start) {
try {
+ // TODO should this take into account our jsFunction_ members?
return node.hasProperty(name) || node.hasNode(name);
} catch (RepositoryException e) {
return false;
Modified: incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableProperty.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableProperty.java?rev=619901&r1=619900&r2=619901&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableProperty.java (original)
+++ incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableProperty.java Fri Feb 8 06:56:17 2008
@@ -41,6 +41,9 @@
this.property = property;
}
+ public void jsConstructor() {
+ }
+
public void jsConstructor(Object res) {
this.property = (Property) res;
}
Modified: incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/wrapper/ScriptableNodeTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/wrapper/ScriptableNodeTest.java?rev=619901&r1=619900&r2=619901&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/wrapper/ScriptableNodeTest.java (original)
+++ incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/wrapper/ScriptableNodeTest.java Fri Feb 8 06:56:17 2008
@@ -19,6 +19,7 @@
package org.apache.sling.scripting.wrapper;
import javax.jcr.Node;
+import javax.jcr.Property;
import org.apache.sling.scripting.RepositoryScriptingTestBase;
import org.apache.sling.scripting.ScriptEngineHelper;
@@ -30,6 +31,7 @@
public class ScriptableNodeTest extends RepositoryScriptingTestBase {
private Node node;
+ private Property textProperty;
private String testText;
private ScriptEngineHelper.Data data;
@@ -40,19 +42,38 @@
node = getNewNode();
testText = "Test-" + System.currentTimeMillis();
node.setProperty("text", testText);
+ node.setProperty("otherProperty", node.getPath());
data = new ScriptEngineHelper.Data();
data.put("node", node);
- data.put("property", node.getProperty("text"));
+ textProperty = node.getProperty("text");
+ data.put("property", textProperty);
}
- /** TODO reactivate this once SLING-154 is fixed */
- public void TODO_FAILS_testPrimaryNodeType() throws Exception {
+ public void testDefaultValue() throws Exception {
+ final ScriptEngineHelper.Data data = new ScriptEngineHelper.Data();
+ data.put("node", getTestRootNode());
+ assertEquals(
+ getTestRootNode().getPath(),
+ script.evalToString("out.print(node)", data)
+ );
+ }
+
+ public void testPrimaryNodeType() throws Exception {
final ScriptEngineHelper.Data data = new ScriptEngineHelper.Data();
data.put("node", getTestRootNode());
assertEquals(
"nt:unstructured",
- script.evalToString("out.print(node.getPrimaryNodeType())", data)
+ script.evalToString("out.print(node.getPrimaryNodeType().getName())", data)
+ );
+ }
+
+ public void testPrimaryNodeTypeProperty() throws Exception {
+ final ScriptEngineHelper.Data data = new ScriptEngineHelper.Data();
+ data.put("node", getTestRootNode());
+ assertEquals(
+ "nt:unstructured",
+ script.evalToString("out.print(node['jcr:primaryType'])", data)
);
}
@@ -64,24 +85,29 @@
}
public void testViaPropertyWithWrappers() throws Exception {
+ // TODO why does that not return the property value??
assertEquals(
- testText,
+ textProperty.getPath(),
script.evalToString("out.print(property)", data)
);
}
- /** TODO reactivate this once SLING-154 is fixed */
- public void TODO_FAILStestViaNodeNoWrappers() throws Exception {
- assertEquals(
- testText,
- script.evalToString("out.print(node.properties.text.value.string)", data)
- );
- }
-
public void testViaNodeDirectPropertyAccess() throws Exception {
assertEquals(
testText,
script.evalToString("out.print(node.text)", data)
);
+ }
+
+ public void testPropertiesIterationNoWrapper() throws Exception {
+ final String code =
+ "var props = node.getProperties();"
+ + " for(i in props) { out.print(props[i].name); out.print(' '); }"
+ ;
+ final String result = script.evalToString(code, data);
+ final String [] names = { "text", "otherProperty" };
+ for(String name : names) {
+ assertTrue("result (" + result + ") contains '" + name + "'", result.contains(name));
+ }
}
}