org.sdmlib.models.classes.templates.TemplateItem Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of SDMLib Show documentation
Show all versions of SDMLib Show documentation
SDMLib is a light weight modeling library. SDMLib intentionally comes without any tool or editor.
package org.sdmlib.models.classes.templates;
import org.sdmlib.StrUtil;
import org.sdmlib.codegen.Parser;
import org.sdmlib.models.classes.ClassModel;
import de.uniks.networkparser.list.SimpleList;
public abstract class TemplateItem extends TemplateTask{
public static final int DEBUG=1;
protected int mode=DEBUG;
protected SimpleList variables=new SimpleList();
public ReplaceText get(String value) {
for(ReplaceText item : variables) {
if(item .getSearch().equals(value)) {
return item;
}
}
return null;
}
@Override
public TemplateResult execute(String searchString, Parser parser, ClassModel model, String... values) {
if(!this.validate(parser, model, values)) {
return new TemplateResult();
}
return run(searchString, parser, model, values);
}
public TemplateResult execute(String... values) {
return run(template, null, null, values);
}
public TemplateResult run(String searchString, Parser parser, ClassModel model, String... values) {
if(values==null || values.length%2==1) {
if(mode==DEBUG) {
throw new RuntimeException("values are: "+values);
}
return new TemplateResult();
}
// Read all Variables from Template
boolean firstFound=false;
for(int i=0;icant be null!");
}
}
TemplateResult text = new TemplateResult(searchString.toString());
boolean foundVar = true;
while(foundVar) {
replacePlaceHolder(text);
firstFound=false;
foundVar = false;
searchString = text.getTextValue();
for(int i=0;i$> by replacement
for (int i = 0; i < variables.size(); i ++) {
ReplaceText replaceText = variables.get(i);
if(replaceText.isActive() == false) {
continue;
}
String placeholder = "{{" + replaceText.getSearch() + "}}";
int pos = -1 - placeholder.length();
pos = text.indexOf(placeholder, pos + placeholder.length());
while (pos >= 0)
{
String newString = replaceText.getText();
text.addImports(replaceText.getImport());
variables.get(i).run(pos, newString);
text.replace(pos, pos + placeholder.length(), newString);
pos = text.indexOf(placeholder, pos + newString.length());
}
}
return text;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy