myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ima...@apache.org
Subject svn commit: r544554 - in /myfaces/tomahawk/trunk/sandbox/core/src/main: java/org/apache/myfaces/custom/graphicimagedynamic/GraphicImageDynamicRenderer.java tld/myfaces_sandbox.tld
Date Tue, 05 Jun 2007 17:04:10 GMT
Author: imario
Date: Tue Jun  5 10:04:09 2007
New Revision: 544554

URL: http://svn.apache.org/viewvc?view=rev&rev=544554
Log:
Allow to configure the imageRenderer as valuebinding (in addition to the ImageRenderer class
name as string).
Encode the url to get in the session id and conversationContext (eventually) into the url.

This allows the developer to use any managed bean to provide the image data.

<s:graphicImageDynamic width=".." height=".." value="#{bean.imageData}" />

imageData is a method with the following signature:

public ImageRenderer getImageData()

This works with any session, application, conversation scoped bean.
In fact it works with request scoped beans too, but then the pro to have an already initialized
bean is lost.

Modified:
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/graphicimagedynamic/GraphicImageDynamicRenderer.java
    myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/graphicimagedynamic/GraphicImageDynamicRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/graphicimagedynamic/GraphicImageDynamicRenderer.java?view=diff&rev=544554&r1=544553&r2=544554
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/graphicimagedynamic/GraphicImageDynamicRenderer.java
(original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/graphicimagedynamic/GraphicImageDynamicRenderer.java
Tue Jun  5 10:04:09 2007
@@ -19,11 +19,17 @@
 
 package org.apache.myfaces.custom.graphicimagedynamic;
 
-import java.io.IOException;
-import java.io.OutputStream;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.myfaces.component.html.util.ParameterResourceHandler;
+import org.apache.myfaces.renderkit.html.ext.HtmlImageRenderer;
+import org.apache.myfaces.renderkit.html.util.AddResource;
+import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
+import org.apache.myfaces.renderkit.html.util.ResourceLoader;
+import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
+import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
+import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
+import org.apache.myfaces.shared_tomahawk.util.ClassUtils;
 
 import javax.faces.FacesException;
 import javax.faces.FactoryFinder;
@@ -34,24 +40,18 @@
 import javax.faces.context.FacesContextFactory;
 import javax.faces.context.ResponseStream;
 import javax.faces.context.ResponseWriter;
+import javax.faces.el.ValueBinding;
 import javax.faces.lifecycle.Lifecycle;
 import javax.faces.lifecycle.LifecycleFactory;
 import javax.faces.webapp.FacesServlet;
 import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.myfaces.component.html.util.ParameterResourceHandler;
-import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
-import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
-import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
-import org.apache.myfaces.renderkit.html.ext.HtmlImageRenderer;
-import org.apache.myfaces.renderkit.html.util.AddResource;
-import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
-import org.apache.myfaces.renderkit.html.util.ResourceLoader;
-import org.apache.myfaces.shared_tomahawk.util.ClassUtils;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
 
 /**
  * @author Sylvain Vieujot
@@ -127,6 +127,7 @@
     }
 
     private static final String RENDERER_PARAM = "_renderer";
+	private static final String VALUE_PARAM = "_value";
     private static final String WIDTH_PARAM = "_width";
     private static final String HEIGHT_PARAM = "_height";
 
@@ -157,16 +158,22 @@
         }
 
         Class imageRendererClass = graphicImageDynamic.getImageRendererClass();
-        if (imageRendererClass == null)
+        if (imageRendererClass != null)
         {
-            throw new FacesException("No imageRendererClass defined for component "
-                                     + component.getId());
+			params.put(RENDERER_PARAM, imageRendererClass.getName());
         }
-        params.put(RENDERER_PARAM, imageRendererClass.getName());
 
-        AddResource addResource = AddResourceFactory.getInstance(context);
-        String url = addResource.getResourceUri(context, new ParameterResourceHandler(this
-                .getClass(), params));
+		ValueBinding imageRendererValueBinding = graphicImageDynamic.getValueBinding("value");
+		if (imageRendererValueBinding != null)
+		{
+			params.put(VALUE_PARAM, imageRendererValueBinding.getExpressionString());
+		}
+
+		AddResource addResource = AddResourceFactory.getInstance(context);
+        String url =
+			context.getExternalContext().encodeResourceURL(
+				addResource.getResourceUri(context, new ParameterResourceHandler(this
+					.getClass(), params)));
         writer.writeAttribute(HTML.SRC_ATTR, url, null);
 
         writer.endElement(HTML.IMG_ELEM);
@@ -213,46 +220,39 @@
         facesContext.setResponseStream(new ImageResponseStream(response.getOutputStream()));
         try
         {
-            Map requestMap = facesContext.getExternalContext().getRequestParameterMap();
+			ImageRenderer imageRenderer = null;
+
+			Map requestMap = facesContext.getExternalContext().getRequestParameterMap();
             Object rendererValue = requestMap.get(RENDERER_PARAM);
-            if (rendererValue == null)
-            {
-                throw new FacesException("no image renderer defined.");
-            }
-            try
-            {
-                Class rendererClass = ClassUtils.classForName(rendererValue.toString());
-                if (!ImageRenderer.class.isAssignableFrom(rendererClass))
-                {
-                    throw new FacesException("Image renderer class [" + rendererValue
-                                             + "] does not implement " + ImageRenderer.class.getName());
-                }
-                try
-                {
-                    ImageRenderer imageRenderer = (ImageRenderer) rendererClass.newInstance();
-                    renderImage(imageRenderer, facesContext);
-                }
-                catch (InstantiationException e)
-                {
-                    throw new FacesException("could not instantiate image renderer class
"
-                                             + rendererValue + " : " + e.getMessage(), e);
-                }
-                catch (IllegalAccessException e)
-                {
-                    throw new FacesException("could not instantiate image renderer class
"
-                                             + rendererValue + " : " + e.getMessage(), e);
-                }
-                catch (Exception e)
-                {
-                    throw new FacesException("could not renderer image "
-                                             + rendererValue + " : " + e.getMessage(), e);
-                }
-            }
-            catch (ClassNotFoundException e)
-            {
-                throw new FacesException("image renderer class not found: " + e.getMessage(),
e);
-            }
-            facesContext.getResponseStream().close();
+			if (rendererValue != null)
+			{
+				imageRenderer = getImageRendererFromClassName(rendererValue.toString());
+			}
+			else
+			{
+				Object rendererValueBinding = requestMap.get(VALUE_PARAM);
+				if (rendererValueBinding != null)
+				{
+					imageRenderer = getImageRendererFromValueBinding(facesContext, rendererValueBinding.toString());
+				}
+			}
+
+
+			if (imageRenderer == null)
+			{
+				throw new FacesException("no image renderer defined.");
+			}
+
+			try
+			{
+				renderImage(imageRenderer, facesContext);
+			}
+			catch (Exception e)
+			{
+				throw new FacesException("could not renderer image "
+										 + rendererValue + " : " + e.getMessage(), e);
+			}
+			facesContext.getResponseStream().close();
         }
         finally
         {
@@ -260,7 +260,45 @@
         }
     }
 
-    protected void renderImage(ImageRenderer imageRenderer, FacesContext facesContext)
+	protected ImageRenderer getImageRendererFromValueBinding(FacesContext facesContext, String
rendererValueBinding)
+	{
+		return (ImageRenderer) facesContext.getApplication().createValueBinding(rendererValueBinding.toString()).getValue(facesContext);
+	}
+
+	protected ImageRenderer getImageRendererFromClassName(String imageRendererClassName)
+	{
+		ImageRenderer imageRenderer;
+		try
+				{
+					Class rendererClass = ClassUtils.classForName(imageRendererClassName.toString());
+			if (!ImageRenderer.class.isAssignableFrom(rendererClass))
+			{
+				throw new FacesException("Image renderer class [" + imageRendererClassName
+										 + "] does not implement " + ImageRenderer.class.getName());
+			}
+			try
+			{
+				imageRenderer = (ImageRenderer) rendererClass.newInstance();
+			}
+			catch (InstantiationException e)
+			{
+				throw new FacesException("could not instantiate image renderer class "
+										 + imageRendererClassName + " : " + e.getMessage(), e);
+			}
+			catch (IllegalAccessException e)
+			{
+				throw new FacesException("could not instantiate image renderer class "
+										 + imageRendererClassName + " : " + e.getMessage(), e);
+			}
+		}
+		catch (ClassNotFoundException e)
+		{
+			throw new FacesException("image renderer class not found: " + e.getMessage(), e);
+		}
+		return imageRenderer;
+	}
+
+	protected void renderImage(ImageRenderer imageRenderer, FacesContext facesContext)
             throws Exception
     {
             ImageContext imageContext = createImageContext(facesContext);

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld?view=diff&rev=544554&r1=544553&r2=544554
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld Tue Jun  5 10:04:09
2007
@@ -676,12 +676,21 @@
 
 		<attribute>
 			<name>imageRendererClass</name>
-			<required>true</required>
+			<required>false</required>
 			<rtexprvalue>false</rtexprvalue>
 			<type>java.lang.String</type>
 			<description>
 				The class which implements org.apache.myfaces.custom.graphicimagedynamic.ImageRenderer.
The image renderer is responsible for loading the image. The class must have a default constructor.
Any request scoped attribute or managed bean is not
 				available when this image renderer is instantiated and used. The image renderer must
render the binary data for the image by using the parameters passed by nested f:param elements
and/or using session or application scoped beans.
+			</description>
+		</attribute>
+		<attribute>
+			<name>value</name>
+			<required>false</required>
+			<rtexprvalue>false</rtexprvalue>
+			<type>java.lang.String</type>
+			<description>
+				A value binding which will be called to get the instance of an org.apache.myfaces.custom.graphicimagedynamic.ImageRenderer.

 			</description>
 		</attribute>
 



Mime
View raw message