Skip to content

Commit fcebde6

Browse files
committed
prepare code validation refactoring (refs #124)
1 parent 4643160 commit fcebde6

File tree

10 files changed

+62
-34
lines changed

10 files changed

+62
-34
lines changed

validator-core/src/main/java/fr/ign/validator/code/CodeList.java

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import java.util.ArrayList;
44
import java.util.Collection;
55
import java.util.Collections;
6+
import java.util.HashMap;
7+
import java.util.LinkedHashMap;
68
import java.util.List;
7-
import java.util.MissingResourceException;
9+
import java.util.Map;
810
import java.util.ResourceBundle;
911

1012
/**
@@ -16,17 +18,24 @@
1618
*
1719
*/
1820
public class CodeList {
19-
21+
/**
22+
* The name of the code list
23+
*/
2024
private String name ;
21-
22-
private ResourceBundle bundle ;
23-
24-
private CodeList(String name){
25+
/**
26+
* The list of allowed codes associated to descriptions
27+
*/
28+
private Map<String,String> codes = new HashMap<>() ;
29+
30+
private CodeList(String name, Map<String, String> codes){
2531
this.name = name;
26-
27-
this.bundle = ResourceBundle.getBundle("codes/"+name);
32+
this.codes = codes;
2833
}
2934

35+
/**
36+
* Get the name of the list
37+
* @return
38+
*/
3039
public String getName() {
3140
return name;
3241
}
@@ -37,9 +46,7 @@ public String getName() {
3746
* @return
3847
*/
3948
public Collection<String> getAllowedValues(){
40-
List<String> keys = new ArrayList<>(bundle.keySet());
41-
Collections.sort(keys);
42-
return keys;
49+
return codes.keySet();
4350
}
4451

4552
/**
@@ -48,22 +55,31 @@ public Collection<String> getAllowedValues(){
4855
* @param code
4956
* @return
5057
*/
51-
public String getTranslation(String code){
52-
try {
53-
return this.bundle.getString(code);
54-
}catch (MissingResourceException e){
55-
return null;
56-
}
58+
public String getDescription(String code){
59+
return codes.get(code);
5760
}
5861

5962
/**
6063
* Gets a code list by a given name
6164
*
65+
* TODO Read JSON file where objects contains at least a name
66+
*
6267
* @param name
6368
* @return
6469
*/
6570
public static CodeList getCodeList(String name){
66-
return new CodeList(name);
71+
/* load bundle file */
72+
ResourceBundle bundle = ResourceBundle.getBundle("codes/"+name);
73+
74+
/* retreive code descriptions */
75+
Map<String,String> codes = new LinkedHashMap<>();
76+
List<String> keys = new ArrayList<>(bundle.keySet());
77+
Collections.sort(keys);
78+
for (String key : keys) {
79+
codes.put(key,bundle.getString(key));
80+
}
81+
82+
return new CodeList(name,codes);
6783
}
6884

6985
}

validator-core/src/main/java/fr/ign/validator/code/CodeListValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public abstract class CodeListValue {
1717
*/
1818
private CodeList codeList ;
1919
/**
20-
* ISO 19115 code for charset
20+
* The value of the code
2121
*/
2222
private String value;
2323

validator-core/src/main/java/fr/ign/validator/metadata/code/CharacterSetCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static CharacterSetCode valueOf(String code){
3737
* @return
3838
*/
3939
public Charset getCharset(){
40-
String charsetName = CODE_LIST.getTranslation(getValue());
40+
String charsetName = CODE_LIST.getDescription(getValue());
4141
if ( null == charsetName ){
4242
return null;
4343
}

validator-core/src/main/java/fr/ign/validator/validation/attribute/GeometryErrorCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static GeometryErrorCode valueOf(String code){
3939
* @return
4040
*/
4141
public String getMessage(){
42-
return CODE_LIST.getTranslation(getValue());
42+
return CODE_LIST.getDescription(getValue());
4343
}
4444

4545

validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/command/CnigExtractIdgestCommand.java

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

33
import java.io.File;
4+
import java.nio.charset.StandardCharsets;
45

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

6263
// write results to file
6364
File resultFile = new File(servitudeFile.getParent(), "idGest.txt");
64-
FileUtils.writeStringToFile(resultFile, idGest);
65+
FileUtils.writeStringToFile(resultFile, idGest,StandardCharsets.UTF_8);
6566
}
6667

6768
}

validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/model/MunicipalityCode.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/**
44
*
5-
* Validates insee codes
5+
* Validates INSEE codes for municipalities
66
*
77
* @author MBorne
88
*
@@ -13,14 +13,14 @@ public class MunicipalityCode {
1313
/**
1414
* Validates a municipality code
1515
*
16-
* @param insee
16+
* @param value
1717
* @return
1818
*/
19-
public static boolean isValidCommune(String insee){
20-
if ( null == insee ){
19+
public static boolean isValid(String value){
20+
if ( null == value ){
2121
return false ;
2222
}
23-
return insee.matches(REGEXP) ;
23+
return value.matches(REGEXP) ;
2424
}
2525

2626
}

validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/validation/attribute/InseeValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void afterValidate(Context context, Document document) throws Exception {
6060
@Override
6161
public void validate(Context context, Attribute<String> validatable) {
6262
String insee = validatable.getBindedValue() ;
63-
if ( ! MunicipalityCode.isValidCommune(insee) ){
63+
if ( ! MunicipalityCode.isValid(insee) ){
6464
context.report(context.createError(CnigErrorCodes.CNIG_INSEE_INVALID)
6565
.setMessageParam("VALUE", insee)
6666
);

validator-plugin-cnig/src/test/java/fr/ign/validator/cnig/command/CnigExtractIdgestCommandTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
import fr.ign.validator.tools.ResourceHelper;
1515

1616
/**
17+
*
18+
* Test command CnigExtractIdgestCommand
19+
*
20+
* TODO use TemporaryFolder
21+
* TODO test on a non SERVITUDE file
1722
*
1823
* @author FCerizay
1924
*

validator-plugin-cnig/src/test/java/fr/ign/validator/cnig/error/CnigErrorCodesTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313
import fr.ign.validator.error.ErrorFactory;
1414
import fr.ign.validator.error.ValidatorError;
1515

16-
16+
/**
17+
*
18+
* Ensure that CnigErrorCodes have default configuration
19+
*
20+
* @author MBorne
21+
*
22+
*/
1723
public class CnigErrorCodesTest {
1824

1925
@Test

validator-plugin-cnig/src/test/java/fr/ign/validator/cnig/models/MunicipalityCodeTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ public class MunicipalityCodeTest {
1212

1313
@Test
1414
public void testValidCommune(){
15-
assertTrue(MunicipalityCode.isValidCommune("25349"));
16-
assertTrue(MunicipalityCode.isValidCommune("2B111"));
15+
assertTrue(MunicipalityCode.isValid("25349"));
16+
assertTrue(MunicipalityCode.isValid("2B111"));
1717
}
1818

1919
@Test
2020
public void testNotValidCommune(){
21-
assertFalse(MunicipalityCode.isValidCommune("2534"));
22-
assertFalse(MunicipalityCode.isValidCommune("253495"));
23-
assertFalse(MunicipalityCode.isValidCommune("2C111"));
21+
assertFalse(MunicipalityCode.isValid("2534"));
22+
assertFalse(MunicipalityCode.isValid("253495"));
23+
assertFalse(MunicipalityCode.isValid("2C111"));
2424
}
2525

2626
}

0 commit comments

Comments
 (0)