use of org.apache.velocity.VelocityContext in project midpoint by Evolveum.
the class VelocityScriptEvaluator method evaluate.
@Override
public <T, V extends PrismValue> List<V> evaluate(ScriptExpressionEvaluatorType expressionType, ExpressionVariables variables, ItemDefinition outputDefinition, Function<Object, Object> additionalConvertor, ScriptExpressionReturnTypeType suggestedReturnType, ObjectResolver objectResolver, Collection<FunctionLibrary> functions, String contextDescription, Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, ExpressionSyntaxException {
VelocityContext context = createVelocityContext(variables, objectResolver, functions, contextDescription, task, result);
String codeString = expressionType.getCode();
if (codeString == null) {
throw new ExpressionEvaluationException("No script code in " + contextDescription);
}
boolean allowEmptyValues = false;
if (expressionType.isAllowEmptyValues() != null) {
allowEmptyValues = expressionType.isAllowEmptyValues();
}
StringWriter resultWriter = new StringWriter();
try {
InternalMonitor.recordScriptExecution();
Velocity.evaluate(context, resultWriter, "", codeString);
} catch (RuntimeException e) {
throw new ExpressionEvaluationException(e.getMessage() + " in " + contextDescription, e);
}
if (outputDefinition == null) {
// No outputDefinition means "void" return type, we can return right now
return null;
}
QName xsdReturnType = outputDefinition.getTypeName();
Class<T> javaReturnType = XsdTypeMapper.toJavaType(xsdReturnType);
if (javaReturnType == null) {
javaReturnType = prismContext.getSchemaRegistry().getCompileTimeClass(xsdReturnType);
}
if (javaReturnType == null) {
// TODO quick and dirty hack - because this could be because of enums defined in schema extension (MID-2399)
// ...and enums (xsd:simpleType) are not parsed into ComplexTypeDefinitions
javaReturnType = (Class) String.class;
}
T evalResult;
try {
evalResult = ExpressionUtil.convertValue(javaReturnType, additionalConvertor, resultWriter.toString(), protector, prismContext);
} catch (IllegalArgumentException e) {
throw new ExpressionEvaluationException(e.getMessage() + " in " + contextDescription, e);
}
List<V> pvals = new ArrayList<V>();
if (allowEmptyValues || !ExpressionUtil.isEmpty(evalResult)) {
pvals.add((V) ExpressionUtil.convertToPrismValue(evalResult, outputDefinition, contextDescription, prismContext));
}
return pvals;
}
use of org.apache.velocity.VelocityContext in project opennms by OpenNMS.
the class GraphConfigGenerator method generateSnmpGraphInternal.
private String generateSnmpGraphInternal(Collection<Report> reports) {
// init VelocityEngine
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
velocityEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
velocityEngine.init();
// create reader and writer for template extraction from jar
InputStream templateInputStream = this.getClass().getClassLoader().getResourceAsStream(INTERN_TEMPLATE_NAME);
StringWriter templateWriter = new StringWriter();
Reader templateReader = new InputStreamReader(templateInputStream);
// create context
VelocityContext context = new VelocityContext();
context.put("reportsList", reports.iterator());
context.put("reportsBody", reports.iterator());
// get template
Velocity.evaluate(context, templateWriter, INTERN_TEMPLATE_NAME, templateReader);
return templateWriter.toString();
}
use of org.apache.velocity.VelocityContext in project opennms by OpenNMS.
the class GraphConfigGenerator method generateSnmpGraphInternal.
private String generateSnmpGraphInternal(Collection<Report> reports, String graphTemplate) {
Velocity.init();
VelocityContext context = new VelocityContext();
context.put("reportsList", reports.iterator());
context.put("reportsBody", reports.iterator());
Template template = Velocity.getTemplate(graphTemplate);
StringWriter sw = new StringWriter();
if (template != null) {
template.merge(context, sw);
}
return sw.toString();
}
use of org.apache.velocity.VelocityContext in project Ebselen by Ardesco.
the class IDEToEbselen method generateJavaFile.
/**
* Writes Sky Selenium format test code into a Java file ready for tests to be run
*
* @param name - Name of the test
* @throws Exception
*/
public void generateJavaFile(String name) throws Exception {
Properties props = new Properties();
props.setProperty("resource.loader", "class");
props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
props.setProperty("class.resource.loader.description", "Velocity Classpath Resource Loader");
VelocityEngine ve = new VelocityEngine(props);
VelocityContext context = new VelocityContext();
context.put("template", name);
context.put("templateclass", name + ".class");
context.put("testname", name);
context.put("testdata", testCode);
Template ebselenTemplate = ve.getTemplate(ebselenTestTemplate);
FileHandler convertedFile = new FileHandler(conversionLocation + File.separator + name + ".java", true);
StringWriter writer = new StringWriter();
ebselenTemplate.merge(context, writer);
convertedFile.write(writer.toString());
convertedFile.close();
LOGGER.info("Selenium IDE test converted and saved as '" + convertedFile.getFilePath() + convertedFile.getFileName() + "'");
}
use of org.apache.velocity.VelocityContext in project Asqatasun by Asqatasun.
the class CodeGeneratorMojo method generate.
/**
*
* @param ve
* @param records
* @throws java.io.IOException
*/
public void generate(VelocityEngine ve, Iterable<CSVRecord> records) throws IOException, ResourceNotFoundException, ParseErrorException, Exception {
// Getting the Template
Template ruleTemplate = ve.getTemplate(templateRule.getPath());
Template pomTemplate = ve.getTemplate(pom.getPath());
Template webappBeansTemplate = ve.getTemplate(templateBeansWebapp.getPath());
Template webappBeansExpressionTemplate = ve.getTemplate(templateBeansExpression.getPath());
Template auditResultConsoleTemplate = ve.getTemplate(templateAuditResultConsole.getPath());
Template auditSetUpFormTemplate = ve.getTemplate(templateAuditSetUpForm.getPath());
Template testCaseTemplate = ve.getTemplate(templateTestCase.getPath());
Template descriptorTemplate = ve.getTemplate(templateDescriptor.getPath());
Template installTemplate = ve.getTemplate(templateInstallSh.getPath());
Template unitTestTemplate = ve.getTemplate(templateUnitTest.getPath());
Template ruleImplementationTestCaseTemplate = ve.getTemplate(templateRuleImplementationTestCase.getPath());
// Create a context and add data to the templateRule placeholder
VelocityContext context = new VelocityContext();
// Fetch templateRule into a StringWriter
FileGenerator fg = new FileGenerator(referential, referentialLabel, destinationFolder, refDescriptor, isCriterionPresent);
fg.createI18NFiles(langSet);
// using the i18n keys.
for (CSVRecord record : records) {
String testLabelDefault = record.get(TEST_LABEL_COLUMN_NAME + langSet.first());
String test = record.get(TEST_CODE_COLUMN_NAME);
for (String lang : langSet) {
writeToI18NFile(fg, record, lang);
}
IS_I18N_REFERENTIAL_CREATED = true;
if (!isCriterionPresent) {
test = test.concat("-1");
}
context = fg.getContextRuleClassFile(referential, PACKAGE_NAME, test, testLabelDefault, context);
fg.writeFileCodeGenerate(context, ruleTemplate, getClassNameFromCsvColumn(record));
fg.writeUnitTestGenerate(context, unitTestTemplate, testLabelDefault, getClassNameFromCsvColumn(record));
String[] testsCasesState = { "Passed", "Failed", "NMI", "NA" };
for (int i = 0; i < testsCasesState.length; i++) {
context.put("state", testsCasesState[i]);
fg.writeTestCaseGenerate(context, testCaseTemplate, getClassNameFromCsvColumn(record), String.valueOf(i + 1));
}
addLevelAndScopeToList(record);
}
fg.createSqlReference();
fg.createSqlTheme();
fg.createSqlCritere();
fg.createSqlTest(levelList, scopeList);
fg.createSqlParameters();
fg.writeAuditSetUpFormBeanGenerate(context, auditSetUpFormTemplate);
fg.writeAuditResultConsoleBeanGenerate(context, auditResultConsoleTemplate);
fg.writeWebappBeansGenerate(context, webappBeansTemplate);
fg.writeWebappBeansExpressionGenerate(context, webappBeansExpressionTemplate);
fg.writeInstallGenerate(context, installTemplate);
fg.writeDescriptorGenerate(context, descriptorTemplate);
fg.writeRuleImplementationTestCaseGenerate(context, ruleImplementationTestCaseTemplate);
fg.adaptPom(context, pomTemplate);
}
Aggregations