FileDocCategorySizeDatePackage
ClassViewHandler.javaAPI DocExample8375Tue Jun 08 11:26:42 BST 2004com.mycompany.jsf.pl

ClassViewHandler

public class ClassViewHandler extends javax.faces.application.ViewHandler
This class is a JSF ViewHandler that works with views created by classes implementing the View interface.
author
Hans Bergsten, Gefion Software
version
1.0

Fields Summary
private static final String
STATE_VAR
protected javax.faces.application.ViewHandler
origViewHandler
private Map
views
Constructors Summary
public ClassViewHandler(javax.faces.application.ViewHandler origViewHandler)
Creates an instance and saves a reference to the previously registered ViewHandler.


                     
       
        this.origViewHandler = origViewHandler;
    
Methods Summary
public java.util.LocalecalculateLocale(javax.faces.context.FacesContext context)
Delegates the call to the previously registered ViewHandler.

        return origViewHandler.calculateLocale(context);
    
public java.lang.StringcalculateRenderKitId(javax.faces.context.FacesContext context)
Delegates the call to the previously registered ViewHandler.

        return origViewHandler.calculateRenderKitId(context);
    
public javax.faces.component.UIViewRootcreateView(javax.faces.context.FacesContext context, java.lang.String viewId)
Returns the UIViewRoot for the specified view ID, created by the createViewRoot() method, with the "locale" and "renderKitId" properties initialized.

        String realViewId = viewId;
        if (viewId.indexOf(".") != -1) {
            realViewId = viewId.substring(0, viewId.indexOf("."));
        }

        UIViewRoot viewRoot = createViewRoot(context, realViewId);
        if (viewRoot != null) {
            if (context.getViewRoot() != null) {
                UIViewRoot oldRoot = context.getViewRoot();
                viewRoot.setLocale(oldRoot.getLocale());
                viewRoot.setRenderKitId(oldRoot.getRenderKitId());
            }
            else {
		ViewHandler activeVH = 
		    context.getApplication().getViewHandler();
                viewRoot.setLocale(activeVH.calculateLocale(context));
                viewRoot.setRenderKitId(activeVH.calculateRenderKitId(context));
            }
        }
        return viewRoot;
    
protected javax.faces.component.UIViewRootcreateViewRoot(javax.faces.context.FacesContext context, java.lang.String viewId)
Returns the UIViewRoot for the view created by the View instance matching the view ID.

        UIViewRoot viewRoot = null;
        View view = (View) views.get(viewId);
        if (view == null) {
            if ("/subscribe".equals(viewId)) {
                view = new SubscribeView();
                views.put(viewId, view);
            }
        }
        if (view != null) {
            viewRoot = view.createView(context);
            viewRoot.setViewId(viewId);
        }
        return viewRoot;
    
public java.lang.StringgetActionURL(javax.faces.context.FacesContext context, java.lang.String viewId)
Delegates the call to the previously registered ViewHandler.

        return origViewHandler.getActionURL(context, viewId);
    
public java.lang.StringgetResourceURL(javax.faces.context.FacesContext context, java.lang.String path)
Delegates the call to the previously registered ViewHandler.

        return origViewHandler.getResourceURL(context, path);
    
protected voidrenderResponse(javax.faces.context.FacesContext context, javax.faces.component.UIComponent component)
Recursively renders the provided component and all its children by calling encodeBegin(), encodeChildren() (if the component renders its children) and encodeEnd().


        component.encodeBegin(context);
        if (component.getRendersChildren()) {
            component.encodeChildren(context);
        }
        else {
            Iterator i = component.getChildren().iterator();
            while (i.hasNext()) {
                renderResponse(context, (UIComponent) i.next());
            }
        }
        component.encodeEnd(context);
    
public voidrenderView(javax.faces.context.FacesContext context, javax.faces.component.UIViewRoot viewToRender)
Gets the view state to save, if any, and puts a reference to the state in a request scope variable where it can be picked up by the writeState() method, and renders the view represented by the provided UIViewRoot instance by calling renderResponse().


        setupResponseWriter(context);

        StateManager sm = context.getApplication().getStateManager();
        SerializedView state = sm.saveSerializedView(context);
	context.getExternalContext().getRequestMap().put(STATE_VAR, state);

        context.getResponseWriter().startDocument();
        renderResponse(context, viewToRender);
        context.getResponseWriter().endDocument();
    
public javax.faces.component.UIViewRootrestoreView(javax.faces.context.FacesContext context, java.lang.String viewId)
Returns the UIViewRoot for the restored view identified by the provided view ID, or null if no state is available.

        String realViewId = viewId;
        if (viewId.indexOf(".") != -1) {
            realViewId = viewId.substring(0, viewId.indexOf("."));
        }

        String renderKitId = 
	    context.getApplication().getViewHandler().
	    calculateRenderKitId(context);

        StateManager sm = context.getApplication().getStateManager();
        return sm.restoreView(context, realViewId, renderKitId);
    
private voidsetupResponseWriter(javax.faces.context.FacesContext context)
Asks the current RenderKit to create a ResponseWriter around the OutputStream for the response body and sets the Content-Type response header to the MIME type selected by the ResponseWriter from the alternatives listed in the Accept request header.


        ServletResponse response = (ServletResponse)
            context.getExternalContext().getResponse();
        OutputStream os = response.getOutputStream();
	Map headers = context.getExternalContext().getRequestHeaderMap();
	String acceptHeader = (String) headers.get("Accept");

	// Work-around for JSF 1.0 RI bug: failing to accept "*/*" and
	// and "text/*" as valid replacements for "text/html"
	if (acceptHeader != null && 
	    (acceptHeader.indexOf("*/*") != -1 ||
	     acceptHeader.indexOf("text/*") != -1)) {
	    acceptHeader += ",text/html";
	}

        RenderKitFactory renderFactory = (RenderKitFactory)
            FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
        RenderKit renderKit = 
            renderFactory.getRenderKit(context,
                 context.getViewRoot().getRenderKitId());
        ResponseWriter writer = 
            renderKit.createResponseWriter(new OutputStreamWriter(os),
                acceptHeader, response.getCharacterEncoding());
        context.setResponseWriter(writer);
        response.setContentType(writer.getContentType());
    
public voidwriteState(javax.faces.context.FacesContext context)
Writes the state captured and saved by the renderView() method to the response with the help of the current StateManager.

	SerializedView state = (SerializedView)
	    context.getExternalContext().getRequestMap().get(STATE_VAR);
	if (state != null) {
	    StateManager sm = context.getApplication().getStateManager();
	    sm.writeState(context, state);
	}