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    
020    package org.crsh.cmdline.matcher.impl;
021    
022    import org.crsh.cmdline.ClassDescriptor;
023    import org.crsh.cmdline.CommandDescriptor;
024    import org.crsh.cmdline.Delimiter;
025    import org.crsh.cmdline.matcher.tokenizer.Tokenizer;
026    
027    import java.util.Iterator;
028    import java.util.LinkedList;
029    import java.util.NoSuchElementException;
030    
031    public final class Parser<T> implements Iterator<Event> {
032    
033      /** . */
034      private final Tokenizer tokenizer;
035    
036      /** . */
037      private final String mainName;
038    
039      /** . */
040      private final Mode mode;
041    
042      /** . */
043      private CommandDescriptor<T, ?> command;
044    
045      /** . */
046      private Status status;
047    
048      /** . */
049      private final LinkedList<Event> next;
050    
051      public Parser(Tokenizer tokenizer, ClassDescriptor<T> command, String mainName, Mode mode) {
052        this.tokenizer = tokenizer;
053        this.command = command;
054        this.mainName = mainName;
055        this.status = new Status.ReadingOption();
056        this.mode = mode;
057        this.next = new LinkedList<Event>();
058      }
059    
060      public Mode getMode() {
061        return mode;
062      }
063    
064      public int getIndex() {
065        return tokenizer.getIndex();
066      }
067    
068      public Status getStatus() {
069        return status;
070      }
071    
072      public Delimiter getDelimiter() {
073        return tokenizer.getDelimiter();
074      }
075    
076      public boolean hasNext() {
077        if (next.isEmpty()) {
078          determine();
079        }
080        return next.size() > 0;
081      }
082    
083      public Event next() {
084        if (!hasNext()) {
085          throw new NoSuchElementException();
086        }
087        return next.removeFirst();
088      }
089    
090      public void remove() {
091        throw new UnsupportedOperationException();
092      }
093    
094      private void determine() {
095        while (next.isEmpty()) {
096          Status.Response nextStatus = status.process(new Status.Request(mode, mainName, tokenizer, command));
097          if (nextStatus.status != null) {
098            this.status = nextStatus.status;
099          }
100          if (nextStatus.events != null) {
101            next.addAll(nextStatus.events);
102          }
103          if (nextStatus.command != null) {
104            command = (CommandDescriptor<T, ?>)nextStatus.command;
105          }
106        }
107      }
108    }