Search in sources :

Example 31 with ArrayImpl

use of lucee.runtime.type.ArrayImpl in project Lucee by lucee.

the class ArgumentImpl method toArray.

/**
 * converts a argument scope to a regular array
 * @param arg argument scope to convert
 * @return resulting array
 */
public static Array toArray(Argument arg) {
    ArrayImpl trg = new ArrayImpl();
    int[] keys = arg.intKeys();
    for (int i = 0; i < keys.length; i++) {
        trg.setEL(keys[i], arg.get(keys[i], null));
    }
    return trg;
}
Also used : ArrayImpl(lucee.runtime.type.ArrayImpl)

Example 32 with ArrayImpl

use of lucee.runtime.type.ArrayImpl in project Lucee by lucee.

the class ScopeSupport method _fill.

private Struct _fill(final Struct parent, String name, Object value, boolean isLast, boolean scriptProteced, boolean sameAsArray) {
    Object curr;
    boolean isArrayDef = sameAsArray;
    Collection.Key key = KeyImpl.init(name);
    // script protect
    if (scriptProteced && value instanceof String) {
        value = ScriptProtect.translate((String) value);
    }
    if (name.length() > 2 && name.endsWith("[]")) {
        isArrayDef = true;
        name = name.substring(0, name.length() - 2);
        key = KeyImpl.getInstance(name);
        curr = parent.get(key, null);
    } else {
        curr = parent.get(key, null);
    }
    if (curr == null) {
        if (isArrayDef) {
            Array arr = new ArrayImpl();
            arr.appendEL(value);
            parent.setEL(key, arr);
        } else
            parent.setEL(key, value);
    } else if (curr instanceof Array) {
        ((Array) curr).appendEL(value);
    } else if (curr instanceof CastableStruct) {
        if (isLast)
            ((CastableStruct) curr).setValue(value);
        else
            return (Struct) curr;
    } else if (curr instanceof Struct) {
        if (isLast)
            parent.setEL(key, value);
        else
            return (Struct) curr;
    } else if (curr instanceof String) {
        if (isArrayDef) {
            Array arr = new ArrayImpl();
            arr.appendEL(curr);
            arr.appendEL(value);
            parent.setEL(key, arr);
        } else if (value instanceof Struct) {
            parent.setEL(key, value);
        } else {
            if (!StringUtil.isEmpty(value)) {
                String existing = Caster.toString(curr, "");
                if (StringUtil.isEmpty(existing))
                    parent.setEL(key, value);
                else
                    parent.setEL(key, Caster.toString(curr, "") + ',' + value);
            }
        }
    }
    if (!isLast) {
        return (Struct) value;
    }
    return null;
}
Also used : Array(lucee.runtime.type.Array) ArrayImpl(lucee.runtime.type.ArrayImpl) Collection(lucee.runtime.type.Collection) CastableStruct(lucee.runtime.type.CastableStruct) Struct(lucee.runtime.type.Struct) CastableStruct(lucee.runtime.type.CastableStruct)

Example 33 with ArrayImpl

use of lucee.runtime.type.ArrayImpl in project Lucee by lucee.

the class ListUtil method listToArray.

/**
 * casts a list to Array object
 * @param list list to cast
 * @param delimiter delimter of the list
 * @return Array Object
 */
public static Array listToArray(String list, String delimiter) {
    if (delimiter.length() == 1)
        return listToArray(list, delimiter.charAt(0));
    if (list.length() == 0)
        return new ArrayImpl();
    if (delimiter.length() == 0) {
        int len = list.length();
        ArrayImpl array = new ArrayImpl();
        // ACF compatibility
        array.appendEL("");
        for (int i = 0; i < len; i++) {
            array.appendEL(list.charAt(i));
        }
        // ACF compatibility
        array.appendEL("");
        return array;
    }
    int len = list.length();
    int last = 0;
    char[] del = delimiter.toCharArray();
    char c;
    ArrayImpl array = new ArrayImpl();
    // try{
    for (int i = 0; i < len; i++) {
        c = list.charAt(i);
        for (int y = 0; y < del.length; y++) {
            if (c == del[y]) {
                array.appendEL(list.substring(last, i));
                last = i + 1;
                break;
            }
        }
    }
    if (last <= len)
        array.appendEL(list.substring(last));
    // }catch(ExpressionException e){}
    return array;
}
Also used : ArrayImpl(lucee.runtime.type.ArrayImpl)

Example 34 with ArrayImpl

use of lucee.runtime.type.ArrayImpl in project Lucee by lucee.

the class ListUtil method listToArray.

private static Array listToArray(String list, String delimiter, boolean multiCharDelim) {
    if (!multiCharDelim || delimiter.length() == 0)
        return listToArray(list, delimiter);
    if (delimiter.length() == 1)
        return listToArray(list, delimiter.charAt(0));
    int len = list.length();
    if (len == 0)
        return new ArrayImpl();
    Array array = new ArrayImpl();
    int from = 0;
    int index;
    int dl = delimiter.length();
    while ((index = list.indexOf(delimiter, from)) != -1) {
        array.appendEL(list.substring(from, index));
        from = index + dl;
    }
    array.appendEL(list.substring(from, len));
    return array;
}
Also used : Array(lucee.runtime.type.Array) ArrayImpl(lucee.runtime.type.ArrayImpl)

Example 35 with ArrayImpl

use of lucee.runtime.type.ArrayImpl in project Lucee by lucee.

the class ListUtil method listToArrayRemoveEmpty.

/**
 * casts a list to Array object remove Empty Elements
 * @param list list to cast
 * @param delimiter delimter of the list
 * @return Array Object
 */
private static Array listToArrayRemoveEmpty(String list, String delimiter, boolean multiCharDelim) {
    if (!multiCharDelim || delimiter.length() == 0)
        return listToArrayRemoveEmpty(list, delimiter);
    if (delimiter.length() == 1)
        return listToArrayRemoveEmpty(list, delimiter.charAt(0));
    int len = list.length();
    if (len == 0)
        return new ArrayImpl();
    Array array = new ArrayImpl();
    int from = 0;
    int index;
    int dl = delimiter.length();
    while ((index = list.indexOf(delimiter, from)) != -1) {
        if (from < index)
            array.appendEL(list.substring(from, index));
        from = index + dl;
    }
    if (from < len)
        array.appendEL(list.substring(from, len));
    return array;
}
Also used : Array(lucee.runtime.type.Array) ArrayImpl(lucee.runtime.type.ArrayImpl)

Aggregations

ArrayImpl (lucee.runtime.type.ArrayImpl)100 Array (lucee.runtime.type.Array)79 Struct (lucee.runtime.type.Struct)33 StructImpl (lucee.runtime.type.StructImpl)24 PageException (lucee.runtime.exp.PageException)14 Entry (java.util.Map.Entry)13 Key (lucee.runtime.type.Collection.Key)12 ArgumentIntKey (lucee.runtime.type.scope.ArgumentIntKey)8 IOException (java.io.IOException)7 Iterator (java.util.Iterator)7 ArrayList (java.util.ArrayList)6 ListIterator (java.util.ListIterator)6 FunctionException (lucee.runtime.exp.FunctionException)6 ForEachQueryIterator (lucee.runtime.type.it.ForEachQueryIterator)6 ApplicationException (lucee.runtime.exp.ApplicationException)5 ExpressionException (lucee.runtime.exp.ExpressionException)5 Query (lucee.runtime.type.Query)5 QueryImpl (lucee.runtime.type.QueryImpl)5 ListAsArray (lucee.runtime.type.wrap.ListAsArray)5 NodeList (org.w3c.dom.NodeList)5