Skip to content
This repository was archived by the owner on Jan 22, 2019. It is now read-only.

Commit f615d77

Browse files
committed
Merge pull request #89 from ITRS-Group/master
Changed logic in Parser _readHeaderLine
2 parents ae6ae4e + 848849b commit f615d77

File tree

4 files changed

+211
-55
lines changed

4 files changed

+211
-55
lines changed

src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public enum Feature // implements FormatFeature // for 2.7
4141
* Default value is false, as per <a href="http://tools.ietf.org/html/rfc4180">RFC-4180</a>.
4242
*/
4343
TRIM_SPACES(false),
44-
44+
4545
/**
4646
* Feature that determines how stream of records (usually CSV lines, but sometimes
4747
* multiple lines when linefeeds are included in quoted values) is exposed:
@@ -802,27 +802,36 @@ protected JsonToken _handleArrayValue() throws IOException
802802
/**
803803
* Method called to process the expected header line
804804
*/
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.
811819
*/
812820

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 */ }
815824
return;
816825
}
817-
// case (b); read all
826+
827+
// either the schema is empty or reorder columns flag is set
818828
String name;
819-
// base setting on existing schema, but drop columns
820829
CsvSchema.Builder builder = _schema.rebuild().clearColumns();
821-
830+
822831
while ((name = _reader.nextString()) != null) {
823832
// one more thing: always trim names, regardless of config settings
824833
name = name.trim();
825-
834+
826835
// See if "old" schema defined type; if so, use that type...
827836
CsvSchema.Column prev = _schema.column(name);
828837
if (prev != null) {
@@ -831,6 +840,7 @@ protected void _readHeaderLine() throws IOException
831840
builder.addColumn(name);
832841
}
833842
}
843+
834844
// Ok: did we get any columns?
835845
CsvSchema newSchema = builder.build();
836846
int size = newSchema.size();

src/main/java/com/fasterxml/jackson/dataformat/csv/CsvSchema.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public class CsvSchema
8181
protected final static int ENCODING_FEATURE_USE_HEADER = 0x0001;
8282
protected final static int ENCODING_FEATURE_SKIP_FIRST_DATA_ROW = 0x0002;
8383
protected final static int ENCODING_FEATURE_ALLOW_COMMENTS = 0x0004;
84+
protected final static int ENCODING_FEATURE_REORDER_COLUMNS = 0x0008;
8485

8586
protected final static int DEFAULT_ENCODING_FEATURES = 0;
8687

@@ -454,6 +455,19 @@ public Builder setUseHeader(boolean b) {
454455
return this;
455456
}
456457

458+
/**
459+
* Use in combination with setUseHeader. When use header flag is
460+
* is set, this setting will reorder the columns defined in this
461+
* schema to match the order set by the header.
462+
*
463+
* @param b Enable / Disable this setting
464+
* @return This Builder instance
465+
*/
466+
public Builder setReorderColumns(boolean b) {
467+
_feature(ENCODING_FEATURE_REORDER_COLUMNS, b);
468+
return this;
469+
}
470+
457471
/**
458472
* Method for specifying whether Schema should indicate that
459473
* the first line that is not a header (if header handling enabled)
@@ -774,6 +788,18 @@ public CsvSchema withUseHeader(boolean state) {
774788
return _withFeature(ENCODING_FEATURE_USE_HEADER, state);
775789
}
776790

791+
/**
792+
* Returns a clone of this instance by changing or setting the
793+
* column reordering flag
794+
*
795+
* @param state New value for setting
796+
* @return A copy of itself, ensuring the setting for
797+
* the column reordering feature.
798+
*/
799+
public CsvSchema withColumnReordering(boolean state) {
800+
return _withFeature(ENCODING_FEATURE_REORDER_COLUMNS, state);
801+
}
802+
777803
/**
778804
* Helper method for constructing and returning schema instance that
779805
* is similar to this one, except that it will be using header line.
@@ -968,6 +994,7 @@ public String getSchemaType() {
968994
*/
969995

970996
public boolean usesHeader() { return (_features & ENCODING_FEATURE_USE_HEADER) != 0; }
997+
public boolean reordersColumns() { return (_features & ENCODING_FEATURE_REORDER_COLUMNS) != 0; }
971998
public boolean skipsFirstDataRow() { return (_features & ENCODING_FEATURE_SKIP_FIRST_DATA_ROW) != 0; }
972999
public boolean allowsComments() { return (_features & ENCODING_FEATURE_ALLOW_COMMENTS) != 0; }
9731000

0 commit comments

Comments
 (0)