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);
}
}
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());
}
}
}
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;
}
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);
}
}
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);
}
}
Aggregations