Search in sources :

Example 11 with StringList

use of lucee.commons.lang.StringList in project Lucee by lucee.

the class VariableInterpreter method parse.

public static StringList parse(String var, boolean doLowerCase) {
    ParserString ps = new ParserString(var);
    String id = readIdentifier(ps, doLowerCase);
    if (id == null)
        return null;
    StringList list = new StringList(id);
    while (true) {
        if (ps.forwardIfCurrent('.')) {
            id = readIdentifier(ps, doLowerCase);
            if (id == null)
                return null;
            list.add(id);
        } else
            break;
    }
    if (ps.isValidIndex())
        return null;
    list.reset();
    return list;
}
Also used : ParserString(lucee.commons.lang.ParserString) StringList(lucee.commons.lang.StringList) ParserString(lucee.commons.lang.ParserString)

Example 12 with StringList

use of lucee.commons.lang.StringList in project Lucee by lucee.

the class VariableInterpreter method getVariableELAsCollection.

public static Object getVariableELAsCollection(PageContext pc, String var, Object defaultValue) {
    StringList list = parse(pc, new ParserString(var), false);
    if (list == null)
        return defaultValue;
    int scope = scopeString2Int(pc.ignoreScopes(), list.next());
    Object coll = null;
    if (scope == Scope.SCOPE_UNDEFINED) {
        try {
            coll = pc.undefinedScope().getCollection(list.current());
        } catch (PageException e) {
            coll = null;
        }
        if (coll == null)
            return defaultValue;
    } else {
        try {
            coll = VariableInterpreter.scope(pc, scope, list.hasNext());
        // coll=pc.scope(scope);
        } catch (PageException e) {
            return defaultValue;
        }
    }
    while (list.hasNext()) {
        coll = pc.getVariableUtil().getCollection(pc, coll, list.next(), null);
        if (coll == null)
            return defaultValue;
    }
    return coll;
}
Also used : PageException(lucee.runtime.exp.PageException) StringList(lucee.commons.lang.StringList) ParserString(lucee.commons.lang.ParserString)

Example 13 with StringList

use of lucee.commons.lang.StringList in project Lucee by lucee.

the class VariableInterpreter method getVariableEL.

/**
 * get a variable from page context
 * @param pc Page Context
 * @param var variable string to get value to
 * @param defaultValue value returnded if variable was not found
 * @return the value or default value if not found
 */
public static Object getVariableEL(PageContext pc, String var, Object defaultValue) {
    StringList list = parse(pc, new ParserString(var), false);
    if (list == null)
        return defaultValue;
    int scope = scopeString2Int(pc.ignoreScopes(), list.next());
    Object coll = null;
    if (scope == Scope.SCOPE_UNDEFINED) {
        coll = pc.undefinedScope().get(KeyImpl.init(list.current()), NullSupportHelper.NULL());
        if (coll == NullSupportHelper.NULL())
            return defaultValue;
    } else {
        try {
            coll = VariableInterpreter.scope(pc, scope, list.hasNext());
        // coll=pc.scope(scope);
        } catch (PageException e) {
            return defaultValue;
        }
    }
    while (list.hasNext()) {
        coll = pc.getVariableUtil().get(pc, coll, KeyImpl.init(list.next()), NullSupportHelper.NULL());
        if (coll == NullSupportHelper.NULL())
            return defaultValue;
    }
    return coll;
}
Also used : PageException(lucee.runtime.exp.PageException) StringList(lucee.commons.lang.StringList) ParserString(lucee.commons.lang.ParserString)

Example 14 with StringList

use of lucee.commons.lang.StringList in project Lucee by lucee.

the class IsDefined method execute.

@Override
public void execute(BIF bif, FunctionLibFunction flf) throws TemplateException {
    Argument arg = bif.getArguments()[0];
    Expression value = arg.getValue();
    if (value instanceof LitString) {
        String str = ((LitString) value).getString();
        StringList sl = VariableInterpreter.parse(str, false);
        if (sl != null) {
            // scope
            str = sl.next();
            int scope = VariableInterpreter.scopeString2Int(bif.ts.ignoreScopes, str);
            if (scope == Scope.SCOPE_UNDEFINED)
                sl.reset();
            // keys
            String[] arr = sl.toArray();
            ArrayUtil.trim(arr);
            // update first arg
            arg.setValue(bif.getFactory().createLitDouble(scope), "number");
            if (arr.length == 1) {
                // LitString.toExprString(str);
                Expression expr = new CollectionKey(bif.getFactory(), arr[0]);
                arg = new Argument(expr, Collection.Key.class.getName());
                bif.addArgument(arg);
            } else {
                CollectionKeyArray expr = new CollectionKeyArray(bif.getFactory(), arr);
                // LiteralStringArray expr = new LiteralStringArray(arr);
                arg = new Argument(expr, Collection.Key[].class.getName());
                bif.addArgument(arg);
            }
        }
    }
// print.out("bif:"+arg.getValue().getClass().getName());
}
Also used : LitString(lucee.transformer.expression.literal.LitString) CollectionKeyArray(lucee.transformer.bytecode.expression.type.CollectionKeyArray) Argument(lucee.transformer.bytecode.expression.var.Argument) Expression(lucee.transformer.expression.Expression) StringList(lucee.commons.lang.StringList) Collection(lucee.runtime.type.Collection) CollectionKey(lucee.transformer.bytecode.expression.type.CollectionKey) LitString(lucee.transformer.expression.literal.LitString) CollectionKey(lucee.transformer.bytecode.expression.type.CollectionKey)

Example 15 with StringList

use of lucee.commons.lang.StringList in project Lucee by lucee.

the class VariableInterpreter method isDefined.

/**
 * check if a variable is defined in Page Context
 * @param pc PageContext to check
 * @param var variable String
 * @return exists or not
 */
public static boolean isDefined(PageContext pc, String var) {
    StringList list = parse(pc, new ParserString(var), false);
    if (list == null)
        return false;
    try {
        int scope = scopeString2Int(pc.ignoreScopes(), list.next());
        Object coll = NULL;
        if (scope == Scope.SCOPE_UNDEFINED) {
            coll = pc.undefinedScope().get(list.current(), null);
            if (coll == null)
                return false;
        } else {
            coll = VariableInterpreter.scope(pc, scope, list.hasNext());
        // coll=pc.scope(scope);
        }
        while (list.hasNext()) {
            coll = pc.getVariableUtil().getCollection(pc, coll, list.next(), null);
            if (coll == null)
                return false;
        }
    } catch (PageException e) {
        return false;
    }
    return true;
}
Also used : PageException(lucee.runtime.exp.PageException) StringList(lucee.commons.lang.StringList) ParserString(lucee.commons.lang.ParserString)

Aggregations

StringList (lucee.commons.lang.StringList)22 ParserString (lucee.commons.lang.ParserString)10 PageException (lucee.runtime.exp.PageException)4 Collection (lucee.runtime.type.Collection)2 URI (java.net.URI)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 CastableStruct (lucee.runtime.type.CastableStruct)1 Struct (lucee.runtime.type.Struct)1 VariableReference (lucee.runtime.type.ref.VariableReference)1 CollectionKey (lucee.transformer.bytecode.expression.type.CollectionKey)1 CollectionKeyArray (lucee.transformer.bytecode.expression.type.CollectionKeyArray)1 Argument (lucee.transformer.bytecode.expression.var.Argument)1 Expression (lucee.transformer.expression.Expression)1 LitString (lucee.transformer.expression.literal.LitString)1 Version (org.osgi.framework.Version)1