All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.snapscript.tree.template.VariableSegment Maven / Gradle / Ivy

There is a newer version: 1.4.6
Show newest version
package org.snapscript.tree.template;

import java.io.Writer;

import org.snapscript.core.Scope;
import org.snapscript.core.State;
import org.snapscript.core.Value;
import org.snapscript.core.convert.StringBuilder;

public class VariableSegment implements Segment {
   
   private String variable;
   private char[] source;
   private int off;
   private int length;
   
   public VariableSegment(char[] source, int off, int length) {
      this.variable = new String(source, off + 2, length - 3);
      this.source = source;
      this.length = length;
      this.off = off;         
   }
   
   @Override
   public void process(Scope scope, Writer writer) throws Exception {
      State state = scope.getState();
      Value value = state.get(variable);
      
      if(value == null) {
         writer.write(source, off, length);
      } else {
         Object token = value.getValue();
         String text = StringBuilder.create(scope, token);
         
         writer.append(text);            
      }
   }   
   
   @Override
   public String toString() {
      return variable;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy