information-system-model-do.../src/main/java/org/gcube/informationsystem/utils/documentation/model/DocumentationGenerator.java

107 lines
3.2 KiB
Java

package org.gcube.informationsystem.utils.documentation.model;
import java.util.Map;
import java.util.Set;
import org.gcube.informationsystem.base.reference.Element;
import org.gcube.informationsystem.types.TypeMapper;
import org.gcube.informationsystem.types.reference.Type;
import org.gcube.informationsystem.utils.documentation.rst.Section;
import org.gcube.informationsystem.utils.documentation.rst.Section.SectionType;
import org.gcube.informationsystem.utils.documentation.rst.table.Table;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public abstract class DocumentationGenerator<E extends Element> {
private static final Logger logger = LoggerFactory.getLogger(DocumentationGenerator.class);
protected final Class<E> clz;
protected final Type type;
protected final String superClassToBeExcluded;
protected DocumentationGenerator(Class<E> clz, String superClassToBeExcluded) {
this.clz = clz;
this.type = TypeMapper.createTypeDefinition(clz);
this.superClassToBeExcluded = superClassToBeExcluded;
}
/*
* E.g. GCubeProperty extends Property
*/
protected StringBuffer getTypeDeclaration() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("**");
stringBuffer.append(type.getName());
stringBuffer.append("**");
Set<String> superClasses = type.getSuperClasses();
superClasses.remove(superClassToBeExcluded);
if(superClasses.size()>0){
stringBuffer.append(" ``extends`` ");
boolean first = true;
for(String superClass : superClasses) {
if(!first) {
stringBuffer.append(", ");
}else {
first = false;
}
stringBuffer.append("**");
stringBuffer.append(superClass);
stringBuffer.append("**");
}
}
return stringBuffer;
}
protected StringBuffer getChangelog() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("The **");
stringBuffer.append(type.getName());
stringBuffer.append("** current version is ");
stringBuffer.append(type.getVersionAsString());
stringBuffer.append(".\n\n");
Map<String, String> changelog = type.getChangelogWithVersionAsString();
Set<String> keySet = changelog.keySet();
stringBuffer.append("Changelog:\n\n");
int i=0;
for(String version : keySet) {
stringBuffer.append("* **");
stringBuffer.append(version);
stringBuffer.append("**: ");
stringBuffer.append(changelog.get(version));
if(i==keySet.size()-1) {
stringBuffer.append(".");
}else {
stringBuffer.append(";");
}
}
stringBuffer.append("\n\n");
return stringBuffer;
}
protected abstract Table getTable();
public StringBuffer generateSection() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("\n");
String name = type.getName();
Section section = new Section();
stringBuffer.append(section.generateSection(SectionType.HEADING_3, name));
stringBuffer.append(type.getDescription());
stringBuffer.append("\n");
String tableTitle = getTypeDeclaration().toString();
Table table = getTable();
stringBuffer.append(table.generateTable(tableTitle));
stringBuffer.append(getChangelog());
logger.info(stringBuffer.toString());
return stringBuffer;
}
}