001/*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019package org.crsh.lang.groovy.closure;
020
021import org.crsh.command.CommandCreationException;
022import org.crsh.command.CommandInvoker;
023import org.crsh.command.ShellCommand;
024
025import java.util.Collections;
026import java.util.Iterator;
027import java.util.List;
028import java.util.Map;
029
030/** @author Julien Viet */
031class CommandElement extends PipeLineElement {
032
033  /** . */
034  final String commandName;
035
036  /** . */
037  final ShellCommand command;
038
039  /** . */
040  final String name;
041
042  /** . */
043  final Map<String, Object> options;
044
045  /** . */
046  final List<Object> args;
047
048  public CommandElement(String commandName, ShellCommand command, String name) {
049    this.commandName = commandName;
050    this.command = command;
051    this.name = name;
052    this.options = null;
053    this.args = null;
054  }
055
056  public CommandElement(String commandName, ShellCommand command, String name, Map<String, Object> options, List<Object> args) {
057    this.commandName = commandName;
058    this.command = command;
059    this.name = name;
060    this.options = options;
061    this.args = args;
062  }
063
064  @Override
065  CommandInvoker make() throws CommandCreationException {
066    return command.resolveInvoker(
067        name != null ? name : "",
068        options != null ? options : Collections.<String, Object>emptyMap(),
069        args != null ? args : Collections.<Object>emptyList());
070  }
071
072  private void format(Object o, StringBuilder buffer) {
073    if (o instanceof String) {
074      buffer.append('"').append(o).append('"');
075    } else if (o instanceof Boolean || o instanceof Number) {
076      buffer.append(o);
077    } else {
078      buffer.append('<').append(o).append('>');
079    }
080  }
081
082  void toString(StringBuilder buffer) {
083    buffer.append(commandName);
084    boolean hasOptions = options != null && options.size() > 0;
085    boolean hasArguments = args != null && args.size() > 0;
086    if (hasOptions || hasArguments) {
087      buffer.append(" {");
088      if (hasOptions) {
089        for (Iterator<Map.Entry<String, Object>> i = options.entrySet().iterator();i.hasNext();) {
090          Map.Entry<String, Object> option = i.next();
091          buffer.append(' ').append(option.getKey()).append('=');
092          format(option.getValue(), buffer);
093          if (i.hasNext()) {
094            buffer.append(";");
095          }
096        }
097        if (hasArguments) {
098          buffer.append(";");
099        }
100      }
101      if (hasArguments) {
102        buffer.append(" [");
103        for (Iterator<Object> i = args.iterator();i.hasNext();) {
104          Object arg = i.next();
105          format(arg, buffer);
106          if (i.hasNext()) {
107            buffer.append(", ");
108          }
109        }
110        buffer.append("]");
111      }
112      buffer.append(" }");
113    }
114  }
115}