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.InputStream; 022 import java.util.ArrayList; 023 import java.util.Arrays; 024 import java.util.Collection; 025 import java.util.List; 026 027 /** 028 * The CSVConfig is used to configure the CSV writer 029 * 030 * @author Martin van den Bemt 031 * @version $Id: $ 032 */ 033 public class CSVConfig { 034 035 /** 036 * specifies if it is a fixed width csv file * 037 */ 038 private boolean fixedWidth; 039 /** 040 * list of fields * 041 */ 042 private List fields; 043 044 /** 045 * Do no do any filling * 046 */ 047 public static final int FILLNONE = 0; 048 /** 049 * Fill content the the left. Mainly usable together with fixedWidth * 050 */ 051 public static final int FILLLEFT = 1; 052 /** 053 * Fill content to the right. Mainly usable together with fixedWidth * 054 */ 055 public static final int FILLRIGHT = 2; 056 057 /** 058 * The fill pattern 059 */ 060 private int fill; 061 /** 062 * The fill char. Defaults to a space 063 */ 064 private char fillChar = ' '; 065 /** 066 * The seperator character. Defaults to , 067 */ 068 private char delimiter = ','; 069 /** 070 * The row separator. Defaults to \n 071 */ 072 private String rowDelimiter = "\n"; 073 /** 074 * Should we ignore the delimiter. Defaults to false 075 */ 076 private boolean ignoreDelimiter = false; 077 /** 078 * the value delimiter. Defaults to " 079 */ 080 private char valueDelimiter = '"'; 081 /** 082 * Should we ignore the value delimiter. Defaults to true 083 */ 084 private boolean ignoreValueDelimiter = true; 085 /** 086 * Specifies if we want to use a field header 087 */ 088 private boolean fieldHeader = false; 089 /** 090 * Specifies if the end of the line needs to be trimmed 091 */ 092 private boolean endTrimmed = false; 093 094 /** 095 * 096 */ 097 public CSVConfig() { 098 super(); 099 } 100 101 /** 102 * @return if the CSV file is fixedWidth 103 */ 104 public boolean isFixedWidth() { 105 return fixedWidth; 106 } 107 108 /** 109 * Specify if the CSV file is fixed width. 110 * Defaults to false 111 * 112 * @param fixedWidth the fixedwidth 113 */ 114 public void setFixedWidth(boolean fixedWidth) { 115 this.fixedWidth = fixedWidth; 116 } 117 118 public void addField(CSVField field) { 119 if (fields == null) { 120 fields = new ArrayList(); 121 } 122 fields.add(field); 123 } 124 125 /** 126 * Set the fields that should be used by the writer. 127 * This will overwrite currently added fields completely! 128 * 129 * @param csvFields the csvfields array. If null it will do nothing 130 */ 131 public void setFields(CSVField[] csvFields) { 132 if (csvFields == null) { 133 return; 134 } 135 fields = new ArrayList(Arrays.asList(csvFields)); 136 } 137 138 /** 139 * Set the fields that should be used by the writer 140 * 141 * @param csvField a collection with fields. If null it will do nothing 142 */ 143 public void setFields(Collection csvField) { 144 if (csvField == null) { 145 return; 146 } 147 fields = new ArrayList(csvField); 148 } 149 150 /** 151 * @return an array with the known fields (even if no fields are specified) 152 */ 153 public CSVField[] getFields() { 154 CSVField[] csvFields = new CSVField[0]; 155 if (fields != null) { 156 return (CSVField[]) fields.toArray(csvFields); 157 } 158 return csvFields; 159 } 160 161 public CSVField getField(String name) { 162 if (fields == null || name == null) { 163 return null; 164 } 165 for (int i = 0; i < fields.size(); i++) { 166 CSVField field = (CSVField) fields.get(i); 167 if (name.equals(field.getName())) { 168 return field; 169 } 170 } 171 return null; 172 } 173 174 /** 175 * @return the fill pattern. 176 */ 177 public int getFill() { 178 return fill; 179 } 180 181 /** 182 * Set the fill pattern. Defaults to {@link #FILLNONE} 183 * <br/>Other options are : {@link #FILLLEFT} and {@link #FILLRIGHT} 184 * 185 * @param fill the fill pattern. 186 */ 187 public void setFill(int fill) { 188 this.fill = fill; 189 } 190 191 /** 192 * @return the fillchar. Defaults to a space. 193 */ 194 public char getFillChar() { 195 return fillChar; 196 } 197 198 /** 199 * Set the fill char 200 * 201 * @param fillChar the fill char 202 */ 203 public void setFillChar(char fillChar) { 204 this.fillChar = fillChar; 205 } 206 207 /** 208 * @return the delimeter used. 209 */ 210 public char getDelimiter() { 211 return delimiter; 212 } 213 214 /** 215 * Set the delimiter to use 216 * 217 * @param delimiter the delimiter character. 218 */ 219 public void setDelimiter(char delimiter) { 220 this.delimiter = delimiter; 221 } 222 223 /** 224 * @return the rowDelimiter used. 225 */ 226 public String getRowDelimiter() { 227 return rowDelimiter; 228 } 229 230 /** 231 * Set the rowDelimiter to use 232 * 233 * @param rowDelimiter the row delimiter character. 234 */ 235 public void setRowDelimiter(String rowDelimiter) { 236 this.rowDelimiter = rowDelimiter; 237 } 238 239 /** 240 * @return if the writer should ignore the delimiter character. 241 */ 242 public boolean isDelimiterIgnored() { 243 return ignoreDelimiter; 244 } 245 246 /** 247 * Specify if the writer should ignore the delimiter. 248 * 249 * @param ignoreDelimiter defaults to false. 250 */ 251 public void setIgnoreDelimiter(boolean ignoreDelimiter) { 252 this.ignoreDelimiter = ignoreDelimiter; 253 } 254 255 /** 256 * @return the value delimeter used. Defaults to " 257 */ 258 public char getValueDelimiter() { 259 return valueDelimiter; 260 } 261 262 /** 263 * Set the value delimiter to use 264 * 265 * @param valueDelimiter the value delimiter character. 266 */ 267 public void setValueDelimiter(char valueDelimiter) { 268 this.valueDelimiter = valueDelimiter; 269 } 270 271 /** 272 * @return if the writer should ignore the value delimiter character. 273 * Defaults to true. 274 */ 275 public boolean isValueDelimiterIgnored() { 276 return ignoreValueDelimiter; 277 } 278 279 /** 280 * Specify if the writer should ignore the value delimiter. 281 * 282 * @param ignoreValueDelimiter defaults to false. 283 */ 284 public void setIgnoreValueDelimiter(boolean ignoreValueDelimiter) { 285 this.ignoreValueDelimiter = ignoreValueDelimiter; 286 } 287 288 /** 289 * @return if a field header is used. Defaults to false 290 */ 291 public boolean isFieldHeader() { 292 return fieldHeader; 293 } 294 295 /** 296 * Specify if you want to use a field header. 297 * 298 * @param fieldHeader true or false. 299 */ 300 public void setFieldHeader(boolean fieldHeader) { 301 this.fieldHeader = fieldHeader; 302 } 303 304 /** 305 * TODO.. 306 * 307 * @see java.lang.Object#equals(java.lang.Object) 308 */ 309 public boolean equals(Object obj) { 310 if (obj == null && !(obj instanceof CSVConfig)) { 311 return false; 312 } 313 return super.equals(obj); 314 // CSVConfig config = (CSVConfig) obj; 315 // getFill() == config.getFill() 316 // getFields().equals(config.getFields()) 317 } 318 319 /** 320 * Creates a config based on a stream. It tries to guess<br/> 321 * NOTE : The stream will be closed. 322 * 323 * @param inputStream the inputstream. 324 * @return the guessed config. 325 */ 326 public static CSVConfig guessConfig(InputStream inputStream) { 327 return null; 328 } 329 330 /** 331 * @return if the end of the line should be trimmed. Default is false. 332 */ 333 public boolean isEndTrimmed() { 334 return endTrimmed; 335 } 336 337 /** 338 * Specify if the end of the line needs to be trimmed. Defaults to false. 339 * 340 * @param endTrimmed 341 */ 342 public void setEndTrimmed(boolean endTrimmed) { 343 this.endTrimmed = endTrimmed; 344 } 345 346 347 }