Skip to content

Commit

Permalink
prepare code validation refactoring (refs #124)
Browse files Browse the repository at this point in the history
  • Loading branch information
mborne committed Oct 1, 2019
1 parent 4643160 commit fcebde6
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 34 deletions.
52 changes: 34 additions & 18 deletions validator-core/src/main/java/fr/ign/validator/code/CodeList.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.MissingResourceException;
import java.util.Map;
import java.util.ResourceBundle;

/**
Expand All @@ -16,17 +18,24 @@
*
*/
public class CodeList {

/**
* The name of the code list
*/
private String name ;

private ResourceBundle bundle ;

private CodeList(String name){
/**
* The list of allowed codes associated to descriptions
*/
private Map<String,String> codes = new HashMap<>() ;

private CodeList(String name, Map<String, String> codes){
this.name = name;

this.bundle = ResourceBundle.getBundle("codes/"+name);
this.codes = codes;
}

/**
* Get the name of the list
* @return
*/
public String getName() {
return name;
}
Expand All @@ -37,9 +46,7 @@ public String getName() {
* @return
*/
public Collection<String> getAllowedValues(){
List<String> keys = new ArrayList<>(bundle.keySet());
Collections.sort(keys);
return keys;
return codes.keySet();
}

/**
Expand All @@ -48,22 +55,31 @@ public Collection<String> getAllowedValues(){
* @param code
* @return
*/
public String getTranslation(String code){
try {
return this.bundle.getString(code);
}catch (MissingResourceException e){
return null;
}
public String getDescription(String code){
return codes.get(code);
}

/**
* Gets a code list by a given name
*
* TODO Read JSON file where objects contains at least a name
*
* @param name
* @return
*/
public static CodeList getCodeList(String name){
return new CodeList(name);
/* load bundle file */
ResourceBundle bundle = ResourceBundle.getBundle("codes/"+name);

/* retreive code descriptions */
Map<String,String> codes = new LinkedHashMap<>();
List<String> keys = new ArrayList<>(bundle.keySet());
Collections.sort(keys);
for (String key : keys) {
codes.put(key,bundle.getString(key));
}

return new CodeList(name,codes);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public abstract class CodeListValue {
*/
private CodeList codeList ;
/**
* ISO 19115 code for charset
* The value of the code
*/
private String value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static CharacterSetCode valueOf(String code){
* @return
*/
public Charset getCharset(){
String charsetName = CODE_LIST.getTranslation(getValue());
String charsetName = CODE_LIST.getDescription(getValue());
if ( null == charsetName ){
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static GeometryErrorCode valueOf(String code){
* @return
*/
public String getMessage(){
return CODE_LIST.getTranslation(getValue());
return CODE_LIST.getDescription(getValue());
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.ign.validator.cnig.command;

import java.io.File;
import java.nio.charset.StandardCharsets;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
Expand Down Expand Up @@ -61,7 +62,7 @@ public void execute() throws Exception {

// write results to file
File resultFile = new File(servitudeFile.getParent(), "idGest.txt");
FileUtils.writeStringToFile(resultFile, idGest);
FileUtils.writeStringToFile(resultFile, idGest,StandardCharsets.UTF_8);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
*
* Validates insee codes
* Validates INSEE codes for municipalities
*
* @author MBorne
*
Expand All @@ -13,14 +13,14 @@ public class MunicipalityCode {
/**
* Validates a municipality code
*
* @param insee
* @param value
* @return
*/
public static boolean isValidCommune(String insee){
if ( null == insee ){
public static boolean isValid(String value){
if ( null == value ){
return false ;
}
return insee.matches(REGEXP) ;
return value.matches(REGEXP) ;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void afterValidate(Context context, Document document) throws Exception {
@Override
public void validate(Context context, Attribute<String> validatable) {
String insee = validatable.getBindedValue() ;
if ( ! MunicipalityCode.isValidCommune(insee) ){
if ( ! MunicipalityCode.isValid(insee) ){
context.report(context.createError(CnigErrorCodes.CNIG_INSEE_INVALID)
.setMessageParam("VALUE", insee)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
import fr.ign.validator.tools.ResourceHelper;

/**
*
* Test command CnigExtractIdgestCommand
*
* TODO use TemporaryFolder
* TODO test on a non SERVITUDE file
*
* @author FCerizay
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
import fr.ign.validator.error.ErrorFactory;
import fr.ign.validator.error.ValidatorError;


/**
*
* Ensure that CnigErrorCodes have default configuration
*
* @author MBorne
*
*/
public class CnigErrorCodesTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public class MunicipalityCodeTest {

@Test
public void testValidCommune(){
assertTrue(MunicipalityCode.isValidCommune("25349"));
assertTrue(MunicipalityCode.isValidCommune("2B111"));
assertTrue(MunicipalityCode.isValid("25349"));
assertTrue(MunicipalityCode.isValid("2B111"));
}

@Test
public void testNotValidCommune(){
assertFalse(MunicipalityCode.isValidCommune("2534"));
assertFalse(MunicipalityCode.isValidCommune("253495"));
assertFalse(MunicipalityCode.isValidCommune("2C111"));
assertFalse(MunicipalityCode.isValid("2534"));
assertFalse(MunicipalityCode.isValid("253495"));
assertFalse(MunicipalityCode.isValid("2C111"));
}

}

0 comments on commit fcebde6

Please sign in to comment.