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
020package org.crsh.lang.script;
021
022import org.crsh.command.CommandCreationException;
023import org.crsh.command.CommandInvoker;
024import org.crsh.command.ShellCommand;
025import org.crsh.repl.REPLSession;
026import org.crsh.command.pipeline.PipeLine;
027import org.crsh.text.Chunk;
028
029import java.util.LinkedList;
030import java.util.regex.Pattern;
031
032/**
033 * A factory for a pipeline.
034 */
035public class PipeLineFactory {
036
037  /** . */
038  final String line;
039
040  /** . */
041  final String name;
042
043  /** . */
044  final String rest;
045
046  /** . */
047  final PipeLineFactory next;
048
049  public String getLine() {
050    return line;
051  }
052
053  public PipeLineFactory getNext() {
054    return next;
055  }
056
057  public PipeLineFactory(String line, PipeLineFactory next) {
058
059    Pattern p = Pattern.compile("^\\s*(\\S+)");
060    java.util.regex.Matcher m = p.matcher(line);
061    String name = null;
062    String rest = null;
063    if (m.find()) {
064      name = m.group(1);
065      rest = line.substring(m.end());
066    }
067
068    //
069    this.name = name;
070    this.rest = rest;
071    this.line = line;
072    this.next = next;
073  }
074
075  public CommandInvoker<Void, Chunk> create(REPLSession session) throws CommandCreationException {
076
077    //
078    LinkedList<CommandInvoker> pipes = new LinkedList<CommandInvoker>();
079    for (PipeLineFactory current = this;current != null;current = current.next) {
080      CommandInvoker commandInvoker = null;
081      if (current.name != null) {
082        ShellCommand command = session.getCommand(current.name);
083        if (command != null) {
084          commandInvoker = command.resolveInvoker(current.rest);
085        }
086      }
087      if (commandInvoker == null) {
088        throw new CommandCreationException(current.name);
089      }
090      pipes.add(commandInvoker);
091    }
092
093    //
094    return new PipeLine(pipes.toArray(new CommandInvoker[pipes.size()]));
095  }
096
097  public PipeLineFactory getLast() {
098    if (next != null) {
099      return next.getLast();
100    }
101    return this;
102  }
103}