org.ajax4jsf.framework.renderer.HeaderResourcesRendererBase Maven / Gradle / Ivy
/**
* 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.renderer;
import java.io.IOException;
import java.util.LinkedHashSet;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import org.ajax4jsf.framework.ajax.xmlfilter.BaseFilter;
import org.ajax4jsf.framework.resource.InternetResource;
/**
* Base renderer for components used JavaScripts and Styles in header. In real
* class override getScripts or/and getStyles methods
*
* @author [email protected] (latest modification by $Author: alexsmirnov $)
* @version $Revision: 1.6 $ $Date: 2006/10/24 18:59:35 $
*
*/
public abstract class HeaderResourcesRendererBase extends RendererBase
implements HeaderResourceProducer {
/*
* (non-Javadoc)
*
* @see org.ajax4jsf.framework.renderer.HeaderResourceProducer#getHeaderScripts(javax.faces.context.FacesContext)
*/
public LinkedHashSet getHeaderScripts(FacesContext context,
UIComponent component) {
return getUrisSet(context, getScripts(), component);
}
/**
* Hoock method to return array of scripts resources to store in head.
*
* @return
*/
protected InternetResource[] getScripts() {
// TODO Auto-generated method stub
return null;
}
/*
* (non-Javadoc)
*
* @see org.ajax4jsf.framework.renderer.HeaderResourceProducer#getHeaderStyles(javax.faces.context.FacesContext)
*/
public LinkedHashSet getHeaderStyles(FacesContext context,
UIComponent component) {
return getUrisSet(context, getStyles(), component);
}
/**
* Hoock method to return array of styles resources to store in head
*
* @return
*/
protected InternetResource[] getStyles() {
// TODO Auto-generated method stub
return null;
}
/**
* @param context
* @param resources
* @param component
* TODO
* @return
*/
private LinkedHashSet getUrisSet(FacesContext context,
InternetResource[] resources, UIComponent component) {
if (null != resources) {
LinkedHashSet uris = new LinkedHashSet(); // Collections.singleton(ajaxScript.getUri(context,
// null));
for (int i = 0; i < resources.length; i++) {
InternetResource resource = resources[i];
uris.add(resource.getUri(context, component));
}
return uris;
}
return null;
}
/*
* (non-Javadoc)
*
* @see org.ajax4jsf.framework.renderer.RendererBase#preEncodeBegin(javax.faces.context.FacesContext,
* javax.faces.component.UIComponent)
*/
protected void preEncodeBegin(FacesContext context, UIComponent component)
throws IOException {
if ((null == context.getExternalContext().getRequestMap().get(
BaseFilter.RESPONSE_WRAPPER_ATTRIBUTE))
/*&& (!AjaxRendererUtils.isAjaxRequest(context))*/) {
// Filter not used - encode scripts and CSS before component.
encodeResourcesArray(context, component, getScripts());
encodeResourcesArray(context, component, getStyles());
}
}
/**
* @param context
* @param component
* @param scripts
* @throws IOException
*/
protected void encodeResourcesArray(FacesContext context, UIComponent component, InternetResource[] scripts) throws IOException {
if (scripts != null) {
for (int i = 0; i < scripts.length; i++) {
scripts[i].encode(context,component);
}
}
}
}