View Javadoc

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                  Object o = map.get(fields[i].getName());
54                  if (o != null) {
55                      String value = o.toString();
56                      value = writeValue(fields[i], value);
57                      sb.append(value);
58                  }
59                  if (!config.isDelimiterIgnored() && fields.length != (i+1)) {
60                      sb.append(config.getDelimiter());
61                  }
62              }
63              if (config.isEndTrimmed()) {
64                  for (int i = sb.length()-1; i >= 0; i--) {
65                      System.out.println("i : " + i);
66                      if (Character.isWhitespace(sb.charAt(i))) {
67                          sb.deleteCharAt(i);
68                      } else {
69                          break;
70                      }
71                  }
72              }
73              sb.append("\n");
74              String line = sb.toString();
75              writer.write(line);
76          } catch(Exception e) {
77              e.printStackTrace();
78          }
79      }
80      
81      protected String writeValue(CSVField field, String value) throws Exception {
82          if (config.isFixedWidth()) {
83              if (value.length() < field.getSize()) {
84                  int fillPattern = config.getFill();
85                  if (field.overrideFill()) {
86                      fillPattern = field.getFill();
87                  }
88                  StringBuffer sb = new StringBuffer();
89                  int fillSize = (field.getSize() - value.length());
90                  char[] fill = new char[fillSize];
91                  Arrays.fill(fill, config.getFillChar());
92                  if (fillPattern == CSVConfig.FILLLEFT) {
93                      sb.append(fill);
94                      sb.append(value);
95                      value = sb.toString();
96                  } else {
97                      // defaults to fillpattern FILLRIGHT when fixedwidth is used
98                      sb.append(value);
99                      sb.append(fill);
100                     value = sb.toString();
101                 }
102             } else if (value.length() > field.getSize()) {
103                 // value to big..
104                 value = value.substring(0, field.getSize());
105             }
106             if (!config.isValueDelimiterIgnored()) {
107                 // add the value delimiter..
108                 value = config.getValueDelimiter()+value+config.getValueDelimiter();
109             }
110         }
111         return value;
112     }
113     /**
114      * @return the CVSConfig or null if not present
115      */
116     public CSVConfig getConfig() {
117         return config;
118     }
119 
120     /**
121      * Set the CSVConfig
122      * @param config the CVSConfig
123      */
124     public void setConfig(CSVConfig config) {
125         this.config = config;
126     }
127     
128     /**
129      * Set the writer to write the CSV file to.
130      * @param writer the writer.
131      */
132     public void setWriter(Writer writer) {
133         this.writer = writer;
134     }
135 
136 }