You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Benedikt Ritter (JIRA)" <ji...@apache.org> on 2014/08/14 22:08:19 UTC

[jira] [Closed] (CSV-21) CSVWriter.writeValue() not using value delimiter

     [ https://issues.apache.org/jira/browse/CSV-21?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Benedikt Ritter closed CSV-21.
------------------------------


> CSVWriter.writeValue() not using value delimiter
> ------------------------------------------------
>
>                 Key: CSV-21
>                 URL: https://issues.apache.org/jira/browse/CSV-21
>             Project: Commons CSV
>          Issue Type: Bug
>            Reporter: Mike Cordes
>            Priority: Minor
>             Fix For: 1.0
>
>   Original Estimate: 5m
>  Remaining Estimate: 5m
>
> CSVWriter.writeValue() only uses value delimiter if field values are fixed width.
> Here is the existing method:
> {code:title=CSVWriter.writeValue() error|borderStyle=solid}
> protected String writeValue(CSVField field, String value) throws Exception {
>         if (config.isFixedWidth()) {
>             if (value.length() < field.getSize()) {
>                 int fillPattern = config.getFill();
>                 if (field.overrideFill()) {
>                     fillPattern = field.getFill();
>                 }
>                 StringBuffer sb = new StringBuffer();
>                 int fillSize = (field.getSize() - value.length());
>                 char[] fill = new char[fillSize];
>                 Arrays.fill(fill, config.getFillChar());
>                 if (fillPattern == CSVConfig.FILLLEFT) {
>                     sb.append(fill);
>                     sb.append(value);
>                     value = sb.toString();
>                 } else {
>                     // defaults to fillpattern FILLRIGHT when fixedwidth is used
>                     sb.append(value);
>                     sb.append(fill);
>                     value = sb.toString();
>                 }
>             } else if (value.length() > field.getSize()) {
>                 // value to big..
>                 value = value.substring(0, field.getSize());
>             }
>             if (!config.isValueDelimiterIgnored()) {
>                 // add the value delimiter..
>                 value = config.getValueDelimiter()+value+config.getValueDelimiter();
>             }
>         }
>         return value;
>    }
> {code}
> The {color:blue}{{if (!config.isValueDelimiterIgnored())}}{color} block should be removed from the {color:blue}{{if (config.isFixedWidth())}}{color} block, like so:
> {code:title=CSVWriter.writeValue() corrected|borderStyle=solid}
> protected String writeValue(CSVField field, String value) throws Exception {
>         if (config.isFixedWidth()) {
>             if (value.length() < field.getSize()) {
>                 int fillPattern = config.getFill();
>                 if (field.overrideFill()) {
>                     fillPattern = field.getFill();
>                 }
>                 StringBuffer sb = new StringBuffer();
>                 int fillSize = (field.getSize() - value.length());
>                 char[] fill = new char[fillSize];
>                 Arrays.fill(fill, config.getFillChar());
>                 if (fillPattern == CSVConfig.FILLLEFT) {
>                     sb.append(fill);
>                     sb.append(value);
>                     value = sb.toString();
>                 } else {
>                     // defaults to fillpattern FILLRIGHT when fixedwidth is used
>                     sb.append(value);
>                     sb.append(fill);
>                     value = sb.toString();
>                 }
>             } else if (value.length() > field.getSize()) {
>                 // value to big..
>                 value = value.substring(0, field.getSize());
>             }
>         }
>         if (!config.isValueDelimiterIgnored()) {
>             // add the value delimiter..
>             value = config.getValueDelimiter()+value+config.getValueDelimiter();
>         }
>         return value;
>    }
> {code}



--
This message was sent by Atlassian JIRA
(v6.2#6252)