There we go. I've pretty much got this, thanks to everyone. Here's a helper
class I drummed up--It's not fully tested, doesn't handle both HTML & text,
and doesn't take parameters yet. I'll try and post a new thread (maybe even
on the wiki?) when I get those details hashed out. For now, this is a good
proof of concept that can be used virtually anywhere (to reiterate: This
requires 1.3 beta 2):





/**
 * Used with WicketTester to capture rendered HTML from a WicketPage.
ex:<p/>
 *
 * <pre>
 * CaptureApplication app = new CaptureApplication();
 * WicketTester renderer = new WicketTester(app);
 * renderer.startPage(WicketPage.class);
 * String html = app.getRenderedText();
 * </pre>
 */
class CaptureApplication extends WebApplication {
  /** Holds HTML rendered from Wicket files after the request cycle is run
through. */
  private String renderedText;



  /** Required for WebApplication interface--this won't be called in
WicketTester. */
  public Class getHomePage() {
    return null;
  }



  /**
   * @throws IllegalStateException if renderedText is null (meaning the
request has not yet been
   * cycled through).
   */
  String getRenderedText() {
    if (renderedText == null) {
      throw new IllegalStateException("Response text has not been rendered
yet");
    }

    return renderedText;
  }



  /** This is automagically called during the Wicket request cycle. */
  protected IRequestCycleProcessor newRequestCycleProcessor() {
    return new InterceptingCycle();
  }



  /**
   * Intercepts request cycle and extracts HTML rendered from Wicket files
before they are sent as a
   * response to the "browser".
   */
  private class InterceptingCycle extends WebRequestCycleProcessor {
    @Override
    /** Intercepts request cycle and stores the rendered HTML in a gettable
String. */
    public void respond(RequestCycle requestCycle) {
      //We need a holder for the HTML that Wicket can work with.
      StringResponse emailResponse = new StringResponse();

      //Now we swap the holder in for the real response.
      WebResponse originalResponse = (WebResponse)
RequestCycle.get().getResponse();
      RequestCycle.get().setResponse(emailResponse);

      //Do the real work...
      super.respond(requestCycle);

      //Snag the rendered HTML...
      renderedText = emailResponse.toString();

      //We need to flip back to the original response to avoid a
ClassCastException on the tail end
      //of the request cycle.
      RequestCycle.get().setResponse(originalResponse);
    }
  }
}
-- 
View this message in context: 
http://www.nabble.com/How-to-get-HTML-source-code-from-a-wicket-page-tf3968790.html#a11548230
Sent from the Wicket - User mailing list archive at Nabble.com.


-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Wicket-user mailing list
Wicket-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wicket-user

Reply via email to