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

164 lines
4.8 KiB
Java

package org.gcube.informationsystem.utils.documentation.generator;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.discovery.knowledge.ModelKnowledge;
import org.gcube.informationsystem.discovery.knowledge.UsageKnowledge;
import org.gcube.informationsystem.types.reference.Type;
import org.gcube.informationsystem.utils.documentation.model.DocumentationGenerator;
import org.gcube.informationsystem.utils.documentation.model.entities.EntityDocumentationGenerator;
import org.gcube.informationsystem.utils.documentation.model.entities.FacetDocumentationGenerator;
import org.gcube.informationsystem.utils.documentation.model.entities.ResourceDocumentationGenerator;
import org.gcube.informationsystem.utils.documentation.model.properties.PropertyDocumentationGenerator;
import org.gcube.informationsystem.utils.documentation.model.relations.ConsistsOfDocumentationGenerator;
import org.gcube.informationsystem.utils.documentation.model.relations.IsRelatedToDocumentationGenerator;
import org.gcube.informationsystem.utils.documentation.model.relations.RelationDocumentationGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class Generator {
private static final Logger logger = LoggerFactory.getLogger(Generator.class);
protected ModelKnowledge modelKnowledge;
public Generator() {
this.modelKnowledge = new ModelKnowledge();
}
public ModelKnowledge getModelKnowledge() {
return modelKnowledge;
}
public Class<? extends DocumentationGenerator> getDocumentationGeneratorClass(AccessType accessType)
throws Exception {
Class<? extends DocumentationGenerator> clz;
switch (accessType) {
case PROPERTY:
clz = PropertyDocumentationGenerator.class;
break;
case ENTITY:
clz = EntityDocumentationGenerator.class;
break;
case RESOURCE:
clz = ResourceDocumentationGenerator.class;
break;
case FACET:
clz = FacetDocumentationGenerator.class;
break;
case RELATION:
clz = RelationDocumentationGenerator.class;
break;
case IS_RELATED_TO:
clz = IsRelatedToDocumentationGenerator.class;
break;
case CONSISTS_OF:
clz = ConsistsOfDocumentationGenerator.class;
break;
default:
throw new Exception("List of types not recognized");
}
return clz;
}
protected DocumentationGenerator getDocumentationGenerator(Type type) throws Exception {
DocumentationGenerator dg = null;
switch (type.getAccessType()) {
case PROPERTY:
dg = new PropertyDocumentationGenerator(type);
dg.setLevel(0);
break;
case ENTITY:
dg = new EntityDocumentationGenerator(type);
dg.setLevel(0);
break;
case RESOURCE:
dg = new ResourceDocumentationGenerator(type);
dg.setLevel(1);
break;
case FACET:
dg = new FacetDocumentationGenerator(type);
dg.setLevel(1);
break;
case RELATION:
dg = new RelationDocumentationGenerator(type);
dg.setLevel(0);
break;
case IS_RELATED_TO:
dg = new IsRelatedToDocumentationGenerator(type);
dg.setLevel(1);
break;
case CONSISTS_OF:
dg = new ConsistsOfDocumentationGenerator(type);
dg.setLevel(1);
break;
default:
throw new Exception("I'm not developed to manage the " + AccessType.class.getSimpleName() + " " + type.getName());
}
UsageKnowledge facetKnowledge = modelKnowledge.getUsageKnowledge(AccessType.FACET);
dg.setFacetKnowledge(facetKnowledge);
UsageKnowledge resourceKnowledge = modelKnowledge.getUsageKnowledge(AccessType.RESOURCE);
dg.setResourceKnowledge(resourceKnowledge);
return dg;
}
protected File getFile(String fileName, boolean newFile) throws IOException {
File file = new File(fileName);
if(file.exists() && newFile) {
file.delete();
}
if(!file.exists()) {
logger.info("Going to create {}", file.getAbsolutePath());
file.createNewFile();
}
return file;
}
protected void writeSectionOnly(Type type, File file, Integer level) throws Exception {
DocumentationGenerator dg = getDocumentationGenerator(type);
if(level!=null) {
dg.setLevel(level);
}
StringBuffer sb = dg.generateSection();
Files.write(file.toPath(), sb.toString().getBytes(), StandardOpenOption.APPEND);
}
protected void writeTypeToFile(Type type, File file, Integer level) throws Exception {
DocumentationGenerator dg = getDocumentationGenerator(type);
if(level!=null) {
dg.setLevel(level);
}
StringBuffer sb = dg.generate();
Files.write(file.toPath(), sb.toString().getBytes(), StandardOpenOption.APPEND);
}
protected void writeTypeToFile(Type type, File file) throws Exception {
writeTypeToFile(type, file, null);
}
}