Coverage Report - net.sf.jmatchparser.template.engine.operation.SetLocalOperation
 
Classes in this File Line Coverage Branch Coverage Complexity
SetLocalOperation
93%
14/15
75%
6/8
3,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.operation;
 19  
 
 20  
 import net.sf.jmatchparser.template.engine.CallStackFrame;
 21  
 import net.sf.jmatchparser.template.engine.ParserState;
 22  
 import net.sf.jmatchparser.template.engine.parameter.Parameter;
 23  
 
 24  
 public class SetLocalOperation extends Operation {
 25  
 
 26  
         private final Parameter varname;
 27  
         private final Parameter value;
 28  
 
 29  
         public SetLocalOperation(String templatePosition, Parameter varname, Parameter value) {
 30  8
                 super(templatePosition);
 31  8
                 this.varname = varname;
 32  8
                 this.value = value;
 33  8
         }
 34  
 
 35  
         @Override
 36  
         public OperationResult execute(ParserState state) {
 37  246
                 String name = varname.getString(state);
 38  246
                 String v = value == null ? null : value.getString(state);
 39  246
                 CallStackFrame frame = state.getCallStack();
 40  246
                 if (frame == null)
 41  0
                         throw new RuntimeException("SETLOCAL command cannot be used outside DEFTEMPLATE");
 42  246
                 if (!frame.containsName(name)) {
 43  246
                         String oldvalue = state.getLocal(name);
 44  246
                         state.setCallStack(new CallStackFrame(frame, name, oldvalue));
 45  
                 }
 46  246
                 if (v != null)
 47  123
                         state.setLocal(name, v);
 48  246
                 return OperationResult.CONTINUE;
 49  
         }
 50  
 }