Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 15 additions & 26 deletions validator-core/src/main/java/fr/ign/validator/data/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import fr.ign.validator.database.Database;
import fr.ign.validator.error.CoreErrorCodes;
import fr.ign.validator.error.ErrorCode;
import fr.ign.validator.mapping.MisplacedFile;
import fr.ign.validator.mapping.MisplacedFileManager;
import fr.ign.validator.model.DocumentModel;
import fr.ign.validator.model.FileModel;
import fr.ign.validator.tools.FileUtils;
Expand Down Expand Up @@ -59,7 +61,7 @@ public class Document implements Validatable {
/**
* Files misplaced from model expactations
*/
private Map<FileModel, File> misplacedFiles = new HashMap<>();
private MisplacedFileManager misplacedFileManager = new MisplacedFileManager();

/**
* Additional informations
Expand Down Expand Up @@ -303,7 +305,7 @@ public void findDocumentFiles(Context context) {

log.info(MARKER, "Found {} by name for '{}' (FILE_MISPLACED)", fileModel, file);
if (context.isFlatValidation()) {
addMisplacedFile(fileModel, file);
this.misplacedFileManager.addMisplacedFile(fileModel, file);
} else {
context.beginModel(fileModel);
context.report(
Expand All @@ -330,7 +332,7 @@ public void findDocumentFiles(Context context) {
/*
* Adds the best remaining candidates.
*/
addMisplacedDocumentFiles();
addMisplacedDocumentFiles(context);

log.info(
MARKER, "List files and directories : completed, {} document file(s) found.",
Expand All @@ -345,32 +347,19 @@ private void addDocumentFile(FileModel fileModel, File path) {
this.documentFiles.add(fileModel.createDocumentFile(path));
}

/*
* Adds a misplaced file only if a better candidate doesn't already exists.
*/
private void addMisplacedFile(FileModel fileModel, File path) {
boolean fileModelExists = false;
for (DocumentFile documentFile : this.documentFiles) {
if (documentFile.getFileModel().equals(fileModel)) {
fileModelExists = true;
}
}
for (Map.Entry<FileModel, File> misplacedFile : this.misplacedFiles.entrySet()) {
if (misplacedFile.getKey().equals(fileModel)) {
fileModelExists = true;
}
}
if (!fileModelExists) {
this.misplacedFiles.put(fileModel, path);
}
}

/*
* Transfers misplaced files to DocumentFiles
*/
private void addMisplacedDocumentFiles() {
for (Map.Entry<FileModel, File> misplacedFile : this.misplacedFiles.entrySet()) {
addDocumentFile(misplacedFile.getKey(), misplacedFile.getValue());
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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class CoreErrorCodes {
public static final ErrorCode DIRECTORY_UNEXPECTED = ErrorCode.valueOf("DIRECTORY_UNEXPECTED");

public static final ErrorCode FILE_MISPLACED = ErrorCode.valueOf("FILE_MISPLACED");
public static final ErrorCode FILE_MODEL_OVERLOAD = ErrorCode.valueOf("FILE_MODEL_OVERLOAD");
public static final ErrorCode FILE_EMPTY = ErrorCode.valueOf("FILE_EMPTY");

public static final ErrorCode XSD_SCHEMA_ERROR = ErrorCode.valueOf("XSD_SCHEMA_ERROR");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package fr.ign.validator.mapping;

import java.io.File;

import fr.ign.validator.model.FileModel;

/**
* Maps header with attributes of a FeatureType
*
* @author DDarras
*/
public class MisplacedFile {

public enum Status {
/**
* Standard status
*/
FILE_MISPLACED,
/**
* Multiple files for same FileModel
*/
FILE_MODEL_OVERLOAD
}

/**
* Input FileModel
*/
private FileModel fileModel;

/**
* Input path
*/
private File file;

private MisplacedFile.Status status;

/**
* @param FileModel fileModel
* @param File path
*/
public MisplacedFile(FileModel fileModel, File file) {
this.fileModel = fileModel;
this.file = file;
this.status = MisplacedFile.Status.FILE_MISPLACED;
}

public FileModel getFileModel() {
return fileModel;
}

public File getFile() {
return file;
}

public MisplacedFile.Status getStatus() {
return status;
}

public void setStatus(MisplacedFile.Status status) {
this.status = status;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package fr.ign.validator.mapping;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;

import fr.ign.validator.model.FileModel;

/**
* Maps header with attributes of a FeatureType
*
* @author DDarras
*/
public class MisplacedFileManager {

public static final Logger log = LogManager.getRootLogger();
public static final Marker MARKER = MarkerManager.getMarker("MisplacedFileManager");

/**
* FileModels / File associations
*/
private List<MisplacedFile> misplacedFiles = new ArrayList<>();

public MisplacedFileManager() {
}

/**
* Adds a misplacedFile, checking if corresponding FileModel is overloaded
*
* @param fileModel
* @param File
*/
public void addMisplacedFile(FileModel fileModel, File File) {
MisplacedFile misplacedFile = new MisplacedFile(fileModel, File);

for (MisplacedFile otherFile : this.misplacedFiles) {
if (otherFile.getFileModel().equals(fileModel)) {
log.info(MARKER, "Found multiple files for model {} (FILE_MODEL_OVERLOAD)", fileModel);
otherFile.setStatus(MisplacedFile.Status.FILE_MODEL_OVERLOAD);
misplacedFile.setStatus(MisplacedFile.Status.FILE_MODEL_OVERLOAD);
continue;
}
}
this.misplacedFiles.add(misplacedFile);
}

public List<MisplacedFile> getMisplacedFiles() {
return misplacedFiles;
}
}
6 changes: 6 additions & 0 deletions validator-core/src/main/resources/error-code.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
"message": "Le fichier '{FILEPATH}' est mal nommé ou mal placé dans l'arborescence du dossier.",
"documentation": "Cette erreur se produit lorsque le nom d'un fichier respecte partiellement le modèle. Le nom du fichier est correct, mais pas son chemin complet"
},
{
"name": "FILE_MODEL_OVERLOAD",
"level": "INFO",
"message": "Le modèle '{FILEMODEL}' est consommé par plusieurs fichiers.",
"documentation": "Cette erreur se produit lorsque un modèle est consommé par plusieurs fichiers mal nommés ou avec un chemin incomplet."
},
{
"name": "FILE_UNEXPECTED",
"level": "WARNING",
Expand Down