From 93d8012b1f83c5e857594cd82605d48e03629c3b Mon Sep 17 00:00:00 2001 From: Bauke Scholtz Date: Sat, 23 Jul 2016 13:37:55 +0200 Subject: [PATCH] Fix #287: add #{of:graphicImageURL()} functions --- .../omnifaces/el/functions/Components.java | 76 ++++++++++++++++++- .../META-INF/omnifaces-functions.taglib.xml | 67 +++++++++++++++- 2 files changed, 138 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/omnifaces/el/functions/Components.java b/src/main/java/org/omnifaces/el/functions/Components.java index 070be1304..25d747e4a 100644 --- a/src/main/java/org/omnifaces/el/functions/Components.java +++ b/src/main/java/org/omnifaces/el/functions/Components.java @@ -12,10 +12,16 @@ */ package org.omnifaces.el.functions; +import static org.omnifaces.util.Faces.getContext; import static org.omnifaces.util.Faces.getELContext; +import java.util.Date; + import javax.el.ValueExpression; import javax.faces.component.UIComponent; +import javax.faces.context.FacesContext; + +import org.omnifaces.resourcehandler.GraphicResource; /** * Collection of EL functions for working with components. @@ -56,4 +62,72 @@ public static Object evalAttribute(UIComponent component, String name) { return component.getAttributes().get(name); } } -} + + /** + *

+ * Returns <o:graphicImage> URL based on given expression string. + *

+ * Usage example: + *

+	 * <a href="#{of:graphicImageURL('bean.getFullImage(image.id)')}">
+	 *     <o:graphicImage value="#{bean.getThumbnailImage(image.id)}" />
+	 * </a>
+	 * 
+ * @param expression Expression string representing the same value as you would use in + * <o:graphicImage>. It must be a quoted string. Any nested quotes can be escaped with backslash. + * @return <o:graphicImage> URL based on given expression string. + * @since 2.5 + */ + public static String graphicImageURL(String expression) { + return graphicImageURLWithTypeAndLastModified(expression, null, null); + } + + /** + *

+ * Returns <o:graphicImage> URL based on given expression string and image type. + *

+ * Usage example: + *

+	 * <a href="#{of:graphicImageURL('bean.getFullImage(image.id)', 'png')}">
+	 *     <o:graphicImage value="#{bean.getThumbnailImage(image.id)}" type="png" />
+	 * </a>
+	 * 
+ * @param expression Expression string representing the same value as you would use in + * <o:graphicImage>. It must be a quoted string. Any nested quotes can be escaped with backslash. + * @param type The image type, represented as file extension. + * E.g. "jpg", "png", "gif", "ico", "svg", "bmp", "tiff", etc. This may be null. + * @return <o:graphicImage> URL based on given expression string and image type. + * @since 2.5 + */ + public static String graphicImageURLWithType(String expression, String type) { + return graphicImageURLWithTypeAndLastModified(expression, type, null); + } + + /** + *

+ * Returns <o:graphicImage> URL based on given expression string, image type and last modified. + *

+ * Usage example: + *

+	 * <a href="#{of:graphicImageURL('bean.getFullImage(image.id)', 'png', image.lastModified)}">
+	 *     <o:graphicImage value="#{bean.getThumbnailImage(image.id)}" type="png" lastModified="#{image.lastModified}" />
+	 * </a>
+	 * 
+ * @param expression Expression string representing the same value as you would use in + * <o:graphicImage>. It must be a quoted string. Any nested quotes can be escaped with backslash. + * @param type The image type, represented as file extension. + * E.g. "jpg", "png", "gif", "ico", "svg", "bmp", "tiff", etc. This may be null. + * @param lastModified The "last modified" timestamp, can be either a {@link Long}, {@link Date}, or {@link String} + * which is parseable as {@link Long}. This may be null. + * @return <o:graphicImage> URL based on given expression string, image type and last modified. + * @since 2.5 + */ + @SuppressWarnings("all") // Eclipse el-syntax. + public static String graphicImageURLWithTypeAndLastModified(String expression, String type, Object lastModified) { + FacesContext context = getContext(); + ValueExpression value = org.omnifaces.util.Components.createValueExpression("#{" + expression + "}", Object.class); + GraphicResource resource = GraphicResource.create(context, value, type, lastModified); + return context.getExternalContext().encodeResourceURL(resource.getRequestPath()); + } + +} \ No newline at end of file diff --git a/src/main/resources/META-INF/omnifaces-functions.taglib.xml b/src/main/resources/META-INF/omnifaces-functions.taglib.xml index 1123840bf..29a0e3b82 100644 --- a/src/main/resources/META-INF/omnifaces-functions.taglib.xml +++ b/src/main/resources/META-INF/omnifaces-functions.taglib.xml @@ -823,10 +823,9 @@ org.omnifaces.el.functions.Objects java.lang.Object coalesce(java.lang.Object, java.lang.Object) - - + - + java.lang.Object evalAttribute(javax.faces.component.UIComponent, java.lang.String) + + + <o:graphicImage> URL based on given expression string. + The expression string must be a quoted string. Any nested quotes can be escaped with backslash. +

+ Usage example: +

+<a href="#{of:graphicImageURL('bean.getFullImage(image.id)')}">
+    <o:graphicImage value="#{bean.getThumbnailImage(image.id)}" />
+</a>
+				
+ ]]> +
+ graphicImageURL + org.omnifaces.el.functions.Components + java.lang.String graphicImageURL(java.lang.String) +
+ + + + <o:graphicImage> URL based on given expression string and image type. + The expression string must be a quoted string. Any nested quotes can be escaped with backslash. + The image type is represented as file extension. E.g. "jpg", "png", "gif", "ico", "svg", "bmp", "tiff", etc. +

+ Usage example: +

+<a href="#{of:graphicImageURL('bean.getFullImage(image.id)', 'png')}">
+    <o:graphicImage value="#{bean.getThumbnailImage(image.id)}" type="png" />
+</a>
+				
+ ]]> +
+ graphicImageURLWithType + org.omnifaces.el.functions.Components + java.lang.String graphicImageURLWithType(java.lang.String, java.lang.String) +
+ + + + <o:graphicImage> URL based on given expression string, image type and last modified. + The expression string must be a quoted string. Any nested quotes can be escaped with backslash. + The image type is represented as file extension. E.g. "jpg", "png", "gif", "ico", "svg", "bmp", "tiff", etc. + The "last modified" timestamp can be either a java.lang.Long, java.util.Date, + or java.lang.String which is parseable as java.lang.Long. +

+ Usage example: +

+<a href="#{of:graphicImageURL('bean.getFullImage(image.id)', 'png', image.lastModified)}">
+    <o:graphicImage value="#{bean.getThumbnailImage(image.id)}" type="png" lastModified="#{image.lastModified}" />
+</a>
+				
+ ]]> +
+ graphicImageURLWithTypeAndLastModified + org.omnifaces.el.functions.Components + java.lang.String graphicImageURLWithTypeAndLastModified(java.lang.String, java.lang.String, java.lang.Object) +
- +