Revision: 1229
http://stripes.svn.sourceforge.net/stripes/?rev=1229&view=rev
Author: bengunter
Date: 2010-05-13 16:19:10 +0000 (Thu, 13 May 2010)
Log Message:
-----------
First cut at a fix for STS-391. The tags have been changed so they stream their
contents to the client instead of buffering them and then dumping the contents
all at once. The new file LayoutComponentRenderer.java was accidentally omitted
in previous commit.
Added Paths:
-----------
branches/1.5.x/stripes/src/net/sourceforge/stripes/tag/layout/LayoutComponentRenderer.java
Added:
branches/1.5.x/stripes/src/net/sourceforge/stripes/tag/layout/LayoutComponentRenderer.java
===================================================================
---
branches/1.5.x/stripes/src/net/sourceforge/stripes/tag/layout/LayoutComponentRenderer.java
(rev 0)
+++
branches/1.5.x/stripes/src/net/sourceforge/stripes/tag/layout/LayoutComponentRenderer.java
2010-05-13 16:19:10 UTC (rev 1229)
@@ -0,0 +1,96 @@
+/**
+ * Created by Ben Gunter on May 12, 2010 at 2:01:33 PM.
+ *
+ * Copyright 2010 Comsquared Systems. All rights reserved.
+ */
+package net.sourceforge.stripes.tag.layout;
+
+import java.io.IOException;
+import java.util.LinkedList;
+
+import javax.servlet.ServletException;
+import javax.servlet.jsp.PageContext;
+import javax.servlet.jsp.tagext.BodyContent;
+
+import net.sourceforge.stripes.exception.StripesRuntimeException;
+
+/**
+ * An object that can be stuffed into a scope (page, request, application,
etc.) and render a layout
+ * component to a string. This allows for use of EL expressions to output a
component (as described
+ * in the book <em>Stripes ... and web development is fun again</em>) without
requiring that all
+ * components be evaluated and buffered just in case a string representation
is needed. The
+ * evaluation happens only when necessary, saving cycles and memory.
+ *
+ * @author Ben Gunter
+ * @since Stripes 1.5.4
+ */
+public class LayoutComponentRenderer {
+ private LinkedList<PageContext> pageContext;
+ private String componentName, layoutName;
+
+ /**
+ * Create a new instance to render the specified component tag to a
string. The tag itself is
+ * only used to get other information that is necessary to turn a
component into a string, such
+ * as layout name and component name.
+ *
+ * @param tag The layout component to render.
+ */
+ public LayoutComponentRenderer(LayoutComponentTag tag) {
+ this.layoutName = tag.getLayoutName();
+ this.componentName = tag.getName();
+ }
+
+ /**
+ * Push a new page context onto the page context stack. The last page
context pushed onto the
+ * stack is the one that will be used to evaluate the component tag's body.
+ */
+ public void pushPageContext(PageContext pageContext) {
+ if (this.pageContext == null) {
+ this.pageContext = new LinkedList<PageContext>();
+ }
+
+ this.pageContext.add(pageContext);
+ }
+
+ /** Pop the last page context off the stack and return it. */
+ public PageContext popPageContext() {
+ if (pageContext == null || pageContext.isEmpty())
+ return null;
+ else
+ return pageContext.removeLast();
+ }
+
+ /** Get the last page context that was pushed onto the stack. */
+ public PageContext getPageContext() {
+ if (pageContext == null || pageContext.isEmpty())
+ return null;
+ else
+ return pageContext.getLast();
+ }
+
+ @Override
+ public String toString() {
+ PageContext pageContext = getPageContext();
+ LayoutContext context = LayoutContext.find(pageContext, layoutName);
+
+ // Save the current component name so it can be restored when we're
done
+ String restore = context.getCurrentComponentName();
+ context.setCurrentComponentName(componentName);
+
+ try {
+ BodyContent body = pageContext.pushBody();
+ pageContext.include(context.getRenderPage());
+ pageContext.popBody();
+ return body.getString();
+ }
+ catch (ServletException e) {
+ throw new StripesRuntimeException(e);
+ }
+ catch (IOException e) {
+ throw new StripesRuntimeException(e);
+ }
+ finally {
+ context.setCurrentComponentName(restore);
+ }
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development