Search in sources :

Example 1 with CastableStruct

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

the class ScopeSupport method fillDecoded.

/**
 * fill th data from given strut and decode it
 *
 * @param raw
 * @param encoding
 * @throws UnsupportedEncodingException
 */
protected void fillDecoded(URLItem[] raw, String encoding, boolean scriptProteced, boolean sameAsArray) throws UnsupportedEncodingException {
    clear();
    String name, value;
    // Object curr;
    for (int i = 0; i < raw.length; i++) {
        name = raw[i].getName();
        value = raw[i].getValue();
        if (raw[i].isUrlEncoded()) {
            name = URLDecoder.decode(name, encoding, true);
            value = URLDecoder.decode(value, encoding, true);
        }
        // MUST valueStruct
        if (name.indexOf('.') != -1) {
            StringList list = ListUtil.listToStringListRemoveEmpty(name, '.');
            if (list.size() > 0) {
                Struct parent = this;
                while (list.hasNextNext()) {
                    parent = _fill(parent, list.next(), new CastableStruct(Struct.TYPE_LINKED), false, scriptProteced, sameAsArray);
                }
                _fill(parent, list.next(), value, true, scriptProteced, sameAsArray);
            }
        }
        // else
        _fill(this, name, value, true, scriptProteced, sameAsArray);
    }
}
Also used : StringList(lucee.commons.lang.StringList) CastableStruct(lucee.runtime.type.CastableStruct) Struct(lucee.runtime.type.Struct) CastableStruct(lucee.runtime.type.CastableStruct)

Example 2 with CastableStruct

use of lucee.runtime.type.CastableStruct 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)

Aggregations

CastableStruct (lucee.runtime.type.CastableStruct)2 Struct (lucee.runtime.type.Struct)2 StringList (lucee.commons.lang.StringList)1 Array (lucee.runtime.type.Array)1 ArrayImpl (lucee.runtime.type.ArrayImpl)1 Collection (lucee.runtime.type.Collection)1