use of org.stringtemplate.v4.misc.ErrorBuffer in project bender by Nextdoor.
the class BenderConfig method swapEnvironmentVariables.
/**
* Parses an input String and replaces instances of {@literal <XXX>}" with the value of the XXX OS
* Environment Variable. This is used as a pre-parser for the Config files, allowing environment
* variables to be swapped at run-time.
*
* @param raw A raw string (not necessarily valid configuration data)
* @return A parsed string with OS variables swapped in
* @throws ConfigurationException If any discovered {@literal <WRAPPED_VALUES>} are not found in
* System.getenv().
*/
public static String swapEnvironmentVariables(String raw) throws ConfigurationException {
ErrorBuffer errors = new ErrorBuffer();
ST template = new ST(raw);
STGroup g = template.groupThatCreatedThisInstance;
g.setListener(errors);
Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
template.add(envName, env.get(envName));
}
String parsed = template.render();
if (errors.errors.size() > 0) {
throw new ConfigurationException(errors.toString());
}
return parsed;
}
use of org.stringtemplate.v4.misc.ErrorBuffer in project antlr4 by tunnelvisionlabs.
the class TestGenerator method generateTestFile.
protected void generateTestFile(STGroup index, STGroup targetGroup, String testdir, Collection<String> testTemplates) {
ErrorBuffer errors = new ErrorBuffer();
targetGroup.setListener(errors);
File targetFolder = getOutputDir(testdir);
String testName = testdir.substring(testdir.lastIndexOf('/') + 1);
File targetFile = new File(targetFolder, "Test" + testName + ".java");
// System.out.println("Generating file "+targetFile.getAbsolutePath());
List<ST> templates = new ArrayList<ST>();
for (String template : testTemplates) {
STGroup testGroup = new STGroupFile(testdir + "/" + template + STGroup.GROUP_FILE_EXTENSION);
importLanguageTemplates(testGroup, targetGroup);
ST testType = testGroup.getInstanceOf("TestType");
if (testType == null) {
warn(String.format("Unable to generate tests for %s: no TestType specified.", template));
continue;
}
ST testMethodTemplate = targetGroup.getInstanceOf(testType.render() + "TestMethod");
if (testMethodTemplate == null) {
warn(String.format("Unable to generate tests for %s: TestType '%s' is not supported by the current runtime.", template, testType.render()));
continue;
}
testMethodTemplate.add(testMethodTemplate.impl.formalArguments.keySet().iterator().next(), testGroup);
templates.add(testMethodTemplate);
}
ST testFileTemplate = targetGroup.getInstanceOf("TestFile");
testFileTemplate.addAggr("file.{Options,name,tests}", index.rawGetDictionary("Options"), testName, templates);
if (visualize) {
STViz viz = testFileTemplate.inspect();
try {
viz.waitForClose();
} catch (InterruptedException ex) {
}
}
try {
String output = testFileTemplate.render();
if (errors.errors.size() > 0) {
System.err.println("errors in " + targetGroup.getName() + ": " + errors);
}
writeFile(targetFile, output);
} catch (IOException ex) {
error(String.format("Failed to write output file: %s", targetFile), ex);
}
}
use of org.stringtemplate.v4.misc.ErrorBuffer in project bndtools by bndtools.
the class StringTemplateEngine method compile.
private ST compile(STGroup group, String name, Resource resource) throws Exception {
ErrorBuffer errors = new ErrorBuffer();
group.setListener(errors);
ST st;
try {
loadRawTemplate(group, name, resource);
st = group.getInstanceOf(name);
} catch (Exception e) {
// Wrap the ST exception, which gives us no detail in its message
throw new IllegalArgumentException(String.format("Failed to compile template '%s': %s", name, errors.toString()));
}
if (st == null)
throw new Exception("Template name not loaded: " + name);
return st;
}
use of org.stringtemplate.v4.misc.ErrorBuffer in project antlr4 by antlr.
the class TestAttributeChecks method testActions.
public void testActions(String location, String[] pairs, String template) {
for (int i = 0; i < pairs.length; i += 2) {
String action = pairs[i];
String expected = pairs[i + 1];
STGroup g = new STGroup('<', '>');
// hush warnings
g.setListener(new ErrorBuffer());
ST st = new ST(g, template);
st.add(location, action);
String grammar = st.render();
testErrors(new String[] { grammar, expected }, false);
}
}
Aggregations