com.floreysoft.jmte.token.StringToken Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmte Show documentation
Show all versions of jmte Show documentation
To build and locally install jar, javadoc and sources, please use:
mvn clean javadoc:jar source:jar install -Dmaven.test.skip=true
Tested on Maven 2.0.9, JDK 1.7
package com.floreysoft.jmte.token;
import java.util.List;
import com.floreysoft.jmte.NamedRenderer;
import com.floreysoft.jmte.Renderer;
import com.floreysoft.jmte.TemplateContext;
import com.floreysoft.jmte.encoder.Encoder;
import com.floreysoft.jmte.renderer.NullRenderer;
import com.floreysoft.jmte.renderer.RawRenderer;
public class StringToken extends ExpressionToken {
// ${,address(NIX),
;long(full)}
private final String defaultValue; // NIX
private final String prefix; //
private final String suffix; //
private final String rendererName; // long
private final String parameters; // full
public StringToken() {
this("", "", null, null, null, null, null);
}
public StringToken(String text, String variableName, String defaultValue,
String prefix, String suffix, String rendererName, String parameters) {
super(variableName);
this.defaultValue = defaultValue;
this.prefix = prefix;
this.suffix = suffix;
this.rendererName = rendererName;
this.parameters = parameters;
setText(text);
}
public StringToken(String variableName) {
this(variableName, variableName, null, null, null, null, null);
}
public StringToken(String text, List segments, String variableName,
String defaultValue, String prefix, String suffix,
String rendererName, String parameters) {
super(segments, variableName);
this.defaultValue = defaultValue;
this.prefix = prefix;
this.suffix = suffix;
this.rendererName = rendererName;
this.parameters = parameters;
setText(text);
}
public StringToken(List segments, String variableName) {
super(segments, variableName);
this.defaultValue = null;
this.prefix = null;
this.suffix = null;
this.rendererName = null;
this.parameters = null;
}
public String getPrefix() {
return prefix;
}
public String getSuffix() {
return suffix;
}
public String getDefaultValue() {
return defaultValue;
}
@SuppressWarnings("unchecked")
@Override
public Object evaluate(TemplateContext context) {
// step 1: get value or default
final Object value = resolveDefault(evaluatePlain(context));
// step 2: using named renderer (if present), type renderer (if present), just toString (if not null) or null (in that order)
// if there is a renderer, determine if it outputs a raw value
final String renderedResult;
boolean rawRendering = false;
final NamedRenderer rendererForName = this.resolveNamedRenderer(context);
if (rendererForName != null && (value != null || rendererForName instanceof NullRenderer)) {
if (rendererForName instanceof RawRenderer) {
rawRendering = true;
}
renderedResult = rendererForName.render(value, parameters, context.locale, context.model);
} else if (value != null) {
final Renderer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy