Skip to content

Instantly share code, notes, and snippets.

@vvasabi
Created July 17, 2011 04:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vvasabi/1087206 to your computer and use it in GitHub Desktop.
Save vvasabi/1087206 to your computer and use it in GitHub Desktop.
public class FaceletRenderer {
private FacesContext context;
public FaceletRenderer(FacesContext context) {
this.context = context;
}
/**
* Render the Facelets template specified.
*
* @param template path to the Facelets template
* @return rendered content of the Facelets template
*/
public String renderView(String template) {
try {
// store the original response writer
ResponseWriter originalWriter = context.getResponseWriter();
// put in a StringWriter to capture the output
StringWriter stringWriter = new StringWriter();
ResponseWriter writer = createResponseWriter(context, stringWriter);
context.setResponseWriter(writer);
// create a UIViewRoot instance using the template specified
ViewHandler viewHandler = context.getApplication().getViewHandler();
UIViewRoot view = viewHandler.createView(context, template);
// the fun part -- do the actual rendering here
ViewDeclarationLanguage vdl = viewHandler
.getViewDeclarationLanguage(context, template);
vdl.buildView(context, view);
renderChildren(context, view);
// restore the response writer
context.setResponseWriter(originalWriter);
return stringWriter.toString();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
/**
* Create ResponseWriter. Taken from FaceletViewDeclarationLanguage.java of
* MyFaces.
*/
private ResponseWriter createResponseWriter(FacesContext context,
Writer writer) {
ExternalContext extContext = context.getExternalContext();
Map<String, Object> requestMap = extContext.getRequestMap();
String contentType = (String)requestMap.get("facelets.ContentType");
String encoding = (String)requestMap.get("facelets.Encoding");
RenderKit renderKit = context.getRenderKit();
return renderKit.createResponseWriter(writer, contentType, encoding);
}
/**
* Render a UIComponent. Taken from JSF.java of Seam 2.2.
*/
private void renderChildren(FacesContext context, UIComponent component)
throws IOException {
List<UIComponent> children = component.getChildren();
for (int i = 0, size = component.getChildCount(); i < size; i++) {
UIComponent child = (UIComponent)children.get(i);
renderChild(context, child);
}
}
/**
* Render the child and all its children components.
*/
private void renderChild(FacesContext context, UIComponent child)
throws IOException {
if (child.isRendered()) {
child.encodeAll(context);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment