// Path: gshell-api/src/main/java/org/apache/geronimo/gshell/command/Variables.java
// public interface Variables
// {
//     void set(String name, Object value) throws ImmutableVariableException;
// 
//     void set(String name, Object value, boolean mutable) throws ImmutableVariableException;
// 
//     Object get(String name);
// 
//     Object get(String name, Object _default);
// 
//     boolean isMutable(String name);
// 
//     boolean isCloaked(String name);
// 
//     void unset(String name) throws ImmutableVariableException;
//     
//     boolean contains(String name);
//     
//     Iterator<String> names();
// 
//     Variables parent();
// 
//     //
//     // Exceptions
//     //
// 
//     class ImmutableVariableException
//         extends RuntimeException
//     {
//         ///CLOVER:OFF
//         
//         public ImmutableVariableException(final String name) {
//             super("Variable is immutable: " + name);
//         }
//     }
// }


import org.apache.commons.jexl.resolver.FlatResolver;
import org.apache.commons.lang.NullArgumentException;
import org.apache.geronimo.gshell.command.Variables;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.jexl.JexlHelper;
import org.apache.commons.jexl.JexlContext;
import org.apache.commons.jexl.Expression;
import org.apache.commons.jexl.ExpressionFactory;
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.geronimo.gshell.commandline;





/**
 * Parser to handle ${...} expressions using
 * <a href="http://jakarta.apache.org/commons/jexl/">JEXL</a>.
 *
 * <p>
 * Supports complex ${xxx} and simple $xxx expressions
 *
 * @version $Rev$ $Date$
 */
public class VariableExpressionParser
{
    //
    // NOTE: May want to add the ${...} bits the the CommandLineParser directly.
    //       This sub-parser is probably only short-term so we can get soemthing working
    //

    private static final Log log = LogFactory.getLog(VariableExpressionParser.class);

    protected JexlContext context;

    public VariableExpressionParser(final Map vars) {
        if (vars == null) {
            throw new NullArgumentException("vars");
        }

        context = JexlHelper.createContext();
        context.setVars(vars);

        if (log.isTraceEnabled()) {
            log.trace("Using variables: " + context.getVars());
        }
    }

// Path: gshell-api/src/main/java/org/apache/geronimo/gshell/command/Variables.java
// public interface Variables
// {
//     void set(String name, Object value) throws ImmutableVariableException;
// 
//     void set(String name, Object value, boolean mutable) throws ImmutableVariableException;
// 
//     Object get(String name);
// 
//     Object get(String name, Object _default);
// 
//     boolean isMutable(String name);
// 
//     boolean isCloaked(String name);
// 
//     void unset(String name) throws ImmutableVariableException;
//     
//     boolean contains(String name);
//     
//     Iterator<String> names();
// 
//     Variables parent();
// 
//     //
//     // Exceptions
//     //
// 
//     class ImmutableVariableException
//         extends RuntimeException
//     {
//         ///CLOVER:OFF
//         
//         public ImmutableVariableException(final String name) {
//             super("Variable is immutable: " + name);
//         }
//     }
// }


// Path: gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/VariableExpressionParser.java
import org.apache.commons.jexl.resolver.FlatResolver;
import org.apache.commons.lang.NullArgumentException;
import org.apache.geronimo.gshell.command.Variables;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.jexl.JexlHelper;
import org.apache.commons.jexl.JexlContext;
import org.apache.commons.jexl.Expression;
import org.apache.commons.jexl.ExpressionFactory;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.geronimo.gshell.commandline;





/**
 * Parser to handle ${...} expressions using
 * <a href="http://jakarta.apache.org/commons/jexl/">JEXL</a>.
 *
 * <p>
 * Supports complex ${xxx} and simple $xxx expressions
 *
 * @version $Rev$ $Date$
 */
public class VariableExpressionParser
{
    //
    // NOTE: May want to add the ${...} bits the the CommandLineParser directly.
    //       This sub-parser is probably only short-term so we can get soemthing working
    //

    private static final Log log = LogFactory.getLog(VariableExpressionParser.class);

    protected JexlContext context;

    public VariableExpressionParser(final Map vars) {
        if (vars == null) {
            throw new NullArgumentException("vars");
        }

        context = JexlHelper.createContext();
        context.setVars(vars);

        if (log.isTraceEnabled()) {
            log.trace("Using variables: " + context.getVars());
        }
    }

    private static Map convertToMap(final Variables vars) {