Java Code Examples for org.apache.el.util.MessageFactory#get()

The following examples show how to use org.apache.el.util.MessageFactory#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ELSupport.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static final Enum<?> coerceToEnum(final Object obj,
        @SuppressWarnings("rawtypes") Class type) {
    if (obj == null || "".equals(obj)) {
        return null;
    }
    if (type.isAssignableFrom(obj.getClass())) {
        return (Enum<?>) obj;
    }
    
    if (!(obj instanceof String)) {
        throw new ELException(MessageFactory.get("error.convert",
                obj, obj.getClass(), type));
    }

    Enum<?> result;
    try {
         result = Enum.valueOf(type, (String) obj);
    } catch (IllegalArgumentException iae) {
        throw new ELException(MessageFactory.get("error.convert",
                obj, obj.getClass(), type));
    }
    return result;
}
 
Example 2
Source File: AstValue.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public void setValue(EvaluationContext ctx, Object value)
        throws ELException {
    Target t = getTarget(ctx);
    ctx.setPropertyResolved(false);
    ELResolver resolver = ctx.getELResolver();

    // coerce to the expected type
    Class<?> targetClass = resolver.getType(ctx, t.base, t.property);
    if (COERCE_TO_ZERO == true
            || !isAssignable(value, targetClass)) {
        resolver.setValue(ctx, t.base, t.property,
                ELSupport.coerceToType(value, targetClass));
    } else {
        resolver.setValue(ctx, t.base, t.property, value);
    }
    if (!ctx.isPropertyResolved()) {
        throw new PropertyNotFoundException(MessageFactory.get(
                "error.resolver.unhandled", t.base, t.property));            
    }
}
 
Example 3
Source File: ELSupport.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Convert an object to Boolean.
 * Null and empty string are false.
 * @param obj the object to convert
 * @return the Boolean value of the object
 * @throws ELException if object is not Boolean or String
 */
public static final Boolean coerceToBoolean(final Object obj)
        throws ELException {
    if (obj == null || "".equals(obj)) {
        return Boolean.FALSE;
    }
    if (obj instanceof Boolean) {
        return (Boolean) obj;
    }
    if (obj instanceof String) {
        return Boolean.valueOf((String) obj);
    }

    throw new ELException(MessageFactory.get("error.convert",
            obj, obj.getClass(), Boolean.class));
}
 
Example 4
Source File: AstIdentifier.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isReadOnly(EvaluationContext ctx) throws ELException {
    VariableMapper varMapper = ctx.getVariableMapper();
    if (varMapper != null) {
        ValueExpression expr = varMapper.resolveVariable(this.image);
        if (expr != null) {
            return expr.isReadOnly(ctx.getELContext());
        }
    }
    ctx.setPropertyResolved(false);
    boolean result = ctx.getELResolver().isReadOnly(ctx, null, this.image);
    if (!ctx.isPropertyResolved()) {
        throw new PropertyNotFoundException(MessageFactory.get(
                "error.resolver.unhandled.null", this.image));
    }
    return result;
}
 
Example 5
Source File: AstIdentifier.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public Object getValue(EvaluationContext ctx) throws ELException {
    VariableMapper varMapper = ctx.getVariableMapper();
    if (varMapper != null) {
        ValueExpression expr = varMapper.resolveVariable(this.image);
        if (expr != null) {
            return expr.getValue(ctx.getELContext());
        }
    }
    ctx.setPropertyResolved(false);
    Object result = ctx.getELResolver().getValue(ctx, null, this.image);
    if (!ctx.isPropertyResolved()) {
        throw new PropertyNotFoundException(MessageFactory.get(
                "error.resolver.unhandled.null", this.image));
    }
    return result;
}
 
Example 6
Source File: AstIdentifier.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
public void setValue(EvaluationContext ctx, Object value)
        throws ELException {
    VariableMapper varMapper = ctx.getVariableMapper();
    if (varMapper != null) {
        ValueExpression expr = varMapper.resolveVariable(this.image);
        if (expr != null) {
            expr.setValue(ctx.getELContext(), value);
            return;
        }
    }
    ctx.setPropertyResolved(false);
    ctx.getELResolver().setValue(ctx, null, this.image, value);
    if (!ctx.isPropertyResolved()) {
        throw new PropertyNotFoundException(MessageFactory.get(
                "error.resolver.unhandled.null", this.image));
    }
}
 
Example 7
Source File: AstValue.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public Class<?> getType(EvaluationContext ctx) throws ELException {
    Target t = getTarget(ctx);
    ctx.setPropertyResolved(false);
    Class<?> result = ctx.getELResolver().getType(ctx, t.base, t.property);
    if (!ctx.isPropertyResolved()) {
        throw new PropertyNotFoundException(MessageFactory.get(
                "error.resolver.unhandled", t.base, t.property));            
    }
    return result;
}
 
Example 8
Source File: AstIdentifier.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void setImage(String image) {
    if (!Validation.isIdentifier(image)) {
        throw new ELException(MessageFactory.get("error.identifier.notjava",
                image));
    }
    this.image = image;
}
 
Example 9
Source File: ELSupport.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private static final Character coerceToCharacter(final ELContext ctx, final Object obj)
        throws ELException {

    if (ctx != null) {
        boolean originalIsPropertyResolved = ctx.isPropertyResolved();
        try {
            Object result = ctx.getELResolver().convertToType(ctx, obj, Character.class);
            if (ctx.isPropertyResolved()) {
                return (Character) result;
            }
        } finally {
            ctx.setPropertyResolved(originalIsPropertyResolved);
        }
    }

    if (obj == null || "".equals(obj)) {
        return Character.valueOf((char) 0);
    }
    if (obj instanceof String) {
        return Character.valueOf(((String) obj).charAt(0));
    }
    if (ELArithmetic.isNumber(obj)) {
        return Character.valueOf((char) ((Number) obj).shortValue());
    }
    Class<?> objType = obj.getClass();
    if (obj instanceof Character) {
        return (Character) obj;
    }

    throw new ELException(MessageFactory.get("error.convert",
            obj, objType, Character.class));
}
 
Example 10
Source File: AstDotSuffix.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public void setImage(String image) {
    if (!Validation.isIdentifier(image)) {
        throw new ELException(MessageFactory.get("error.identifier.notjava",
                image));
    }
    this.image = image;
}
 
Example 11
Source File: ExpressionBuilder.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(Node node) throws ELException {
    if (node instanceof AstFunction) {

        AstFunction funcNode = (AstFunction) node;

        if (this.fnMapper == null) {
            throw new ELException(MessageFactory.get("error.fnMapper.null"));
        }
        Method m = fnMapper.resolveFunction(funcNode.getPrefix(), funcNode
                .getLocalName());
        if (m == null) {
            throw new ELException(MessageFactory.get(
                    "error.fnMapper.method", funcNode.getOutputName()));
        }
        int pcnt = m.getParameterTypes().length;
        if (node.jjtGetNumChildren() != pcnt) {
            throw new ELException(MessageFactory.get(
                    "error.fnMapper.paramcount", funcNode.getOutputName(),
                    "" + pcnt, "" + node.jjtGetNumChildren()));
        }
    } else if (node instanceof AstIdentifier && this.varMapper != null) {
        String variable = ((AstIdentifier) node).getImage();

        // simply capture it
        this.varMapper.resolveVariable(variable);
    }
}
 
Example 12
Source File: AstValue.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public Class<?> getType(EvaluationContext ctx) throws ELException {
    Target t = getTarget(ctx);
    ctx.setPropertyResolved(false);
    Class<?> result = ctx.getELResolver().getType(ctx, t.base, t.property);
    if (!ctx.isPropertyResolved()) {
        throw new PropertyNotFoundException(MessageFactory.get(
                "error.resolver.unhandled", t.base, t.property));            
    }
    return result;
}
 
Example 13
Source File: ELSupport.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
public static final Object coerceToType(final Object obj,
        final Class<?> type) throws ELException {
    if (type == null || Object.class.equals(type) ||
            (obj != null && type.isAssignableFrom(obj.getClass()))) {
        return obj;
    }
    if (String.class.equals(type)) {
        return coerceToString(obj);
    }
    if (ELArithmetic.isNumberType(type)) {
        return coerceToNumber(obj, type);
    }
    if (Character.class.equals(type) || Character.TYPE == type) {
        return coerceToCharacter(obj);
    }
    if (Boolean.class.equals(type) || Boolean.TYPE == type) {
        return coerceToBoolean(obj);
    }
    if (type.isEnum()) {
        return coerceToEnum(obj, type);
    }

    // new to spec
    if (obj == null)
        return null;
    if (obj instanceof String) {
        PropertyEditor editor = PropertyEditorManager.findEditor(type);
        if (editor == null) {
            if ("".equals(obj)) {
                return null;
            }
            throw new ELException(MessageFactory.get("error.convert", obj,
                    obj.getClass(), type));
        } else {
            try {
                editor.setAsText((String) obj);
                return editor.getValue();
            } catch (RuntimeException e) {
                if ("".equals(obj)) {
                    return null;
                }
                throw new ELException(MessageFactory.get("error.convert",
                        obj, obj.getClass(), type), e);
            }
        }
    }
    throw new ELException(MessageFactory.get("error.convert",
            obj, obj.getClass(), type));
}
 
Example 14
Source File: ExpressionBuilder.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
private static final Node createNodeInternal(String expr)
        throws ELException {
    if (expr == null) {
        throw new ELException(MessageFactory.get("error.null"));
    }

    Node n = cache.get(expr);
    if (n == null) {
        try {
            n = (new ELParser(new StringReader(expr)))
                    .CompositeExpression();

            // validate composite expression
            int numChildren = n.jjtGetNumChildren();
            if (numChildren == 1) {
                n = n.jjtGetChild(0);
            } else {
                Class<?> type = null;
                Node child = null;
                for (int i = 0; i < numChildren; i++) {
                    child = n.jjtGetChild(i);
                    if (child instanceof AstLiteralExpression)
                        continue;
                    if (type == null)
                        type = child.getClass();
                    else {
                        if (!type.equals(child.getClass())) {
                            throw new ELException(MessageFactory.get(
                                    "error.mixed", expr));
                        }
                    }
                }
            }

            if (n instanceof AstDeferredExpression
                    || n instanceof AstDynamicExpression) {
                n = n.jjtGetChild(0);
            }
            cache.put(expr, n);
        } catch (Exception e) {
            throw new ELException(
                    MessageFactory.get("error.parseFail", expr), e);
        }
    }
    return n;
}
 
Example 15
Source File: AstValue.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public Object getValue(EvaluationContext ctx) throws ELException {
    Object base = this.children[0].getValue(ctx);
    int propCount = this.jjtGetNumChildren();
    int i = 1;
    Object suffix = null;
    ELResolver resolver = ctx.getELResolver();
    while (base != null && i < propCount) {
        suffix = this.children[i].getValue(ctx);
        if (i + 1 < propCount &&
                (this.children[i+1] instanceof AstMethodParameters)) {
            AstMethodParameters mps =
                (AstMethodParameters) this.children[i+1];
            if (base instanceof Optional && "orElseGet".equals(suffix) &&
                    mps.jjtGetNumChildren() == 1) {
                Node paramFoOptional = mps.jjtGetChild(0);
                if (!(paramFoOptional instanceof AstLambdaExpression ||
                        paramFoOptional instanceof LambdaExpression)) {
                    throw new ELException(MessageFactory.get(
                            "stream.optional.paramNotLambda", suffix));
                }
            }
            // This is a method
            Object[] paramValues = mps.getParameters(ctx);
            base = resolver.invoke(ctx, base, suffix,
                    getTypesFromValues(paramValues), paramValues);
            i+=2;
        } else {
            // This is a property
            if (suffix == null) {
                return null;
            }

            ctx.setPropertyResolved(false);
            base = resolver.getValue(ctx, base, suffix);
            i++;
        }
    }
    if (!ctx.isPropertyResolved()) {
        throw new PropertyNotFoundException(MessageFactory.get(
                "error.resolver.unhandled", base, suffix));
    }
    return base;
}
 
Example 16
Source File: ELSupport.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
protected static final Number coerceToNumber(final Number number,
        final Class<?> type) throws ELException {
    if (Long.TYPE == type || Long.class.equals(type)) {
        return Long.valueOf(number.longValue());
    }
    if (Double.TYPE == type || Double.class.equals(type)) {
        return Double.valueOf(number.doubleValue());
    }
    if (Integer.TYPE == type || Integer.class.equals(type)) {
        return Integer.valueOf(number.intValue());
    }
    if (BigInteger.class.equals(type)) {
        if (number instanceof BigDecimal) {
            return ((BigDecimal) number).toBigInteger();
        }
        if (number instanceof BigInteger) {
            return number;
        }
        return BigInteger.valueOf(number.longValue());
    }
    if (BigDecimal.class.equals(type)) {
        if (number instanceof BigDecimal) {
            return number;
        }
        if (number instanceof BigInteger) {
            return new BigDecimal((BigInteger) number);
        }
        return new BigDecimal(number.doubleValue());
    }
    if (Byte.TYPE == type || Byte.class.equals(type)) {
        return Byte.valueOf(number.byteValue());
    }
    if (Short.TYPE == type || Short.class.equals(type)) {
        return Short.valueOf(number.shortValue());
    }
    if (Float.TYPE == type || Float.class.equals(type)) {
        return Float.valueOf(number.floatValue());
    }
    if (Number.class.equals(type)) {
        return number;
    }

    throw new ELException(MessageFactory.get("error.convert",
            number, number.getClass(), type));
}
 
Example 17
Source File: AstIdentifier.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public Object getValue(EvaluationContext ctx) throws ELException {
    // Lambda parameters
    if (ctx.isLambdaArgument(this.image)) {
        return ctx.getLambdaArgument(this.image);
    }

    // Variable mapper
    VariableMapper varMapper = ctx.getVariableMapper();
    if (varMapper != null) {
        ValueExpression expr = varMapper.resolveVariable(this.image);
        if (expr != null) {
            return expr.getValue(ctx.getELContext());
        }
    }

    // EL Resolvers
    ctx.setPropertyResolved(false);
    Object result;
    /* Putting the Boolean into the ELContext is part of a performance
     * optimisation for ScopedAttributeELResolver. When looking up "foo",
     * the resolver can't differentiate between ${ foo } and ${ foo.bar }.
     * This is important because the expensive class lookup only needs to
     * be performed in the later case. This flag tells the resolver if the
     * lookup can be skipped.
     */
    if (parent instanceof AstValue) {
        ctx.putContext(this.getClass(), Boolean.FALSE);
    } else {
        ctx.putContext(this.getClass(), Boolean.TRUE);
    }
    try {
        result = ctx.getELResolver().getValue(ctx, null, this.image);
    } finally {
        // Always reset the flag to false so the optimisation is not applied
        // inappropriately
        ctx.putContext(this.getClass(), Boolean.FALSE);
    }

    if (ctx.isPropertyResolved()) {
        return result;
    }

    // Import
    result = ctx.getImportHandler().resolveClass(this.image);
    if (result != null) {
        return new ELClass((Class<?>) result);
    }
    result = ctx.getImportHandler().resolveStatic(this.image);
    if (result != null) {
        try {
            return ((Class<?>) result).getField(this.image).get(null);
        } catch (IllegalArgumentException | IllegalAccessException
                | NoSuchFieldException | SecurityException e) {
            throw new ELException(e);
        }
    }

    throw new PropertyNotFoundException(MessageFactory.get(
            "error.resolver.unhandled.null", this.image));
}
 
Example 18
Source File: ELSupport.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
protected static final Number coerceToNumber(final Number number,
        final Class<?> type) throws ELException {
    if (Long.TYPE == type || Long.class.equals(type)) {
        return Long.valueOf(number.longValue());
    }
    if (Double.TYPE == type || Double.class.equals(type)) {
        return Double.valueOf(number.doubleValue());
    }
    if (Integer.TYPE == type || Integer.class.equals(type)) {
        return Integer.valueOf(number.intValue());
    }
    if (BigInteger.class.equals(type)) {
        if (number instanceof BigDecimal) {
            return ((BigDecimal) number).toBigInteger();
        }
        if (number instanceof BigInteger) {
            return number;
        }
        return BigInteger.valueOf(number.longValue());
    }
    if (BigDecimal.class.equals(type)) {
        if (number instanceof BigDecimal) {
            return number;
        }
        if (number instanceof BigInteger) {
            return new BigDecimal((BigInteger) number);
        }
        return new BigDecimal(number.doubleValue());
    }
    if (Byte.TYPE == type || Byte.class.equals(type)) {
        return Byte.valueOf(number.byteValue());
    }
    if (Short.TYPE == type || Short.class.equals(type)) {
        return Short.valueOf(number.shortValue());
    }
    if (Float.TYPE == type || Float.class.equals(type)) {
        return Float.valueOf(number.floatValue());
    }
    if (Number.class.equals(type)) {
        return number;
    }

    throw new ELException(MessageFactory.get("error.convert",
            number, number.getClass(), type));
}
 
Example 19
Source File: AstValue.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
private Object[] convertArgs(Object[] src, Method m) {
    Class<?>[] types = m.getParameterTypes();
    if (types.length == 0) {
        // Treated as if parameters have been provided so src is ignored
        return EMPTY_ARRAY;
    }
    
    int paramCount = types.length;

    if (m.isVarArgs() && paramCount > 1 && (src == null || paramCount > src.length) ||
            !m.isVarArgs() && (paramCount > 0 && src == null ||
                    src != null && src.length != paramCount)) {
        String srcCount = null;
        if (src != null) {
            srcCount = Integer.toString(src.length);
        }
        String msg;
        if (m.isVarArgs()) {
            msg = MessageFactory.get("error.invoke.tooFewParams",
                    m.getName(), srcCount, Integer.toString(paramCount));
        } else {
            msg = MessageFactory.get("error.invoke.wrongParams",
                    m.getName(), srcCount, Integer.toString(paramCount));
        }
        throw new IllegalArgumentException(msg);
    }

    if (src == null) {
        // Must be a varargs method with a single parameter.
        // Use a new array every time since the called code could modify the
        // contents of the array
        return new Object[1];
    }

    Object[] dest = new Object[paramCount];

    for (int i = 0; i < paramCount - 1; i++) {
        dest[i] = ELSupport.coerceToType(src[i], types[i]);
    }

    if (m.isVarArgs()) {
        Object[] varArgs = (Object[]) Array.newInstance(
                m.getParameterTypes()[paramCount - 1].getComponentType(),
                src.length - (paramCount - 1));
        for (int i = 0; i < src.length - (paramCount - 1); i ++) {
            varArgs[i] = ELSupport.coerceToType(src[paramCount - 1 + i],
                    types[paramCount - 1].getComponentType());
        }
        dest[paramCount - 1] = varArgs;
    } else {
        dest[paramCount - 1] = ELSupport.coerceToType(
                src[paramCount - 1], types[paramCount - 1]);
    }

    return dest;
}
 
Example 20
Source File: ValueExpressionLiteral.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
public void setValue(ELContext context, Object value) {
    throw new PropertyNotWritableException(MessageFactory.get(
            "error.value.literal.write", this.value));
}