org.snapscript.tree.template.VariableSegment Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of snap Show documentation
Show all versions of snap Show documentation
Dynamic scripting for the JVM
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;
}
}