fixed bug for global skins

This commit is contained in:
hns 2001-02-20 14:23:35 +00:00
parent c708ff0c29
commit 0a2cbec8f7
3 changed files with 40 additions and 7 deletions

View file

@ -607,24 +607,30 @@ public class HopExtension {
try { try {
Skin skin = null; Skin skin = null;
ESNode handlerNode = global ? null : (ESNode) thisObject; ESNode handlerNode = global ? null : (ESNode) thisObject;
ESObject paramObject = null;
if (arguments.length > 1 && arguments[1] instanceof ESObject)
paramObject = (ESObject) arguments[1];
// first, see if the first argument already is a skin object. If not, it's the name of the skin to be called // first, see if the first argument already is a skin object. If not, it's the name of the skin to be called
if (arguments[0] instanceof ESWrapper) { if (arguments[0] instanceof ESWrapper) {
Object obj = ((ESWrapper) arguments[0]).toJavaObject (); Object obj = ((ESWrapper) arguments[0]).toJavaObject ();
if (obj instanceof Skin) if (obj instanceof Skin)
skin = (Skin) obj; skin = (Skin) obj;
} }
if (skin == null) if (skin == null)
skin = reval.getSkin (handlerNode, arguments[0].toString ()); skin = reval.getSkin (handlerNode, arguments[0].toString ());
if (asString) if (asString)
reval.res.pushStringBuffer (); reval.res.pushStringBuffer ();
try { try {
skin.render (reval, handlerNode); skin.render (reval, handlerNode, paramObject);
} catch (NullPointerException npx) { } catch (NullPointerException npx) {
reval.res.write ("[Skin not found: "+arguments[0]+"]"); reval.res.write ("[Skin not found: "+arguments[0]+"]");
} }
if (asString) if (asString)
return new ESString (reval.res.popStringBuffer ()); return new ESString (reval.res.popStringBuffer ());
} catch (Exception x) { } catch (Exception x) {
x.printStackTrace ();
throw new EcmaScriptException ("renderSkin: "+x); throw new EcmaScriptException ("renderSkin: "+x);
} }
return ESNull.theNull; return ESNull.theNull;

View file

@ -655,14 +655,17 @@ public class RequestEvaluator implements Runnable {
public Skin getSkin (ESObject thisObject, String skinname) { public Skin getSkin (ESObject thisObject, String skinname) {
INode n = null; INode n = null;
if (thisObject != null && thisObject instanceof ESNode) Prototype proto = null;
if (thisObject != null && thisObject instanceof ESNode) {
n = ((ESNode) thisObject).getNode (); n = ((ESNode) thisObject).getNode ();
Prototype proto = app.getPrototype (n); proto = app.getPrototype (n);
} else // the requested skin is global
proto = app.typemgr.getPrototype ("global");
Skin skin = null; Skin skin = null;
if (proto != null) if (proto != null)
skin = proto.getSkin (skinname); skin = proto.getSkin (skinname);
// if we have a thisObject and didn't find the skin, try in hopobject // if we have a thisObject and didn't find the skin, try in hopobject
if (skin == null) { if (skin == null && n != null) {
proto = app.typemgr.getPrototype ("hopobject"); proto = app.typemgr.getPrototype ("hopobject");
if (proto != null) if (proto != null)
skin = proto.getSkin (skinname); skin = proto.getSkin (skinname);

View file

@ -55,12 +55,12 @@ public class Skin {
parts = partBuffer.toArray (); parts = partBuffer.toArray ();
} }
public void render (RequestEvaluator reval, ESNode thisNode) { public void render (RequestEvaluator reval, ESNode thisNode, ESObject paramObject) {
if (parts == null) if (parts == null)
return; return;
for (int i=0; i<parts.length; i++) { for (int i=0; i<parts.length; i++) {
if (parts[i] instanceof Macro) if (parts[i] instanceof Macro)
((Macro) parts[i]).render (reval, thisNode); ((Macro) parts[i]).render (reval, thisNode, paramObject);
else else
reval.res.write (parts[i]); reval.res.write (parts[i]);
} }
@ -165,11 +165,17 @@ public class Skin {
} }
public void render (RequestEvaluator reval, ESNode thisNode) { public void render (RequestEvaluator reval, ESNode thisNode, ESObject paramObject) {
if ("response".equalsIgnoreCase (handler)) { if ("response".equalsIgnoreCase (handler)) {
renderFromResponse (reval); renderFromResponse (reval);
return; return;
} else if ("request".equalsIgnoreCase (handler)) {
renderFromRequest (reval);
return;
} else if ("param".equalsIgnoreCase (handler)) {
renderFromParam (reval, paramObject);
return;
} }
try { try {
@ -246,6 +252,24 @@ public class Skin {
reval.res.write (reval.res.message); reval.res.write (reval.res.message);
} }
private void renderFromRequest (RequestEvaluator reval) {
Object value = reval.req.get (name);
if (value != null)
reval.res.write (value);
}
private void renderFromParam (RequestEvaluator reval, ESObject paramObject) {
if (paramObject == null)
reval.res.write ("[HopMacro error: Skin requires a parameter object]");
else {
try {
ESValue value = paramObject.getProperty (name, name.hashCode());
if (value != null && value != ESUndefined.theUndefined)
reval.res.write (value);
} catch (EcmaScriptException ignore) {}
}
}
public String toString () { public String toString () {
return "[HopMacro: "+handler+","+name+"]"; return "[HopMacro: "+handler+","+name+"]";
} }