~ubuntu-branches/ubuntu/quantal/commons-csv/quantal

« back to all changes in this revision

Viewing changes to src/java/org/apache/commons/csv/writer/CSVWriter.java

  • Committer: Bazaar Package Importer
  • Author(s): Jan-Pascal van Best
  • Date: 2007-07-27 09:45:30 UTC
  • Revision ID: james.westby@ubuntu.com-20070727094530-iy6ls22i7yj3p0sg
Tags: upstream-0.1-SNAPSHOT+svn558885
ImportĀ upstreamĀ versionĀ 0.1-SNAPSHOT+svn558885

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Licensed to the Apache Software Foundation (ASF) under one
 
3
 * or more contributor license agreements.  See the NOTICE file
 
4
 * distributed with this work for additional information
 
5
 * regarding copyright ownership.  The ASF licenses this file
 
6
 * to you under the Apache License, Version 2.0 (the
 
7
 * "License"); you may not use this file except in compliance
 
8
 * with the License.  You may obtain a copy of the License at
 
9
 *
 
10
 * http://www.apache.org/licenses/LICENSE-2.0
 
11
 *
 
12
 * Unless required by applicable law or agreed to in writing,
 
13
 * software distributed under the License is distributed on an
 
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 
15
 * KIND, either express or implied.  See the License for the
 
16
 * specific language governing permissions and limitations
 
17
 * under the License.
 
18
 */
 
19
package org.apache.commons.csv.writer;
 
20
 
 
21
import java.io.Writer;
 
22
import java.util.Arrays;
 
23
import java.util.Map;
 
24
 
 
25
 
 
26
/**
 
27
 * CSVWriter
 
28
 *
 
29
 * @author Martin van den Bemt
 
30
 * @version $Id: $
 
31
 */
 
32
public class CSVWriter {
 
33
 
 
34
    /** The CSV config **/
 
35
    private CSVConfig config;
 
36
    /** The writer **/
 
37
    private Writer writer;
 
38
    /**
 
39
     * 
 
40
     */
 
41
    public CSVWriter() {
 
42
    }
 
43
    
 
44
    public CSVWriter(CSVConfig config) {
 
45
        setConfig(config);
 
46
    }
 
47
 
 
48
    public void writeRecord(Map map) {
 
49
        CSVField[] fields = config.getFields();
 
50
        try {
 
51
            StringBuffer sb = new StringBuffer();
 
52
            for (int i = 0; i < fields.length; i++) {
 
53
                String value = (String) map.get(fields[i].getName());
 
54
                value = writeValue(fields[i], value);
 
55
                sb.append(value);
 
56
                if (!config.isDelimiterIgnored() && fields.length != (i+1)) {
 
57
                    sb.append(config.getDelimiter());
 
58
                }
 
59
            }
 
60
            if (config.isEndTrimmed()) {
 
61
                for (int i = sb.length()-1; i >= 0; i--) {
 
62
                    System.out.println("i : " + i);
 
63
                    if (Character.isWhitespace(sb.charAt(i))) {
 
64
                        sb.deleteCharAt(i);
 
65
                    } else {
 
66
                        break;
 
67
                    }
 
68
                }
 
69
            }
 
70
            sb.append("\n");
 
71
            String line = sb.toString();
 
72
            writer.write(line);
 
73
        } catch(Exception e) {
 
74
            e.printStackTrace();
 
75
        }
 
76
    }
 
77
    
 
78
    protected String writeValue(CSVField field, String value) throws Exception {
 
79
        if (config.isFixedWidth()) {
 
80
            if (value.length() < field.getSize()) {
 
81
                int fillPattern = config.getFill();
 
82
                if (field.overrideFill()) {
 
83
                    fillPattern = field.getFill();
 
84
                }
 
85
                StringBuffer sb = new StringBuffer();
 
86
                int fillSize = (field.getSize() - value.length());
 
87
                char[] fill = new char[fillSize];
 
88
                Arrays.fill(fill, config.getFillChar());
 
89
                if (fillPattern == CSVConfig.FILLLEFT) {
 
90
                    sb.append(fill);
 
91
                    sb.append(value);
 
92
                    value = sb.toString();
 
93
                } else {
 
94
                    // defaults to fillpattern FILLRIGHT when fixedwidth is used
 
95
                    sb.append(value);
 
96
                    sb.append(fill);
 
97
                    value = sb.toString();
 
98
                }
 
99
            } else if (value.length() > field.getSize()) {
 
100
                // value to big..
 
101
                value = value.substring(0, field.getSize());
 
102
            }
 
103
            if (!config.isValueDelimiterIgnored()) {
 
104
                // add the value delimiter..
 
105
                value = config.getValueDelimiter()+value+config.getValueDelimiter();
 
106
            }
 
107
        }
 
108
        return value;
 
109
    }
 
110
    /**
 
111
     * @return the CVSConfig or null if not present
 
112
     */
 
113
    public CSVConfig getConfig() {
 
114
        return config;
 
115
    }
 
116
 
 
117
    /**
 
118
     * Set the CSVConfig
 
119
     * @param config the CVSConfig
 
120
     */
 
121
    public void setConfig(CSVConfig config) {
 
122
        this.config = config;
 
123
    }
 
124
    
 
125
    /**
 
126
     * Set the writer to write the CSV file to.
 
127
     * @param writer the writer.
 
128
     */
 
129
    public void setWriter(Writer writer) {
 
130
        this.writer = writer;
 
131
    }
 
132
 
 
133
}