Search in sources :

Example 1 with CodeGeneratorException

use of org.ballerinalang.code.generator.exception.CodeGeneratorException in project ballerina by ballerina-lang.

the class CodeGenerator method compileTemplate.

/**
 * This method will compile and return template of passed template definition.
 *
 * @param defaultTemplateDir template directory which contains set of templates
 * @param templateName       template file name to be used as template
 * @return compiled template generated for template definition.
 * @throws CodeGeneratorException throws IOException when compilation error occurs.
 */
private Template compileTemplate(String defaultTemplateDir, String templateName) throws CodeGeneratorException {
    String templatesDirPath = System.getProperty(GeneratorConstants.TEMPLATES_DIR_PATH_KEY, defaultTemplateDir);
    ClassPathTemplateLoader cpTemplateLoader = new ClassPathTemplateLoader((templatesDirPath));
    FileTemplateLoader fileTemplateLoader = new FileTemplateLoader(templatesDirPath);
    cpTemplateLoader.setSuffix(GeneratorConstants.TEMPLATES_SUFFIX);
    fileTemplateLoader.setSuffix(GeneratorConstants.TEMPLATES_SUFFIX);
    Handlebars handlebars = new Handlebars().with(cpTemplateLoader, fileTemplateLoader);
    handlebars.registerHelpers(StringHelpers.class);
    handlebars.registerHelper("equals", (object, options) -> {
        CharSequence result;
        Object param0 = options.param(0);
        if (param0 == null) {
            throw new IllegalArgumentException("found 'null', expected 'string'");
        }
        if (object != null && object.toString().equals(param0.toString())) {
            result = options.fn(options.context);
        } else {
            result = null;
        }
        return result;
    });
    try {
        return handlebars.compile(templateName);
    } catch (IOException e) {
        throw new CodeGeneratorException("Error while compiling template", e);
    }
}
Also used : Handlebars(com.github.jknack.handlebars.Handlebars) ClassPathTemplateLoader(com.github.jknack.handlebars.io.ClassPathTemplateLoader) CodeGeneratorException(org.ballerinalang.code.generator.exception.CodeGeneratorException) IOException(java.io.IOException) FileTemplateLoader(com.github.jknack.handlebars.io.FileTemplateLoader)

Example 2 with CodeGeneratorException

use of org.ballerinalang.code.generator.exception.CodeGeneratorException in project ballerina by ballerina-lang.

the class ClientGeneratorPlugin method process.

@Override
public void process(ServiceNode serviceNode, List<AnnotationAttachmentNode> annotations) {
    CodeGenerator codegen = new CodeGenerator();
    PrintStream err = System.err;
    AnnotationAttachmentNode config = GeneratorUtils.getAnnotationFromList("ClientConfig", GeneratorConstants.SWAGGER_PKG_ALIAS, annotations);
    // Generate client only if requested by providing the client config annotation
    if (isClientGenerationEnabled(config)) {
        try {
            ClientContextHolder context = ClientContextHolder.buildContext(serviceNode, endpoints);
            codegen.writeGeneratedSource(GeneratorConstants.GenType.CLIENT, context, getOutputFilePath(serviceNode));
        } catch (CodeGeneratorException e) {
            err.println("Client code was not generated: " + e.getMessage());
        }
    }
}
Also used : PrintStream(java.io.PrintStream) CodeGeneratorException(org.ballerinalang.code.generator.exception.CodeGeneratorException) CodeGenerator(org.ballerinalang.code.generator.CodeGenerator) ClientContextHolder(org.ballerinalang.code.generator.model.ClientContextHolder) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 3 with CodeGeneratorException

use of org.ballerinalang.code.generator.exception.CodeGeneratorException in project ballerina by ballerina-lang.

the class ResourceContextHolder method buildContext.

public static ResourceContextHolder buildContext(ResourceNode resource) throws CodeGeneratorException {
    ResourceContextHolder context = new ResourceContextHolder();
    context.name = resource.getName().getValue();
    context.parameters = new ArrayList<>();
    for (VariableNode node : resource.getParameters()) {
        ParameterContextHolder parameter = ParameterContextHolder.buildContext(node);
        if (parameter != null) {
            context.parameters.add(parameter);
        }
    }
    // Iterate through all resource level annotations and find out resource configuration information
    AnnotationAttachmentNode ann = GeneratorUtils.getAnnotationFromList(GeneratorConstants.RES_CONFIG_ANNOTATION, GeneratorConstants.HTTP_PKG_ALIAS, resource.getAnnotationAttachments());
    if (ann == null) {
        throw new CodeGeneratorException("Incomplete resource configuration found");
    }
    BLangRecordLiteral bLiteral = ((BLangRecordLiteral) ((BLangAnnotationAttachment) ann).getExpression());
    List<BLangRecordLiteral.BLangRecordKeyValue> list = bLiteral.getKeyValuePairs();
    Map<String, String[]> attrs = GeneratorUtils.getKeyValuePairAsMap(list);
    // We don't expect multiple http methods to be supported by single action
    // We only consider first content type for a single resource
    context.method = attrs.get(GeneratorConstants.ATTR_METHODS) != null ? attrs.get(GeneratorConstants.ATTR_METHODS)[0] : null;
    context.contentType = attrs.get(GeneratorConstants.ATTR_CONSUMES) != null ? attrs.get(GeneratorConstants.ATTR_CONSUMES)[0] : null;
    String path = attrs.get(GeneratorConstants.ATTR_PATH) != null ? attrs.get(GeneratorConstants.ATTR_PATH)[0] : null;
    context.path = context.getTemplatePath(path);
    return context;
}
Also used : VariableNode(org.ballerinalang.model.tree.VariableNode) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) CodeGeneratorException(org.ballerinalang.code.generator.exception.CodeGeneratorException) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 4 with CodeGeneratorException

use of org.ballerinalang.code.generator.exception.CodeGeneratorException in project ballerina by ballerina-lang.

the class CodeGenerator method writeGeneratedSource.

/**
 * Write generated source of a given <code>context</code> to a file at <code>outpath</code>.
 *
 * @param type    Generator Type to be used. Following types are supported.
 *                <ul>
 *                <li>client</li>
 *                <li>openapi</li>
 *                <li>swagger</li>
 *                </ul>
 * @param context Definition object containing required properties to be extracted for code generation
 * @param outPath resulting file path
 * @throws CodeGeneratorException when error occurred while generating the code
 */
public void writeGeneratedSource(GeneratorConstants.GenType type, ClientContextHolder context, String outPath) throws CodeGeneratorException {
    try (PrintWriter writer = new PrintWriter(outPath, "UTF-8")) {
        String generatedSource = generateOutput(type, context);
        writer.println(generatedSource);
    } catch (FileNotFoundException e) {
        throw new CodeGeneratorException("Error while writing converted string due to output file not found", e);
    } catch (UnsupportedEncodingException e) {
        throw new CodeGeneratorException("Error while writing converted string due to unsupported encoding", e);
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CodeGeneratorException(org.ballerinalang.code.generator.exception.CodeGeneratorException) PrintWriter(java.io.PrintWriter)

Example 5 with CodeGeneratorException

use of org.ballerinalang.code.generator.exception.CodeGeneratorException in project ballerina by ballerina-lang.

the class CodeGenerator method getConvertedString.

/**
 * Get converted string for given service definition.
 *
 * @param object       Object to be built as parsable context
 * @param templateDir  Template directory which contains templates for specific output type.
 * @param templateName Name of the template to be use for code generation.
 * @return generated string representation of passed object(ballerina service node)
 * @throws CodeGeneratorException when error occurs while compile, build context.
 */
public String getConvertedString(Object object, String templateDir, String templateName) throws CodeGeneratorException {
    Template template = compileTemplate(templateDir, templateName);
    Context context = Context.newBuilder(object).resolver(MapValueResolver.INSTANCE, JavaBeanValueResolver.INSTANCE, FieldValueResolver.INSTANCE).build();
    try {
        return template.apply(context);
    } catch (IOException e) {
        throw new CodeGeneratorException("Error while generating converted string", e);
    }
}
Also used : Context(com.github.jknack.handlebars.Context) CodeGeneratorException(org.ballerinalang.code.generator.exception.CodeGeneratorException) IOException(java.io.IOException) Template(com.github.jknack.handlebars.Template)

Aggregations

CodeGeneratorException (org.ballerinalang.code.generator.exception.CodeGeneratorException)5 IOException (java.io.IOException)2 AnnotationAttachmentNode (org.ballerinalang.model.tree.AnnotationAttachmentNode)2 Context (com.github.jknack.handlebars.Context)1 Handlebars (com.github.jknack.handlebars.Handlebars)1 Template (com.github.jknack.handlebars.Template)1 ClassPathTemplateLoader (com.github.jknack.handlebars.io.ClassPathTemplateLoader)1 FileTemplateLoader (com.github.jknack.handlebars.io.FileTemplateLoader)1 FileNotFoundException (java.io.FileNotFoundException)1 PrintStream (java.io.PrintStream)1 PrintWriter (java.io.PrintWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 CodeGenerator (org.ballerinalang.code.generator.CodeGenerator)1 ClientContextHolder (org.ballerinalang.code.generator.model.ClientContextHolder)1 VariableNode (org.ballerinalang.model.tree.VariableNode)1 BLangAnnotationAttachment (org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment)1 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)1