diff --git a/validator-core/src/main/java/fr/ign/validator/Context.java b/validator-core/src/main/java/fr/ign/validator/Context.java index 5ae48b51..51a6e444 100644 --- a/validator-core/src/main/java/fr/ign/validator/Context.java +++ b/validator-core/src/main/java/fr/ign/validator/Context.java @@ -16,6 +16,7 @@ import fr.ign.validator.data.Attribute; import fr.ign.validator.data.Document; import fr.ign.validator.data.DocumentFile; +import fr.ign.validator.data.Header; import fr.ign.validator.data.Row; import fr.ign.validator.data.Table; import fr.ign.validator.data.file.MetadataFile; @@ -160,6 +161,11 @@ public class Context { */ private boolean enableConditions = false; + /** + * Header class to use. + */ + private Class headerClass = Header.class; + public Context() { registerDefaultListeners(); } @@ -817,4 +823,14 @@ public void setEnableConditions(boolean enableConditions) { this.enableConditions = enableConditions; } + public void setHeaderClass(Class class1) { + if (Header.class.isAssignableFrom(class1)) { + this.headerClass = class1; + } + } + + public Class getHeaderClass() { + return this.headerClass; + } + } diff --git a/validator-core/src/main/java/fr/ign/validator/data/Document.java b/validator-core/src/main/java/fr/ign/validator/data/Document.java index c8883948..359d49e4 100644 --- a/validator-core/src/main/java/fr/ign/validator/data/Document.java +++ b/validator-core/src/main/java/fr/ign/validator/data/Document.java @@ -353,12 +353,6 @@ private void addDocumentFile(FileModel fileModel, File path) { private void addMisplacedDocumentFiles(Context context) { for (MisplacedFile misplacedFile : this.misplacedFileManager.getMisplacedFiles()) { - if (misplacedFile.getStatus() == MisplacedFile.Status.FILE_MODEL_OVERLOAD) { - context.report( - context.createError(CoreErrorCodes.FILE_MODEL_OVERLOAD) - .setMessageParam("FILEMODEL", misplacedFile.getFileModel().getName()) - ); - } addDocumentFile(misplacedFile.getFileModel(), misplacedFile.getFile()); } } diff --git a/validator-core/src/main/java/fr/ign/validator/data/Header.java b/validator-core/src/main/java/fr/ign/validator/data/Header.java index 3a2340b3..31fd6069 100644 --- a/validator-core/src/main/java/fr/ign/validator/data/Header.java +++ b/validator-core/src/main/java/fr/ign/validator/data/Header.java @@ -35,6 +35,10 @@ public Header(String relativePath, FeatureTypeMapper mapping) { this.mapping = mapping; } + public String getRelativePath() { + return this.relativePath; + } + @Override public void validate(Context context) { context.beginData(this); @@ -70,11 +74,7 @@ public void validate(Context context) { .setMessageParam("FILEPATH", relativePath) ); } else if (!missingAttribute.getConstraints().isRequired()) { - context.report( - context.createError(CoreErrorCodes.TABLE_MISSING_NULLABLE_ATTRIBUTE) - .setMessageParam("ATTRIBUTE_NAME", missingAttribute.getName()) - .setMessageParam("FILEPATH", relativePath) - ); + this.reportTableMissingNullableAttribute(missingAttribute, context); } else { context.report( context.createError(CoreErrorCodes.TABLE_MISSING_ATTRIBUTE) @@ -88,4 +88,18 @@ public void validate(Context context) { context.endData(this); } + /** + * Create report when missing attribute is required + * + * @param missingAttribute + * @param context + */ + public void reportTableMissingNullableAttribute(AttributeType missingAttribute, Context context) { + context.report( + context.createError(CoreErrorCodes.TABLE_MISSING_NULLABLE_ATTRIBUTE) + .setMessageParam("ATTRIBUTE_NAME", missingAttribute.getName()) + .setMessageParam("FILEPATH", relativePath) + ); + } + } diff --git a/validator-core/src/main/java/fr/ign/validator/data/Table.java b/validator-core/src/main/java/fr/ign/validator/data/Table.java index e473fb74..fbb14d2e 100644 --- a/validator-core/src/main/java/fr/ign/validator/data/Table.java +++ b/validator-core/src/main/java/fr/ign/validator/data/Table.java @@ -1,5 +1,7 @@ package fr.ign.validator.data; +import java.lang.reflect.Constructor; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Marker; @@ -71,8 +73,20 @@ public void doValidate(Context context) { */ String[] columns = reader.getHeader(); FeatureTypeMapper mapping = new FeatureTypeMapper(columns, featureType); - Header header = new Header(relativePath, mapping); - header.validate(context); + + try { + Class headerClass = context.getHeaderClass(); + log.info(MARKER, "headerClass: {}", headerClass.getName()); + Constructor headerConstructor = headerClass.getConstructor(String.class, FeatureTypeMapper.class); + Header header = (Header) headerConstructor.newInstance(relativePath, mapping); + log.info(MARKER, "header: {}", header.getClass().getName()); + header.validate(context); + } catch (Exception e) { + log.error(MARKER, "FATAL ERROR. message: {} ; trace: {}", e.getMessage(), e.getStackTrace()); + context.report( + context.createError(CoreErrorCodes.VALIDATOR_EXCEPTION) + ); + } /* * feature validation diff --git a/validator-core/src/main/resources/error-code.json b/validator-core/src/main/resources/error-code.json index f503a264..d955255e 100644 --- a/validator-core/src/main/resources/error-code.json +++ b/validator-core/src/main/resources/error-code.json @@ -567,6 +567,12 @@ "message": "Seuls les fichiers avec les extensions suivantes sont autorisés : xml, pdf, csv, MapInfo (map, mid), ArcGIS (shp, dbf), geojson, gml", "documentation": "" }, + { + "name": "CNIG_TABLE_MISSING_NULLABLE_ATTRIBUTE", + "level": "ERROR", + "message": "Le fichier optionnel '{FILEMODEL_NAME}' est absent du document.", + "documentation": "Cette erreur se produit lorsqu'un fichier marqué comme mandatory=WARNING est absent dans le cadre d'une validation CNIG." + }, { "name": "CNIG_IDURBA_INVALID", "level": "ERROR", diff --git a/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/CnigPlugin.java b/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/CnigPlugin.java index 1ce9fa1d..6f6c3a91 100644 --- a/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/CnigPlugin.java +++ b/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/CnigPlugin.java @@ -1,6 +1,7 @@ package fr.ign.validator.cnig; import fr.ign.validator.Context; +import fr.ign.validator.cnig.data.CnigHeader; import fr.ign.validator.cnig.process.CreateShapefilesPostProcess; import fr.ign.validator.cnig.process.CustomizeIdurbaPreProcess; import fr.ign.validator.cnig.process.DocUrbaComPostProcess; @@ -48,6 +49,11 @@ public void setup(Context context) { // --normalize is required with CNIG plugin context.setNormalizeEnabled(true); + /** + * Use CnigHeader class instead of Header + */ + context.setHeaderClass(CnigHeader.class); + /* * PreProcess - Customize idurba */ diff --git a/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/data/CnigHeader.java b/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/data/CnigHeader.java new file mode 100644 index 00000000..fb02ea19 --- /dev/null +++ b/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/data/CnigHeader.java @@ -0,0 +1,27 @@ +package fr.ign.validator.cnig.data; + +import fr.ign.validator.Context; +import fr.ign.validator.cnig.error.CnigErrorCodes; +import fr.ign.validator.data.Header; +import fr.ign.validator.mapping.FeatureTypeMapper; +import fr.ign.validator.model.AttributeType; + +public class CnigHeader extends Header { + + /** + * @param columns + * @param mapping + */ + public CnigHeader(String relativePath, FeatureTypeMapper mapping) { + super(relativePath, mapping); + } + + @Override + public void reportTableMissingNullableAttribute(AttributeType missingAttribute, Context context) { + context.report( + context.createError(CnigErrorCodes.CNIG_TABLE_MISSING_NULLABLE_ATTRIBUTE) + .setMessageParam("ATTRIBUTE_NAME", missingAttribute.getName()) + .setMessageParam("FILEPATH", this.getRelativePath()) + ); + } +} \ No newline at end of file diff --git a/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/error/CnigErrorCodes.java b/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/error/CnigErrorCodes.java index b32f7219..b64c6bd1 100644 --- a/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/error/CnigErrorCodes.java +++ b/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/error/CnigErrorCodes.java @@ -17,6 +17,13 @@ public class CnigErrorCodes { public static final ErrorCode CNIG_PIECE_ECRITE_ONLY_PDF = ErrorCode.valueOf("CNIG_PIECE_ECRITE_ONLY_PDF"); public static final ErrorCode CNIG_FILE_EXTENSION_INVALID = ErrorCode.valueOf("CNIG_FILE_EXTENSION_INVALID"); + /** + * Overrides CORE_TABLE_MISSING_NULLABLE_ATTRIBUTE + */ + public static final ErrorCode CNIG_TABLE_MISSING_NULLABLE_ATTRIBUTE = ErrorCode.valueOf( + "CNIG_TABLE_MISSING_NULLABLE_ATTRIBUTE" + ); + /** * DU - Reported when ZONE_URBA.IDURBA doesn't match any format */ diff --git a/validator-plugin-cnig/src/test/java/fr/ign/validator/cnig/regress/CnigValidatorRegressTest.java b/validator-plugin-cnig/src/test/java/fr/ign/validator/cnig/regress/CnigValidatorRegressTest.java index d58e3ca3..ffea807a 100644 --- a/validator-plugin-cnig/src/test/java/fr/ign/validator/cnig/regress/CnigValidatorRegressTest.java +++ b/validator-plugin-cnig/src/test/java/fr/ign/validator/cnig/regress/CnigValidatorRegressTest.java @@ -210,7 +210,8 @@ public void test50545_CC_20130902() throws Exception { */ ReportAssert.assertCount(1, CnigErrorCodes.CNIG_METADATA_SPECIFICATION_NOT_FOUND, report); ReportAssert.assertCount(1, CnigErrorCodes.CNIG_METADATA_REFERENCESYSTEMIDENTIFIER_URI_NOT_FOUND, report); - ReportAssert.assertCount(2, ErrorLevel.ERROR, report); + ReportAssert.assertCount(1, CnigErrorCodes.CNIG_TABLE_MISSING_NULLABLE_ATTRIBUTE, report); + ReportAssert.assertCount(1 + 1 + 1, ErrorLevel.ERROR, report); /* * check warnings @@ -222,9 +223,10 @@ public void test50545_CC_20130902() throws Exception { /* * check some infos */ - ReportAssert.assertCount(1, CoreErrorCodes.TABLE_MISSING_NULLABLE_ATTRIBUTE, report); + ReportAssert.assertCount(0, CoreErrorCodes.TABLE_MISSING_NULLABLE_ATTRIBUTE, report); + ReportAssert.assertCount(1, CnigErrorCodes.CNIG_TABLE_MISSING_NULLABLE_ATTRIBUTE, report); { - ValidatorError error = report.getErrorsByCode(CoreErrorCodes.TABLE_MISSING_NULLABLE_ATTRIBUTE).get(0); + ValidatorError error = report.getErrorsByCode(CnigErrorCodes.CNIG_TABLE_MISSING_NULLABLE_ATTRIBUTE).get(0); assertEquals("DATECOG", error.getAttribute()); } @@ -325,7 +327,8 @@ public void test19182_CC_20150517() throws Exception { ReportAssert.assertCount(2, CoreErrorCodes.ATTRIBUTE_GEOMETRY_INVALID, report); ReportAssert.assertCount(19, CoreErrorCodes.ATTRIBUTE_UNEXPECTED_VALUE, report); ReportAssert.assertCount(1, CnigErrorCodes.CNIG_GEOMETRY_COMPLEXITY_ERROR, report); - ReportAssert.assertCount(1 + 2 + 2 + 19 + 1, ErrorLevel.ERROR, report); + ReportAssert.assertCount(1, CnigErrorCodes.CNIG_TABLE_MISSING_NULLABLE_ATTRIBUTE, report); + ReportAssert.assertCount(1 + 2 + 2 + 19 + 1 + 1, ErrorLevel.ERROR, report); /* * check warnings @@ -760,7 +763,10 @@ public void test30014_PLU_20171013() throws Exception { ReportAssert.assertCount(0, CoreErrorCodes.DATABASE_CONSTRAINT_MISMATCH, report); ReportAssert.assertCount(1, CoreErrorCodes.TABLE_FOREIGN_KEY_NOT_FOUND, report); ReportAssert.assertCount(2, CnigErrorCodes.CNIG_PIECE_ECRITE_ONLY_PDF, report); - ReportAssert.assertCount(23, ErrorLevel.ERROR, report); + ReportAssert.assertCount(6, CnigErrorCodes.CNIG_TABLE_MISSING_NULLABLE_ATTRIBUTE, report); + ReportAssert.assertCount(1, CoreErrorCodes.ATTRIBUTE_UNEXPECTED_VALUE, report); + ReportAssert.assertCount(1, CoreErrorCodes.ATTRIBUTE_FILE_NOT_FOUND, report); + ReportAssert.assertCount(18 + 0 + 1 + 2 + 6 + 1 + 1, ErrorLevel.ERROR, report); ReportAssert.assertCount(0, CnigErrorCodes.CNIG_GENERATEUR_SUP_NOT_FOUND, report); ReportAssert.assertCount(0, CnigErrorCodes.CNIG_ASSIETTE_SUP_NOT_FOUND, report); @@ -825,7 +831,7 @@ public void test30014_PLU_20171013_flatOption() throws Exception { * check errors */ ReportAssert.assertCount(0, CnigErrorCodes.CNIG_PIECE_ECRITE_ONLY_PDF, report); - ReportAssert.assertCount(21, ErrorLevel.ERROR, report); + ReportAssert.assertCount(27, ErrorLevel.ERROR, report); /* * check warnings @@ -875,7 +881,8 @@ public void test200011781_PLUi_20180101() throws Exception { */ ReportAssert.assertCount(4, CoreErrorCodes.ATTRIBUTE_GEOMETRY_INVALID, report); ReportAssert.assertCount(1, CoreErrorCodes.ATTRIBUTE_INVALID_REGEXP, report); - ReportAssert.assertCount(5, ErrorLevel.ERROR, report); + ReportAssert.assertCount(1, CnigErrorCodes.CNIG_TABLE_MISSING_NULLABLE_ATTRIBUTE, report); + ReportAssert.assertCount(4 + 1 + 1, ErrorLevel.ERROR, report); /* * check warnings