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     */
019    package org.crsh.lang.groovy.closure;
020    
021    import org.crsh.command.CommandInvoker;
022    import org.crsh.command.ShellCommand;
023    
024    import java.util.ArrayList;
025    import java.util.Collections;
026    import java.util.HashMap;
027    import java.util.Iterator;
028    import java.util.List;
029    import java.util.Map;
030    
031    /** @author Julien Viet */
032    class CommandElement extends PipeLineElement {
033    
034      /** . */
035      final String commandName;
036    
037      /** . */
038      final ShellCommand command;
039    
040      /** . */
041      final String name;
042    
043      /** . */
044      final Map<String, Object> options;
045    
046      /** . */
047      final List<Object> args;
048    
049      public CommandElement(String commandName, ShellCommand command, String name) {
050        this.commandName = commandName;
051        this.command = command;
052        this.name = name;
053        this.options = null;
054        this.args = null;
055      }
056    
057      public CommandElement(String commandName, ShellCommand command, String name, Map<String, Object> options, List<Object> args) {
058        this.commandName = commandName;
059        this.command = command;
060        this.name = name;
061        this.options = options;
062        this.args = args;
063      }
064    
065      @Override
066      CommandInvoker make() {
067        return command.resolveInvoker(
068            name != null ? name : "",
069            options != null ? options : Collections.<String, Object>emptyMap(),
070            args != null ? args : Collections.<Object>emptyList());
071      }
072    
073      private void format(Object o, StringBuilder buffer) {
074        if (o instanceof String) {
075          buffer.append('"').append(o).append('"');
076        } else if (o instanceof Boolean || o instanceof Number) {
077          buffer.append(o);
078        } else {
079          buffer.append('<').append(o).append('>');
080        }
081      }
082    
083      void toString(StringBuilder buffer) {
084        buffer.append(commandName);
085        boolean hasOptions = options != null && options.size() > 0;
086        boolean hasArguments = args != null && args.size() > 0;
087        if (hasOptions || hasArguments) {
088          buffer.append(" {");
089          if (hasOptions) {
090            for (Iterator<Map.Entry<String, Object>> i = options.entrySet().iterator();i.hasNext();) {
091              Map.Entry<String, Object> option = i.next();
092              buffer.append(' ').append(option.getKey()).append('=');
093              format(option.getValue(), buffer);
094              if (i.hasNext()) {
095                buffer.append(";");
096              }
097            }
098            if (hasArguments) {
099              buffer.append(";");
100            }
101          }
102          if (hasArguments) {
103            buffer.append(" [");
104            for (Iterator<Object> i = args.iterator();i.hasNext();) {
105              Object arg = i.next();
106              format(arg, buffer);
107              if (i.hasNext()) {
108                buffer.append(", ");
109              }
110            }
111            buffer.append("]");
112          }
113          buffer.append(" }");
114        }
115      }
116    }