Search in sources :

Example 81 with ExpressionException

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

the class ArrayUtil method _toDoubleValue.

private static double _toDoubleValue(Array array, int i) throws ExpressionException {
    Object obj = array.get(i, null);
    if (obj == null)
        throw new ExpressionException("there is no element at position [" + i + "] or the element is null");
    double tmp = Caster.toDoubleValue(obj, true, Double.NaN);
    if (Double.isNaN(tmp))
        throw new CasterException(obj, Double.class);
    return tmp;
}
Also used : CasterException(lucee.runtime.exp.CasterException) ExpressionException(lucee.runtime.exp.ExpressionException)

Example 82 with ExpressionException

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

the class ListUtil method listInsertAt.

/**
 * casts a list to Array object, remove all empty items at start and end of the list and store count to info
 * @param list list to cast
 * @param pos
 * @param delimiter delimter of the list
 * @return Array Object
 * @throws ExpressionException
 */
public static String listInsertAt(String list, int pos, String value, String delimiter, boolean ignoreEmpty) throws ExpressionException {
    if (pos < 1)
        throw new ExpressionException("invalid string list index [" + (pos) + "]");
    char[] del = delimiter.toCharArray();
    char c;
    StringBuilder result = new StringBuilder();
    String end = "";
    int len;
    // remove at start
    if (ignoreEmpty) {
        outer: while (list.length() > 0) {
            c = list.charAt(0);
            for (int i = 0; i < del.length; i++) {
                if (c == del[i]) {
                    list = list.substring(1);
                    result.append(c);
                    continue outer;
                }
            }
            break;
        }
    }
    // remove at end
    if (ignoreEmpty) {
        outer: while (list.length() > 0) {
            c = list.charAt(list.length() - 1);
            for (int i = 0; i < del.length; i++) {
                if (c == del[i]) {
                    len = list.length();
                    list = list.substring(0, len - 1 < 0 ? 0 : len - 1);
                    end = c + end;
                    continue outer;
                }
            }
            break;
        }
    }
    len = list.length();
    int last = 0;
    int count = 0;
    outer: for (int i = 0; i < len; i++) {
        c = list.charAt(i);
        for (int y = 0; y < del.length; y++) {
            if (c == del[y]) {
                if (!ignoreEmpty || last < i) {
                    if (pos == ++count) {
                        result.append(value);
                        result.append(del[0]);
                    }
                }
                result.append(list.substring(last, i));
                result.append(c);
                last = i + 1;
                continue outer;
            }
        }
    }
    count++;
    if (last <= len) {
        if (pos == count) {
            result.append(value);
            result.append(del[0]);
        }
        result.append(list.substring(last));
    }
    if (pos > count) {
        throw new ExpressionException("invalid string list index [" + (pos) + "], indexes go from 1 to " + (count));
    }
    return result + end;
}
Also used : ExpressionException(lucee.runtime.exp.ExpressionException)

Example 83 with ExpressionException

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

the class MemberUtil method callWithNamedValues.

public static Object callWithNamedValues(PageContext pc, Object coll, Collection.Key methodName, Struct args, short type, String strType) throws PageException {
    Map<Key, FunctionLibFunction> members = getMembers(pc, type);
    FunctionLibFunction member = members.get(methodName);
    if (member != null) {
        List<FunctionLibFunctionArg> _args = member.getArg();
        FunctionLibFunctionArg arg;
        if (args.size() < _args.size()) {
            Object val;
            ArrayList<Ref> refs = new ArrayList<Ref>();
            arg = _args.get(0);
            refs.add(new Casting(arg.getTypeAsString(), arg.getType(), new LFunctionValue(new LString(arg.getName()), coll)));
            for (int y = 1; y < _args.size(); y++) {
                arg = _args.get(y);
                // match by name
                val = args.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 = args.get(aliases[x], null);
                            if (val != null)
                                break;
                        }
                    }
                }
                if (val == null) {
                    if (arg.getRequired()) {
                        String[] names = member.getMemberNames();
                        String n = ArrayUtil.isEmpty(names) ? "" : names[0];
                        throw new ExpressionException("missing required argument [" + arg.getName() + "] for member function call [" + n + "]");
                    }
                } else {
                    refs.add(new Casting(arg.getTypeAsString(), arg.getType(), new LFunctionValue(new LString(arg.getName()), val)));
                // refs.add(new LFunctionValue(new LString(arg.getName()),new Casting(pc,arg.getTypeAsString(),arg.getType(),val)));
                }
            }
            return new BIFCall(coll, member, refs.toArray(new Ref[refs.size()])).getValue(pc);
        }
    }
    throw new ExpressionException("No matching function member [" + methodName + "] for call with named arguments found, available function members are [" + lucee.runtime.type.util.ListUtil.sort(CollectionUtil.getKeyList(members.keySet().iterator(), ","), "textnocase", "asc", ",") + "]");
}
Also used : ArrayList(java.util.ArrayList) LString(lucee.runtime.interpreter.ref.literal.LString) LString(lucee.runtime.interpreter.ref.literal.LString) ExpressionException(lucee.runtime.exp.ExpressionException) Casting(lucee.runtime.interpreter.ref.cast.Casting) Ref(lucee.runtime.interpreter.ref.Ref) FunctionLibFunction(lucee.transformer.library.function.FunctionLibFunction) LFunctionValue(lucee.runtime.interpreter.ref.literal.LFunctionValue) BIFCall(lucee.runtime.interpreter.ref.func.BIFCall) Key(lucee.runtime.type.Collection.Key) FunctionLibFunctionArg(lucee.transformer.library.function.FunctionLibFunctionArg)

Example 84 with ExpressionException

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

the class StructSupport method invalidKey.

/**
 * throw exception for invalid key
 * @param key Invalid key
 * @return returns an invalid key Exception
 */
public static ExpressionException invalidKey(Config config, Struct sct, Key key, String in) {
    String appendix = StringUtil.isEmpty(in, true) ? "" : " in the " + in;
    Iterator<Key> it = sct.keyIterator();
    Key k;
    while (it.hasNext()) {
        k = it.next();
        if (k.equals(key))
            return new ExpressionException("the value from key [" + key.getString() + "] " + appendix + " is NULL, which is the same as not existing in CFML");
    }
    config = ThreadLocalPageContext.getConfig(config);
    if (config != null && config.debug())
        return new ExpressionException(ExceptionUtil.similarKeyMessage(sct, key.getString(), "key", "keys", in, true));
    return new ExpressionException("key [" + key.getString() + "] doesn't exist" + appendix);
}
Also used : ExpressionException(lucee.runtime.exp.ExpressionException)

Example 85 with ExpressionException

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

the class VariableUtilImpl method get.

@Override
public Object get(PageContext pc, Object coll, String key) throws PageException {
    // Objects
    if (coll instanceof Objects) {
        return ((Objects) coll).get(pc, KeyImpl.init(key));
    } else // Collection
    if (coll instanceof Collection) {
        return ((Collection) coll).get(KeyImpl.init(key));
    } else // Map
    if (coll instanceof Map) {
        Object rtn = null;
        try {
            rtn = ((Map) coll).get(key);
            // if(rtn==null)rtn=((Map)coll).get(MapAsStruct.getCaseSensitiveKey((Map)coll, key));
            if (rtn != null)
                return rtn;
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
        }
        rtn = Reflector.getProperty(coll, key, null);
        if (rtn != null)
            return rtn;
        throw new ExpressionException("Key [" + key + "] doesn't exist in Map (" + Caster.toClassName(coll) + ")", "keys are [" + keyList(((Map) coll)) + "]");
    } else // List
    if (coll instanceof List) {
        try {
            Object rtn = ((List) coll).get(Caster.toIntValue(key) - 1);
            if (rtn == null)
                throw new ExpressionException("Key [" + key + "] doesn't exist in List");
            return rtn;
        } catch (IndexOutOfBoundsException e) {
            throw new ExpressionException("Key [" + key + "] doesn't exist in List");
        }
    } else // Native Array
    if (Decision.isNativeArray(coll)) {
        Object rtn = ArrayUtil.get(coll, Caster.toIntValue(key) - 1, null);
        if (rtn == null)
            throw new ExpressionException("Key [" + key + "] doesn't exist in Native Array");
        return rtn;
    } else // Node
    if (coll instanceof Node) {
        return XMLStructFactory.newInstance((Node) coll, false).get(key);
    }
    // Direct Object Access
    if (pc.getConfig().getSecurityManager().getAccess(SecurityManager.TYPE_DIRECT_JAVA_ACCESS) == SecurityManager.VALUE_YES) {
        return Reflector.getProperty(coll, key);
    }
    throw new ExpressionException("No matching property [" + key + "] found");
}
Also used : Node(org.w3c.dom.Node) Objects(lucee.runtime.type.Objects) Collection(lucee.runtime.type.Collection) List(java.util.List) Map(java.util.Map) 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