
com.adobe.granite.ui.components.ExpressionHelper Maven / Gradle / Ivy
/*************************************************************************
*
* ADOBE CONFIDENTIAL
* __________________
*
* Copyright 2013 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
**************************************************************************/
package com.adobe.granite.ui.components;
import java.util.Locale;
import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestWrapper;
import javax.servlet.jsp.PageContext;
import org.apache.sling.api.SlingHttpServletRequest;
/**
* A helper to deal with EL easier.
*/
public class ExpressionHelper {
private ExpressionResolver resolver;
private SlingHttpServletRequest request;
public ExpressionHelper(ExpressionResolver resolver, PageContext pageContext) {
this(resolver, toSlingRequest(pageContext.getRequest()));
}
public ExpressionHelper(ExpressionResolver resolver, SlingHttpServletRequest servletRequest) {
this.resolver = resolver;
this.request = servletRequest;
}
/**
* Resolves the given expression as string. Request's locale will be used.
* @param expression the expression to be resolved
* @return the resolved expression as a string
*/
public String getString(String expression) {
return get(expression, String.class);
}
/**
* Resolves the given expression as string, with the given locale.
* @param expression the expression to be resolved
* @param locale the locale
* @return the resolved expression as a string
*/
public String getString(String expression, Locale locale) {
return get(expression, locale, String.class);
}
/**
* Resolves the given expression as boolean. Request's locale will be used.
* @param expression the expression to be resolved
* @return the resolved expression as a boolean
*/
public boolean getBoolean(String expression) {
return get(expression, Boolean.class);
}
/**
* Resolves the given expression as boolean, with the given locale.
* @param expression the expression to be resolved
* @param locale the locale
* @return the resolved expression as a boolean
*/
public boolean getBoolean(String expression, Locale locale) {
return get(expression, locale, Boolean.class);
}
/**
* Resolves the given expression. Request's locale will be used.
* @param expression the expression to be resolved
* @param expectedType the expected type of the resolved expression
* @param the type of the resolved expression
* @return the resolved expression
*/
public T get(String expression, Class expectedType) {
return get(expression, request.getLocale(), expectedType);
}
/**
* Resolves the given expression, with the given locale.
* @param expression the expression to be resolved
* @param locale the locale
* @param expectedType the expected type of the resolved expression
* @param the type of the resolved expression
* @return the resolved expression
*/
public T get(String expression, Locale locale, Class expectedType) {
if (expression == null) return null;
return resolver.resolve(expression, locale, expectedType, request);
}
private static SlingHttpServletRequest toSlingRequest(ServletRequest req) {
while (!(req instanceof SlingHttpServletRequest)) {
if (req instanceof ServletRequestWrapper) {
req = ((ServletRequestWrapper) req).getRequest();
} else {
throw new IllegalStateException("request wrong class");
}
}
return (SlingHttpServletRequest) req;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy