001// Generated by delombok at Sun Jun 30 16:31:41 CEST 2019 002package com.credibledoc.substitution.doc.module.substitution.markdown.table; 003 004import com.credibledoc.substitution.core.resource.ResourceService; 005import com.credibledoc.substitution.core.template.TemplateService; 006import com.github.javaparser.JavaParser; 007import com.github.javaparser.ast.CompilationUnit; 008import com.github.javaparser.ast.body.EnumConstantDeclaration; 009import com.github.javaparser.ast.body.EnumDeclaration; 010import com.github.javaparser.javadoc.Javadoc; 011import net.steppschuh.markdowngenerator.table.Table; 012import org.springframework.stereotype.Service; 013import javax.inject.Inject; 014 015/** 016 * This service generates markdown tables, see the {@link #createMarkdownTableFromEnum(Table.Builder, Class)} method. 017 018 * 019 020 * @author Kyrylo Semenko 021 */ 022@Service 023public class TableService { 024 private static final String EMPTY_STRING = ""; 025 private static final String ONE_SPACE = " "; 026 private static final String NEW_LINE = "\\r\\n|\\n"; 027 028 /** 029 * Generates content of a table with two columns from {@link Enum} fields. 030 031 * <p> 032 033 * Example of usage: 034 035 * <pre> 036 037 * public String generate() { 038 039 * Table.Builder tableBuilder = new Table.Builder() 040 041 * .addRow("Application name", "Description"); 042 043 * 044 045 * return tableService.createMarkdownTableFromEnum(tableBuilder, TacticHolder.class); 046 047 * } 048 049 * </pre> 050 051 * 052 053 * @param tableBuilder en empty table with header 054 055 * @param enumClass the data source 056 057 * @return the first column with enum fields and a second column with 058 059 * fields JavaDoc 060 */ 061 public String createMarkdownTableFromEnum(Table.Builder tableBuilder, Class<?> enumClass) { 062 String resourceRelativePath = ResourceService.getInstance().getResource(enumClass); 063 String sourceCode = TemplateService.getInstance().getTemplateContent(resourceRelativePath); 064 CompilationUnit compilationUnit = JavaParser.parse(sourceCode); 065 EnumDeclaration enumDeclaration = (EnumDeclaration) compilationUnit.getTypes().get(0); 066 for (EnumConstantDeclaration enumConstantDeclaration : enumDeclaration.getEntries()) { 067 String name = enumConstantDeclaration.getName().asString(); 068 String javadocString = EMPTY_STRING; 069 Javadoc javadoc = enumConstantDeclaration.getJavadoc().orElse(null); 070 if (javadoc != null) { 071 javadocString = javadoc.toText().replaceAll(NEW_LINE, ONE_SPACE); 072 } 073 tableBuilder.addRow(name, javadocString); 074 } 075 return tableBuilder.build().toString(); 076 } 077 078 @Inject 079 @java.lang.SuppressWarnings("all") 080 public TableService() { 081 } 082}