Coverage Report - net.sf.jmatchparser.template.engine.template.command.plain.OptionalCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
OptionalCommand
96%
27/28
64%
9/14
5,5
 
 1  
 /*
 2  
  * Copyright (c) 2006 - 2011 Michael Schierl
 3  
  * All rights reserved.
 4  
  * 
 5  
  * This program is free software: you can redistribute it and/or modify
 6  
  * it under the terms of the GNU General Public License as published by
 7  
  * the Free Software Foundation, either version 2 of the License, or
 8  
  * (at your option) any later version.
 9  
  * 
 10  
  * This program is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  * 
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 package net.sf.jmatchparser.template.engine.template.command.plain;
 19  
 
 20  
 import net.sf.jmatchparser.template.engine.operation.ForkOperation;
 21  
 import net.sf.jmatchparser.template.engine.operation.JumpOperation;
 22  
 import net.sf.jmatchparser.template.engine.operation.Label;
 23  
 import net.sf.jmatchparser.template.engine.template.PlainBlockCommandState;
 24  
 import net.sf.jmatchparser.template.engine.template.MatchTemplateImpl;
 25  
 
 26  
 public class OptionalCommand extends Command {
 27  
 
 28  
         private final boolean repeated;
 29  
 
 30  3
         public OptionalCommand(boolean repeated) {
 31  3
                 this.repeated = repeated;
 32  3
         }
 33  
 
 34  
         @Override
 35  
         public PlainBlockCommandState parse(MatchTemplateImpl template, String parameters) {
 36  36
                 Label startLabel = new Label(), endLabel = new Label(), forkLabel = new Label();
 37  36
                 startLabel.setDestinationToNextCommand(template);
 38  36
                 template.appendOperation(new ForkOperation(template.getCurrentTemplatePosition(), forkLabel, false));
 39  36
                 template.appendOperation(new JumpOperation(template.getCurrentTemplatePosition(), endLabel));
 40  36
                 forkLabel.setDestinationToNextCommand(template);
 41  36
                 int pos = parameters.indexOf(" ");
 42  36
                 int pos2 = parameters.indexOf("\t");
 43  36
                 if (pos2 != -1 && (pos == -1 || pos > pos2))
 44  0
                         pos = pos2;
 45  
                 String commandName, commandParameters;
 46  36
                 if (pos == -1) {
 47  2
                         commandName = parameters.toLowerCase();
 48  2
                         commandParameters = "";
 49  
                 } else {
 50  34
                         commandName = parameters.substring(0, pos).toLowerCase();
 51  34
                         commandParameters = parameters.substring(pos + 1);
 52  
                 }
 53  36
                 Command cmd = Command.getCommand(commandName);
 54  36
                 if (cmd != null) {
 55  35
                         PlainBlockCommandState bcs = cmd.parse(template, commandParameters);
 56  35
                         if (bcs != null)
 57  1
                                 throw new RuntimeException("Optional/Repeated command cannot be used for block commands!");
 58  34
                 } else {
 59  1
                         throw new RuntimeException("Unsupported command: " + commandName);
 60  
                 }
 61  34
                 if (repeated) {
 62  6
                         template.appendOperation(new JumpOperation(template.getCurrentTemplatePosition(), startLabel));
 63  
                 }
 64  34
                 endLabel.setDestinationToNextCommand(template);
 65  34
                 return null;
 66  
         }
 67  
 
 68  
 }