// Path: gshell-core/src/main/java/org/apache/geronimo/gshell/Shell.java
// public class Shell
// {
//     //
//     // TODO: Introduce Shell interface?
//     //
// 
//     private static final Log log = LogFactory.getLog(Shell.class);
// 
//     private final IO io;
// 
//     private final ShellContainer shellContainer = new ShellContainer();
// 
//     private final CommandManager commandManager;
// 
//     private final CommandLineBuilder commandLineBuilder;
// 
//     private final Variables variables = new VariablesImpl();
// 
//     public Shell(final IO io) throws CommandException {
//         if (io == null) {
//             throw new NullArgumentException("io");
//         }
// 
//         this.io = io;
// 
//         shellContainer.registerComponentInstance(this);
//         shellContainer.registerComponentImplementation(CommandManagerImpl.class);
//         shellContainer.registerComponentImplementation(CommandLineBuilder.class);
// 
//         //
//         // TODO: Refactor to use the container, now that we have one
//         //
// 
//         this.commandManager = (CommandManager) shellContainer.getComponentInstanceOfType(CommandManager.class);
//         this.commandLineBuilder = (CommandLineBuilder) shellContainer.getComponentInstanceOfType(CommandLineBuilder.class);
// 
//         //
//         // HACK: Set some default variables
//         //
// 
//         variables.set(StandardVariables.PROMPT, "> ");
//     }
// 
//     public Shell() throws CommandException {
//         this(new IO());
//     }
// 
//     public Variables getVariables() {
//         return variables;
//     }
// 
//     public IO getIO() {
//         return io;
//     }
// 
//     public CommandManager getCommandManager() {
//         return commandManager;
//     }
// 
//     public Object execute(final String commandLine) throws Exception {
//         assert commandLine != null;
// 
//         if (log.isInfoEnabled()) {
//             log.info("Executing (String): " + commandLine);
//         }
// 
//         CommandLine cl = commandLineBuilder.create(commandLine);
//         return cl.execute();
//     }
// 
//     //
//     // CommandExecutor
//     //
// 
//     private void dump(final Variables vars) {
//         Iterator<String> iter = vars.names();
// 
//         if (iter.hasNext()) {
//             log.debug("Variables:");
//         }
// 
//         while (iter.hasNext()) {
//             String name = iter.next();
// 
//             log.debug("    " + name + "=" + vars.get(name));
//         }
//     }
// 
//     public Object execute(final String commandName, final Object[] args) throws Exception {
//         assert commandName != null;
//         assert args != null;
// 
//         boolean debug = log.isDebugEnabled();
// 
//         if (log.isInfoEnabled()) {
//             log.info("Executing (" + commandName + "): " + Arguments.asString(args));
//         }
// 
//         // Setup the command container
//         ShellContainer container = new ShellContainer(shellContainer);
// 
//         final CommandDefinition def = commandManager.getCommandDefinition(commandName);
//         final Class type = def.loadClass();
// 
//         //
//         // TODO: Pass the command instance the name it was registered with?, could be an alias
//         //
// 
//         container.registerComponentInstance(def);
//         container.registerComponentImplementation(type);
// 
//         // container.start() ?
// 
//         Command cmd = (Command) container.getComponentInstanceOfType(Command.class);
// 
//         //
//         // TODO: DI all bits if we can, then free up "context" to replace "category" as a term
//         //
// 
//         final Variables vars = new VariablesImpl(getVariables());
// 
//         cmd.init(new CommandContext() {
//             public IO getIO() {
//                 return io;
//             }
// 
//             public Variables getVariables() {
//                 return vars;
//             }
// 
//             MessageSource messageSource;
// 
//             public MessageSource getMessageSource() {
//                 // Lazy init the messages, commands many not need them
//                 if (messageSource == null) {
//                     messageSource = new MessageSourceImpl(type.getName() + "Messages");
//                 }
// 
//                 return messageSource;
//             }
//         });
// 
//         // Setup command timings
//         StopWatch watch = null;
//         if (debug) {
//             watch = new StopWatch();
//             watch.start();
//         }
// 
//         Object result;
//         try {
//             result = cmd.execute(args);
// 
//             if (debug) {
//                 log.debug("Command completed in " + watch);
//             }
//         }
//         finally {
//             cmd.destroy();
// 
//             shellContainer.removeChildContainer(container);
//             // container.stop() container.dispose() ?
//         }
// 
//         return result;
//     }
// 
//     public Object execute(final Object... args) throws Exception {
//         assert args != null;
//         assert args.length > 1;
// 
//         if (log.isInfoEnabled()) {
//             log.info("Executing (Object...): " + Arguments.asString(args));
//         }
// 
//         return execute(String.valueOf(args[0]), Arguments.shift(args));
//     }
// }


import java.io.StringReader;
import org.apache.geronimo.gshell.commandline.parser.CommandLineParser;
import org.apache.geronimo.gshell.commandline.parser.ASTCommandLine;
import org.apache.geronimo.gshell.commandline.parser.ParseException;
import org.apache.geronimo.gshell.Shell;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.lang.NullArgumentException;
import java.io.Reader;
/*
 * 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;





/**
 * Builds {@link CommandLine} instances ready for executing.
 *
 * @version $Rev$ $Date$
 */
public class CommandLineBuilder
{
    private static final Log log = LogFactory.getLog(CommandLineBuilder.class);

// Path: gshell-core/src/main/java/org/apache/geronimo/gshell/Shell.java
// public class Shell
// {
//     //
//     // TODO: Introduce Shell interface?
//     //
// 
//     private static final Log log = LogFactory.getLog(Shell.class);
// 
//     private final IO io;
// 
//     private final ShellContainer shellContainer = new ShellContainer();
// 
//     private final CommandManager commandManager;
// 
//     private final CommandLineBuilder commandLineBuilder;
// 
//     private final Variables variables = new VariablesImpl();
// 
//     public Shell(final IO io) throws CommandException {
//         if (io == null) {
//             throw new NullArgumentException("io");
//         }
// 
//         this.io = io;
// 
//         shellContainer.registerComponentInstance(this);
//         shellContainer.registerComponentImplementation(CommandManagerImpl.class);
//         shellContainer.registerComponentImplementation(CommandLineBuilder.class);
// 
//         //
//         // TODO: Refactor to use the container, now that we have one
//         //
// 
//         this.commandManager = (CommandManager) shellContainer.getComponentInstanceOfType(CommandManager.class);
//         this.commandLineBuilder = (CommandLineBuilder) shellContainer.getComponentInstanceOfType(CommandLineBuilder.class);
// 
//         //
//         // HACK: Set some default variables
//         //
// 
//         variables.set(StandardVariables.PROMPT, "> ");
//     }
// 
//     public Shell() throws CommandException {
//         this(new IO());
//     }
// 
//     public Variables getVariables() {
//         return variables;
//     }
// 
//     public IO getIO() {
//         return io;
//     }
// 
//     public CommandManager getCommandManager() {
//         return commandManager;
//     }
// 
//     public Object execute(final String commandLine) throws Exception {
//         assert commandLine != null;
// 
//         if (log.isInfoEnabled()) {
//             log.info("Executing (String): " + commandLine);
//         }
// 
//         CommandLine cl = commandLineBuilder.create(commandLine);
//         return cl.execute();
//     }
// 
//     //
//     // CommandExecutor
//     //
// 
//     private void dump(final Variables vars) {
//         Iterator<String> iter = vars.names();
// 
//         if (iter.hasNext()) {
//             log.debug("Variables:");
//         }
// 
//         while (iter.hasNext()) {
//             String name = iter.next();
// 
//             log.debug("    " + name + "=" + vars.get(name));
//         }
//     }
// 
//     public Object execute(final String commandName, final Object[] args) throws Exception {
//         assert commandName != null;
//         assert args != null;
// 
//         boolean debug = log.isDebugEnabled();
// 
//         if (log.isInfoEnabled()) {
//             log.info("Executing (" + commandName + "): " + Arguments.asString(args));
//         }
// 
//         // Setup the command container
//         ShellContainer container = new ShellContainer(shellContainer);
// 
//         final CommandDefinition def = commandManager.getCommandDefinition(commandName);
//         final Class type = def.loadClass();
// 
//         //
//         // TODO: Pass the command instance the name it was registered with?, could be an alias
//         //
// 
//         container.registerComponentInstance(def);
//         container.registerComponentImplementation(type);
// 
//         // container.start() ?
// 
//         Command cmd = (Command) container.getComponentInstanceOfType(Command.class);
// 
//         //
//         // TODO: DI all bits if we can, then free up "context" to replace "category" as a term
//         //
// 
//         final Variables vars = new VariablesImpl(getVariables());
// 
//         cmd.init(new CommandContext() {
//             public IO getIO() {
//                 return io;
//             }
// 
//             public Variables getVariables() {
//                 return vars;
//             }
// 
//             MessageSource messageSource;
// 
//             public MessageSource getMessageSource() {
//                 // Lazy init the messages, commands many not need them
//                 if (messageSource == null) {
//                     messageSource = new MessageSourceImpl(type.getName() + "Messages");
//                 }
// 
//                 return messageSource;
//             }
//         });
// 
//         // Setup command timings
//         StopWatch watch = null;
//         if (debug) {
//             watch = new StopWatch();
//             watch.start();
//         }
// 
//         Object result;
//         try {
//             result = cmd.execute(args);
// 
//             if (debug) {
//                 log.debug("Command completed in " + watch);
//             }
//         }
//         finally {
//             cmd.destroy();
// 
//             shellContainer.removeChildContainer(container);
//             // container.stop() container.dispose() ?
//         }
// 
//         return result;
//     }
// 
//     public Object execute(final Object... args) throws Exception {
//         assert args != null;
//         assert args.length > 1;
// 
//         if (log.isInfoEnabled()) {
//             log.info("Executing (Object...): " + Arguments.asString(args));
//         }
// 
//         return execute(String.valueOf(args[0]), Arguments.shift(args));
//     }
// }


// Path: gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/CommandLineBuilder.java
import java.io.StringReader;
import org.apache.geronimo.gshell.commandline.parser.CommandLineParser;
import org.apache.geronimo.gshell.commandline.parser.ASTCommandLine;
import org.apache.geronimo.gshell.commandline.parser.ParseException;
import org.apache.geronimo.gshell.Shell;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.lang.NullArgumentException;
import java.io.Reader;

/*
 * 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;





/**
 * Builds {@link CommandLine} instances ready for executing.
 *
 * @version $Rev$ $Date$
 */
public class CommandLineBuilder
{
    private static final Log log = LogFactory.getLog(CommandLineBuilder.class);

    private final Shell shell;