Commit e6fb3682 authored by PONCELET's avatar PONCELET
Browse files

rebase on sonar netcdf definition

Add dimension javacode in json
parent 61d834ac
This diff is collapsed.
......@@ -112,7 +112,8 @@ public class AsciiDoctorGenerator {
writeLine("include::"+computeAdocName(group, path)+"[]",context.globalOutput);
startTable(context);
writeAttributes(group.getAttributes(), context);
if (!group.getAttributes().isEmpty())
writeGroupAttributes(group.getAttributes(), context);
writeTypes(group.getTypes(), context);
writeDimensions( group.getDims(), context);
writeCoordinateVariables( group.getCoordinateVariables(), context);
......@@ -131,7 +132,8 @@ public class AsciiDoctorGenerator {
writeSubHeader("Types", context);
// write types
for (Type v : types) {
writeTableLine(v.getName(), v.getObligation(), v.getComment(), context);
writeLine(" 2+|{var}" + v.getName() + " |" + v.getComment(),context.currentOutput);
}
creturn(context.currentOutput);
}
......@@ -144,14 +146,14 @@ public class AsciiDoctorGenerator {
String value = v.getName();
if(v.value!=null && v.value.length()>0)
value=value+" = "+v.value;
writeTableLine(value, v.getObligation(), v.getComment(), context);
writeTableLine( " ","{var}"+value, v.getObligation(), v.getComment(), context);
}
creturn(context.currentOutput);
writeLine(" ",context.currentOutput);
}
private void writeAttributes(List<Attribute> attributes, Context context) throws IOException {
private void writeGroupAttributes(List<Attribute> attributes, Context context) throws IOException {
writeSubHeader( "Group attributes", context);
for(Attribute v:attributes)
{
......@@ -169,14 +171,17 @@ public class AsciiDoctorGenerator {
value+="\"";
}
}
writeTableLine(value, v.getObligation(), v.getComment(), context);
writeTableLine(" ","{attr}"+value, v.getObligation(), v.getComment(), context);
}
writeLine(" ", context.currentOutput);
}
private void writeCoordinateVariables(List<Variable> variables, Context context) throws IOException {
if(variables==null || variables.isEmpty())
return;
reallyWriteVariable("Coordinate variables", variables, context);
writeLine(" ", context.currentOutput);
}
private void reallyWriteVariable(String header,List<Variable> variables, Context context) throws IOException {
......@@ -187,7 +192,7 @@ public class AsciiDoctorGenerator {
//leave an empty line between two variables
if(!first)
{
writeTableLine("", "", "", context);
writeLine(" ", context.currentOutput);
}
first=false;
......@@ -201,13 +206,12 @@ public class AsciiDoctorGenerator {
value+=dims.replace('[','(').replace(']', ')');
// really write
writeTableLine(value, v.getObligation(), v.getComment(), context);
writeTableLine(" ","{var}"+value, v.getObligation(), v.getComment(), context);
//write sub attributes
writeVariableAttributes(v.getAttributes(),context);
//add empty line
//writeTableLine("","", "", context);
}
creturn(context.currentOutput);
}
private void writeVariables(List<Variable> variables, Context context) throws IOException {
if(variables.isEmpty())
......@@ -251,35 +255,48 @@ public class AsciiDoctorGenerator {
}
}
}
if(v.getComment() != null && !v.getComment().isEmpty())
{
if (v.getObligation() != null && !v.getObligation().isEmpty() )
{
writeTableLine(" ","{attr}"+value,v.getObligation(),v.getComment(),context);
}else
{
writeLine(" 2+|"+"{attr}"+value+ " |"+v.getComment(), context.currentOutput);
}
} else
{
writeLine(" 3+|"+"{attr}"+value+ " ", context.currentOutput);
}
writeTableLine(value, v.getObligation(), v.getComment(), context);
}
}
private void writeSubHeader(String name, Context context) throws IOException {
// Create header for types
writeTableLine(name, "", "", context);
writeTableLine("e",name, "", "", context);
}
private void writeTableLine(String desc, String obligation, String comment, Context context) throws IOException {
private void writeTableLine(String prefix,String desc, String obligation, String comment, Context context) throws IOException {
if(obligation==null)
obligation="";
if(comment==null)
comment="";
writeLine("|" + desc + " |" + obligation + " |" + comment,context.currentOutput);
writeLine(prefix+"|" + desc + " |" + obligation + " |" + comment,context.currentOutput);
}
private void startTable(Context context) throws IOException {
writeLine("|=============================================================================================================================================================================================================="
,context.currentOutput);
writeLine(":var: {nbsp}{nbsp}{nbsp}{nbsp}\n"+
":attr: {var}{var}\n"+
"[%autowidth,options=\"header\",]\n"+
"|===",context.currentOutput);
writeLine("|Description |Obligation |Comment",context.currentOutput);
creturn(context.currentOutput);
}
private void endTable(Context context) throws IOException {
writeLine("|=============================================================================================================================================================================================================="
,context.currentOutput);
writeLine("|===",context.currentOutput);
}
}
......@@ -8,6 +8,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyStore.Entry;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
......@@ -30,7 +31,7 @@ public class JavaSourceCodeGenerator {
public void run(Group root, File outputDir) throws Exception
{
Path outputPath=Paths.get(outputDir.getAbsolutePath(), OUTPUT_FOLDER_PREDEFINED_LAYER);
generateSourceFile(Paths.get(outputDir.getAbsolutePath(),OUTPUT_FOLDER_API).toFile(), root.getSubGroups());
generateSourceFile(Paths.get(outputDir.getAbsolutePath(),OUTPUT_FOLDER_API).toFile(), Arrays.asList(root));
new PredefinedLayersGenerator().generateSourceFile(root.getSubGroups(),outputPath);
}
......
......@@ -21,10 +21,15 @@ public class Base {
public String getObligation() {
return obligation;
}
public String getSubcomment() {
if(subcomment==null)
return "";
return subcomment;
}
protected final String name;
protected final String description;
protected final String comment;
protected final String subcomment;
protected final String obligation;
public Base(String name, String description, String comment, String obligation) {
......@@ -51,6 +56,9 @@ public class Base {
this.obligation = obligation;
}
subcomment="";
}
}
......@@ -12,11 +12,11 @@ public class SCDimensionAdapter {
String size = dimension.javaValue == null ? "100" : dimension.javaValue;
return Arrays.asList(
String.format("\t\t%s = declareDimension(%s, %s); //%s", getDimVariableName(dimension), getDimNameDeclaration(dimension), size, dimension.comment==null ? "" : dimension.comment));
String.format("\t\t%s = declareDimension(%s, %s); //%s", getDimCodeName(dimension.name), getDimNameDeclaration(dimension), size, dimension.comment==null ? "" : dimension.comment));
}
public static List<String> getSourceAccessor(Dimension dim) {
String name=getDimVariableName(dim);
String name=getDimCodeName(dim.name);
return Arrays.asList(String.format("\t/**\n"
+ "\t * @return the %s\n"
+ "\t */\n"
......@@ -29,30 +29,37 @@ public class SCDimensionAdapter {
return name.substring(0, 1).toUpperCase()+name.substring(1);
}
private static boolean endsWithDim(String name) {
return name.endsWith("Dim") || name.endsWith("dim");
}
/**
* return the variable declaration
* */
public static String getSourceDeclaration(Dimension dimension) {
return String.format("\tprivate NCDimension %s;", getDimVariableName(dimension));
return String.format("\tprivate NCDimension %s;", getDimCodeName(dimension.name));
}
/**
* return dimension variable name
* */
public static String getDimVariableName(Dimension dimension)
public static String getDimCodeName(String dimension)
{
return endsWithDim(dimension.name) ? dimension.name : dimension.name+"Dim";
// return endsWithDim(dimension.name) ? dimension.name : dimension.name+"Dim";
return "dim_"+dimension;
}
/**
* return dimension name in netcdf
* */
public static String getDimNCName(Dimension dimension)
{
return dimension.name;
}
/**
* return dimension name declaration
* return dimension name declaration ie the constant string used to store name value
* */
public static String getDimNameDeclaration(Dimension dimension)
{
return endsWithDim(dimension.name) ? dimension.name.toUpperCase() + "_NAME" : dimension.name.toUpperCase() + "_DIM_NAME";
return dimension.name.toUpperCase() + "_DIM_NAME";
}
}
......@@ -16,33 +16,33 @@ import fr.ifremer.fr.xsf.generator.DataTypeConverter;
public class SCGroupAdapter {
private static boolean forceVariableDeclaration=false;
private static Map<Group, String> groupToClassName = new HashMap<>();
/** @return class name **/
public static String getClassName(Group group, String groupPath) {
if (groupToClassName.containsKey(group)) {
return groupToClassName.get(group);
}
String className;
String groupName = StringUtils.deleteWhitespace(WordUtils.capitalizeFully(group.name.replace("_", " ")));
if (groupName.contentEquals("VendorSpecific")) {
String [] sp = groupPath.split("/");
String prefix = StringUtils.deleteWhitespace(WordUtils.capitalizeFully(sp[sp.length - 2].replace("_", " ")));
className = prefix + "VendorSpecific";
} else {
className = groupName;
}
if (className.isEmpty())
className="Root";
if (!className.endsWith("Group"))
className += "Grp";
groupToClassName.put(group, className);
return className;
}
/**
......@@ -56,8 +56,9 @@ public class SCGroupAdapter {
// package declaration
String outPackage = isSpecific ? outputPackage + ".vendor_specific" : outputPackage;
sourceCode.add("package " + outPackage + ";\n");
sourceCode.add("\nimport java.util.Arrays;\n");
sourceCode.add("import java.util.ArrayList;");
sourceCode.add("import java.util.Arrays;\n");
// Get import and layer declaration lines
SortedSet<String> classToImportSet = new TreeSet<>();
......@@ -67,26 +68,29 @@ public class SCGroupAdapter {
classToImportSet.add("import fr.ifremer.globe.netcdf.ucar.NCGroup;");
if (!group.variables.isEmpty())
classToImportSet.add("import fr.ifremer.globe.netcdf.ucar.NCVariable;");
classToImportSet.add("import fr.ifremer.globe.netcdf.ucar.NCDimension;");
classToImportSet.forEach(importLine -> sourceCode.add(importLine));
// Class
sourceCode.add(String.format("\n/** Class generated by %s **/", SCGroupAdapter.class.getSimpleName()));
sourceCode.add("@SuppressWarnings(\"unused\")");
sourceCode.add(String.format("public class %s extends NCGroup {", getClassName(group, "")));
// Group name
//if group is a sensor, group name is computed
if (group.getGroupCategory() == GroupCategory.eStandard)
sourceCode.add(String.format("\n\tpublic static final String GROUP_NAME = \"%s\";", group.name));
// Dimension name code generation
if(!group.dims.isEmpty())
{
sourceCode.add("\n\t/**dimensions names declaration*/");
group.dims.forEach(d->
sourceCode.add(String.format("\tpublic static final String %s=\"%s\";", SCDimensionAdapter.getDimNameDeclaration(d),SCDimensionAdapter.getDimVariableName(d))));
sourceCode.add(String.format("\tpublic static final String %s=\"%s\";", SCDimensionAdapter.getDimNameDeclaration(d),SCDimensionAdapter.getDimNCName(d))));
sourceCode.add("\n");
}
......@@ -105,17 +109,17 @@ public class SCGroupAdapter {
// Start constructor if not a sensor
switch (group.getGroupCategory())
{
case eStandard:
sourceCode.add(String.format(
"\n\tpublic %s(NCGroup parent, IDataProxy dataProxy) throws NCException {\n\t\tsuper(GROUP_NAME,parent);",
getClassName(group, "")));
break;
case eSensor:
sourceCode.add(String.format(
"\n\tpublic %s(NCGroup parent, IDataProxy dataProxy, String sensor_id) throws NCException {\n\t\tsuper(sensor_id,parent);",
getClassName(group, "")));
break;
case eStandard:
sourceCode.add(String.format(
"\n\tpublic %s(NCGroup parent, IDataProxy dataProxy) throws NCException {\n\t\tsuper(GROUP_NAME,parent);",
getClassName(group, "")));
break;
case eSensor:
sourceCode.add(String.format(
"\n\tpublic %s(NCGroup parent, IDataProxy dataProxy, String sensor_id) throws NCException {\n\t\tsuper(sensor_id,parent);",
getClassName(group, "")));
break;
}
// Sub groups
......@@ -247,7 +251,19 @@ public class SCGroupAdapter {
{
sourceCode.add(localIdent+"# dimensions names declaration");
group.dims.forEach(d->
sourceCode.add(localIdent+String.format("%s = \"%s\"", SCDimensionAdapter.getDimNameDeclaration(d),SCDimensionAdapter.getDimVariableName(d))));
sourceCode.add(localIdent+String.format("%s = \"%s\"", SCDimensionAdapter.getDimNameDeclaration(d),SCDimensionAdapter.getDimNCName(d))));
}
// Coordinate variables Accessors
if (group.coordinatevariables!=null && !group.coordinatevariables.isEmpty()) {
sourceCode.add(localIdent+"# Coordinate Variables names");
String localPath=path;
group.coordinatevariables.forEach(v -> {
try {
sourceCode.add(String.format(localIdent+"%s = \"%s%s\"", v.name.toUpperCase(),localPath,v.name));
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
// Variables declaration
if (!group.variables.isEmpty()) {
......
......@@ -2,6 +2,8 @@ package fr.ifremer.fr.xsf.generator.model;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import fr.ifremer.fr.xsf.generator.DataTypeConverter;
......@@ -43,7 +45,7 @@ public class SCVariableAdapter {
for (String dim : variable.dims) {
if (env.stream().noneMatch(d -> d.name.contentEquals(dim))) {
codeLines.add(String.format("\t\tNCDimension %s = this.getDimension(\"%s\");", dim, dim));
codeLines.add(String.format("\t\tNCDimension %s = this.getDimension(\"%s\");", SCDimensionAdapter.getDimCodeName(dim), dim));
}
}
......@@ -51,7 +53,7 @@ public class SCVariableAdapter {
if(!variable.dims.isEmpty())
{
dimString = new StringBuilder("Arrays.asList(");
variable.dims.forEach(d -> dimString.append(d + (variable.dims.indexOf(d) == variable.dims.size()-1 ? ")" : ", ")));
variable.dims.forEach(d -> dimString.append(SCDimensionAdapter.getDimCodeName(d) + (variable.dims.indexOf(d) == variable.dims.size()-1 ? ")" : ", ")));
} else
{
//scalar Variable
......@@ -77,14 +79,18 @@ public class SCVariableAdapter {
// "comment" attribute
if (variable.comment!=null && !variable.comment.trim().isEmpty()) {
String[] lines = variable.comment.trim().split("\\n");
if (lines.length > 1) {
codeLines.add(String.format("\t\t%s.addAttribute(\"comment\", \"%s\\n\" +", variable.name, lines[0]));
List<String> al =
new ArrayList<String>(Arrays.asList(lines));
String subComments=variable.getSubcomment();
if (!subComments.trim().isEmpty())
al.add(subComments);
if (al.size()> 1) {
codeLines.add(String.format("\t\t%s.addAttribute(\"comment\", \"%s\\n\" +", variable.name, al.get(0)));
for (int i = 1; i < lines.length - 1; i++) {
codeLines.add(String.format("\t\t\t\"%s\\n\" +", lines[i]));
for (int i = 1; i < al.size() - 1; i++) {
codeLines.add(String.format("\t\t\t\"%s\\n\" +", al.get(i)));
}
codeLines.add(String.format("\t\t\t\"%s\\n\"", lines[lines.length - 1]));
codeLines.add(String.format("\t\t\t\"%s\\n\"", al.get(al.size() - 1)));
codeLines.add("\t\t);");
} else {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment