Search in sources :

Example 16 with ExpressionException

use of lucee.runtime.exp.ExpressionException in project Lucee by lucee.

the class Caster method _castTo.

private static Object _castTo(PageContext pc, String strType, Object o) throws PageException {
    if (o instanceof Component) {
        Component comp = ((Component) o);
        if (comp.instanceOf(strType))
            return o;
        throw new ExpressionException("can't cast Component of Type [" + comp.getAbsName() + "] to [" + strType + "]");
    }
    if (o instanceof Pojo) {
        Component cfc = AxisCaster.toComponent(pc, ((Pojo) o), strType, null);
        if (cfc != null)
            return cfc;
        throw new ExpressionException("can't cast Pojo of Type [" + o.getClass().getName() + "] to [" + strType + "]");
    }
    if (strType.endsWith("[]") && Decision.isArray(o)) {
        String _strType = strType.substring(0, strType.length() - 2);
        short _type = CFTypes.toShort(_strType, false, (short) -1);
        Array arr = Caster.toArray(o, null);
        if (arr != null) {
            // convert the values
            Iterator<Entry<Key, Object>> it = arr.entryIterator();
            Array _arr = new ArrayImpl();
            Entry<Key, Object> e;
            Object src, trg;
            boolean hasChanged = false;
            while (it.hasNext()) {
                e = it.next();
                src = e.getValue();
                trg = castTo(pc, _type, _strType, src);
                _arr.setEL(e.getKey(), trg);
                if (src != trg)
                    hasChanged = true;
            }
            if (!hasChanged)
                return arr;
            return _arr;
        }
    }
    throw new CasterException(o, strType);
}
Also used : Pojo(lucee.runtime.net.rpc.Pojo) CasterException(lucee.runtime.exp.CasterException) ArrayImpl(lucee.runtime.type.ArrayImpl) ExpressionException(lucee.runtime.exp.ExpressionException) Array(lucee.runtime.type.Array) XMLMultiElementArray(lucee.runtime.text.xml.struct.XMLMultiElementArray) ListAsArray(lucee.runtime.type.wrap.ListAsArray) Entry(java.util.Map.Entry) JavaObject(lucee.runtime.java.JavaObject) Component(lucee.runtime.Component) Key(lucee.runtime.type.Collection.Key)

Example 17 with ExpressionException

use of lucee.runtime.exp.ExpressionException in project Lucee by lucee.

the class DataSourceServiceImpl method getDrivers.

@Override
public Struct getDrivers() throws ServiceException, SecurityException {
    checkReadAccess();
    Struct rtn = new StructImpl();
    Struct driver;
    try {
        Resource luceeContext = ResourceUtil.toResourceExisting(pc(), "/lucee/admin/dbdriver/");
        Resource[] children = luceeContext.listResources(new ExtensionResourceFilter(Constants.getComponentExtensions()));
        String name;
        for (int i = 0; i < children.length; i++) {
            driver = new StructImpl();
            name = ListFirst.call(pc(), children[i].getName(), ".");
            driver.setEL(KeyConstants._name, name);
            driver.setEL("handler", children[i].getName());
            rtn.setEL(name, driver);
        }
    } catch (ExpressionException e) {
        throw new ServiceException(e.getMessage());
    }
    return rtn;
}
Also used : StructImpl(lucee.runtime.type.StructImpl) ServiceException(coldfusion.server.ServiceException) Resource(lucee.commons.io.res.Resource) ExtensionResourceFilter(lucee.commons.io.res.filter.ExtensionResourceFilter) ExpressionException(lucee.runtime.exp.ExpressionException) Struct(lucee.runtime.type.Struct)

Example 18 with ExpressionException

use of lucee.runtime.exp.ExpressionException in project Lucee by lucee.

the class BIF method callWithNamedValues.

@Override
public Object callWithNamedValues(PageContext pageContext, Struct values, boolean doIncludePath) throws PageException {
    ArrayList<FunctionLibFunctionArg> flfas = flf.getArg();
    Iterator<FunctionLibFunctionArg> it = flfas.iterator();
    FunctionLibFunctionArg arg;
    Object val;
    List<Ref> refs = new ArrayList<Ref>();
    while (it.hasNext()) {
        arg = it.next();
        // match by name
        val = values.get(arg.getName(), null);
        // match by alias
        if (val == null) {
            String alias = arg.getAlias();
            if (!StringUtil.isEmpty(alias, true)) {
                String[] aliases = lucee.runtime.type.util.ListUtil.trimItems(lucee.runtime.type.util.ListUtil.listToStringArray(alias, ','));
                for (int x = 0; x < aliases.length; x++) {
                    val = values.get(aliases[x], null);
                    if (val != null)
                        break;
                }
            }
        }
        if (val == null) {
            if (arg.getRequired()) {
                String[] names = flf.getMemberNames();
                String n = ArrayUtil.isEmpty(names) ? "" : names[0];
                throw new ExpressionException("missing required argument [" + arg.getName() + "] for build in function call [" + n + "]");
            }
        } else {
            refs.add(new Casting(arg.getTypeAsString(), arg.getType(), new LFunctionValue(new LString(arg.getName()), val)));
        }
    }
    BIFCall call = new BIFCall(flf, refs.toArray(new Ref[refs.size()]));
    return call.getValue(pageContext);
}
Also used : ArrayList(java.util.ArrayList) LString(lucee.runtime.interpreter.ref.literal.LString) ExpressionException(lucee.runtime.exp.ExpressionException) LString(lucee.runtime.interpreter.ref.literal.LString) Casting(lucee.runtime.interpreter.ref.cast.Casting) Ref(lucee.runtime.interpreter.ref.Ref) LFunctionValue(lucee.runtime.interpreter.ref.literal.LFunctionValue) BIFCall(lucee.runtime.interpreter.ref.func.BIFCall) FunctionLibFunctionArg(lucee.transformer.library.function.FunctionLibFunctionArg)

Example 19 with ExpressionException

use of lucee.runtime.exp.ExpressionException in project Lucee by lucee.

the class UDFGSProperty method validate.

static final void validate(String validate, Struct validateParams, Object obj) throws PageException {
    if (StringUtil.isEmpty(validate, true))
        return;
    validate = validate.trim().toLowerCase();
    if (!validate.equals("regex") && !Decision.isValid(validate, obj))
        throw new ExpressionException(createMessage(validate, obj));
    // range
    if (validateParams == null)
        return;
    if (validate.equals("integer") || validate.equals("numeric") || validate.equals("number")) {
        double min = Caster.toDoubleValue(validateParams.get(KeyConstants._min, null), false, Double.NaN);
        double max = Caster.toDoubleValue(validateParams.get(KeyConstants._max, null), false, Double.NaN);
        double d = Caster.toDoubleValue(obj);
        if (!Double.isNaN(min) && d < min)
            throw new ExpressionException(validate + " [" + Caster.toString(d) + "] is out of range, value must be more than or equal to [" + min + "]");
        if (!Double.isNaN(max) && d > max)
            throw new ExpressionException(validate + " [" + Caster.toString(d) + "] is out of range, value must be less than or equal to [" + max + "]");
    } else if (validate.equals("string")) {
        double min = Caster.toDoubleValue(validateParams.get(MIN_LENGTH, null), false, Double.NaN);
        double max = Caster.toDoubleValue(validateParams.get(MAX_LENGTH, null), false, Double.NaN);
        String str = Caster.toString(obj);
        int l = str.length();
        if (!Double.isNaN(min) && l < ((int) min))
            throw new ExpressionException("string [" + str + "] is to short [" + l + "], the string must be at least [" + min + "] characters");
        if (!Double.isNaN(max) && l > ((int) max))
            throw new ExpressionException("string [" + str + "] is to long [" + l + "], the string can have a maximum length of [" + max + "] characters");
    } else if (validate.equals("regex")) {
        String pattern = Caster.toString(validateParams.get(KeyConstants._pattern, null), null);
        String value = Caster.toString(obj);
        if (!StringUtil.isEmpty(pattern, true) && !IsValid.regex(value, pattern))
            throw new ExpressionException("the string [" + value + "] does not match the regular expression pattern [" + pattern + "]");
    }
}
Also used : ExpressionException(lucee.runtime.exp.ExpressionException)

Example 20 with ExpressionException

use of lucee.runtime.exp.ExpressionException in project Lucee by lucee.

the class UDFRemoveProperty method callWithNamedValues.

@Override
public Object callWithNamedValues(PageContext pageContext, Struct values, boolean doIncludePath) throws PageException {
    UDFUtil.argumentCollection(values, getFunctionArguments());
    Key key = arguments[0].getName();
    Object value = values.get(key, null);
    if (value == null) {
        Key[] keys = CollectionUtil.keys(values);
        if (keys.length == 1) {
            value = values.get(keys[0]);
        } else
            throw new ExpressionException("The parameter " + key + " to function " + getFunctionName() + " is required but was not passed in.");
    }
    return remove(pageContext, value);
}
Also used : Key(lucee.runtime.type.Collection.Key) ExpressionException(lucee.runtime.exp.ExpressionException)

Aggregations

ExpressionException (lucee.runtime.exp.ExpressionException)136 Element (org.w3c.dom.Element)24 Key (lucee.runtime.type.Collection.Key)15 SecurityException (lucee.runtime.exp.SecurityException)13 ArrayList (java.util.ArrayList)12 Struct (lucee.runtime.type.Struct)12 List (java.util.List)11 Collection (lucee.runtime.type.Collection)11 Entry (java.util.Map.Entry)10 Node (org.w3c.dom.Node)10 Resource (lucee.commons.io.res.Resource)9 Iterator (java.util.Iterator)8 PageException (lucee.runtime.exp.PageException)8 Map (java.util.Map)7 CasterException (lucee.runtime.exp.CasterException)7 NodeList (org.w3c.dom.NodeList)7 PageContextImpl (lucee.runtime.PageContextImpl)6 PageSource (lucee.runtime.PageSource)5 Casting (lucee.runtime.interpreter.ref.cast.Casting)5 ArrayImpl (lucee.runtime.type.ArrayImpl)5