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

org.ajax4jsf.framework.resource.ClientScript 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!
/**
 * Licensed under the Common Development and Distribution License,
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.sun.com/cddl/
 *   
 * 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.framework.resource;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.ajax4jsf.framework.util.message.Messages;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * Resource for AJAX client-side script. Render one time per page.
 * @author [email protected] (latest modification by $Author: alexsmirnov $)
 * @version $Revision: 1.6 $ $Date: 2006/09/26 18:56:29 $
 *
 */
public abstract class ClientScript extends JarResource {

	private static final Log log = LogFactory.getLog(ClientScript.class);
	
	protected boolean usePrototype = false;
	/**
	 * Set JavaScript renderer and modification time to application-startup time.
	 */
	public ClientScript() {
		super();
		setRenderer(new ScriptRenderer());
		InternetResourceBuilder resourceBuilder = InternetResourceBuilder.getInstance();
		String key = this.getClass().getName();
		try {
			// Search already registered resource for this class.
			InternetResource resource = resourceBuilder.getResource(key);
			this.setKey(resource.getKey());
		} catch(ResourceNotFoundException ex){
			// If script not registered, append it to builder.
			setLastModified(new Date(resourceBuilder.getStartTime()));
			resourceBuilder.addResource(key,this);
		}
		String script ;
		if (getJavaScript().startsWith("/")) {
			script = getJavaScript();
		} else {
			script = this.getClass().getPackage().getName().replace('.', '/')+"/"
			+ getJavaScript();			
		}
		setPath(script);
	}
	
	
	/* (non-Javadoc)
	 * @see org.ajax4jsf.framework.resource.InternetResourceBase#getLastModified()
	 */
	public Date getLastModified() {

		if (isCacheable()) {
			return super.getLastModified();
		} else {
			return new Date(System.currentTimeMillis()+1000L);
		}
	}


	/* (non-Javadoc)
	 * @see org.ajax4jsf.framework.resource.InternetResourceBase#isCacheable()
	 */
	public boolean isCacheable() {
		return true;
	}

	/**
	 * @return Returns the javaScript.
	 */
	public abstract String getJavaScript();

	/* (non-Javadoc)
	 * @see org.ajax4jsf.framework.resource.InternetResourceBase#encode(javax.faces.context.FacesContext, java.lang.Object, java.util.Map)
	 */
	public void encode(FacesContext context, Object data, Map attributes) throws IOException {
		if (isNotAjaxRequest(context)) {
			encodePrototype(context);
			super.encode(context, data, attributes);
		} else if (log.isDebugEnabled()) {
			log.debug(Messages.getMessage(Messages.SKIP_ENCODING_HTML_INFO, getKey()));
		}

	}

	/* (non-Javadoc)
	 * @see org.ajax4jsf.framework.resource.InternetResourceBase#encodeBegin(javax.faces.context.FacesContext, java.lang.Object, java.util.Map)
	 */
	public void encodeBegin(FacesContext context, Object component, Map attrs) throws IOException {
		if (isNotAjaxRequest(context)) {
			encodePrototype(context);
			super.encodeBegin(context, component, attrs);
		} else if (log.isDebugEnabled()) {
			log.debug(Messages.getMessage(Messages.SKIP_ENCODE_BEGIN_HTML_INFO, getKey()));
		}
	}

	/* (non-Javadoc)
	 * @see org.ajax4jsf.framework.resource.InternetResourceBase#encodeEnd(javax.faces.context.FacesContext, java.lang.Object)
	 */
	public void encodeEnd(FacesContext context, Object component) throws IOException {
		if (isNotAjaxRequest(context)) {
			super.encodeEnd(context, component);
		} else if (log.isDebugEnabled()) {
			log.debug(Messages.getMessage(Messages.SKIP_ENCODE_END_HTML_INFO, getKey()));
		}
	}
	

	/* (non-Javadoc)
	 * @see org.ajax4jsf.framework.resource.InternetResourceBase#encode(javax.faces.context.FacesContext, java.lang.Object)
	 */
	public void encode(FacesContext context, Object data) throws IOException {
		if (isNotAjaxRequest(context)) {
			encodePrototype(context);
			super.encode(context, data);
		}else if (log.isDebugEnabled()) {
			log.debug(Messages.getMessage(Messages.SKIP_ENCODING_HTML_INFO, getKey()));
		}
	}

	/**
	 * @return Returns the usePrototype.
	 */
	protected boolean isUsePrototype() {
		return usePrototype;
	}


	private boolean isNotAjaxRequest(FacesContext context){
		return true;//! AjaxRendererUtils.isAjaxRequest(context);
	}
	
	private void encodePrototype(FacesContext context) throws  IOException{
		if (isUsePrototype()) {
			InternetResourceBuilder.getInstance().createResource(null,PrototypeScript.class.getName()).encode(context,null);
		}

	}


	/**
	 * @param usePrototype the usePrototype to set
	 */
	public void setUsePrototype(boolean usePrototype) {
		this.usePrototype = usePrototype;
	}
	
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy