001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 package org.apache.commons.csv.writer; 020 021 import java.io.Writer; 022 import java.util.Arrays; 023 import java.util.Map; 024 025 026 /** 027 * CSVWriter 028 * 029 * @author Martin van den Bemt 030 * @version $Id: $ 031 */ 032 public class CSVWriter { 033 034 /** 035 * The CSV config * 036 */ 037 private CSVConfig config; 038 /** 039 * The writer * 040 */ 041 private Writer writer; 042 043 /** 044 * 045 */ 046 public CSVWriter() { 047 } 048 049 public CSVWriter(CSVConfig config) { 050 setConfig(config); 051 } 052 053 public void writeRecord(Map map) { 054 CSVField[] fields = config.getFields(); 055 try { 056 StringBuffer sb = new StringBuffer(); 057 for (int i = 0; i < fields.length; i++) { 058 Object o = map.get(fields[i].getName()); 059 if (o != null) { 060 String value = o.toString(); 061 value = writeValue(fields[i], value); 062 sb.append(value); 063 } 064 if (!config.isDelimiterIgnored() && fields.length != (i + 1)) { 065 sb.append(config.getDelimiter()); 066 } 067 } 068 if (config.isEndTrimmed()) { 069 for (int i = sb.length() - 1; i >= 0; i--) { 070 System.out.println("i : " + i); 071 if (Character.isWhitespace(sb.charAt(i))) { 072 sb.deleteCharAt(i); 073 } else { 074 break; 075 } 076 } 077 } 078 sb.append(config.getRowDelimiter()); 079 String line = sb.toString(); 080 writer.write(line); 081 } catch (Exception e) { 082 e.printStackTrace(); 083 } 084 } 085 086 protected String writeValue(CSVField field, String value) throws Exception { 087 if (config.isFixedWidth()) { 088 if (value.length() < field.getSize()) { 089 int fillPattern = config.getFill(); 090 if (field.overrideFill()) { 091 fillPattern = field.getFill(); 092 } 093 StringBuffer sb = new StringBuffer(); 094 int fillSize = (field.getSize() - value.length()); 095 char[] fill = new char[fillSize]; 096 Arrays.fill(fill, config.getFillChar()); 097 if (fillPattern == CSVConfig.FILLLEFT) { 098 sb.append(fill); 099 sb.append(value); 100 value = sb.toString(); 101 } else { 102 // defaults to fillpattern FILLRIGHT when fixedwidth is used 103 sb.append(value); 104 sb.append(fill); 105 value = sb.toString(); 106 } 107 } else if (value.length() > field.getSize()) { 108 // value to big.. 109 value = value.substring(0, field.getSize()); 110 } 111 } 112 if (!config.isValueDelimiterIgnored()) { 113 // add the value delimiter.. 114 value = config.getValueDelimiter() + value + config.getValueDelimiter(); 115 } 116 return value; 117 } 118 119 /** 120 * @return the CVSConfig or null if not present 121 */ 122 public CSVConfig getConfig() { 123 return config; 124 } 125 126 /** 127 * Set the CSVConfig 128 * 129 * @param config the CVSConfig 130 */ 131 public void setConfig(CSVConfig config) { 132 this.config = config; 133 } 134 135 /** 136 * Set the writer to write the CSV file to. 137 * 138 * @param writer the writer. 139 */ 140 public void setWriter(Writer writer) { 141 this.writer = writer; 142 } 143 144 }