Package org.apache.commons.jexl2

Examples of org.apache.commons.jexl2.JexlContext


        return result;
    }

    public static JexlContext addFieldsToContext(final Object attributable, final JexlContext jexlContext) {
        JexlContext context = jexlContext == null ? new MapContext() : jexlContext;

        try {
            for (PropertyDescriptor desc : Introspector.getBeanInfo(attributable.getClass()).getPropertyDescriptors()) {
                final Class<?> type = desc.getPropertyType();
                final String fieldName = desc.getName();

                if ((!fieldName.startsWith("pc"))
                        && (!ArrayUtils.contains(IGNORE_FIELDS, fieldName))
                        && (!Iterable.class.isAssignableFrom(type))
                        && (!type.isArray())) {
                    try {
                        final Method getter = desc.getReadMethod();

                        final Object fieldValue;

                        if (getter == null) {
                            final Field field = attributable.getClass().getDeclaredField(fieldName);
                            field.setAccessible(true);
                            fieldValue = field.get(attributable);
                        } else {
                            fieldValue = getter.invoke(attributable);
                        }

                        context.set(fieldName, fieldValue == null
                                ? ""
                                : (type.equals(Date.class)
                                ? DataFormat.format((Date) fieldValue, false)
                                : fieldValue));
View Full Code Here


    }

    public static JexlContext addAttrsToContext(final Collection<? extends AbstractAttr> attrs,
            final JexlContext jexlContext) {

        JexlContext context = jexlContext == null
                ? new MapContext()
                : jexlContext;

        for (AbstractAttr attr : attrs) {
            if (attr.getSchema() != null) {
                List<String> attrValues = attr.getValuesAsStrings();
                String expressionValue = attrValues.isEmpty()
                        ? ""
                        : attrValues.get(0);

                LOG.debug("Add attribute {} with value {}", attr.getSchema().getName(), expressionValue);

                context.set(attr.getSchema().getName(), expressionValue);
            }
        }

        return context;
    }
View Full Code Here

    }

    public static JexlContext addDerAttrsToContext(final Collection<? extends AbstractDerAttr> derAttrs,
            final Collection<? extends AbstractAttr> attrs, final JexlContext jexlContext) {

        JexlContext context = jexlContext == null
                ? new MapContext()
                : jexlContext;

        for (AbstractDerAttr derAttr : derAttrs) {
            if (derAttr.getSchema() != null) {
                String expressionValue = derAttr.getValue(attrs);
                if (expressionValue == null) {
                    expressionValue = "";
                }

                LOG.debug("Add derived attribute {} with value {}", derAttr.getSchema().getName(), expressionValue);

                context.set(derAttr.getSchema().getName(), expressionValue);
            }
        }

        return context;
    }
View Full Code Here

    }

    public static JexlContext addVirAttrsToContext(final Collection<? extends AbstractVirAttr> virAttrs,
            final JexlContext jexlContext) {

        JexlContext context = jexlContext == null
                ? new MapContext()
                : jexlContext;

        for (AbstractVirAttr virAttr : virAttrs) {
            if (virAttr.getSchema() != null) {
                List<String> attrValues = virAttr.getValues();
                String expressionValue = attrValues.isEmpty()
                        ? ""
                        : attrValues.get(0);

                LOG.debug("Add virtual attribute {} with value {}", virAttr.getSchema().getName(), expressionValue);

                context.set(virAttr.getSchema().getName(), expressionValue);
            }
        }

        return context;
    }
View Full Code Here

        return context;
    }

    public static String evaluate(final String expression, final AbstractAttributableTO attributableTO) {
        final JexlContext context = new MapContext();

        addFieldsToContext(attributableTO, context);

        for (AttributeTO attr : attributableTO.getAttrs()) {
            List<String> values = attr.getValues();
            String expressionValue = values.isEmpty()
                    ? ""
                    : values.get(0);

            LOG.debug("Add attribute {} with value {}", attr.getSchema(), expressionValue);

            context.set(attr.getSchema(), expressionValue);
        }
        for (AttributeTO derAttr : attributableTO.getDerAttrs()) {
            List<String> values = derAttr.getValues();
            String expressionValue = values.isEmpty()
                    ? ""
                    : values.get(0);

            LOG.debug("Add derived attribute {} with value {}", derAttr.getSchema(), expressionValue);

            context.set(derAttr.getSchema(), expressionValue);
        }
        for (AttributeTO virAttr : attributableTO.getVirAttrs()) {
            List<String> values = virAttr.getValues();
            String expressionValue = values.isEmpty()
                    ? ""
                    : values.get(0);

            LOG.debug("Add virtual attribute {} with value {}", virAttr.getSchema(), expressionValue);

            context.set(virAttr.getSchema(), expressionValue);
        }

        // Evaluate expression using the context prepared before
        return evaluate(expression, context);
    }
View Full Code Here

    }

    public static boolean evaluateMandatoryCondition(final String mandatoryCondition,
            final AbstractAttributable attributable) {

        JexlContext jexlContext = new MapContext();
        addAttrsToContext(attributable.getAttrs(), jexlContext);
        addDerAttrsToContext(attributable.getDerAttrs(), attributable.getAttrs(), jexlContext);
        addVirAttrsToContext(attributable.getVirAttrs(), jexlContext);

        return Boolean.parseBoolean(evaluate(mandatoryCondition, jexlContext));
View Full Code Here

public class ELUtil {
    public static Object invoke(String expression, Map<String, Object> context) {
        JexlEngine jexl = new JexlEngine();
        Expression el = jexl.createExpression(expression);
        JexlContext elContext = new MapContext();
        for (String property : context.keySet()) {
            elContext.set(property, context.get(property));
        }
        return el.evaluate(elContext);
    }
View Full Code Here

     *
     * @return the newly created context
     */
    private JexlContext createContext()
    {
        JexlContext ctx = new MapContext();
        initializeContext(ctx);
        return ctx;
    }
View Full Code Here

   * @return String - the formatted string resulting from the expression, or null if the formatting fails.
   */
  @Override
  public String formatName(Result result) {
    String formatted;
    JexlContext context = new MapContext();

    this.populateContext(context, result);
    try {
      formatted = (String) this.parsedExpr.evaluate(context);
    } catch (JexlException jexlExc) {
View Full Code Here

     * {@link FeatureState#DISABLED} otherwise.
     */
    @Override
    public FeatureState process(final ContextManager contextManager)
    {
        final JexlEngine jexlEngine = new JexlEngine();
        Expression jexlExpression;
        if (expression != null) {
            jexlExpression = jexlEngine.createExpression(expression);
        } else {
            jexlExpression = getOldExpression(jexlEngine);
        }
       
        final Map<String, Object> contextMap = contextManager.getContext(context);
View Full Code Here

TOP

Related Classes of org.apache.commons.jexl2.JexlContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.