package com.nesterovskyBros; import java.util.AbstractList; import java.util.Collections; import java.util.List; import javax.el.ValueExpression; import javax.faces.context.FacesContext; /** * JSF functions used in the application. */ public class Functions { /** * Resolves resource url. * * @param path resource path, possibly relative. * @return resolved path. */ public static String url(String path) throws Exception { FacesContext facesContext = FacesContext.getCurrentInstance(); return facesContext.getApplication().getViewHandler(). getResourceURL(facesContext, path); } /** * A factory of {@link ScopeComponent} instances. */ public static class ScopeProvider { /** * Gets an instance of {@link ScopeComponent}. * @return an instance of {@link ScopeComponent}. */ public ScopeComponent getValue() { if (value == null) { value = new ScopeComponent(); } return value; } /** * Sets a value of {@link ScopeComponent}. * @param value a value of {@link ScopeComponent}. */ public void setValue(ScopeComponent value) { this.value = value; } /** * A scope component. */ private ScopeComponent value; } /** *

Creates a scope component.

*

{@link ScopeComponent} introduces itself into the databinding scope * using value of id attribute as name, allowing to acquire its clientId.

*

* <f:subview id="name" binding="#{ex:scope()}"> * ... * #{name.childId}:item * <f:subview> *

*

The other feature is uniqueId attribute, which is used to output * something only once. uniqueId is used as a discriminator. *

* <f:subview uniqueId="my-script-id" binding="ex:scope()"> * <script ... * <f:subview> *

* @return an instance of {@link ScopeComponent}. */ public static Object scope() throws Exception { return new ScopeProvider(); } /** * Returns a list of integers with values from 0 and up to size not including. * @param size a size of list. * @return a list of integers. */ public static List sequence(final int size) throws Exception { return new AbstractList() { @Override public Integer get(int index) { return index; } @Override public int size() { return size; } }; } /** * Returns a list of indices of a specified list. * @param list a list to get indices for. * @return a list of integers. */ public static List indices(List list) throws Exception { if (list == null) { return Collections.emptyList(); } return sequence(list.size()); } /** * Returns a value for the value expression. * @param value a value expression to get value for. * @return a a value for the value expression. */ public static Object value(ValueExpression value) throws Exception { if (value == null) { return null; } return value.getValue(FacesContext.getCurrentInstance().getELContext()); } }