Issue
I'm sure there must be an elegant way to turn Spring Boot configuration meta-data files into HTML (or markdown or ...). While I could write a parser myself I'm convinced that this has already been solved. Yet, I failed to find a lead for that so far.
Solution
Here is a simple example of converting Spring Boot configuration metadata into HTML using org.springframework.boot:spring-boot-configuration-processor
and org.freemarker:freemarker
libraries.
Parse Metadata
Use classes from spring-boot-configuration-processor
library to parse the JSON file,
File metadataFile = new File("spring-configuration-metadata.json");
ConfigurationMetadata metadata =
new JsonMarshaller().read(new FileInputStream(metadataFile));
HTML Generation
Once you have the metadata converted into Java object, use FreeMarker
templated to convert it into an HTML file. It uses metadata.ftl
template to convert the metadata
object into metadata.html
.
Configuration cfg = new Configuration(new Version(2, 3, 27));
// template is loaded from 'templates' folder under resources
cfg.setClassForTemplateLoading(
SpringConfigurationMetadataProcessorTest.class, "/templates");
cfg.setDefaultEncoding("UTF-8");
cfg.setLocale(Locale.US);
cfg.setTemplateExceptionHandler(
TemplateExceptionHandler.RETHROW_HANDLER);
Map<String, Object> input = new HashMap<String, Object>();
input.put("title", "Spring Configuration Metadata Example");
input.put("metadata", metadata);
input.put("GROUP", ItemMetadata.ItemType.GROUP);
input.put("PROPERTY", ItemMetadata.ItemType.PROPERTY);
Template template = cfg.getTemplate("metadata.ftl");
// write output into a file
Writer fileWriter = new FileWriter(new File("metadata.html"));
try {
template.process(input, fileWriter);
} finally {
fileWriter.close();
}
Here is the example metadata.ftl
,
<html>
<head>
<title>${title}</title>
</head>
<body>
<h1>${title}</h1>
<#if metadata.hints??>
<section>
<h1>Hints</h1>
<#list metadata.hints as hint>
<section>
<dl>
<dt><b>name: </b>${hint.name}</dt>
<#if hint.values?? && hint.values?size != 0>
<dt><b>values:</b></dt>
<#list hint.values as value>
<#if value.value??>
<dd><b>value: </b>${value.value}</dd>
</#if>
<#if value.description??>
<dd><b>description: </b>${value.description}</dd>
</#if>
<br/>
</#list>
</#if>
<#if hint.providers?? && hint.providers?size != 0>
<dt><b>providers:</b></dt>
<#list hint.providers as provider>
<#if provider.name??>
<dd><b>name: </b>${provider.name}</dd>
</#if>
<#if provider.parameters?? && provider.parameters?size != 0>
<dd><b>parameters:</b></dd>
<#list provider.parameters as key, value>
<dd> <b>${key}: </b>${value}
</dd>
</#list>
</#if>
<br/>
</#list>
</dl>
</#if>
</section>
</#list>
</section>
</#if>
<#if metadata.items??>
<section>
<h1>Groups</h1>
<#list metadata.items as item>
<#if item.isOfItemType(GROUP)>
<section>
<dl>
<#if item.sourceType??>
<dt><b>sourceType: </b> ${item.sourceType}</dt>
</#if>
<#if item.name??>
<dt><b>name: </b> ${item.name}</dt>
</#if>
<#if item.description??>
<dt><b>description: </b> ${item.description}</dt>
</#if>
<#if item.type??>
<dt><b>type: </b> ${item.type}</dt>
</#if>
<#if item.sourceMethod??>
<dt><b>sourceMethod: </b> ${item.sourceMethod}</dt>
</#if>
<#if item.defaultValue??>
<#if item.defaultValue?is_enumerable>
<dt><b>defaultValue: </b></dt>
<#list item.defaultValue as df>
<dd>${df}</dd>
</#list>
<#else>
<dt><b>defaultValue: </b> ${item.defaultValue}</dt>
</#if>
</#if>
<#if item.deprecation??>
<dt><b>deprecation: </b></dt>
<#if item.deprecation.reason??>
<dd><b>reason: </b> ${item.deprecation.reason}</dd>
</#if>
<#if item.deprecation.replacement??>
<dd><b>replacement: </b> ${item.deprecation.replacement}</dd>
</#if>
<#if item.deprecation.level??>
<dd><b>level: </b> ${item.deprecation.level}</dd>
</#if>
</#if>
</dl>
</section>
</#if>
</#list>
<h1>Properties</h1>
<#list metadata.items as item>
<#if item.isOfItemType(PROPERTY)>
<section>
<dl>
<#if item.sourceType??>
<dt><b>sourceType: </b> ${item.sourceType}</dt>
</#if>
<#if item.name??>
<dt><b>name: </b> ${item.name}</dt>
</#if>
<#if item.description??>
<dt><b>description: </b> ${item.description}</dt>
</#if>
<#if item.type??>
<dt><b>type: </b> ${item.type}</dt>
</#if>
<#if item.sourceMethod??>
<dt><b>sourceMethod: </b> ${item.sourceMethod}</dt>
</#if>
<#if item.defaultValue??>
<#if item.defaultValue?is_enumerable>
<dt><b>defaultValue: </b></dt>
<#list item.defaultValue as df>
<dd>${df}</dd>
</#list>
<#else>
<dt><b>defaultValue: </b> ${item.defaultValue}</dt>
</#if>
</#if>
<#if item.deprecation??>
<dt><b>deprecation: </b></dt>
<#if item.deprecation.reason??>
<dd><b>reason: </b> ${item.deprecation.reason}</dd>
</#if>
<#if item.deprecation.replacement??>
<dd><b>replacement: </b> ${item.deprecation.replacement}</dd>
</#if>
<#if item.deprecation.level??>
<dd><b>level: </b> ${item.deprecation.level}</dd>
</#if>
</#if>
</dl>
</section>
</#if>
</#list>
</section>
</#if>
</body>
</html>
Answered By - Indra Basak
Answer Checked By - Willingham (JavaFixing Volunteer)