Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

FIX : Ajout controle présence caractère non utf8 dans Utilitaires. Ap… #122

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
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public void checkFileContent(Demande demandeExemp) throws FileCheckingException,
this.checkZones(newLine.toString());//ENTETE vérification des zones saisies par l'utilisateur
this.checkMandatoryZones(newLine.toString(), demande.getTypeExemp());
while ((ligne = bufLecteur.readLine()) != null) { //LIGNES EXEMPLAIRES Tant qu'il y a des lignes à lire dans le fichier
Utilitaires.isValidUtf8(ligne);
this.checkAnormalLineOfExemplary(ligneCourantePositionNumber, ligne); //Détecte une ligne de données vide
checkBodyLine(ligne); //controle adequation taille entete taille ligne exemplaire, controle champ vide, controle format de la date pour un index en Date | Auteur | Titre
ligneCourantePositionNumber++; //pointeur sur ligne en cours d'analyse dans fichier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ private void checkBodyLine(String ligne, DemandeModif demandeModif) throws FileC
throw new FileCheckingException(Constant.ERR_FILE_ERRLINE + ligneCourante
+ Constant.ERR_FILE_LINELENGTH);
}
Utilitaires.isValidUtf8(ligne);
try {
String[] tabligne = ligne.split(";");
checkRcr(tabligne[1], demandeModif.getRcr(), ligneCourante);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public void checkFileContent(Demande demandeRecouv) throws FileCheckingException
}

while ((ligne = bufLecteur.readLine()) != null) { //Tant qu'il y a des lignes à lire dans le fichier
Utilitaires.isValidUtf8(ligne);
this.checkAnormalLineOfExemplary(ligneCourante, ligne); //Détecte une ligne de données vide
//Supprime les éventuels ; que l'utilisateur aurait pu rajouter à la fin des lignes
ligne = Utilitaires.removeSemicolonFromEndOfLine(ligne);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ private void check3Cols(String ligne) throws FileCheckingException {
*/
private void checkBodyLine(String ligne, DemandeSupp demandeSupp) throws FileCheckingException {
try {
Utilitaires.isValidUtf8(ligne);
// contrôle de la longueur de la ligne
if (ligne.split(";").length > 3) {
throw new FileCheckingException(Constant.ERR_FILE_LINE + ligne + " : " + Constant.ERR_FILE_3COL_SUPP_ANY_LINE);
Expand Down
16 changes: 16 additions & 0 deletions core/src/main/java/fr/abes/item/core/utilitaire/Utilitaires.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -370,4 +373,17 @@ public static String getLabelTypeDemande(TYPE_DEMANDE typeDemande) {
};
}

public static void isValidUtf8(String input) throws FileCheckingException {
CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
String messageErreur = "le fichier contient des caracters qui ne sont pas en UTF8";
try {
decoder.decode(java.nio.ByteBuffer.wrap(input.getBytes(StandardCharsets.UTF_8)));
} catch (CharacterCodingException e) {
throw new FileCheckingException(messageErreur);
}
if(input.contains("�")){
throw new FileCheckingException(messageErreur);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,10 @@ void replacementOfDiacriticalAccents() {
String var2 = Utilitaires.replaceDiacritical(var);
assertEquals("e, e, e, e, a, a, a, i, i, o, o, u, u, u, y, ae, oe, c, n, oe", var2);
}

@Test
void testIsValidUtf8() throws FileCheckingException {
assertThrows(FileCheckingException.class, () -> {Utilitaires.isValidUtf8("test�test");});
Utilitaires.isValidUtf8("teékjhfd-èç_kjkfsdj)");
}
}
Loading