Skip to content

Commit

Permalink
Fix #287: add #{of:graphicImageURL()} functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Bauke Scholtz committed Jul 23, 2016
1 parent 15ed981 commit 93d8012
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 5 deletions.
76 changes: 75 additions & 1 deletion src/main/java/org/omnifaces/el/functions/Components.java
Expand Up @@ -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.
Expand Down Expand Up @@ -56,4 +62,72 @@ public static Object evalAttribute(UIComponent component, String name) {
return component.getAttributes().get(name);
}
}
}

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

/**
* <p>
* Returns <code>&lt;o:graphicImage&gt;</code> URL based on given expression string and image type.
* <p>
* Usage example:
* <pre>
* &lt;a href="#{of:graphicImageURL('bean.getFullImage(image.id)', 'png')}"&gt;
* &lt;o:graphicImage value="#{bean.getThumbnailImage(image.id)}" type="png" /&gt;
* &lt;/a&gt;
* </pre>
* @param expression Expression string representing the same value as you would use in
* <code>&lt;o:graphicImage&gt;</code>. 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 <code>null</code>.
* @return <code>&lt;o:graphicImage&gt;</code> 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);
}

/**
* <p>
* Returns <code>&lt;o:graphicImage&gt;</code> URL based on given expression string, image type and last modified.
* <p>
* Usage example:
* <pre>
* &lt;a href="#{of:graphicImageURL('bean.getFullImage(image.id)', 'png', image.lastModified)}"&gt;
* &lt;o:graphicImage value="#{bean.getThumbnailImage(image.id)}" type="png" lastModified="#{image.lastModified}" /&gt;
* &lt;/a&gt;
* </pre>
* @param expression Expression string representing the same value as you would use in
* <code>&lt;o:graphicImage&gt;</code>. 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 <code>null</code>.
* @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 <code>null</code>.
* @return <code>&lt;o:graphicImage&gt;</code> 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());
}

}
67 changes: 63 additions & 4 deletions src/main/resources/META-INF/omnifaces-functions.taglib.xml
Expand Up @@ -823,10 +823,9 @@
<function-class>org.omnifaces.el.functions.Objects</function-class>
<function-signature>java.lang.Object coalesce(java.lang.Object, java.lang.Object)</function-signature>
</function>



<!-- Components =============================================================================================== -->

<function>
<description>
<![CDATA[
Expand All @@ -840,9 +839,69 @@
<function-signature>java.lang.Object evalAttribute(javax.faces.component.UIComponent, java.lang.String)</function-signature>
</function>

<function>
<description>
<![CDATA[
Returns <code>&lt;o:graphicImage&gt;</code> URL based on given expression string.
The expression string must be a quoted string. Any nested quotes can be escaped with backslash.
<p>
Usage example:
<pre>
&lt;a href="#{of:graphicImageURL('bean.getFullImage(image.id)')}"&gt;
&lt;o:graphicImage value="#{bean.getThumbnailImage(image.id)}" /&gt;
&lt;/a&gt;
</pre>
]]>
</description>
<function-name>graphicImageURL</function-name>
<function-class>org.omnifaces.el.functions.Components</function-class>
<function-signature>java.lang.String graphicImageURL(java.lang.String)</function-signature>
</function>

<function>
<description>
<![CDATA[
Returns <code>&lt;o:graphicImage&gt;</code> 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.
<p>
Usage example:
<pre>
&lt;a href="#{of:graphicImageURL('bean.getFullImage(image.id)', 'png')}"&gt;
&lt;o:graphicImage value="#{bean.getThumbnailImage(image.id)}" type="png" /&gt;
&lt;/a&gt;
</pre>
]]>
</description>
<function-name>graphicImageURLWithType</function-name>
<function-class>org.omnifaces.el.functions.Components</function-class>
<function-signature>java.lang.String graphicImageURLWithType(java.lang.String, java.lang.String)</function-signature>
</function>

<function>
<description>
<![CDATA[
Returns <code>&lt;o:graphicImage&gt;</code> 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 <code>java.lang.Long</code>, <code>java.util.Date</code>,
or <code>java.lang.String</code> which is parseable as <code>java.lang.Long</code>.
<p>
Usage example:
<pre>
&lt;a href="#{of:graphicImageURL('bean.getFullImage(image.id)', 'png', image.lastModified)}"&gt;
&lt;o:graphicImage value="#{bean.getThumbnailImage(image.id)}" type="png" lastModified="#{image.lastModified}" /&gt;
&lt;/a&gt;
</pre>
]]>
</description>
<function-name>graphicImageURLWithTypeAndLastModified</function-name>
<function-class>org.omnifaces.el.functions.Components</function-class>
<function-signature>java.lang.String graphicImageURLWithTypeAndLastModified(java.lang.String, java.lang.String, java.lang.Object)</function-signature>
</function>

<!-- Faces (request URL/header shortcuts) ===================================================================== -->

<function>
<description>
<![CDATA[
Expand Down

0 comments on commit 93d8012

Please sign in to comment.