Example usage for javax.el ValueExpression setValue

List of usage examples for javax.el ValueExpression setValue

Introduction

In this page you can find the example usage for javax.el ValueExpression setValue.

Prototype

public abstract void setValue(ELContext context, Object value);

Source Link

Usage

From source file:org.richfaces.component.UIDatascroller.java

private void updateModel(int newPage) {
    UIData dataTable = getDataTable();//from w w  w . j  a  v a  2 s  .c o m

    if (isRendered(dataTable)) {
        dataTable.setFirst((newPage - 1) * getRows(dataTable));
    }

    Map<String, Object> attributes = dataTable.getAttributes();
    attributes.put(SCROLLER_STATE_ATTRIBUTE, newPage);

    FacesContext context = getFacesContext();
    ValueExpression ve = getValueExpression("page");
    if (ve != null) {
        try {
            ve.setValue(context.getELContext(), newPage);
            attributes.remove(SCROLLER_STATE_ATTRIBUTE);
        } catch (ELException e) {
            String messageStr = e.getMessage();
            Throwable result = e.getCause();
            while (null != result && result.getClass().isAssignableFrom(ELException.class)) {
                messageStr = result.getMessage();
                result = result.getCause();
            }
            FacesMessage message;
            if (null == messageStr) {
                message = MessageUtil.getMessage(context, UIInput.UPDATE_MESSAGE_ID,
                        new Object[] { MessageUtil.getLabel(context, this) });
            } else {
                message = new FacesMessage(FacesMessage.SEVERITY_ERROR, messageStr, messageStr);
            }
            context.getExternalContext().log(message.getSummary(), result);
            context.addMessage(getClientId(context), message);
            context.renderResponse();
        } catch (IllegalArgumentException e) {
            FacesMessage message = MessageUtil.getMessage(context, UIInput.UPDATE_MESSAGE_ID,
                    new Object[] { MessageUtil.getLabel(context, this) });
            context.getExternalContext().log(message.getSummary(), e);
            context.addMessage(getClientId(context), message);
            context.renderResponse();
        } catch (Exception e) {
            FacesMessage message = MessageUtil.getMessage(context, UIInput.UPDATE_MESSAGE_ID,
                    new Object[] { MessageUtil.getLabel(context, this) });
            context.getExternalContext().log(message.getSummary(), e);
            context.addMessage(getClientId(context), message);
            context.renderResponse();
        }
    }
}

From source file:com.jsmartframework.web.manager.ExpressionHandler.java

private void setExpressionFilePart(String expr, String jParam) throws ServletException, IOException {
    Matcher matcher = EL_PATTERN.matcher(expr);
    if (matcher.find()) {

        String beanMethod = matcher.group(1);
        String[] methodSign = beanMethod.split(Constants.EL_SEPARATOR);

        if (methodSign.length > 0 && WebContext.containsAttribute(methodSign[0])) {
            beanMethod = String.format(Constants.JSP_EL, beanMethod);

            ELContext context = WebContext.getPageContext().getELContext();
            ValueExpression valueExpr = WebContext.getExpressionFactory().createValueExpression(context,
                    beanMethod, Object.class);

            Object value = WebContext.getRequest().getPart(TagHandler.J_PART + jParam);
            valueExpr.setValue(context, value);
        }/* w  ww.  j a  v a  2s .c  o m*/
    }
}

From source file:org.nuxeo.ecm.platform.ui.web.rest.services.URLPolicyServiceImpl.java

@Override
public void applyRequestParameters(FacesContext facesContext) {
    // try to set document view
    ExpressionFactory ef = facesContext.getApplication().getExpressionFactory();
    ELContext context = facesContext.getELContext();

    HttpServletRequest httpRequest = (HttpServletRequest) facesContext.getExternalContext().getRequest();

    URLPatternDescriptor pattern = getURLPatternDescriptor(httpRequest);
    if (pattern == null) {
        return;/*from   w  ww .j  a  v a 2  s.c o m*/
    }

    DocumentView docView = getDocumentViewFromRequest(pattern.getName(), httpRequest);
    // pattern applies => document view will not be null
    if (docView != null) {
        String documentViewBinding = pattern.getDocumentViewBinding();
        if (documentViewBinding != null && !"".equals(documentViewBinding)) {
            // try to set it from custom mapping
            ValueExpression ve = ef.createValueExpression(context, pattern.getDocumentViewBinding(),
                    Object.class);
            ve.setValue(context, docView);
        }
    }

    Map<String, String> docViewParameters = null;
    if (docView != null) {
        docViewParameters = docView.getParameters();
    }
    ValueBindingDescriptor[] bindings = pattern.getValueBindings();
    if (bindings != null && httpRequest.getAttribute(URLPolicyService.DISABLE_ACTION_BINDING_KEY) == null) {
        for (ValueBindingDescriptor binding : bindings) {
            if (!binding.getCallSetter()) {
                continue;
            }
            String paramName = binding.getName();
            // try doc view parameters
            Object value = null;
            if (docViewParameters != null && docViewParameters.containsKey(paramName)) {
                value = docView.getParameter(paramName);
            } else {
                // try request attributes
                value = httpRequest.getAttribute(paramName);
            }
            String expr = binding.getExpression();
            if (ComponentTagUtils.isValueReference(expr)) {
                ValueExpression ve = ef.createValueExpression(context, expr, Object.class);
                try {
                    ve.setValue(context, value);
                } catch (ELException e) {
                    log.error("Could not apply request parameter '" + value + "' to expression '" + expr + "'",
                            e);
                }
            }
        }
    }
}

From source file:org.nuxeo.ecm.platform.ui.web.component.file.UIInputFile.java

@Override
public void updateModel(FacesContext context) {
    if (context == null) {
        throw new IllegalArgumentException();
    }//from   w w  w .j  av  a  2 s.c  o m

    if (!isValid() || !isLocalValueSet()) {
        return;
    }
    ValueExpression ve = getValueExpression("value");
    if (ve == null) {
        return;
    }
    try {
        InputFileInfo local = getFileInfoLocalValue();
        String choice = local.getConvertedChoice();
        // set blob and filename
        if (InputFileChoice.DELETE.equals(choice)) {
            // set filename first to avoid error in case it maps the blob filename
            ValueExpression vef = getValueExpression("filename");
            if (vef != null) {
                vef.setValue(context.getELContext(), local.getConvertedFilename());
            }
            ve.setValue(context.getELContext(), local.getConvertedBlob());
            setValue(null);
            setLocalValueSet(false);
        } else if (InputFileChoice.isUploadOrKeepTemp(choice)) {
            // set blob first to avoid error in case the filename maps the blob filename
            ve.setValue(context.getELContext(), local.getConvertedBlob());
            setValue(null);
            setLocalValueSet(false);
            ValueExpression vef = getValueExpression("filename");
            if (vef != null) {
                vef.setValue(context.getELContext(), local.getConvertedFilename());
            }
        } else if (InputFileChoice.KEEP.equals(choice)) {
            // reset local value
            setValue(null);
            setLocalValueSet(false);
            if (getEditFilename()) {
                // set filename
                ValueExpression vef = getValueExpression("filename");
                if (vef != null) {
                    vef.setValue(context.getELContext(), local.getConvertedFilename());
                }
            }
        }
        return;
    } catch (ELException e) {
        String messageStr = e.getMessage();
        Throwable result = e.getCause();
        while (result != null && result instanceof ELException) {
            messageStr = result.getMessage();
            result = result.getCause();
        }
        FacesMessage message;
        if (messageStr == null) {
            message = MessageFactory.getMessage(context, UPDATE_MESSAGE_ID,
                    MessageFactory.getLabel(context, this));
        } else {
            message = new FacesMessage(FacesMessage.SEVERITY_ERROR, messageStr, messageStr);
        }
        context.addMessage(getClientId(context), message);
        setValid(false);
    } catch (IllegalArgumentException | ConverterException e) {
        FacesMessage message = MessageFactory.getMessage(context, UPDATE_MESSAGE_ID,
                MessageFactory.getLabel(context, this));
        context.addMessage(getClientId(context), message);
        setValid(false);
    }
}

From source file:com.jsmartframework.web.manager.ExpressionHandler.java

void setExpressionValue(String expr, String jParam) {
    if (isReadOnlyParameter(jParam)) {
        return;//from w w  w  .  ja  v a2  s.  c  om
    }

    Matcher matcher = EL_PATTERN.matcher(expr);
    if (matcher.find()) {

        String beanMethod = matcher.group(1);
        String[] methodSign = beanMethod.split(Constants.EL_SEPARATOR);

        if (methodSign.length > 0 && WebContext.containsAttribute(methodSign[0])) {
            beanMethod = String.format(Constants.JSP_EL, beanMethod);

            ELContext context = WebContext.getPageContext().getELContext();
            ValueExpression valueExpr = WebContext.getExpressionFactory().createValueExpression(context,
                    beanMethod, Object.class);

            Object value = WebContext.getRequest().getParameter(TagHandler.J_TAG + jParam);

            if (!HANDLER.containsUnescapeMethod(methodSign)) {
                value = escapeValue((String) value);
            }
            valueExpr.setValue(context, value);
        }
    }
}

From source file:inti.ws.spring.resource.FilteredWebResource.java

@Override
public void update() throws Exception {
    ExpressionFactory factory;/*from   ww w  .  j a  v  a 2s .co m*/
    ValueExpression var;
    Object val;
    StringBuilder builder = new StringBuilder(32);
    MessageDigest digest = DIGESTS.get();

    for (WebResource dependency : dependencies) {
        dependency.updateIfNeeded();
    }

    super.update();

    factory = ExpressionFactory.newInstance();
    content = factory.createValueExpression(context, compressedFile, String.class);
    for (Map.Entry<String, Object> parameter : parameters.entrySet()) {
        var = factory.createValueExpression(context, "${" + parameter.getKey() + '}', String.class);
        val = parameter.getValue();
        if (val instanceof WebResource) {
            ((WebResource) val).updateIfNeeded();
            val = ((WebResource) val).getContent().hashCode();
        }
        var.setValue(context, val);
    }
    compressedFile = (String) content.getValue(context);

    digest.reset();
    builder.append(Hex.encodeHexString(digest.digest(compressedFile.getBytes(StandardCharsets.UTF_8))));
    messageDigest = builder.toString();
    builder.delete(0, builder.length());

    DATE_FORMATTER.formatDate(lastModified, builder);
    lastModifiedString = builder.toString();
}

From source file:inti.ws.spring.resource.template.TemplateResource.java

@Override
public void update() throws Exception {
    ExpressionFactory factory;//from   ww  w. j a v  a 2 s.c o  m
    ValueExpression var;
    Object val;
    StringBuilder builder = new StringBuilder(2048);
    MessageDigest digest = DIGESTS.get();

    factory = ExpressionFactory.newInstance();

    for (WebResource file : files) {
        if (file.hasChanged()) {
            file.update();
        }
        builder.append(applyTemplate(factory, file.getName(), file.getContent().replaceAll("\\s+", " ")));
        builder.append(',');
    }
    builder.delete(builder.length() - 1, builder.length());

    super.update();

    content = factory.createValueExpression(context, compressedFile, String.class);

    var = factory.createValueExpression(context, "${files}", String.class);
    var.setValue(context, builder.toString());

    if (parameters != null) {

        for (Map.Entry<String, Object> parameter : parameters.entrySet()) {
            var = factory.createValueExpression(context, "${" + parameter.getKey() + '}', String.class);
            val = parameter.getValue();

            if ("$filename".equals(val)) {
                val = resource.getFile();
            } else if ("$modulename".equals(val)) {
                val = moduleName;
            }

            var.setValue(context, val);
        }

    }
    compressedFile = (String) content.getValue(context);
    builder.delete(0, builder.length());

    digest.reset();
    builder.append(Hex.encodeHexString(digest.digest(compressedFile.getBytes(StandardCharsets.UTF_8))));
    messageDigest = builder.toString();
    builder.delete(0, builder.length());

    DATE_FORMATTER.formatDate(lastModified, builder);
    lastModifiedString = builder.toString();
}

From source file:com.jsmartframework.web.manager.ExpressionHandler.java

private void setExpressionDate(String expr, String jParam) throws ServletException {
    if (isReadOnlyParameter(jParam)) {
        return;/* w  w w. j  av  a2 s . co  m*/
    }

    Matcher matcher = EL_PATTERN.matcher(expr);
    if (matcher.find()) {

        String beanMethod = matcher.group(1);
        String[] methodSign = beanMethod.split(Constants.EL_SEPARATOR);

        if (methodSign.length > 0 && WebContext.containsAttribute(methodSign[0])) {
            beanMethod = String.format(Constants.JSP_EL, beanMethod);

            ELContext context = WebContext.getPageContext().getELContext();
            ValueExpression valueExpr = WebContext.getExpressionFactory().createValueExpression(context,
                    beanMethod, Object.class);
            String value = WebContext.getRequest().getParameter(TagHandler.J_DATE + jParam);

            if (StringUtils.isNotBlank(value)) {
                Throwable throwable = null;
                try {
                    valueExpr.setValue(context, value);
                    return;
                } catch (Exception ex) {
                    throwable = ex;
                }

                Long timeMillis = Long.parseLong(value);
                try {
                    valueExpr.setValue(context, new Date(timeMillis));
                    return;
                } catch (Exception ex) {
                    throwable = ex;
                }

                try {
                    valueExpr.setValue(context,
                            Instant.ofEpochMilli(timeMillis).atZone(ZoneId.systemDefault()).toLocalDateTime());
                    return;
                } catch (Exception ex) {
                    throwable = ex;
                }

                try {
                    valueExpr.setValue(context, new DateTime(timeMillis));
                    return;
                } catch (Exception ex) {
                    throwable = ex;
                }

                if (throwable != null) {
                    throw new ServletException(throwable.getMessage());
                }
            } else {
                valueExpr.setValue(context, null);
            }
        }
    }
}

From source file:py.una.pol.karaku.util.ControllerHelper.java

private void updateModel(UIComponent formulario) {

    FacesContext context = getContext();
    ELContext elContext = getContext().getELContext();
    Iterator<UIComponent> iter = formulario.getFacetsAndChildren();
    while (iter.hasNext()) {

        UIComponent component = iter.next();
        // Si es un valor submiteable, o INPUTEaBLE
        if (component instanceof UISelectOne) {
            UISelectOne com = (UISelectOne) component;
            Object newValue = com.getSubmittedValue();
            ValueExpression value = com.getValueExpression(EL_VALUE_PROPERTY);
            if (newValue != null && com.getConverter() != null) {
                // Si tiene un converter definido, entonces utilizamos ese
                // converter para obtener el valor
                newValue = com.getConverter().getAsObject(context, com, newValue.toString());
            }//w  w  w.j a  va2 s .co  m
            value.setValue(elContext, newValue);
        } else if (component instanceof UICalendar) {
            UICalendar com = (UICalendar) component;
            Object newValue = com.getSubmittedValue();
            if (newValue != null) {
                ValueExpression value = com.getValueExpression(EL_VALUE_PROPERTY);
                newValue = getConverter().getAsObject(context, component, newValue.toString());
                value.setValue(elContext, newValue);
            }

        } else if (component instanceof UIInput && !(component instanceof UICalendar)) {
            UIInput com = (UIInput) component;
            Object newValue = com.getSubmittedValue();
            ValueExpression value = com.getValueExpression(EL_VALUE_PROPERTY);
            if (value.getType(elContext).equals(Quantity.class)) {
                if (StringUtils.isValid(newValue)) {
                    newValue = new Quantity((String) newValue);
                } else {
                    newValue = Quantity.ZERO;
                }
            }

            if (newValue instanceof String && !StringUtils.isValid(newValue)) {
                newValue = null;
            }
            value.setValue(elContext, newValue);
        }
        updateModel(component);
    }
}

From source file:com.jsmartframework.web.manager.ExpressionHandler.java

@SuppressWarnings("all")
private void setSelectionValue(String expr, String jParam) {
    Matcher matcher = EL_PATTERN.matcher(expr);
    if (matcher.find()) {

        String beanMethod = matcher.group(1);
        String[] methodSign = beanMethod.split(Constants.EL_SEPARATOR);

        if (methodSign.length > 0 && WebContext.containsAttribute(methodSign[0])) {

            HttpServletRequest request = WebContext.getRequest();
            beanMethod = String.format(Constants.JSP_EL, beanMethod);

            // Get parameter mapped by TagHandler.J_VALUES
            String valuesParam = request.getParameter(TagHandler.J_SEL + jParam);
            Matcher valuesMatcher = TagHandler.J_TAG_PATTERN.matcher(valuesParam);

            Object object = null;
            List<Object> list = null;
            Scroll scroll = null;/*from   w w  w  .  ja v  a2s.co m*/

            if (valuesMatcher.find()) {
                object = getExpressionValue(TagEncrypter.decrypt(request, valuesMatcher.group(2)));
            }

            if (object instanceof ListAdapter) {
                String scrollParam = request.getParameter(TagHandler.J_SCROLL + jParam);
                scroll = GSON.fromJson(scrollParam, Scroll.class);

                list = ((ListAdapter) object).load(scroll.getIndex(), scroll.getOffset(), scroll.getSize());

            } else if (object instanceof TableAdapter) {
                String scrollParam = request.getParameter(TagHandler.J_SCROLL + jParam);
                scroll = GSON.fromJson(scrollParam, Scroll.class);

                list = ((TableAdapter) object).load(scroll.getIndex(), scroll.getOffset(), scroll.getSize(),
                        scroll.getSort(), scroll.getOrder(), scroll.getFilters());

            } else if (object instanceof List<?>) {
                list = (List<Object>) object;
            }

            if (list != null && !list.isEmpty()) {
                ELContext context = WebContext.getPageContext().getELContext();
                ValueExpression valueExpr = WebContext.getExpressionFactory().createValueExpression(context,
                        beanMethod, Object.class);

                Integer index = Integer.parseInt(request.getParameter(TagHandler.J_SEL_VAL + jParam));

                // Case scroll list with adapter need to calculate the difference between
                // the first index of the loaded content with the clicked list item index
                if (scroll != null) {
                    index -= scroll.getIndex();
                }
                valueExpr.setValue(context, list.get(index));
            }
        }
    }
}