use of org.apache.velocity.VelocityContext in project camel by apache.
the class ApiComponentGeneratorMojo method getApiContext.
private VelocityContext getApiContext() {
final VelocityContext context = new VelocityContext();
context.put("componentName", componentName);
context.put("componentPackage", componentPackage);
context.put("apis", apis);
context.put("helper", getClass());
context.put("collectionName", getApiCollectionName());
context.put("apiNameEnum", getApiNameEnum());
return context;
}
use of org.apache.velocity.VelocityContext in project camel by apache.
the class DocumentGeneratorMojo method getDocumentContext.
private VelocityContext getDocumentContext() throws MavenReportException {
final VelocityContext context = new VelocityContext();
context.put("helper", this);
// project GAV
context.put("groupId", project.getGroupId());
context.put("artifactId", project.getArtifactId());
context.put("version", project.getVersion());
// component URI format
// look for single API, no endpoint-prefix
@SuppressWarnings("unchecked") final Set<String> apiNames = new TreeSet<String>(collection.getApiNames());
context.put("apiNames", apiNames);
String suffix;
if (apiNames.size() == 1 && ((Set) apiNames).contains("")) {
suffix = "://endpoint?[options]";
} else {
suffix = "://endpoint-prefix/endpoint?[options]";
}
context.put("uriFormat", scheme + suffix);
// API helpers
final Map<String, ApiMethodHelper> apiHelpers = new TreeMap<String, ApiMethodHelper>();
for (Object element : collection.getApiHelpers().entrySet()) {
Map.Entry entry = (Map.Entry) element;
apiHelpers.put(((ApiName) entry.getKey()).getName(), (ApiMethodHelper) entry.getValue());
}
context.put("apiHelpers", apiHelpers);
// API methods and endpoint configurations
final Map<String, Class<? extends ApiMethod>> apiMethods = new TreeMap<String, Class<? extends ApiMethod>>();
final Map<String, Class<?>> apiConfigs = new TreeMap<String, Class<?>>();
for (Object element : collection.getApiMethods().entrySet()) {
Map.Entry entry = (Map.Entry) element;
final String name = ((ApiName) entry.getValue()).getName();
@SuppressWarnings("unchecked") Class<? extends ApiMethod> apiMethod = (Class<? extends ApiMethod>) entry.getKey();
apiMethods.put(name, apiMethod);
Class<?> configClass;
try {
configClass = getProjectClassLoader().loadClass(getEndpointConfigName(apiMethod));
} catch (ClassNotFoundException e) {
throw new MavenReportException(e.getMessage(), e);
} catch (MojoExecutionException e) {
throw new MavenReportException(e.getMessage(), e);
}
apiConfigs.put(name, configClass);
}
context.put("apiMethods", apiMethods);
context.put("apiConfigs", apiConfigs);
// API component properties
context.put("scheme", this.scheme);
context.put("componentName", this.componentName);
Class<?> configClass;
try {
configClass = getProjectClassLoader().loadClass(getComponentConfig());
} catch (ClassNotFoundException e) {
throw new MavenReportException(e.getMessage(), e);
} catch (MojoExecutionException e) {
throw new MavenReportException(e.getMessage(), e);
}
context.put("componentConfig", configClass);
// get declared and derived fields for component config
// use get/set methods instead of fields, since this class could inherit others, that have private fields
// so getDeclaredFields() won't work, like it does for generated endpoint config classes!!!
final Map<String, String> configFields = new TreeMap<String, String>();
do {
IntrospectionSupport.ClassInfo classInfo = IntrospectionSupport.cacheClass(configClass);
for (IntrospectionSupport.MethodInfo method : classInfo.methods) {
if (method.isSetter) {
configFields.put(method.getterOrSetterShorthandName, getCanonicalName(method.method.getParameterTypes()[0]));
}
}
configClass = configClass.getSuperclass();
} while (configClass != null && !configClass.equals(Object.class));
context.put("componentConfigFields", configFields);
return context;
}
use of org.apache.velocity.VelocityContext in project camel by apache.
the class AbstractApiMethodGeneratorMojo method getApiMethodContext.
private VelocityContext getApiMethodContext(List<ApiMethodParser.ApiMethodModel> models) throws MojoExecutionException {
VelocityContext context = getCommonContext(models);
context.put("enumName", getEnumName());
return context;
}
use of org.apache.velocity.VelocityContext in project camel by apache.
the class CamelSalesforceMojo method processDescription.
void processDescription(File pkgDir, SObjectDescription description, GeneratorUtility utility, String generatedDate) throws IOException {
// generate a source file for SObject
final VelocityContext context = new VelocityContext();
context.put("packageName", packageName);
context.put("utility", utility);
context.put("esc", StringEscapeUtils.class);
context.put("desc", description);
context.put("generatedDate", generatedDate);
context.put("useStringsForPicklists", useStringsForPicklists);
final String pojoFileName = description.getName() + JAVA_EXT;
final File pojoFile = new File(pkgDir, pojoFileName);
try (final Writer writer = new OutputStreamWriter(new FileOutputStream(pojoFile), StandardCharsets.UTF_8)) {
final Template pojoTemplate = engine.getTemplate(SOBJECT_POJO_VM, UTF_8);
pojoTemplate.merge(context, writer);
}
if (useOptionals) {
final String optionalFileName = description.getName() + "Optional" + JAVA_EXT;
final File optionalFile = new File(pkgDir, optionalFileName);
try (final Writer writer = new OutputStreamWriter(new FileOutputStream(optionalFile), StandardCharsets.UTF_8)) {
final Template optionalTemplate = engine.getTemplate(SOBJECT_POJO_OPTIONAL_VM, UTF_8);
optionalTemplate.merge(context, writer);
}
}
// write required Enumerations for any picklists
for (SObjectField field : description.getFields()) {
if (utility.isPicklist(field) || utility.isMultiSelectPicklist(field)) {
final String enumName = description.getName() + "_" + utility.enumTypeName(field.getName());
final String enumFileName = enumName + JAVA_EXT;
final File enumFile = new File(pkgDir, enumFileName);
context.put("field", field);
context.put("enumName", enumName);
final Template enumTemplate = engine.getTemplate(SOBJECT_PICKLIST_VM, UTF_8);
try (final Writer writer = new OutputStreamWriter(new FileOutputStream(enumFile), StandardCharsets.UTF_8)) {
enumTemplate.merge(context, writer);
}
}
}
// write the QueryRecords class
final String queryRecordsFileName = "QueryRecords" + description.getName() + JAVA_EXT;
final File queryRecordsFile = new File(pkgDir, queryRecordsFileName);
final Template queryTemplate = engine.getTemplate(SOBJECT_QUERY_RECORDS_VM, UTF_8);
try (final Writer writer = new OutputStreamWriter(new FileOutputStream(queryRecordsFile), StandardCharsets.UTF_8)) {
queryTemplate.merge(context, writer);
}
if (useOptionals) {
// write the QueryRecords Optional class
final String queryRecordsOptionalFileName = "QueryRecords" + description.getName() + "Optional" + JAVA_EXT;
final File queryRecordsOptionalFile = new File(pkgDir, queryRecordsOptionalFileName);
final Template queryRecordsOptionalTemplate = engine.getTemplate(SOBJECT_QUERY_RECORDS_OPTIONAL_VM, UTF_8);
try (final Writer writer = new OutputStreamWriter(new FileOutputStream(queryRecordsOptionalFile), StandardCharsets.UTF_8)) {
queryRecordsOptionalTemplate.merge(context, writer);
}
}
}
use of org.apache.velocity.VelocityContext in project camel by apache.
the class VelocityTest method testVelocityContext.
@Test
public void testVelocityContext() throws Exception {
Exchange exchange = template.request("direct:a", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody("");
exchange.getIn().setHeader("name", "Christian");
Map<String, Object> variableMap = new HashMap<String, Object>();
Map<String, Object> headersMap = new HashMap<String, Object>();
headersMap.put("name", "Willem");
variableMap.put("headers", headersMap);
variableMap.put("body", "Monday");
variableMap.put("exchange", exchange);
VelocityContext velocityContext = new VelocityContext(variableMap);
exchange.getIn().setHeader(VelocityConstants.VELOCITY_CONTEXT, velocityContext);
exchange.setProperty("item", "7");
}
});
assertEquals("Dear Willem. You ordered item 7 on Monday.", exchange.getOut().getBody());
assertEquals("Christian", exchange.getOut().getHeader("name"));
}
Aggregations