Search in sources :

Example 11 with VelocityContext

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;
}
Also used : VelocityContext(org.apache.velocity.VelocityContext)

Example 12 with VelocityContext

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;
}
Also used : IntrospectionSupport(org.apache.camel.util.IntrospectionSupport) VelocityContext(org.apache.velocity.VelocityContext) ApiMethod(org.apache.camel.util.component.ApiMethod) TreeSet(java.util.TreeSet) ApiMethodHelper(org.apache.camel.util.component.ApiMethodHelper) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ApiName(org.apache.camel.util.component.ApiName) TreeMap(java.util.TreeMap) Map(java.util.Map) TreeMap(java.util.TreeMap) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 13 with VelocityContext

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;
}
Also used : VelocityContext(org.apache.velocity.VelocityContext)

Example 14 with VelocityContext

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);
        }
    }
}
Also used : SObjectField(org.apache.camel.component.salesforce.api.dto.SObjectField) VelocityContext(org.apache.velocity.VelocityContext) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) Template(org.apache.velocity.Template)

Example 15 with VelocityContext

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"));
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) VelocityContext(org.apache.velocity.VelocityContext) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

VelocityContext (org.apache.velocity.VelocityContext)98 StringWriter (java.io.StringWriter)26 Template (org.apache.velocity.Template)20 IOException (java.io.IOException)16 VelocityEngine (org.apache.velocity.app.VelocityEngine)13 File (java.io.File)10 ArrayList (java.util.ArrayList)8 ExecuteResult (com.ctrip.platform.dal.daogen.entity.ExecuteResult)7 Progress (com.ctrip.platform.dal.daogen.entity.Progress)7 Writer (java.io.Writer)7 Map (java.util.Map)7 Callable (java.util.concurrent.Callable)7 JavaCodeGenContext (com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)6 OutputStreamWriter (java.io.OutputStreamWriter)6 Properties (java.util.Properties)6 PrintWriter (java.io.PrintWriter)5 HashMap (java.util.HashMap)5 CSharpCodeGenContext (com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 FileOutputStream (java.io.FileOutputStream)3