Author: nbelaevski
Date: 2008-07-17 09:00:42 -0400 (Thu, 17 Jul 2008)
New Revision: 9653
Removed:
trunk/framework/impl/src/main/java/org/ajax4jsf/context/ViewResources.java
Modified:
trunk/framework/impl/src/main/java/org/ajax4jsf/application/AjaxViewHandler.java
trunk/framework/impl/src/main/java/org/ajax4jsf/context/AjaxContextImpl.java
Log:
Duplicate resources-handling code removed
Modified:
trunk/framework/impl/src/main/java/org/ajax4jsf/application/AjaxViewHandler.java
===================================================================
---
trunk/framework/impl/src/main/java/org/ajax4jsf/application/AjaxViewHandler.java 2008-07-17
13:00:34 UTC (rev 9652)
+++
trunk/framework/impl/src/main/java/org/ajax4jsf/application/AjaxViewHandler.java 2008-07-17
13:00:42 UTC (rev 9653)
@@ -193,7 +193,6 @@
} else {
super.renderView(context, root);
}
- ajaxContext.processHeadResources(context);
}
/**
Modified: trunk/framework/impl/src/main/java/org/ajax4jsf/context/AjaxContextImpl.java
===================================================================
---
trunk/framework/impl/src/main/java/org/ajax4jsf/context/AjaxContextImpl.java 2008-07-17
13:00:34 UTC (rev 9652)
+++
trunk/framework/impl/src/main/java/org/ajax4jsf/context/AjaxContextImpl.java 2008-07-17
13:00:42 UTC (rev 9653)
@@ -32,18 +32,13 @@
import java.util.Locale;
import java.util.Map;
import java.util.Set;
-import java.util.regex.Pattern;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
-import javax.el.ExpressionFactory;
-import javax.el.ValueExpression;
import javax.faces.FacesException;
import javax.faces.FactoryFinder;
-import javax.faces.application.Application;
import javax.faces.application.StateManager;
import javax.faces.application.ViewHandler;
-import javax.faces.application.StateManager.SerializedView;
import javax.faces.component.NamingContainer;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
@@ -62,15 +57,8 @@
import org.ajax4jsf.renderkit.AjaxRendererUtils;
import org.ajax4jsf.renderkit.RendererUtils;
import org.ajax4jsf.renderkit.RendererUtils.HTML;
-import org.ajax4jsf.resource.InternetResourceBuilder;
-import org.ajax4jsf.resource.ResourceNotFoundException;
-import org.ajax4jsf.util.ELUtils;
-import org.ajax4jsf.webapp.BaseFilter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.richfaces.skin.Skin;
-import org.richfaces.skin.SkinFactory;
-import org.richfaces.skin.SkinNotFoundException;
/**
* This class incapsulated
@@ -83,20 +71,8 @@
public static final String SERVLET_ERROR_EXCEPTION_ATTRIBUTE =
"javax.servlet.error.exception";
- public static final String RESOURCES_PROCESSED =
"org.ajax4jsf.framework.HEADER_PROCESSED";
-
- //we do not apply extended CSS styling for control for Opera & Safari
- //as they have their own advanced styling
- private static final Pattern USER_AGENTS = Pattern.compile(" AppleWebKit/|^Opera/|
Opera ");
-
- private static final String INIT_PARAMETER_PREFIX = "_init_parameter_";
-
- //Object to put into application map as a value of cached parameter if it is null
- private static final Object NULL = new Object();
-
private static final Log log = LogFactory.getLog(AjaxContext.class);
-
Set<String> ajaxAreasToRender = new HashSet<String>();
Set<String> ajaxAreasToProcess = null;
@@ -267,237 +243,6 @@
out.endElement(HTML.HTML_ELEMENT);
}
- /**
- * @param context
- * @param root
- * @throws FacesException
- */
- public void processHeadResources(FacesContext context)
- throws FacesException {
- ExternalContext externalContext = context.getExternalContext();
- Map<String,Object> requestMap = externalContext.getRequestMap();
- if (!Boolean.TRUE.equals(requestMap.get(RESOURCES_PROCESSED))) {
- if (null != requestMap.get(BaseFilter.RESPONSE_WRAPPER_ATTRIBUTE)) {
- if (log.isDebugEnabled()) {
- log
- .debug("Process component tree for collect used scripts and styles");
- }
- UIViewRoot root = context.getViewRoot();
- ViewResources viewResources = new ViewResources();
- String skinStyleSheetUri = null;
- String skinExtendedStyleSheetUri = null;
- try {
- Skin skin = SkinFactory.getInstance().getSkin(context);
- // Set default style sheet for current skin.
- skinStyleSheetUri = (String) skin.getParameter(context,
- Skin.generalStyleSheet);
- // Set default style sheet for current skin.
- skinExtendedStyleSheetUri = (String) skin.getParameter(context,
- Skin.extendedStyleSheet);
- // For a "NULL" skin, do not collect components stylesheets
- if ("false".equals(skin.getParameter(context,
- Skin.loadStyleSheets))) {
- viewResources.setProcessStyles(false);
- }
- } catch (SkinNotFoundException e) {
- log.warn("Current Skin is not found", e);
- }
- InternetResourceBuilder internetResourceBuilder = InternetResourceBuilder
- .getInstance();
- // Check init parameters for a resources processing.
- String scriptStrategy = externalContext
- .getInitParameter(InternetResourceBuilder.LOAD_SCRIPT_STRATEGY_PARAM);
- if (null != scriptStrategy) {
- if (InternetResourceBuilder.LOAD_NONE
- .equals(scriptStrategy)) {
- viewResources.setProcessScripts(false);
- } else if (InternetResourceBuilder.LOAD_ALL
- .equals(scriptStrategy)) {
- viewResources.setProcessScripts(false);
- // For an "ALL" strategy, it is not necessary to load scripts in the ajax
request
- if (!this.isAjaxRequest(context)) {
- try {
- viewResources
- .addScript(internetResourceBuilder
- .createResource(
- this,
- InternetResourceBuilder.COMMON_FRAMEWORK_SCRIPT)
- .getUri(context, null));
- viewResources
- .addScript(internetResourceBuilder
- .createResource(
- this,
- InternetResourceBuilder.COMMON_UI_SCRIPT)
- .getUri(context, null));
-
- } catch (ResourceNotFoundException e) {
- if (log.isWarnEnabled()) {
- log
- .warn("No aggregated javaScript library found "
- + e.getMessage());
- }
- }
-
- }
- }
- }
-
- boolean useStdControlsSkinning = false;
-
- String stdControlsSkinning = getInitParameterValue(context,
InternetResourceBuilder.STD_CONTROLS_SKINNING_PARAM);
- if (stdControlsSkinning != null) {
- useStdControlsSkinning =
InternetResourceBuilder.ENABLE.equals(stdControlsSkinning);
- }
-
- boolean useStdControlsSkinningClasses = true;
-
- String stdControlsSkinningClasses = getInitParameterValue(context,
InternetResourceBuilder.STD_CONTROLS_SKINNING_CLASSES_PARAM);
- if (stdControlsSkinningClasses != null) {
- useStdControlsSkinningClasses =
InternetResourceBuilder.ENABLE.equals(stdControlsSkinningClasses);
- }
-
- boolean useExtendedSkinning = isExtendedSkinningEnabled(externalContext);
-
- String styleStrategy = externalContext
- .getInitParameter(InternetResourceBuilder.LOAD_STYLE_STRATEGY_PARAM);
-
- if (InternetResourceBuilder.LOAD_NONE.equals(styleStrategy)) {
- viewResources.setProcessStyles(false);
- } else if (InternetResourceBuilder.LOAD_ALL
- .equals(styleStrategy)) {
- viewResources.setProcessStyles(false);
- // For an "ALL" strategy, it is not necessary to load styles
- // in the ajax request
- if (!this.isAjaxRequest(context)) {
- String commonStyle = InternetResourceBuilder.COMMON_STYLE_PREFIX;
-
- if (useStdControlsSkinning
- || useStdControlsSkinningClasses) {
- if (useExtendedSkinning) {
- commonStyle += "-ext";
- } else {
- commonStyle += "-bas";
- }
-
- if (useStdControlsSkinning
- && useStdControlsSkinningClasses) {
- commonStyle += "-both";
- } else if (useStdControlsSkinning) {
- commonStyle += "-styles";
- } else if (useStdControlsSkinningClasses) {
- commonStyle += "-classes";
- }
- }
-
- commonStyle += InternetResourceBuilder.COMMON_STYLE_EXTENSION;
-
- try {
- viewResources.addStyle(internetResourceBuilder
- .createResource(this, commonStyle).getUri(
- context, null));
-
- } catch (ResourceNotFoundException e) {
- if (log.isWarnEnabled()) {
- log.warn("No aggregated stylesheet found "
- + e.getMessage());
- }
- }
-
- }
- } else {
- if (useStdControlsSkinning) {
- viewResources.addStyle(
- internetResourceBuilder.createResource(
- this, InternetResourceBuilder.STD_CONTROLS_BASIC_STYLE)
- .getUri(context, null));
-
- if (useExtendedSkinning) {
- viewResources.addStyle(
- internetResourceBuilder.createResource(
- this, InternetResourceBuilder.STD_CONTROLS_EXTENDED_STYLE)
- .getUri(context, null));
- }
- }
-
- if (useStdControlsSkinningClasses) {
- viewResources.addStyle(
- internetResourceBuilder.createResource(
- this, InternetResourceBuilder.STD_CONTROLS_BASIC_CLASSES_STYLE)
- .getUri(context, null));
-
- if (useExtendedSkinning) {
- viewResources.addStyle(
- internetResourceBuilder.createResource(
- this, InternetResourceBuilder.STD_CONTROLS_EXTENDED_CLASSES_STYLE)
- .getUri(context, null));
- }
- }
- }
-
- viewResources.collect(context);
- Set<String> scripts = viewResources.getScripts();
- if (scripts.size() > 0) {
- if (log.isDebugEnabled()) {
- StringBuffer buff = new StringBuffer(
- "Scripts for insert into head : \n");
- for (Iterator<String> iter = scripts.iterator(); iter.hasNext();) {
- String script = iter.next();
- buff.append(script).append("\n");
- }
- log.debug(buff.toString());
- }
- requestMap.put(SCRIPTS_PARAMETER, scripts);
- }
-
- Set<String> styles = viewResources.getStyles();
- // Append Skin StyleSheet after a
- if (null != skinStyleSheetUri) {
- String resourceURL = context.getApplication()
- .getViewHandler().getResourceURL(context,
- skinStyleSheetUri);
- styles.add(resourceURL);
- }
-
- if (null != skinExtendedStyleSheetUri && useExtendedSkinning) {
- String resourceURL =
context.getApplication().getViewHandler().getResourceURL(context,
- skinExtendedStyleSheetUri);
- styles.add(resourceURL);
- }
- if (styles.size() > 0) {
- if (log.isDebugEnabled()) {
- StringBuffer buff = new StringBuffer(
- "Styles for insert into head : \n");
- for (Iterator<String> iter = styles.iterator(); iter.hasNext();) {
- String style = (String) iter.next();
- buff.append(style).append("\n");
- }
- log.debug(buff.toString());
- }
- requestMap.put(STYLES_PARAMETER, styles);
- }
-
- Set<String> usersStyles = viewResources.getUserStyles();
- if (usersStyles.size() > 0) {
- if (log.isDebugEnabled()) {
- StringBuffer buff = new StringBuffer(
- "User styles for insert into head : \n");
- for (Iterator<String> iter = usersStyles.iterator(); iter.hasNext();) {
- String style = (String) iter.next();
- buff.append(style).append("\n");
- }
- log.debug(buff.toString());
- }
- requestMap.put(USER_STYLES_PARAMETER, usersStyles);
- }
- // Mark as processed.
- requestMap.put(RESOURCES_PROCESSED, Boolean.TRUE);
- // Save viewId for a parser selection
- requestMap.put(AjaxViewHandler.VIEW_ID_KEY, root.getViewId());
- }
-
- }
- }
-
public void saveViewState(FacesContext context) throws IOException {
ResponseWriter writer = context.getResponseWriter();
StateManager stateManager = context.getApplication().getStateManager();
@@ -806,74 +551,4 @@
this.oncomplete = oncomplete;
}
- private boolean isExtendedSkinningEnabled(ExternalContext context) {
- String userAgent = context.getRequestHeaderMap().get("User-Agent");
- if (userAgent != null) {
- boolean apply = !USER_AGENTS.matcher(userAgent).find();
-
- if (log.isDebugEnabled()) {
- log.debug("Got User-Agent: " + userAgent);
- log.debug("Applying extended CSS controls styling = " + apply);
- }
-
- return apply;
- } else {
- if (log.isDebugEnabled()) {
- log.debug("User-Agent is null, applying extended CSS controls styling");
- }
-
- return true;
- }
- }
-
- private String evaluate(FacesContext context, Object parameterValue) {
- if (parameterValue == NULL || parameterValue == null) {
- return null;
- } else if (parameterValue instanceof ValueExpression) {
- ValueExpression expression = (ValueExpression) parameterValue;
-
- return (String) expression.getValue(context.getELContext());
- } else {
- return parameterValue.toString();
- }
- }
-
- private String getInitParameterValue(FacesContext context, String parameterName) {
-
- String key = INIT_PARAMETER_PREFIX + parameterName;
-
- ExternalContext externalContext = context.getExternalContext();
- Map<String, Object> applicationMap = externalContext.getApplicationMap();
- Object mutex = externalContext.getRequest();
- Object parameterValue = null;
-
- synchronized (mutex) {
- parameterValue = applicationMap.get(key);
-
- if (parameterValue == null) {
-
- String initParameter = externalContext.getInitParameter(parameterName);
- if (initParameter != null) {
-
- if (ELUtils.isValueReference(initParameter)) {
- Application application = context.getApplication();
- ExpressionFactory expressionFactory = application.getExpressionFactory();
-
- parameterValue = expressionFactory.createValueExpression(context.getELContext(),
- initParameter,
- String.class);
- } else {
- parameterValue = initParameter;
- }
-
- } else {
- parameterValue = NULL;
- }
-
- applicationMap.put(key, parameterValue);
- }
- }
-
- return evaluate(context, parameterValue);
- }
}
Deleted: trunk/framework/impl/src/main/java/org/ajax4jsf/context/ViewResources.java
===================================================================
--- trunk/framework/impl/src/main/java/org/ajax4jsf/context/ViewResources.java 2008-07-17
13:00:34 UTC (rev 9652)
+++ trunk/framework/impl/src/main/java/org/ajax4jsf/context/ViewResources.java 2008-07-17
13:00:42 UTC (rev 9653)
@@ -1,173 +0,0 @@
-/**
- *
- */
-package org.ajax4jsf.context;
-
-import java.util.Iterator;
-import java.util.LinkedHashSet;
-import java.util.Set;
-
-import javax.faces.FactoryFinder;
-import javax.faces.component.UIComponent;
-import javax.faces.component.UIViewRoot;
-import javax.faces.context.FacesContext;
-import javax.faces.render.RenderKit;
-import javax.faces.render.RenderKitFactory;
-import javax.faces.render.Renderer;
-
-import org.ajax4jsf.renderkit.HeaderResourceProducer;
-import org.ajax4jsf.renderkit.UserResourceRenderer;
-
-/**
- * @author asmirnov
- *
- */
-public class ViewResources {
-
- LinkedHashSet<String> scripts = new LinkedHashSet<String>();
- LinkedHashSet<String> styles = new LinkedHashSet<String>();
- LinkedHashSet<String> userScripts = new LinkedHashSet<String>();
- LinkedHashSet<String> userStyles = new LinkedHashSet<String>();
- boolean processStyles = true;
- boolean processScripts = true;
- RenderKit renderKit = null;
-
- public void collect(FacesContext context) {
- UIViewRoot root = context.getViewRoot();
- RenderKitFactory rkFactory = (RenderKitFactory) FactoryFinder
- .getFactory(FactoryFinder.RENDER_KIT_FACTORY);
- renderKit = rkFactory.getRenderKit(context, context
- .getViewRoot().getRenderKitId());
- processHeadResources(context, root);
- //styles.addAll(userStyles);
- scripts.addAll(userScripts);
- }
-
- /**
- * Append nessesary scripts and styles from component ( if renderer
- * implements {@link HeaderResourceProducer}) and recursive process all
- * facets and childrens.
- *
- * @param context
- * TODO
- * @param root
- */
- private void processHeadResources(FacesContext context, UIComponent root) {
- Renderer renderer = getRenderer(context, root);
- if (null != renderer) {
- if ((processScripts || processStyles ) && renderer instanceof
HeaderResourceProducer) {
- HeaderResourceProducer producer = (HeaderResourceProducer) renderer;
- if (processScripts) {
- Set<String> set = producer.getHeaderScripts(context, root);
- if (null != set) {
- scripts.addAll(set);
- }
-
- }
- if (processStyles) {
- Set<String> set = producer.getHeaderStyles(context, root);
- if (null != set) {
- styles.addAll(set);
- }
-
- }
- } else if (renderer instanceof UserResourceRenderer) {
- UserResourceRenderer producer = (UserResourceRenderer) renderer;
- Set<String> set = producer.getHeaderScripts(context, root);
- if (null != set) {
- userScripts.addAll(set);
- }
- set = producer.getHeaderStyles(context, root);
- if (null != set) {
- userStyles.addAll(set);
- }
- }
-
- }
- for (Iterator<UIComponent> iter = root.getFacets().values().iterator(); iter
- .hasNext();) {
- UIComponent child = iter.next();
- processHeadResources(context, child);
- }
- for (Iterator<UIComponent> iter = root.getChildren().iterator(); iter.hasNext();)
{
- UIComponent child = iter.next();
- processHeadResources(context, child);
- }
- }
-
- /**
- * Find renderer for given component.
- *
- * @param context
- * @param comp
- * @param renderKit
- * @return
- */
- private Renderer getRenderer(FacesContext context, UIComponent comp) {
-
- String rendererType = comp.getRendererType();
- if (rendererType != null) {
- return (renderKit.getRenderer(comp.getFamily(), rendererType));
- } else {
- return (null);
- }
-
- }
-
- /**
- * @return the processStyles
- */
- public boolean isProcessStyles() {
- return processStyles;
- }
-
- /**
- * @param processStyles the processStyles to set
- */
- public void setProcessStyles(boolean processStyles) {
- this.processStyles = processStyles;
- }
-
- /**
- * @return the processScripts
- */
- public boolean isProcessScripts() {
- return processScripts;
- }
-
- /**
- * @param processScripts the processScripts to set
- */
- public void setProcessScripts(boolean processScripts) {
- this.processScripts = processScripts;
- }
-
- /**
- * @return the scripts
- */
- public Set<String> getScripts() {
- return scripts;
- }
-
- /**
- * @return the styles
- */
- public Set<String> getStyles() {
- return styles;
- }
-
- /**
- * @return user styles
- */
- public Set<String> getUserStyles() {
- return userStyles;
- }
-
- public void addScript(String scriptUrl) {
- scripts.add(scriptUrl);
- }
-
- public void addStyle(String styleUrl) {
- styles.add(styleUrl);
- }
-}