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.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 AccessType[] accessTypes; public Generator() { accessTypes = new AccessType[] { AccessType.PROPERTY, AccessType.ENTITY, AccessType.RESOURCE, AccessType.FACET, AccessType.RELATION, AccessType.CONSISTS_OF, AccessType.IS_RELATED_TO }; } public Class getDocumentationGeneratorClass(AccessType accessType) throws Exception { Class 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); break; case ENTITY: dg = new EntityDocumentationGenerator(type); break; case RESOURCE: dg = new ResourceDocumentationGenerator(type); break; case FACET: dg = new FacetDocumentationGenerator(type); break; case RELATION: dg = new RelationDocumentationGenerator(type); break; case IS_RELATED_TO: dg = new IsRelatedToDocumentationGenerator(type); break; case CONSISTS_OF: dg = new ConsistsOfDocumentationGenerator(type); break; default: throw new Exception("I'm not developed to manage the " + AccessType.class.getSimpleName() + " " + type.getName()); } 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 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); } }