Improving code

master
Luca Frosini 1 year ago
parent 25fad84b2d
commit 82f2c26b3e

3
.gitignore vendored

@ -1 +1,4 @@
/target/
/entities.rst
/properties.rst
/relations.rst

@ -5,6 +5,11 @@
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>

@ -1,2 +1,16 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8

@ -12,6 +12,8 @@ import java.util.ServiceLoader;
import org.gcube.informationsystem.base.reference.AccessType;
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.discovery.ElementSpecilizationDiscovery;
import org.gcube.informationsystem.utils.discovery.RegistrationProvider;
import org.gcube.informationsystem.utils.documentation.model.DocumentationGenerator;
@ -90,8 +92,8 @@ public class DocGenerator {
File f = getFile(cm);
for(Class<? extends E> clz : discovery.getDiscovered()) {
@SuppressWarnings("rawtypes")
DocumentationGenerator dgInstance = cm.getDocumentationGeneratorInstance(clz);
Type type = TypeMapper.createTypeDefinition(clz);
DocumentationGenerator dgInstance = cm.getDocumentationGeneratorInstance(type);
StringBuffer sb = dgInstance.generateSection();
Files.write(f.toPath(), sb.toString().getBytes(), StandardOpenOption.APPEND);
}
@ -99,8 +101,9 @@ public class DocGenerator {
protected void generateSingle(ModelTypes cm) throws Exception {
File f = getFile(cm);
@SuppressWarnings("rawtypes")
DocumentationGenerator dgInstance = cm.getDocumentationGeneratorInstance(cm.getAccessType().getTypeClass());
Class<? extends Element> clz = cm.getAccessType().getTypeClass();
Type type = TypeMapper.createTypeDefinition(clz);
DocumentationGenerator dgInstance = cm.getDocumentationGeneratorInstance(type);
StringBuffer sb = dgInstance.generateSection();
Files.write(f.toPath(), sb.toString().getBytes(), StandardOpenOption.APPEND);
}

@ -3,6 +3,8 @@ package org.gcube.informationsystem.utils.documentation;
import java.lang.reflect.Constructor;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.types.TypeMapper;
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;
@ -51,9 +53,9 @@ public enum ModelTypes {
return dgClz;
}
public DocumentationGenerator getDocumentationGeneratorInstance(Class clz) throws Exception {
Constructor<? extends DocumentationGenerator> constructor = dgClz.getConstructor(clz.getClass());
return constructor.newInstance(clz);
public DocumentationGenerator getDocumentationGeneratorInstance(Type type) throws Exception {
Constructor<? extends DocumentationGenerator> constructor = dgClz.getConstructor(Type.class);
return constructor.newInstance(type);
}
public boolean discover() {

@ -3,11 +3,13 @@ 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.types.reference.properties.PropertyDefinition;
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.Cell;
import org.gcube.informationsystem.utils.documentation.rst.table.Row;
import org.gcube.informationsystem.utils.documentation.rst.table.RowType;
import org.gcube.informationsystem.utils.documentation.rst.table.Table;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -15,20 +17,124 @@ import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public abstract class DocumentationGenerator<E extends Element> {
public abstract class DocumentationGenerator {
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);
protected DocumentationGenerator(Type type, String superClassToBeExcluded) {
this.type = type;
this.superClassToBeExcluded = superClassToBeExcluded;
}
protected Row getPropertiesBoldRow() {
Row row = new Row(RowType.NORMAL);
Cell name = new Cell();
name.setText("**Name**");
row.appendCell(name);
Cell type = new Cell();
type.setText("**Type**");
row.appendCell(type);
Cell attributes = new Cell();
attributes.setText("**Attributes**");
row.appendCell(attributes);
Cell description = new Cell();
description.setText("**Description**");
row.appendCell(description);
return row;
}
protected Row getPropertiesHeadingRow() {
Row row = new Row(RowType.HEADING);
Cell name = new Cell();
name.setText("Name");
row.appendCell(name);
Cell type = new Cell();
type.setText("Type");
row.appendCell(type);
Cell attributes = new Cell();
attributes.setText("Attributes");
row.appendCell(attributes);
Cell description = new Cell();
description.setText("Description");
row.appendCell(description);
return row;
}
protected String getPropertyAttributes(PropertyDefinition propertyDefinition) {
StringBuffer stringBuffer = new StringBuffer();
boolean mandatory = propertyDefinition.isMandatory();
stringBuffer.append("``Mandatory:");
stringBuffer.append(mandatory);
boolean readOnly = propertyDefinition.isReadonly();
stringBuffer.append("`` ``ReadOnly:");
stringBuffer.append(readOnly);
boolean notNull = propertyDefinition.isNotnull();
stringBuffer.append("`` ``NotNull:");
stringBuffer.append(notNull);
String regex = propertyDefinition.getRegexp();
if(regex!=null && regex.compareTo("")!=0) {
stringBuffer.append("`` ``Regex:");
stringBuffer.append(regex);
}
Integer max = propertyDefinition.getMax();
if(max!=null) {
stringBuffer.append("`` ``Max:");
stringBuffer.append(max);
}
Integer min = propertyDefinition.getMin();
if(min!=null) {
stringBuffer.append("`` ``Min:");
stringBuffer.append(min);
}
stringBuffer.append("``");
return stringBuffer.toString();
}
protected Row getPropertyRow(PropertyDefinition propertyDefinition) {
Row row = new Row(RowType.NORMAL);
Cell name = new Cell();
name.setText(propertyDefinition.getName());
row.appendCell(name);
Cell type = new Cell();
type.setText(propertyDefinition.getType());
row.appendCell(type);
Cell attributes = new Cell();
attributes.setText(getPropertyAttributes(propertyDefinition));
row.appendCell(attributes);
Cell description = new Cell();
description.setText(propertyDefinition.getDescription());
row.appendCell(description);
return row;
}
protected Row getNoPropertyRow() {
return getRowCellSpan("This type does not define any additional attributes.", 4);
}
protected Row addCellSpanToRow(Row row, String content, int cellSpan) {
Cell cellToSpan = new Cell();
cellToSpan.setText(content);
for(int i=0; i<cellSpan; i++) {
row.appendCell(cellToSpan);
}
return row;
}
protected Row getRowCellSpan(String content, int cellSpan) {
Row row = new Row(RowType.NORMAL);
return addCellSpanToRow(row, content, cellSpan);
}
/*
* E.g. GCubeProperty extends Property
*/
@ -38,7 +144,9 @@ public abstract class DocumentationGenerator<E extends Element> {
stringBuffer.append(type.getName());
stringBuffer.append("**");
Set<String> superClasses = type.getSuperClasses();
superClasses.remove(superClassToBeExcluded);
if(superClassToBeExcluded!=null) {
superClasses.remove(superClassToBeExcluded);
}
if(superClasses.size()>0){
stringBuffer.append(" ``extends`` ");
boolean first = true;
@ -84,7 +192,6 @@ public abstract class DocumentationGenerator<E extends Element> {
protected abstract Table getTable();
public StringBuffer generateSection() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("\n");

@ -1,10 +1,9 @@
package org.gcube.informationsystem.utils.documentation.model.entities;
import org.gcube.informationsystem.base.reference.entities.EntityElement;
import org.gcube.informationsystem.model.reference.entities.Entity;
import org.gcube.informationsystem.types.TypeMapper;
import org.gcube.informationsystem.types.reference.Type;
import org.gcube.informationsystem.utils.documentation.model.DocumentationGenerator;
import org.gcube.informationsystem.utils.documentation.rst.table.Cell;
import org.gcube.informationsystem.utils.documentation.rst.table.Row;
import org.gcube.informationsystem.utils.documentation.rst.table.RowType;
import org.gcube.informationsystem.utils.documentation.rst.table.Table;
@ -12,33 +11,23 @@ import org.gcube.informationsystem.utils.documentation.rst.table.Table;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class EntityDocumentationGenerator<E extends Entity> extends DocumentationGenerator<E> {
public class EntityDocumentationGenerator extends DocumentationGenerator {
public EntityDocumentationGenerator(Class<E> clz) {
super(clz, TypeMapper.getType(EntityElement.class));
public EntityDocumentationGenerator(Type type) {
super(type, TypeMapper.getType(EntityElement.class));
}
protected Row getHeadingRow() {
protected Row getEntityHeadingRow() {
Row row = new Row(RowType.HEADING);
Cell name = new Cell();
name.setText("Name");
row.appendCell(name);
Cell type = new Cell();
type.setText("Type");
row.appendCell(type);
Cell attributes = new Cell();
attributes.setText("Attributes");
row.appendCell(attributes);
Cell description = new Cell();
description.setText("Description");
row.appendCell(description);
row = addCellSpanToRow(row, "Properties", 4);
return row;
}
@Override
protected Table getTable() {
Table table = new Table();
table.appendRow(getHeadingRow());
table.appendRow(getEntityHeadingRow());
table.appendRow(getPropertiesBoldRow());
return table;
}

@ -1,41 +1,45 @@
package org.gcube.informationsystem.utils.documentation.model.entities;
import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.utils.documentation.rst.table.Cell;
import org.gcube.informationsystem.utils.documentation.rst.table.Row;
import org.gcube.informationsystem.utils.documentation.rst.table.RowType;
import java.util.HashSet;
import java.util.Set;
import org.gcube.informationsystem.types.reference.Type;
import org.gcube.informationsystem.types.reference.properties.PropertyDefinition;
import org.gcube.informationsystem.utils.documentation.rst.table.Table;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class FacetDocumentationGenerator<F extends Facet> extends EntityDocumentationGenerator<F> {
public FacetDocumentationGenerator(Class<F> clz) {
super(clz);
}
public class FacetDocumentationGenerator extends EntityDocumentationGenerator {
protected Row getHeadingRow() {
Row row = new Row(RowType.HEADING);
Cell name = new Cell();
name.setText("Name");
row.appendCell(name);
Cell type = new Cell();
type.setText("Type");
row.appendCell(type);
Cell attributes = new Cell();
attributes.setText("Attributes");
row.appendCell(attributes);
Cell description = new Cell();
description.setText("Description");
row.appendCell(description);
return row;
public FacetDocumentationGenerator(Type type) {
super(type);
}
@Override
protected Table getTable() {
Table table = new Table();
table.appendRow(getHeadingRow());
table.appendRow(getEntityHeadingRow());
table.appendRow(getPropertiesBoldRow());
Set<PropertyDefinition> properties = type.getProperties();
if(properties!=null && properties.size()>0) {
Set<PropertyDefinition> notMandatoryProperties = new HashSet<>();
for(PropertyDefinition propertyDefinition : properties) {
if(propertyDefinition.isMandatory()) {
/*
* Adding Mandatory properties first in the table
*/
table.appendRow(getPropertyRow(propertyDefinition));
}else {
notMandatoryProperties.add(propertyDefinition);
}
}
for(PropertyDefinition propertyDefinition : notMandatoryProperties) {
table.appendRow(getPropertyRow(propertyDefinition));
}
}else {
table.appendRow(getNoPropertyRow());
}
return table;
}

@ -1,6 +1,6 @@
package org.gcube.informationsystem.utils.documentation.model.entities;
import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.types.reference.Type;
import org.gcube.informationsystem.utils.documentation.rst.table.Cell;
import org.gcube.informationsystem.utils.documentation.rst.table.Row;
import org.gcube.informationsystem.utils.documentation.rst.table.RowType;
@ -9,10 +9,10 @@ import org.gcube.informationsystem.utils.documentation.rst.table.Table;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ResourceDocumentationGenerator<R extends Resource> extends EntityDocumentationGenerator<R> {
public class ResourceDocumentationGenerator extends EntityDocumentationGenerator {
public ResourceDocumentationGenerator(Class<R> clz) {
super(clz);
public ResourceDocumentationGenerator(Type type) {
super(type);
}
protected Row getHeadingRow() {

@ -4,99 +4,26 @@ import java.util.HashSet;
import java.util.Set;
import org.gcube.informationsystem.base.reference.properties.PropertyElement;
import org.gcube.informationsystem.model.reference.properties.Property;
import org.gcube.informationsystem.types.TypeMapper;
import org.gcube.informationsystem.types.reference.Type;
import org.gcube.informationsystem.types.reference.properties.PropertyDefinition;
import org.gcube.informationsystem.utils.documentation.model.DocumentationGenerator;
import org.gcube.informationsystem.utils.documentation.rst.table.Cell;
import org.gcube.informationsystem.utils.documentation.rst.table.Row;
import org.gcube.informationsystem.utils.documentation.rst.table.RowType;
import org.gcube.informationsystem.utils.documentation.rst.table.Table;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class PropertyDocumentationGenerator<P extends Property> extends DocumentationGenerator<P> {
public class PropertyDocumentationGenerator extends DocumentationGenerator {
public PropertyDocumentationGenerator(Class<P> clz) {
super(clz, TypeMapper.getType(PropertyElement.class));
}
protected Row getHeadingRow() {
Row row = new Row(RowType.HEADING);
Cell name = new Cell();
name.setText("Name");
row.appendCell(name);
Cell type = new Cell();
type.setText("Type");
row.appendCell(type);
Cell attributes = new Cell();
attributes.setText("Attributes");
row.appendCell(attributes);
Cell description = new Cell();
description.setText("Description");
row.appendCell(description);
return row;
}
protected String getPropertyAttributes(PropertyDefinition propertyDefinition) {
StringBuffer stringBuffer = new StringBuffer();
boolean mandatory = propertyDefinition.isMandatory();
stringBuffer.append("``Mandatory:");
stringBuffer.append(mandatory);
boolean readOnly = propertyDefinition.isReadonly();
stringBuffer.append("`` ``ReadOnly:");
stringBuffer.append(readOnly);
boolean notNull = propertyDefinition.isNotnull();
stringBuffer.append("`` ``NotNull:");
stringBuffer.append(notNull);
String regex = propertyDefinition.getRegexp();
if(regex!=null && regex.compareTo("")!=0) {
stringBuffer.append("`` ``Regex:");
stringBuffer.append(regex);
}
Integer max = propertyDefinition.getMax();
if(max!=null) {
stringBuffer.append("`` ``Max:");
stringBuffer.append(max);
}
Integer min = propertyDefinition.getMin();
if(min!=null) {
stringBuffer.append("`` ``Min:");
stringBuffer.append(min);
}
stringBuffer.append("``");
return stringBuffer.toString();
}
protected Row getPropertyRow(PropertyDefinition propertyDefinition) {
Row row = new Row(RowType.NORMAL);
Cell name = new Cell();
name.setText(propertyDefinition.getName());
row.appendCell(name);
Cell type = new Cell();
type.setText(propertyDefinition.getType());
row.appendCell(type);
Cell attributes = new Cell();
attributes.setText(getPropertyAttributes(propertyDefinition));
row.appendCell(attributes);
Cell description = new Cell();
description.setText(propertyDefinition.getDescription());
row.appendCell(description);
return row;
public PropertyDocumentationGenerator(Type type) {
super(type, TypeMapper.getType(PropertyElement.class));
}
@Override
protected Table getTable() {
Table table = new Table();
table.appendRow(getHeadingRow());
table.appendRow(getPropertiesHeadingRow());
Set<PropertyDefinition> properties = type.getProperties();
if(properties!=null && properties.size()>0) {
Set<PropertyDefinition> notMandatoryProperties = new HashSet<>();
@ -119,16 +46,5 @@ public class PropertyDocumentationGenerator<P extends Property> extends Document
return table;
}
protected Row getNoPropertyRow() {
Row row = new Row(RowType.NORMAL);
Cell noProp = new Cell();
noProp.setText("This type does not define any additional attributes.");
row.appendCell(noProp);
row.appendCell(noProp);
row.appendCell(noProp);
row.appendCell(noProp);
return row;
}
}

@ -1,8 +1,6 @@
package org.gcube.informationsystem.utils.documentation.model.relations;
import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.types.reference.Type;
import org.gcube.informationsystem.utils.documentation.rst.table.Cell;
import org.gcube.informationsystem.utils.documentation.rst.table.Row;
import org.gcube.informationsystem.utils.documentation.rst.table.RowType;
@ -11,10 +9,10 @@ import org.gcube.informationsystem.utils.documentation.rst.table.Table;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ConsistsOfDocumentationGenerator<CO extends ConsistsOf<Resource, Facet>> extends RelationDocumentationGenerator<CO> {
public class ConsistsOfDocumentationGenerator extends RelationDocumentationGenerator {
public ConsistsOfDocumentationGenerator(Class<CO> clz) {
super(clz);
public ConsistsOfDocumentationGenerator(Type type) {
super(type);
}
protected Row getHeadingRow() {

@ -1,7 +1,6 @@
package org.gcube.informationsystem.utils.documentation.model.relations;
import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
import org.gcube.informationsystem.types.reference.Type;
import org.gcube.informationsystem.utils.documentation.rst.table.Cell;
import org.gcube.informationsystem.utils.documentation.rst.table.Row;
import org.gcube.informationsystem.utils.documentation.rst.table.RowType;
@ -10,10 +9,10 @@ import org.gcube.informationsystem.utils.documentation.rst.table.Table;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class IsRelatedToDocumentationGenerator<IRT extends IsRelatedTo<Resource, Resource>> extends RelationDocumentationGenerator<IRT> {
public class IsRelatedToDocumentationGenerator extends RelationDocumentationGenerator {
public IsRelatedToDocumentationGenerator(Class<IRT> clz) {
super(clz);
public IsRelatedToDocumentationGenerator(Type type) {
super(type);
}
protected Row getHeadingRow() {

@ -1,10 +1,8 @@
package org.gcube.informationsystem.utils.documentation.model.relations;
import org.gcube.informationsystem.base.reference.relations.RelationElement;
import org.gcube.informationsystem.model.reference.entities.Entity;
import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.model.reference.relations.Relation;
import org.gcube.informationsystem.types.TypeMapper;
import org.gcube.informationsystem.types.reference.Type;
import org.gcube.informationsystem.utils.documentation.model.DocumentationGenerator;
import org.gcube.informationsystem.utils.documentation.rst.table.Cell;
import org.gcube.informationsystem.utils.documentation.rst.table.Row;
@ -14,10 +12,10 @@ import org.gcube.informationsystem.utils.documentation.rst.table.Table;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class RelationDocumentationGenerator<R extends Relation<? extends Resource, ? extends Entity>> extends DocumentationGenerator<R> {
public class RelationDocumentationGenerator extends DocumentationGenerator {
public RelationDocumentationGenerator(Class<R> clz) {
super(clz, TypeMapper.getType(RelationElement.class));
public RelationDocumentationGenerator(Type type) {
super(type, TypeMapper.getType(RelationElement.class));
}
protected Row getHeadingRow() {

@ -4,16 +4,12 @@ import java.io.File;
import java.net.URL;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class GenerateTest {
private static final Logger logger = LoggerFactory.getLogger(GenerateTest.class);
@Test
public void testDocGeneration() throws Exception {
URL jsonFileURL = this.getClass().getClassLoader().getResource("logback-test.xml");

@ -15,7 +15,7 @@ public class TableTest {
private static final Logger logger = LoggerFactory.getLogger(TableTest.class);
@Test
// @Test
public void test() throws Exception {
Table table = new Table();

Loading…
Cancel
Save