Skip to content

Commit c6cfc38

Browse files
Improvements in encoding and validationexceptions
1 parent 155f93f commit c6cfc38

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/main/java/io/frictionlessdata/tableschema/field/BooleanField.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.net.URI;
99
import java.util.*;
10+
import java.util.stream.Collectors;
1011

1112
public class BooleanField extends Field<Boolean> {
1213
@JsonIgnore
@@ -59,7 +60,9 @@ public Boolean parseValue(String value, String format, Map<String, Object> optio
5960
return false;
6061

6162
}else{
62-
throw new TypeInferringException("Value "+value+" not in 'trueValues' or 'falseValues'");
63+
String trueStr = (null == trueValues) ? "" : " ("+String.join(", ", trueValues)+") ";
64+
String falseStr = (null == falseValues) ? "" : " ("+String.join(", ", falseValues)+") ";
65+
throw new TypeInferringException("Value '"+value+"' not in 'trueValues' "+ trueStr +" or 'falseValues' "+ falseStr + "");
6366
}
6467
}
6568

src/main/java/io/frictionlessdata/tableschema/field/Field.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,10 +627,12 @@ T castValue(String value, boolean enforceConstraints, Map<String, Object> option
627627

628628
return castValue;
629629

630-
}catch(ConstraintsException ce){
630+
} catch(ConstraintsException ce){
631631
throw ce;
632632

633-
}catch(Exception e){
633+
} catch(TypeInferringException e){
634+
throw new InvalidCastException(e.getMessage());
635+
} catch(Exception e){
634636
throw new InvalidCastException(e);
635637
}
636638
}

src/main/java/io/frictionlessdata/tableschema/tabledatasource/TableDataSource.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import org.apache.commons.csv.CSVFormat;
99

1010
import java.io.*;
11+
import java.nio.charset.Charset;
12+
import java.nio.charset.StandardCharsets;
1113
import java.nio.file.Files;
1214
import java.nio.file.Path;
1315
import java.util.HashMap;
@@ -218,6 +220,21 @@ static Path toSecure(Path testPath, Path referencePath) throws IOException {
218220
return resolvedPath;
219221
}
220222

223+
/**
224+
* Get the standard {@link Charset} (encoding) to use if none is specified. According to the Datapackage
225+
* specs, this should not be the platform default, but UTF-8: "specify the character encoding of the
226+
* resource’s data file. The values should be one of the “Preferred MIME Names” for a character encoding
227+
* registered with IANA . If no value for this key is specified then the default is UTF-8."
228+
* From: https://specs.frictionlessdata.io/data-resource/#metadata-properties
229+
*
230+
* We assume the same should apply for Tables.
231+
*
232+
* @return the default Charset
233+
*/
234+
public static Charset getDefaultEncoding() {
235+
return StandardCharsets.UTF_8;
236+
}
237+
221238
/**
222239
* Data format, currently either CSV or JSON. Formats like Excel are not supported
223240
*/

0 commit comments

Comments
 (0)