/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2011-2016 Incapture Technologies LLC
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package reflex.node;

import com.google.common.collect.ImmutableList;

import reflex.IReflexHandler;
import reflex.ReflexException;
import reflex.ReflexParser;
import reflex.Scope;
import reflex.debug.IReflexDebugger;
import reflex.value.ReflexValue;
import reflex.value.internal.ReflexNullValue;
import reflex.value.internal.ReflexUndefinedValue;

public class IdentifierNode extends BaseNode {

    private String identifier;
    private String namespacePrefix;

    public IdentifierNode(int lineNumber, IReflexHandler handler, Scope scope, String id, String namespacePrefix) {
        super(lineNumber, handler, scope);
        identifier = id;
        this.namespacePrefix = namespacePrefix;
    }

    @Override
    public ReflexValue evaluate(IReflexDebugger debugger, Scope scope) {
        debugger.stepStart(this, scope);
        // RAP-2872
        // First attempt to resolve the usual way (fast fix)
        // If not around, try to search from the first dotted part for a variable until we get
        // a resolution. If we resolve to something and that variable type is a map, use the remaining
        // part to resolve to a value using map lookup semantics.

        ReflexValue value;

        if (identifier.equals("reserved")) {
            value = new ReflexValue(ImmutableList.copyOf(ReflexParser.tokenNames));
        } else {
            value = scope.resolve(identifier, namespacePrefix);
            if (value == null) {
                value = resolveMapDotter(scope);
                if (value == null) {
                    value = new ReflexUndefinedValue(lineNumber);
                }
            }
        }
        debugger.stepEnd(this, value, scope);
        return value;
    }

    @Override
    public String toString() {
        return identifier;
    }

    private ReflexValue resolveMapDotter(Scope scope) {
        String[] allParts = identifier.split("\\.");
        StringBuilder currentResolve = new StringBuilder();
        for (int pos = 0; pos < allParts.length; pos++) {
            if (currentResolve.length() != 0) {
                currentResolve.append(".");
            }
            currentResolve.append(allParts[pos]);
            ReflexValue value = scope.resolve(currentResolve.toString(), namespacePrefix);
            if (value == null) {
                throw new ReflexException(lineNumber, "no such variable: " + currentResolve.toString());
            } else if (value.isMap()) {
                ReflexValue innerVal = value;
                int ender = pos + 1;
                for (; ender < allParts.length; ender++) {
                    if (!innerVal.asMap().containsKey(allParts[ender])) {
                        innerVal = null;
                        break;
                    }
                    Object v = innerVal.asMap().get(allParts[ender]);
                    innerVal = (v == null) 
                            ? new ReflexNullValue(lineNumber) 
                            : (v instanceof ReflexValue) 
                                ? (ReflexValue) v 
                                : new ReflexValue(v);
                    if (!innerVal.isMap()) break;
                }
                if (ender < allParts.length - 1) throw new ReflexException(lineNumber, "no such variable: " + this);
                return innerVal;
            }
        }
        return null;
    }
}

@@ -1184,57 +1184,8 @@
 e;%0A%0A
-import com.google.common.collect.ImmutableList;%0A%0A
 impo
@@ -1245,36 +1245,8 @@
 on;%0A
-import reflex.ReflexParser;%0A
 impo
@@ -2283,174 +2283,8 @@
 lue 
-value;%0A%0A        if (identifier.equals(%22reserved%22)) %7B%0A            value = new ReflexValue(ImmutableList.copyOf(ReflexParser.tokenNames));%0A        %7D else %7B%0A            
 valu
@@ -2331,36 +2331,32 @@
 refix);%0A        
-   
- 
 if (value == nul
@@ -2352,36 +2352,32 @@
 alue == null) %7B%0A
-    
             valu
@@ -2409,36 +2409,32 @@
 e);%0A            
- 
-   
 if (value == nul
@@ -2430,36 +2430,32 @@
 alue == null) %7B%0A
-    
                 
@@ -2500,34 +2500,16 @@
 umber);%0A
-                %7D%0A
         

Revert change to IdentifierNode.java