if (response == null)
throw new IOException("No response object in context.");
try {
String decoratorName = (String) node.jjtGetChild(0).value(adapter);
StringWriter bodyContent = new StringWriter(1024);
int bodyNode = 1;
if (node.jjtGetNumChildren() == 3)
bodyNode = 2;
node.jjtGetChild(bodyNode).render(adapter, bodyContent);
Factory factory = PortletContext.getContext().getSiteMeshFactory();
Decorator decorator = factory.getDecoratorMapper().getNamedDecorator(request, decoratorName);
if (decorator != null) {
com.opensymphony.module.sitemesh.PageParser parser = factory.getPageParser("text/html");
HTMLPage page = (HTMLPage) ((FastPageParser) parser).parse(new StringReader(bodyContent.toString()));
Context context = VelocityManager.getInstance()
.createContext(ActionContext.getContext().getValueStack(), request, response);
context.put("page", page);
if (node.jjtGetNumChildren() == 3)
context.put("title", (String) node.jjtGetChild(1).value(adapter));
else
context.put("title", page.getTitle());
StringWriter buffer = new StringWriter();
page.writeBody(OutputConverter.getWriter(buffer));
context.put("body", buffer.toString());
buffer = new StringWriter();
page.writeHead(OutputConverter.getWriter(buffer));
context.put("head", buffer.toString());
context.put("params", params);
writer.write(VelocityUtils.getRenderedTemplate(decorator.getPage(), context));
} else {
throw new IOException("could not find decorator with name: " + decoratorName);
}