rewrote the mechanism to write out to the response object.
encoding, prefix and suffix attributes can now be set for any and all macros (not sure if this will create problems) Macros handled by objects now check if a function called handler_macro is defined. If so, the function is called. Otherwise, the macro evaluates to a property of the object with the handler name.
This commit is contained in:
parent
6ae2db92ff
commit
49474a3317
1 changed files with 52 additions and 50 deletions
|
@ -71,7 +71,7 @@ public class Skin {
|
||||||
if (lastIdx < l)
|
if (lastIdx < l)
|
||||||
partBuffer.add (new String (cnt, lastIdx, l - lastIdx));
|
partBuffer.add (new String (cnt, lastIdx, l - lastIdx));
|
||||||
|
|
||||||
parts = partBuffer.toArray ();
|
parts = partBuffer.toArray ();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -298,10 +298,19 @@ public class Skin {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (objectFound) {
|
if (objectFound) {
|
||||||
Object v = reval.invokeDirectFunction (handlerObject, name+"_macro", arguments);
|
// check if a function called name_macro is defined.
|
||||||
// ESValue v = handlerObject.doIndirectCall (reval.evaluator, handlerObject, name+"_macro", arguments);
|
// if so, the macro evaluates to the function. Otherwise,
|
||||||
|
// a property/field with the name is used, if defined.
|
||||||
|
Object v = null;
|
||||||
|
if (reval.hasFunction (handlerObject, name+"_macro")) {
|
||||||
|
// System.err.println ("Getting macro from function");
|
||||||
|
v = reval.invokeDirectFunction (handlerObject, name+"_macro", arguments);
|
||||||
|
} else {
|
||||||
|
// System.err.println ("Getting macro from property");
|
||||||
|
v = reval.getProperty (handlerObject, name);
|
||||||
|
}
|
||||||
if (v != null)
|
if (v != null)
|
||||||
reval.res.write (v);
|
writeToResponse (v.toString (), reval.res);
|
||||||
} else {
|
} else {
|
||||||
String msg = "[HopMacro unhandled: "+getFullName()+"]";
|
String msg = "[HopMacro unhandled: "+getFullName()+"]";
|
||||||
reval.res.write (" "+msg+" ");
|
reval.res.write (" "+msg+" ");
|
||||||
|
@ -320,40 +329,59 @@ public class Skin {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void renderFromResponse (RequestEvaluator reval) {
|
private void renderFromResponse (RequestEvaluator reval) {
|
||||||
String encoding = (String) parameters.get ("encoding");
|
Object value = null;
|
||||||
if ("title".equals (name) && reval.res.title != null)
|
// as a transitional solution, try to get the value from the
|
||||||
reval.res.write (encode (reval.res.title, encoding));
|
// hardcoded fields in the response object. If not present, try
|
||||||
else if ("head".equals (name) && reval.res.head != null)
|
// the response object's data object.
|
||||||
reval.res.write (encode (reval.res.head, encoding));
|
if ("title".equals (name))
|
||||||
else if ("body".equals (name) && reval.res.body != null)
|
value = reval.res.title;
|
||||||
reval.res.write (encode (reval.res.body, encoding));
|
else if ("head".equals (name))
|
||||||
else if ("message".equals (name) && reval.res.message != null)
|
value = reval.res.head;
|
||||||
reval.res.write (encode (reval.res.message, encoding));
|
else if ("body".equals (name))
|
||||||
|
value = reval.res.body;
|
||||||
|
else if ("message".equals (name))
|
||||||
|
value = reval.res.message;
|
||||||
|
if (value == null)
|
||||||
|
value = reval.res.get (name);
|
||||||
|
if (value != null)
|
||||||
|
writeToResponse (value.toString (), reval.res);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void renderFromRequest (RequestEvaluator reval) {
|
private void renderFromRequest (RequestEvaluator reval) {
|
||||||
String encoding = (String) parameters.get ("encoding");
|
|
||||||
Object value = reval.req.get (name);
|
Object value = reval.req.get (name);
|
||||||
if (value != null)
|
if (value != null)
|
||||||
reval.res.write (encode (value.toString (), encoding));
|
writeToResponse (value.toString (), reval.res);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void renderFromParam (RequestEvaluator reval, HashMap paramObject) {
|
private void renderFromParam (RequestEvaluator reval, HashMap paramObject) {
|
||||||
String encoding = (String) parameters.get ("encoding");
|
|
||||||
if (paramObject == null)
|
if (paramObject == null)
|
||||||
reval.res.write ("[HopMacro error: Skin requires a parameter object]");
|
reval.res.write ("[HopMacro error: Skin requires a parameter object]");
|
||||||
else {
|
else {
|
||||||
Object value = paramObject.get (name);
|
Object value = paramObject.get (name);
|
||||||
if (value != null)
|
if (value != null)
|
||||||
reval.res.write (encode (value.toString (), encoding));
|
writeToResponse (value.toString (), reval.res);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility method for performing different kind of character encodings on the
|
* Utility method for writing text out to the response object.
|
||||||
* macro output.
|
|
||||||
*/
|
*/
|
||||||
public String encode (String text, String encoding) {
|
void writeToResponse (String text, ResponseTrans res) {
|
||||||
|
if (text == null || text.length() == 0)
|
||||||
|
return;
|
||||||
|
String encoding = (String) parameters.get ("encoding");
|
||||||
|
String prefix = (String) parameters.get ("prefix");
|
||||||
|
String suffix = (String) parameters.get ("suffix");
|
||||||
|
res.write (encode (prefix, encoding));
|
||||||
|
res.write (encode (text, encoding));
|
||||||
|
res.write (encode (suffix, encoding));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility method for performing different kind of character
|
||||||
|
* encodings on the macro output.
|
||||||
|
*/
|
||||||
|
String encode (String text, String encoding) {
|
||||||
if (encoding == null || text == null)
|
if (encoding == null || text == null)
|
||||||
return text;
|
return text;
|
||||||
if ("html".equalsIgnoreCase (encoding))
|
if ("html".equalsIgnoreCase (encoding))
|
||||||
|
@ -367,6 +395,7 @@ public class Skin {
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public String toString () {
|
public String toString () {
|
||||||
return "[HopMacro: "+getFullName()+"]";
|
return "[HopMacro: "+getFullName()+"]";
|
||||||
}
|
}
|
||||||
|
@ -390,30 +419,3 @@ public class Skin {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue