001// Generated by delombok at Thu May 02 07:34:48 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 * @author Kyrylo Semenko 019 */ 020@Service 021public class TableService { 022 private static final String EMPTY_STRING = ""; 023 private static final String ONE_SPACE = " "; 024 private static final String NEW_LINE = "\\r\\n|\\n"; 025 026 /** 027 * Generates content of a table with two columns from {@link Enum} fields. 028 * <p> 029 * Example of usage: 030 * <pre> 031 * public String generate() { 032 * Table.Builder tableBuilder = new Table.Builder() 033 * .addRow("Application name", "Description"); 034 * 035 * return tableService.createMarkdownTableFromEnum(tableBuilder, TacticHolder.class); 036 * } 037 * </pre> 038 * 039 * @param tableBuilder en empty table with header 040 * @param enumClass the data source 041 * @return the first column with enum fields and a second column with 042 * fields JavaDoc 043 */ 044 public String createMarkdownTableFromEnum(Table.Builder tableBuilder, Class<?> enumClass) { 045 String resourceRelativePath = ResourceService.getInstance().getResource(enumClass); 046 String sourceCode = TemplateService.getInstance().getTemplateContent(resourceRelativePath); 047 CompilationUnit compilationUnit = JavaParser.parse(sourceCode); 048 EnumDeclaration enumDeclaration = (EnumDeclaration) compilationUnit.getTypes().get(0); 049 for (EnumConstantDeclaration enumConstantDeclaration : enumDeclaration.getEntries()) { 050 String name = enumConstantDeclaration.getName().asString(); 051 String javadocString = EMPTY_STRING; 052 Javadoc javadoc = enumConstantDeclaration.getJavadoc().orElse(null); 053 if (javadoc != null) { 054 javadocString = javadoc.toText().replaceAll(NEW_LINE, ONE_SPACE); 055 } 056 tableBuilder.addRow(name, javadocString); 057 } 058 return tableBuilder.build().toString(); 059 } 060 061 @Inject 062 @java.lang.SuppressWarnings("all") 063 public TableService() { 064 } 065}