From d0d0517993c6bded599cb4320cb80e134e69826c Mon Sep 17 00:00:00 2001 From: hns Date: Mon, 19 Dec 2005 22:17:10 +0000 Subject: [PATCH] Committing patch from Juerg Lehni: * Implement HopObject.getResource(String filename), returns a helma.framework.repository.Resource object defined for the prototype. * Minor optimization in put(). --- src/helma/scripting/rhino/HopObject.java | 35 ++++++++++++++++++++---- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/helma/scripting/rhino/HopObject.java b/src/helma/scripting/rhino/HopObject.java index d49a68b8..086d2a4e 100644 --- a/src/helma/scripting/rhino/HopObject.java +++ b/src/helma/scripting/rhino/HopObject.java @@ -20,6 +20,7 @@ import helma.scripting.ScriptingException; import helma.scripting.ScriptingEngine; import helma.framework.core.*; import helma.framework.ResponseTrans; +import helma.framework.repository.Resource; import helma.objectmodel.*; import helma.objectmodel.db.*; import org.mozilla.javascript.*; @@ -285,6 +286,30 @@ public class HopObject extends ScriptableObject implements Wrapper, PropertyReco return true; } + /** + * Returns a prototype's resource of a given name. Walks up the prototype's + * inheritance chain if the resource is not found + * + * @param resourceName the name of the resource, e.g. "type.properties", + * "messages.properties", "script.js", etc. + * @return the resource, if found, null otherwise + */ + public Object jsFunction_getResource(String resourceName) { + Context cx = Context.getCurrentContext(); + RhinoEngine engine = (RhinoEngine) cx.getThreadLocal("engine"); + Prototype prototype = engine.core.app.getPrototypeByName(this.node.getPrototype()); + while (prototype != null) { + Resource[] resources = prototype.getResources(); + for (int i = resources.length - 1; i >= 0; i--) { + Resource resource = resources[i]; + if (resource.exists() && resource.getShortName().equals(resourceName)) + return Context.toObject(resource, core.global); + } + prototype = prototype.getParentPrototype(); + } + return null; + } + /** * Render a skin and return its output as string. * @@ -746,14 +771,14 @@ public class HopObject extends ScriptableObject implements Wrapper, PropertyReco node.unset(name); } else if (value instanceof Scriptable) { Scriptable s = (Scriptable) value; - - if ("Date".equals(s.getClassName())) { + String className = s.getClassName(); + if ("Date".equals(className)) { node.setDate(name, new Date((long) ScriptRuntime.toNumber(s))); - } else if ("String".equals(s.getClassName())) { + } else if ("String".equals(className)) { node.setString(name, ScriptRuntime.toString(s)); - } else if ("Number".equals(s.getClassName())) { + } else if ("Number".equals(className)) { node.setFloat(name, ScriptRuntime.toNumber(s)); - } else if ("Boolean".equals(s.getClassName())) { + } else if ("Boolean".equals(className)) { node.setBoolean(name, ScriptRuntime.toBoolean(s)); } else { node.setJavaObject(name, s);