Skip to content

Commit c464add

Browse files
authored
feat(cnig): different header based on plugin (#418)
1 parent 9018f4c commit c464add

File tree

9 files changed

+111
-20
lines changed

9 files changed

+111
-20
lines changed

validator-core/src/main/java/fr/ign/validator/Context.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import fr.ign.validator.data.Attribute;
1717
import fr.ign.validator.data.Document;
1818
import fr.ign.validator.data.DocumentFile;
19+
import fr.ign.validator.data.Header;
1920
import fr.ign.validator.data.Row;
2021
import fr.ign.validator.data.Table;
2122
import fr.ign.validator.data.file.MetadataFile;
@@ -160,6 +161,11 @@ public class Context {
160161
*/
161162
private boolean enableConditions = false;
162163

164+
/**
165+
* Header class to use.
166+
*/
167+
private Class<?> headerClass = Header.class;
168+
163169
public Context() {
164170
registerDefaultListeners();
165171
}
@@ -817,4 +823,14 @@ public void setEnableConditions(boolean enableConditions) {
817823
this.enableConditions = enableConditions;
818824
}
819825

826+
public void setHeaderClass(Class<?> class1) {
827+
if (Header.class.isAssignableFrom(class1)) {
828+
this.headerClass = class1;
829+
}
830+
}
831+
832+
public Class<?> getHeaderClass() {
833+
return this.headerClass;
834+
}
835+
820836
}

validator-core/src/main/java/fr/ign/validator/data/Document.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -353,12 +353,6 @@ private void addDocumentFile(FileModel fileModel, File path) {
353353
private void addMisplacedDocumentFiles(Context context) {
354354

355355
for (MisplacedFile misplacedFile : this.misplacedFileManager.getMisplacedFiles()) {
356-
if (misplacedFile.getStatus() == MisplacedFile.Status.FILE_MODEL_OVERLOAD) {
357-
context.report(
358-
context.createError(CoreErrorCodes.FILE_MODEL_OVERLOAD)
359-
.setMessageParam("FILEMODEL", misplacedFile.getFileModel().getName())
360-
);
361-
}
362356
addDocumentFile(misplacedFile.getFileModel(), misplacedFile.getFile());
363357
}
364358
}

validator-core/src/main/java/fr/ign/validator/data/Header.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public Header(String relativePath, FeatureTypeMapper mapping) {
3535
this.mapping = mapping;
3636
}
3737

38+
public String getRelativePath() {
39+
return this.relativePath;
40+
}
41+
3842
@Override
3943
public void validate(Context context) {
4044
context.beginData(this);
@@ -70,11 +74,7 @@ public void validate(Context context) {
7074
.setMessageParam("FILEPATH", relativePath)
7175
);
7276
} else if (!missingAttribute.getConstraints().isRequired()) {
73-
context.report(
74-
context.createError(CoreErrorCodes.TABLE_MISSING_NULLABLE_ATTRIBUTE)
75-
.setMessageParam("ATTRIBUTE_NAME", missingAttribute.getName())
76-
.setMessageParam("FILEPATH", relativePath)
77-
);
77+
this.reportTableMissingNullableAttribute(missingAttribute, context);
7878
} else {
7979
context.report(
8080
context.createError(CoreErrorCodes.TABLE_MISSING_ATTRIBUTE)
@@ -88,4 +88,18 @@ public void validate(Context context) {
8888
context.endData(this);
8989
}
9090

91+
/**
92+
* Create report when missing attribute is required
93+
*
94+
* @param missingAttribute
95+
* @param context
96+
*/
97+
public void reportTableMissingNullableAttribute(AttributeType<?> missingAttribute, Context context) {
98+
context.report(
99+
context.createError(CoreErrorCodes.TABLE_MISSING_NULLABLE_ATTRIBUTE)
100+
.setMessageParam("ATTRIBUTE_NAME", missingAttribute.getName())
101+
.setMessageParam("FILEPATH", relativePath)
102+
);
103+
}
104+
91105
}

validator-core/src/main/java/fr/ign/validator/data/Table.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package fr.ign.validator.data;
22

3+
import java.lang.reflect.Constructor;
4+
35
import org.apache.logging.log4j.LogManager;
46
import org.apache.logging.log4j.Logger;
57
import org.apache.logging.log4j.Marker;
@@ -71,8 +73,20 @@ public void doValidate(Context context) {
7173
*/
7274
String[] columns = reader.getHeader();
7375
FeatureTypeMapper mapping = new FeatureTypeMapper(columns, featureType);
74-
Header header = new Header(relativePath, mapping);
75-
header.validate(context);
76+
77+
try {
78+
Class<?> headerClass = context.getHeaderClass();
79+
log.info(MARKER, "headerClass: {}", headerClass.getName());
80+
Constructor<?> headerConstructor = headerClass.getConstructor(String.class, FeatureTypeMapper.class);
81+
Header header = (Header) headerConstructor.newInstance(relativePath, mapping);
82+
log.info(MARKER, "header: {}", header.getClass().getName());
83+
header.validate(context);
84+
} catch (Exception e) {
85+
log.error(MARKER, "FATAL ERROR. message: {} ; trace: {}", e.getMessage(), e.getStackTrace());
86+
context.report(
87+
context.createError(CoreErrorCodes.VALIDATOR_EXCEPTION)
88+
);
89+
}
7690

7791
/*
7892
* feature validation

validator-core/src/main/resources/error-code.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,12 @@
567567
"message": "Seuls les fichiers avec les extensions suivantes sont autorisés : xml, pdf, csv, MapInfo (map, mid), ArcGIS (shp, dbf), geojson, gml",
568568
"documentation": ""
569569
},
570+
{
571+
"name": "CNIG_TABLE_MISSING_NULLABLE_ATTRIBUTE",
572+
"level": "ERROR",
573+
"message": "Le fichier optionnel '{FILEMODEL_NAME}' est absent du document.",
574+
"documentation": "Cette erreur se produit lorsqu'un fichier marqué comme mandatory=WARNING est absent dans le cadre d'une validation CNIG."
575+
},
570576
{
571577
"name": "CNIG_IDURBA_INVALID",
572578
"level": "ERROR",

validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/CnigPlugin.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fr.ign.validator.cnig;
22

33
import fr.ign.validator.Context;
4+
import fr.ign.validator.cnig.data.CnigHeader;
45
import fr.ign.validator.cnig.process.CreateShapefilesPostProcess;
56
import fr.ign.validator.cnig.process.CustomizeIdurbaPreProcess;
67
import fr.ign.validator.cnig.process.DocUrbaComPostProcess;
@@ -48,6 +49,11 @@ public void setup(Context context) {
4849
// --normalize is required with CNIG plugin
4950
context.setNormalizeEnabled(true);
5051

52+
/**
53+
* Use CnigHeader class instead of Header
54+
*/
55+
context.setHeaderClass(CnigHeader.class);
56+
5157
/*
5258
* PreProcess - Customize idurba
5359
*/
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package fr.ign.validator.cnig.data;
2+
3+
import fr.ign.validator.Context;
4+
import fr.ign.validator.cnig.error.CnigErrorCodes;
5+
import fr.ign.validator.data.Header;
6+
import fr.ign.validator.mapping.FeatureTypeMapper;
7+
import fr.ign.validator.model.AttributeType;
8+
9+
public class CnigHeader extends Header {
10+
11+
/**
12+
* @param columns
13+
* @param mapping
14+
*/
15+
public CnigHeader(String relativePath, FeatureTypeMapper mapping) {
16+
super(relativePath, mapping);
17+
}
18+
19+
@Override
20+
public void reportTableMissingNullableAttribute(AttributeType<?> missingAttribute, Context context) {
21+
context.report(
22+
context.createError(CnigErrorCodes.CNIG_TABLE_MISSING_NULLABLE_ATTRIBUTE)
23+
.setMessageParam("ATTRIBUTE_NAME", missingAttribute.getName())
24+
.setMessageParam("FILEPATH", this.getRelativePath())
25+
);
26+
}
27+
}

validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/error/CnigErrorCodes.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ public class CnigErrorCodes {
1717
public static final ErrorCode CNIG_PIECE_ECRITE_ONLY_PDF = ErrorCode.valueOf("CNIG_PIECE_ECRITE_ONLY_PDF");
1818
public static final ErrorCode CNIG_FILE_EXTENSION_INVALID = ErrorCode.valueOf("CNIG_FILE_EXTENSION_INVALID");
1919

20+
/**
21+
* Overrides CORE_TABLE_MISSING_NULLABLE_ATTRIBUTE
22+
*/
23+
public static final ErrorCode CNIG_TABLE_MISSING_NULLABLE_ATTRIBUTE = ErrorCode.valueOf(
24+
"CNIG_TABLE_MISSING_NULLABLE_ATTRIBUTE"
25+
);
26+
2027
/**
2128
* DU - Reported when ZONE_URBA.IDURBA doesn't match any format
2229
*/

validator-plugin-cnig/src/test/java/fr/ign/validator/cnig/regress/CnigValidatorRegressTest.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@ public void test50545_CC_20130902() throws Exception {
210210
*/
211211
ReportAssert.assertCount(1, CnigErrorCodes.CNIG_METADATA_SPECIFICATION_NOT_FOUND, report);
212212
ReportAssert.assertCount(1, CnigErrorCodes.CNIG_METADATA_REFERENCESYSTEMIDENTIFIER_URI_NOT_FOUND, report);
213-
ReportAssert.assertCount(2, ErrorLevel.ERROR, report);
213+
ReportAssert.assertCount(1, CnigErrorCodes.CNIG_TABLE_MISSING_NULLABLE_ATTRIBUTE, report);
214+
ReportAssert.assertCount(1 + 1 + 1, ErrorLevel.ERROR, report);
214215

215216
/*
216217
* check warnings
@@ -222,9 +223,10 @@ public void test50545_CC_20130902() throws Exception {
222223
/*
223224
* check some infos
224225
*/
225-
ReportAssert.assertCount(1, CoreErrorCodes.TABLE_MISSING_NULLABLE_ATTRIBUTE, report);
226+
ReportAssert.assertCount(0, CoreErrorCodes.TABLE_MISSING_NULLABLE_ATTRIBUTE, report);
227+
ReportAssert.assertCount(1, CnigErrorCodes.CNIG_TABLE_MISSING_NULLABLE_ATTRIBUTE, report);
226228
{
227-
ValidatorError error = report.getErrorsByCode(CoreErrorCodes.TABLE_MISSING_NULLABLE_ATTRIBUTE).get(0);
229+
ValidatorError error = report.getErrorsByCode(CnigErrorCodes.CNIG_TABLE_MISSING_NULLABLE_ATTRIBUTE).get(0);
228230
assertEquals("DATECOG", error.getAttribute());
229231
}
230232

@@ -325,7 +327,8 @@ public void test19182_CC_20150517() throws Exception {
325327
ReportAssert.assertCount(2, CoreErrorCodes.ATTRIBUTE_GEOMETRY_INVALID, report);
326328
ReportAssert.assertCount(19, CoreErrorCodes.ATTRIBUTE_UNEXPECTED_VALUE, report);
327329
ReportAssert.assertCount(1, CnigErrorCodes.CNIG_GEOMETRY_COMPLEXITY_ERROR, report);
328-
ReportAssert.assertCount(1 + 2 + 2 + 19 + 1, ErrorLevel.ERROR, report);
330+
ReportAssert.assertCount(1, CnigErrorCodes.CNIG_TABLE_MISSING_NULLABLE_ATTRIBUTE, report);
331+
ReportAssert.assertCount(1 + 2 + 2 + 19 + 1 + 1, ErrorLevel.ERROR, report);
329332

330333
/*
331334
* check warnings
@@ -760,7 +763,10 @@ public void test30014_PLU_20171013() throws Exception {
760763
ReportAssert.assertCount(0, CoreErrorCodes.DATABASE_CONSTRAINT_MISMATCH, report);
761764
ReportAssert.assertCount(1, CoreErrorCodes.TABLE_FOREIGN_KEY_NOT_FOUND, report);
762765
ReportAssert.assertCount(2, CnigErrorCodes.CNIG_PIECE_ECRITE_ONLY_PDF, report);
763-
ReportAssert.assertCount(23, ErrorLevel.ERROR, report);
766+
ReportAssert.assertCount(6, CnigErrorCodes.CNIG_TABLE_MISSING_NULLABLE_ATTRIBUTE, report);
767+
ReportAssert.assertCount(1, CoreErrorCodes.ATTRIBUTE_UNEXPECTED_VALUE, report);
768+
ReportAssert.assertCount(1, CoreErrorCodes.ATTRIBUTE_FILE_NOT_FOUND, report);
769+
ReportAssert.assertCount(18 + 0 + 1 + 2 + 6 + 1 + 1, ErrorLevel.ERROR, report);
764770
ReportAssert.assertCount(0, CnigErrorCodes.CNIG_GENERATEUR_SUP_NOT_FOUND, report);
765771
ReportAssert.assertCount(0, CnigErrorCodes.CNIG_ASSIETTE_SUP_NOT_FOUND, report);
766772

@@ -825,7 +831,7 @@ public void test30014_PLU_20171013_flatOption() throws Exception {
825831
* check errors
826832
*/
827833
ReportAssert.assertCount(0, CnigErrorCodes.CNIG_PIECE_ECRITE_ONLY_PDF, report);
828-
ReportAssert.assertCount(21, ErrorLevel.ERROR, report);
834+
ReportAssert.assertCount(27, ErrorLevel.ERROR, report);
829835

830836
/*
831837
* check warnings
@@ -875,7 +881,8 @@ public void test200011781_PLUi_20180101() throws Exception {
875881
*/
876882
ReportAssert.assertCount(4, CoreErrorCodes.ATTRIBUTE_GEOMETRY_INVALID, report);
877883
ReportAssert.assertCount(1, CoreErrorCodes.ATTRIBUTE_INVALID_REGEXP, report);
878-
ReportAssert.assertCount(5, ErrorLevel.ERROR, report);
884+
ReportAssert.assertCount(1, CnigErrorCodes.CNIG_TABLE_MISSING_NULLABLE_ATTRIBUTE, report);
885+
ReportAssert.assertCount(4 + 1 + 1, ErrorLevel.ERROR, report);
879886

880887
/*
881888
* check warnings

0 commit comments

Comments
 (0)