@@ -41,7 +41,7 @@ public enum Feature // implements FormatFeature // for 2.7
41
41
* Default value is false, as per <a href="http://tools.ietf.org/html/rfc4180">RFC-4180</a>.
42
42
*/
43
43
TRIM_SPACES (false ),
44
-
44
+
45
45
/**
46
46
* Feature that determines how stream of records (usually CSV lines, but sometimes
47
47
* multiple lines when linefeeds are included in quoted values) is exposed:
@@ -802,27 +802,36 @@ protected JsonToken _handleArrayValue() throws IOException
802
802
/**
803
803
* Method called to process the expected header line
804
804
*/
805
- protected void _readHeaderLine () throws IOException
806
- {
807
- /* Two separate cases:
808
- *
809
- * (a) We already have a Schema with columns; if so, header will be skipped
810
- * (b) Otherwise, need to find column definitions; empty one is not acceptable
805
+ protected void _readHeaderLine () throws IOException {
806
+ /*
807
+ When the header line is present and the settings ask for it
808
+ to be processed, two different options are possible:
809
+
810
+ a) The schema has been populated. In this case, build a new
811
+ schema where the order matches the *actual* order in which
812
+ the given CSV file offers its columns, iif _schema.reordersColumns()
813
+ is set to true; there cases the consumer of the csv file
814
+ knows about the columns but not necessarily the order in
815
+ which they are defined.
816
+
817
+ b) The schema has not been populated. In this case, build a
818
+ default schema based on the columns found in the header.
811
819
*/
812
820
813
- if (_schema .size () > 0 ) { // case (a); skip all/any
814
- while (_reader .nextString () != null ) { }
821
+ if (_schema .size () > 0 && !_schema .reordersColumns ()) {
822
+ //noinspection StatementWithEmptyBody
823
+ while (_reader .nextString () != null ) { /* does nothing */ }
815
824
return ;
816
825
}
817
- // case (b); read all
826
+
827
+ // either the schema is empty or reorder columns flag is set
818
828
String name ;
819
- // base setting on existing schema, but drop columns
820
829
CsvSchema .Builder builder = _schema .rebuild ().clearColumns ();
821
-
830
+
822
831
while ((name = _reader .nextString ()) != null ) {
823
832
// one more thing: always trim names, regardless of config settings
824
833
name = name .trim ();
825
-
834
+
826
835
// See if "old" schema defined type; if so, use that type...
827
836
CsvSchema .Column prev = _schema .column (name );
828
837
if (prev != null ) {
@@ -831,6 +840,7 @@ protected void _readHeaderLine() throws IOException
831
840
builder .addColumn (name );
832
841
}
833
842
}
843
+
834
844
// Ok: did we get any columns?
835
845
CsvSchema newSchema = builder .build ();
836
846
int size = newSchema .size ();
0 commit comments