All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ajax4jsf.renderers.ajax.AjaxPageRenderer Maven / Gradle / Ivy

Go to download

Ajax4jsf is an open source extension to the JavaServer Faces standard that adds AJAX capability to JSF applications without requiring the writing of any JavaScript.

The newest version!
/**
 * Copyright 2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.ajax4jsf.renderers.ajax;

import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.servlet.ServletResponse;

import org.ajax4jsf.framework.ajax.AjaxContainer;
import org.ajax4jsf.framework.ajax.AjaxViewRoot;
import org.ajax4jsf.framework.ajax.xmlfilter.BaseFilter;
import org.ajax4jsf.framework.renderer.AjaxContainerRenderer;
import org.ajax4jsf.framework.renderer.AjaxRendererUtils;
import org.ajax4jsf.framework.renderer.RendererUtils.HTML;


/**
 * @author shura
 *
 * Render full Html page for AJAX view. 
 * Facet "head" rendered in <head> of page.
 */
public class AjaxPageRenderer extends AjaxContainerRenderer {

    public static final String RENDERER_TYPE = "org.ajax4jsf.components.AjaxPageRenderer";
    
    private static Map doctypes;
    
    static {
    	// Fill doctype, content-type and namespace map for different formats.
    	doctypes= new HashMap();
    	doctypes.put("html-transitional", new String[]{"\n","text/html",null});
    	doctypes.put("html", new String[]{"\n","text/html",null});
    	doctypes.put("html-frameset", new String[]{"\n","text/html",null});
    	doctypes.put("xhtml", new String[]{"\n","application/xhtml+xml","http://www.w3.org/1999/xhtml"});
    	doctypes.put("xhtml-transitional", new String[]{"\n","application/xhtml+xml","http://www.w3.org/1999/xhtml"});
    	doctypes.put("xhtml-frameset", new String[]{"\n","application/xhtml+xml","http://www.w3.org/1999/xhtml"});
    	doctypes.put("html-3.2", new String[]{"\n","text/html",null});
    }
    
//    private PreparedTemplate pageStyles = HtmlCompiler.compileResource("com/exadel/vcp/renderers/templates/ajax/page-styles.xml");
    protected void preEncodeBegin(FacesContext context, UIComponent component) throws IOException {
    	// Scripts must be encoded in component, not before html element
    }
    
    /* (non-Javadoc)
     * @see javax.faces.render.Renderer#encodeBegin(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
     */
    public void doEncodeBegin(ResponseWriter out,FacesContext context, UIComponent component)
            throws IOException {
        AjaxContainer ajax = (AjaxContainer) component;
//        ServletResponse response = (ServletResponse) context.getExternalContext().getResponse();
        Map attributes = component.getAttributes();
        String format = (String) attributes.get("format");
		String contentType = null;
		String namespace = null;
//        String characterEncoding = out.getCharacterEncoding();
		if(null != format){
			String[] docType = (String[]) doctypes.get(format);
			if(null != docType){
				contentType = docType[1];
				namespace = docType[2];
				out.write(docType[0]);
			}
		}
        if(null == contentType) {
        	contentType = (String) attributes.get("contentType");
        }
        if(null != contentType) {
//			response.setContentType(contentType /*+ ";charset=" + characterEncoding*/);        	
        }
        // TODO - create "format" attribute and insert properly DOCTYPE declaratiom
        out.startElement("html",component);
        if(null == namespace){
        	namespace = (String) attributes.get("namespace");
        }
        if(null != namespace){
        	out.writeAttribute("xmlns", namespace, "namespace");
        }
        // TODO - html attributes. lang - from current locale ?
        Locale locale = context.getViewRoot().getLocale();
        out.writeAttribute(HTML.lang_ATTRIBUTE,locale.toString(),"lang");
        if ( ! ajax.isAjaxRequest()) {
            out.startElement("head",component);
        	// Out title - requied html element. 
            Object title =  attributes.get("pageTitle");
        	String viewId = context.getViewRoot().getViewId();
            if(null == title) {
				title=viewId;//use viewId for empty title
            }
            out.startElement(HTML.title_ELEM, component);
            out.writeText(title, "pageTitle");
            out.endElement(HTML.title_ELEM);
            // Page base - set to current view action url.
//            String pageBase = context.getApplication().getViewHandler().getActionURL(context, viewId);
//            out.startElement("base", component);
//            out.writeURIAttribute("href", pageBase, "pageBase");
//            out.endElement("base");            	
//            pageStyles.encode(this, context, component);
            UIComponent headFacet = component.getFacet("head");
            if(headFacet != null){
//                context.getExternalContext().log("Render head facet in AJAX Page");
                renderChild(context,headFacet);
            }
            if(null != contentType){
            	out.startElement("meta", component);
            	out.writeAttribute("http-equiv", "Content-Type", null);
            	out.writeAttribute("content", contentType /*+ ";charset=" + characterEncoding*/, null);
            	out.endElement("meta");
            }
    		if ((null == context.getExternalContext().getRequestMap().get(
    				BaseFilter.RESPONSE_WRAPPER_ATTRIBUTE))){
    			// Filter not used - encode scripts and CSS before component.
    			encodeResourcesArray(context, component, getScripts());
    			encodeResourcesArray(context, component, getStyles());    			
    		}
            out.endElement("head");
        }
        out.startElement("body",component);
        getUtils().encodePassThru(context,component);
        getUtils().encodeAttributesFromArray(context,component,HTML.PASS_THRU_STYLES);
        // TODO - special body attributes :
        getUtils().encodeAttribute(context, component, "onload");
        getUtils().encodeAttribute(context, component, "onunload");
        // onload, onunload
    }
    /* (non-Javadoc)
     * @see javax.faces.render.Renderer#encodeEnd(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
     */
    public void doEncodeEnd(ResponseWriter out,FacesContext context, UIComponent component)
            throws IOException {
        out.endElement("body");
        out.endElement("html");
//        DebugUtils.traceView("ViewRoot in AJAX Page encode end");    
        }
    
	/* (non-Javadoc)
	 * @see org.ajax4jsf.framework.renderer.AjaxContainerRenderer#encodeChildren(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
	 */
	public void encodeChildren(FacesContext context, UIComponent component) throws IOException {
		if(! AjaxRendererUtils.isAjaxRequest(context)) {
			super.encodeChildren(context,component);
		} else {
        UIComponent submittedComponent = ((AjaxViewRoot) context.getViewRoot()).getSubmittedRegion(context);
        // Page control all output instead of viewroot.
        if (null == submittedComponent || component == submittedComponent ) {
    		encodeAjax(context, component);
		} else {
			((AjaxContainer) submittedComponent).encodeAjax(context);
		}
		}
	}

	/* (non-Javadoc)
	 * For ajax requests, ViewRoot render childrens directly.
	 * @see org.ajax4jsf.framework.renderer.AjaxContainerRenderer#getRendersChildren()
	 */
	public boolean getRendersChildren() {
		FacesContext context = FacesContext.getCurrentInstance();
		// For non Ajax request, view root not render children 
		if(! AjaxRendererUtils.isAjaxRequest(context)) {
			return false;
		}
		// Ajax Request. Control all output.
		return true;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy