From ca2a42e20434c0d5589b3937a16cc2158fe795f9 Mon Sep 17 00:00:00 2001 From: hns Date: Thu, 23 May 2002 14:11:08 +0000 Subject: [PATCH] implemented support for "prefix" and "suffix" for macros that directly write out to response buffer. --- src/helma/framework/ResponseTrans.java | 19 +++++++++++++++++++ src/helma/framework/core/Skin.java | 13 ++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/helma/framework/ResponseTrans.java b/src/helma/framework/ResponseTrans.java index b6867a48..c580ece0 100644 --- a/src/helma/framework/ResponseTrans.java +++ b/src/helma/framework/ResponseTrans.java @@ -143,6 +143,15 @@ public class ResponseTrans implements Externalizable { return b.toString (); } + /** + * Returns the number of characters written to the response buffer so far. + */ + public int getBufferLength() { + if (buffer == null) + return 0; + return buffer.length (); + } + /** * Append a string to the response unchanged. */ @@ -174,6 +183,16 @@ public class ResponseTrans implements Externalizable { buffer.append (c, start, length); } + /** + * Insert string somewhere in the response buffer. Caller has to make sure + * that buffer exists and its length is larger than offset. str may be null, in which + * case nothing happens. + */ + public void insert (int offset, String str) { + if (str != null) + buffer.insert (offset, str); + } + /** * Replace special characters with entities, including <, > and ", thus allowing * no HTML tags. diff --git a/src/helma/framework/core/Skin.java b/src/helma/framework/core/Skin.java index a61263b4..bcf75554 100644 --- a/src/helma/framework/core/Skin.java +++ b/src/helma/framework/core/Skin.java @@ -314,6 +314,8 @@ public class Skin { // if so, the macro evaluates to the function. Otherwise, // a property/field with the name is used, if defined. Object v = null; + // remember length of response buffer before calling macro + int oldLength = reval.res.getBufferLength (); if (app.scriptingEngine.hasFunction (handlerObject, name+"_macro", reval)) { // System.err.println ("Getting macro from function"); v = app.scriptingEngine.invoke (handlerObject, name+"_macro", arguments, null, reval); @@ -321,8 +323,17 @@ public class Skin { // System.err.println ("Getting macro from property"); v = app.scriptingEngine.get (handlerObject, name, reval); } - if (v != null) + // check if macro wrote out to response buffer + int newLength = reval.res.getBufferLength (); + if (newLength > oldLength) { + // insert prefix and append suffix + String prefix = (String) parameters.get ("prefix"); + String suffix = (String) parameters.get ("suffix"); + reval.res.insert (oldLength, prefix); + reval.res.write (suffix); + } else if (v != null) { writeToResponse (v.toString (), reval.res); + } } else { String msg = "[HopMacro unhandled: "+getFullName()+"]"; reval.res.write (" "+msg+" ");