mirror of
https://github.com/apache/commons-csv.git
synced 2025-02-07 18:49:24 +00:00
Removed the writer package (SANDBOX-269), fixed width fields are out of the scope of [csv]
git-svn-id: https://svn.apache.org/repos/asf/commons/sandbox/csv/trunk@1297383 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e7434e5cb5
commit
4ea25c050e
@ -1,323 +0,0 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.commons.csv.writer;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The CSVConfig is used to configure the CSV writer
|
||||
*
|
||||
* @author Martin van den Bemt
|
||||
* @version $Id: $
|
||||
*/
|
||||
public class CSVConfig {
|
||||
|
||||
/** specifies if it is a fixed width csv file */
|
||||
private boolean fixedWidth;
|
||||
|
||||
/** list of fields */
|
||||
private List<CSVField> fields;
|
||||
|
||||
/** Do no do any filling */
|
||||
public static final int FILLNONE = 0;
|
||||
|
||||
/** Fill content the the left. Mainly usable together with fixedWidth */
|
||||
public static final int FILLLEFT = 1;
|
||||
|
||||
/** Fill content to the right. Mainly usable together with fixedWidth */
|
||||
public static final int FILLRIGHT = 2;
|
||||
|
||||
/** The fill pattern */
|
||||
private int fill;
|
||||
|
||||
/** The fill char. Defaults to a space */
|
||||
private char fillChar = ' ';
|
||||
|
||||
/** The seperator character. Defaults to ,*/
|
||||
private char delimiter = ',';
|
||||
|
||||
/** The row separator. Defaults to \n */
|
||||
private String rowDelimiter = "\n";
|
||||
|
||||
/** Should we ignore the delimiter. Defaults to false */
|
||||
private boolean ignoreDelimiter = false;
|
||||
|
||||
/** the value delimiter. Defaults to " */
|
||||
private char valueDelimiter = '"';
|
||||
|
||||
/** Should we ignore the value delimiter. Defaults to true */
|
||||
private boolean ignoreValueDelimiter = true;
|
||||
|
||||
/** Specifies if we want to use a field header */
|
||||
private boolean fieldHeader = false;
|
||||
|
||||
/** Specifies if the end of the line needs to be trimmed */
|
||||
private boolean endTrimmed = false;
|
||||
|
||||
/**
|
||||
* @return if the CSV file is fixedWidth
|
||||
*/
|
||||
public boolean isFixedWidth() {
|
||||
return fixedWidth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify if the CSV file is fixed width.
|
||||
* Defaults to false
|
||||
*
|
||||
* @param fixedWidth the fixedwidth
|
||||
*/
|
||||
public void setFixedWidth(boolean fixedWidth) {
|
||||
this.fixedWidth = fixedWidth;
|
||||
}
|
||||
|
||||
public void addField(CSVField field) {
|
||||
if (fields == null) {
|
||||
fields = new ArrayList<CSVField>();
|
||||
}
|
||||
fields.add(field);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the fields that should be used by the writer.
|
||||
* This will overwrite currently added fields completely!
|
||||
*
|
||||
* @param csvFields the csvfields array. If null it will do nothing
|
||||
*/
|
||||
public void setFields(CSVField[] csvFields) {
|
||||
if (csvFields == null) {
|
||||
return;
|
||||
}
|
||||
fields = new ArrayList(Arrays.asList(csvFields));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the fields that should be used by the writer
|
||||
*
|
||||
* @param csvField a collection with fields. If null it will do nothing
|
||||
*/
|
||||
public void setFields(Collection csvField) {
|
||||
if (csvField == null) {
|
||||
return;
|
||||
}
|
||||
fields = new ArrayList(csvField);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return an array with the known fields (even if no fields are specified)
|
||||
*/
|
||||
public CSVField[] getFields() {
|
||||
CSVField[] csvFields = new CSVField[0];
|
||||
if (fields != null) {
|
||||
return (CSVField[]) fields.toArray(csvFields);
|
||||
}
|
||||
return csvFields;
|
||||
}
|
||||
|
||||
public CSVField getField(String name) {
|
||||
if (fields == null || name == null) {
|
||||
return null;
|
||||
}
|
||||
for (int i = 0; i < fields.size(); i++) {
|
||||
CSVField field = (CSVField) fields.get(i);
|
||||
if (name.equals(field.getName())) {
|
||||
return field;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the fill pattern.
|
||||
*/
|
||||
public int getFill() {
|
||||
return fill;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the fill pattern. Defaults to {@link #FILLNONE}
|
||||
* <br/>Other options are : {@link #FILLLEFT} and {@link #FILLRIGHT}
|
||||
*
|
||||
* @param fill the fill pattern.
|
||||
*/
|
||||
public void setFill(int fill) {
|
||||
this.fill = fill;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the fillchar. Defaults to a space.
|
||||
*/
|
||||
public char getFillChar() {
|
||||
return fillChar;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the fill char
|
||||
*
|
||||
* @param fillChar the fill char
|
||||
*/
|
||||
public void setFillChar(char fillChar) {
|
||||
this.fillChar = fillChar;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the delimeter used.
|
||||
*/
|
||||
public char getDelimiter() {
|
||||
return delimiter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the delimiter to use
|
||||
*
|
||||
* @param delimiter the delimiter character.
|
||||
*/
|
||||
public void setDelimiter(char delimiter) {
|
||||
this.delimiter = delimiter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the rowDelimiter used.
|
||||
*/
|
||||
public String getRowDelimiter() {
|
||||
return rowDelimiter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the rowDelimiter to use
|
||||
*
|
||||
* @param rowDelimiter the row delimiter character.
|
||||
*/
|
||||
public void setRowDelimiter(String rowDelimiter) {
|
||||
this.rowDelimiter = rowDelimiter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return if the writer should ignore the delimiter character.
|
||||
*/
|
||||
public boolean isDelimiterIgnored() {
|
||||
return ignoreDelimiter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify if the writer should ignore the delimiter.
|
||||
*
|
||||
* @param ignoreDelimiter defaults to false.
|
||||
*/
|
||||
public void setIgnoreDelimiter(boolean ignoreDelimiter) {
|
||||
this.ignoreDelimiter = ignoreDelimiter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the value delimeter used. Defaults to "
|
||||
*/
|
||||
public char getValueDelimiter() {
|
||||
return valueDelimiter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value delimiter to use
|
||||
*
|
||||
* @param valueDelimiter the value delimiter character.
|
||||
*/
|
||||
public void setValueDelimiter(char valueDelimiter) {
|
||||
this.valueDelimiter = valueDelimiter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return if the writer should ignore the value delimiter character.
|
||||
* Defaults to true.
|
||||
*/
|
||||
public boolean isValueDelimiterIgnored() {
|
||||
return ignoreValueDelimiter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify if the writer should ignore the value delimiter.
|
||||
*
|
||||
* @param ignoreValueDelimiter defaults to false.
|
||||
*/
|
||||
public void setIgnoreValueDelimiter(boolean ignoreValueDelimiter) {
|
||||
this.ignoreValueDelimiter = ignoreValueDelimiter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return if a field header is used. Defaults to false
|
||||
*/
|
||||
public boolean isFieldHeader() {
|
||||
return fieldHeader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify if you want to use a field header.
|
||||
*
|
||||
* @param fieldHeader true or false.
|
||||
*/
|
||||
public void setFieldHeader(boolean fieldHeader) {
|
||||
this.fieldHeader = fieldHeader;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO..
|
||||
*
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
public boolean equals(Object obj) {
|
||||
if (!(obj instanceof CSVConfig)) {
|
||||
return false;
|
||||
}
|
||||
return super.equals(obj);
|
||||
// CSVConfig config = (CSVConfig) obj;
|
||||
// getFill() == config.getFill()
|
||||
// getFields().equals(config.getFields())
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a config based on a stream. It tries to guess<br/>
|
||||
* NOTE : The stream will be closed.
|
||||
*
|
||||
* @param inputStream the inputstream.
|
||||
* @return the guessed config.
|
||||
*/
|
||||
public static CSVConfig guessConfig(InputStream inputStream) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return if the end of the line should be trimmed. Default is false.
|
||||
*/
|
||||
public boolean isEndTrimmed() {
|
||||
return endTrimmed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify if the end of the line needs to be trimmed. Defaults to false.
|
||||
*
|
||||
* @param endTrimmed
|
||||
*/
|
||||
public void setEndTrimmed(boolean endTrimmed) {
|
||||
this.endTrimmed = endTrimmed;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,196 +0,0 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.commons.csv.writer;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
/**
|
||||
* Tries to guess a config based on an InputStream.
|
||||
*
|
||||
* @author Martin van den Bemt
|
||||
* @version $Id: $
|
||||
*/
|
||||
public class CSVConfigGuesser {
|
||||
|
||||
/**
|
||||
* The stream to read
|
||||
*/
|
||||
private InputStream in;
|
||||
/**
|
||||
* if the file has a field header (need this info, to be able to guess better)
|
||||
* Defaults to false
|
||||
*/
|
||||
private boolean hasFieldHeader = false;
|
||||
/**
|
||||
* The found config
|
||||
*/
|
||||
protected CSVConfig config;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public CSVConfigGuesser() {
|
||||
this.config = new CSVConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param in the inputstream to guess from
|
||||
*/
|
||||
public CSVConfigGuesser(InputStream in) {
|
||||
this();
|
||||
setInputStream(in);
|
||||
}
|
||||
|
||||
public void setInputStream(InputStream in) {
|
||||
this.in = in;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow override.
|
||||
*
|
||||
* @return the inputstream that was set.
|
||||
*/
|
||||
protected InputStream getInputStream() {
|
||||
return in;
|
||||
}
|
||||
|
||||
/**
|
||||
* Guess the config based on the first 10 (or less when less available)
|
||||
* records of a CSV file.
|
||||
*
|
||||
* @return the guessed config.
|
||||
*/
|
||||
public CSVConfig guess() {
|
||||
try {
|
||||
// tralalal
|
||||
BufferedReader bIn = new BufferedReader(new InputStreamReader((getInputStream())));
|
||||
String[] lines = new String[10];
|
||||
String line = null;
|
||||
int counter = 0;
|
||||
while ((line = bIn.readLine()) != null && counter <= 10) {
|
||||
lines[counter] = line;
|
||||
counter++;
|
||||
}
|
||||
if (counter < 10) {
|
||||
// remove nulls from the array, so we can skip the null checking.
|
||||
String[] newLines = new String[counter];
|
||||
System.arraycopy(lines, 0, newLines, 0, counter);
|
||||
lines = newLines;
|
||||
}
|
||||
analyseLines(lines);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (in != null) {
|
||||
try {
|
||||
in.close();
|
||||
} catch (Exception e) {
|
||||
// ignore exception.
|
||||
}
|
||||
}
|
||||
}
|
||||
CSVConfig conf = config;
|
||||
// cleanup the config.
|
||||
config = null;
|
||||
return conf;
|
||||
}
|
||||
|
||||
protected void analyseLines(String[] lines) {
|
||||
guessFixedWidth(lines);
|
||||
guessFieldSeperator(lines);
|
||||
}
|
||||
|
||||
/**
|
||||
* Guess if this file is fixedwidth.
|
||||
* Just basing the fact on all lines being of the same length
|
||||
*
|
||||
* @param lines
|
||||
*/
|
||||
protected void guessFixedWidth(String[] lines) {
|
||||
int lastLength = 0;
|
||||
// assume fixedlength.
|
||||
config.setFixedWidth(true);
|
||||
for (int i = 0; i < lines.length; i++) {
|
||||
if (i == 0) {
|
||||
lastLength = lines[i].length();
|
||||
} else {
|
||||
if (lastLength != lines[i].length()) {
|
||||
config.setFixedWidth(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected void guessFieldSeperator(String[] lines) {
|
||||
if (config.isFixedWidth()) {
|
||||
guessFixedWidthSeperator(lines);
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < lines.length; i++) {
|
||||
}
|
||||
}
|
||||
|
||||
protected void guessFixedWidthSeperator(String[] lines) {
|
||||
// keep track of the fieldlength
|
||||
int previousMatch = -1;
|
||||
for (int i = 0; i < lines[0].length(); i++) {
|
||||
char last = ' ';
|
||||
boolean charMatches = true;
|
||||
for (int j = 0; j < lines.length; j++) {
|
||||
if (j == 0) {
|
||||
last = lines[j].charAt(i);
|
||||
}
|
||||
if (last != lines[j].charAt(i)) {
|
||||
charMatches = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (charMatches) {
|
||||
if (previousMatch == -1) {
|
||||
previousMatch = 0;
|
||||
}
|
||||
CSVField field = new CSVField();
|
||||
field.setName("field" + config.getFields().length + 1);
|
||||
field.setSize((i - previousMatch));
|
||||
config.addField(field);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return if the field uses a field header. Defaults to false.
|
||||
*/
|
||||
public boolean hasFieldHeader() {
|
||||
return hasFieldHeader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify if the CSV file has a field header
|
||||
*
|
||||
* @param hasFieldHeader true or false
|
||||
*/
|
||||
public void setHasFieldHeader(boolean hasFieldHeader) {
|
||||
this.hasFieldHeader = hasFieldHeader;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,114 +0,0 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.commons.csv.writer;
|
||||
|
||||
|
||||
/**
|
||||
* @author Martin van den Bemt
|
||||
* @version $Id: $
|
||||
*/
|
||||
public class CSVField {
|
||||
|
||||
private String name;
|
||||
private int size;
|
||||
private int fill;
|
||||
private boolean overrideFill;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public CSVField() {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name the name of the field
|
||||
*/
|
||||
public CSVField(String name) {
|
||||
setName(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name the name of the field
|
||||
* @param size the size of the field
|
||||
*/
|
||||
public CSVField(String name, int size) {
|
||||
setName(name);
|
||||
setSize(size);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the name of the field
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the name of the field
|
||||
*
|
||||
* @param name the name
|
||||
*/
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the size of the field
|
||||
*/
|
||||
public int getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the size of the field.
|
||||
* The size will be ignored when fixedwidth is set to false in the CSVConfig
|
||||
*
|
||||
* @param size the size of the field.
|
||||
*/
|
||||
public void setSize(int size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the fill pattern.
|
||||
*/
|
||||
public int getFill() {
|
||||
return fill;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets overrideFill to true.
|
||||
*
|
||||
* @param fill the file pattern
|
||||
*/
|
||||
public void setFill(int fill) {
|
||||
overrideFill = true;
|
||||
this.fill = fill;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does this field override fill ?
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean overrideFill() {
|
||||
return overrideFill;
|
||||
}
|
||||
|
||||
}
|
@ -1,144 +0,0 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.commons.csv.writer;
|
||||
|
||||
import java.io.Writer;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* CSVWriter
|
||||
*
|
||||
* @author Martin van den Bemt
|
||||
* @version $Id: $
|
||||
*/
|
||||
public class CSVWriter {
|
||||
|
||||
/**
|
||||
* The CSV config *
|
||||
*/
|
||||
private CSVConfig config;
|
||||
/**
|
||||
* The writer *
|
||||
*/
|
||||
private Writer writer;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public CSVWriter() {
|
||||
}
|
||||
|
||||
public CSVWriter(CSVConfig config) {
|
||||
setConfig(config);
|
||||
}
|
||||
|
||||
public void writeRecord(Map map) {
|
||||
CSVField[] fields = config.getFields();
|
||||
try {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i = 0; i < fields.length; i++) {
|
||||
Object o = map.get(fields[i].getName());
|
||||
if (o != null) {
|
||||
String value = o.toString();
|
||||
value = writeValue(fields[i], value);
|
||||
sb.append(value);
|
||||
}
|
||||
if (!config.isDelimiterIgnored() && fields.length != (i + 1)) {
|
||||
sb.append(config.getDelimiter());
|
||||
}
|
||||
}
|
||||
if (config.isEndTrimmed()) {
|
||||
for (int i = sb.length() - 1; i >= 0; i--) {
|
||||
System.out.println("i : " + i);
|
||||
if (Character.isWhitespace(sb.charAt(i))) {
|
||||
sb.deleteCharAt(i);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
sb.append(config.getRowDelimiter());
|
||||
String line = sb.toString();
|
||||
writer.write(line);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the CVSConfig or null if not present
|
||||
*/
|
||||
public CSVConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the CSVConfig
|
||||
*
|
||||
* @param config the CVSConfig
|
||||
*/
|
||||
public void setConfig(CSVConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the writer to write the CSV file to.
|
||||
*
|
||||
* @param writer the writer.
|
||||
*/
|
||||
public void setWriter(Writer writer) {
|
||||
this.writer = writer;
|
||||
}
|
||||
|
||||
}
|
@ -1,91 +0,0 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.commons.csv.writer;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Tests for the config guesser.
|
||||
*
|
||||
* @author Martin van den Bemt
|
||||
* @version $Id: $
|
||||
*/
|
||||
public class CSVConfigGuesserTest extends TestCase {
|
||||
|
||||
public void testSetters() throws Exception {
|
||||
CSVConfigGuesser guesser = new CSVConfigGuesser();
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]);
|
||||
guesser.setInputStream(in);
|
||||
assertEquals(in, guesser.getInputStream());
|
||||
guesser = new CSVConfigGuesser(in);
|
||||
assertEquals(in, guesser.getInputStream());
|
||||
assertEquals(false, guesser.hasFieldHeader());
|
||||
guesser.setHasFieldHeader(true);
|
||||
assertEquals(true, guesser.hasFieldHeader());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a format like
|
||||
* 1234 ; abcd ; 1234 ;
|
||||
*/
|
||||
public void testConfigGuess1() {
|
||||
CSVConfig expected = new CSVConfig();
|
||||
expected.setDelimiter(';');
|
||||
expected.setValueDelimiter(' ');
|
||||
expected.setFill(CSVConfig.FILLRIGHT);
|
||||
expected.setIgnoreValueDelimiter(false);
|
||||
expected.setFixedWidth(true);
|
||||
CSVField field = new CSVField();
|
||||
field.setSize(4);
|
||||
expected.addField(field);
|
||||
expected.addField(field);
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append("1234;abcd;1234\n");
|
||||
sb.append("abcd;1234;abcd");
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(sb.toString().getBytes());
|
||||
CSVConfigGuesser guesser = new CSVConfigGuesser(in);
|
||||
CSVConfig guessed = guesser.guess();
|
||||
assertEquals(expected.isFixedWidth(), guessed.isFixedWidth());
|
||||
assertEquals(expected.getFields().length, guessed.getFields().length);
|
||||
assertEquals(expected.getFields()[0].getSize(), guessed.getFields()[0].getSize());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a format like
|
||||
* 1234,123123,12312312,213123
|
||||
* 1,2,3,4
|
||||
*/
|
||||
public void testConfigGuess2() {
|
||||
CSVConfig expected = new CSVConfig();
|
||||
expected.setDelimiter(';');
|
||||
expected.setValueDelimiter(' ');
|
||||
expected.setFill(CSVConfig.FILLRIGHT);
|
||||
expected.setIgnoreValueDelimiter(false);
|
||||
// expected.setFixedWidth(false);
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append("1,2,3,4\n");
|
||||
sb.append("abcd,1234,abcd,1234");
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(sb.toString().getBytes());
|
||||
CSVConfigGuesser guesser = new CSVConfigGuesser(in);
|
||||
CSVConfig guessed = guesser.guess();
|
||||
assertEquals(expected.isFixedWidth(), guessed.isFixedWidth());
|
||||
}
|
||||
}
|
@ -1,103 +0,0 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.commons.csv.writer;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Testcase for the CSVConfig
|
||||
*
|
||||
* @author Martin van den Bemt
|
||||
* @version $Id: $
|
||||
*/
|
||||
public class CSVConfigTest extends TestCase {
|
||||
|
||||
|
||||
public void testFixedWith() {
|
||||
CSVConfig config = new CSVConfig();
|
||||
assertEquals(false, config.isFixedWidth());
|
||||
config.setFixedWidth(true);
|
||||
assertEquals(true, config.isFixedWidth());
|
||||
}
|
||||
|
||||
public void testFields() {
|
||||
CSVConfig config = new CSVConfig();
|
||||
assertEquals(0, config.getFields().length);
|
||||
config.setFields((CSVField[]) null);
|
||||
assertEquals(0, config.getFields().length);
|
||||
config.setFields((Collection) null);
|
||||
assertEquals(0, config.getFields().length);
|
||||
CSVField field = new CSVField();
|
||||
field.setName("field1");
|
||||
config.addField(field);
|
||||
assertEquals(field, config.getFields()[0]);
|
||||
assertEquals(null, config.getField(null));
|
||||
assertEquals(null, config.getField("field11"));
|
||||
assertEquals(field, config.getField("field1"));
|
||||
}
|
||||
|
||||
public void testFill() {
|
||||
CSVConfig config = new CSVConfig();
|
||||
assertEquals(CSVConfig.FILLNONE, config.getFill());
|
||||
config.setFill(CSVConfig.FILLLEFT);
|
||||
assertEquals(CSVConfig.FILLLEFT, config.getFill());
|
||||
config.setFill(CSVConfig.FILLRIGHT);
|
||||
assertEquals(CSVConfig.FILLRIGHT, config.getFill());
|
||||
assertEquals(' ', config.getFillChar());
|
||||
config.setFillChar('m');
|
||||
assertEquals('m', config.getFillChar());
|
||||
}
|
||||
|
||||
public void testDelimiter() {
|
||||
CSVConfig config = new CSVConfig();
|
||||
assertEquals(',', config.getDelimiter());
|
||||
config.setDelimiter(';');
|
||||
assertEquals(';', config.getDelimiter());
|
||||
assertEquals(false, config.isDelimiterIgnored());
|
||||
config.setIgnoreDelimiter(true);
|
||||
assertEquals(true, config.isDelimiterIgnored());
|
||||
}
|
||||
|
||||
public void testValueDelimiter() {
|
||||
CSVConfig config = new CSVConfig();
|
||||
assertEquals('"', config.getValueDelimiter());
|
||||
config.setValueDelimiter('m');
|
||||
assertEquals('m', config.getValueDelimiter());
|
||||
assertEquals(true, config.isValueDelimiterIgnored());
|
||||
config.setIgnoreValueDelimiter(false);
|
||||
assertEquals(false, config.isValueDelimiterIgnored());
|
||||
}
|
||||
|
||||
public void testFieldHeader() {
|
||||
CSVConfig config = new CSVConfig();
|
||||
assertEquals(false, config.isFieldHeader());
|
||||
config.setFieldHeader(true);
|
||||
assertEquals(true, config.isFieldHeader());
|
||||
}
|
||||
|
||||
public void testTrimEnd() {
|
||||
CSVConfig config = new CSVConfig();
|
||||
assertEquals(false, config.isEndTrimmed());
|
||||
config.setEndTrimmed(true);
|
||||
assertEquals(true, config.isEndTrimmed());
|
||||
}
|
||||
|
||||
}
|
@ -1,52 +0,0 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.commons.csv.writer;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @author Martin van den Bemt
|
||||
* @version $Id: $
|
||||
*/
|
||||
public class CSVFieldTest extends TestCase {
|
||||
|
||||
public void testCSVField() {
|
||||
CSVField field = new CSVField();
|
||||
assertEquals(null, field.getName());
|
||||
field.setName("id");
|
||||
assertEquals("id", field.getName());
|
||||
assertEquals(0, field.getSize());
|
||||
field.setSize(10);
|
||||
assertEquals(10, field.getSize());
|
||||
field = new CSVField("name");
|
||||
assertEquals("name", field.getName());
|
||||
field = new CSVField("name", 10);
|
||||
assertEquals("name", field.getName());
|
||||
assertEquals(10, field.getSize());
|
||||
}
|
||||
|
||||
public void testFill() {
|
||||
CSVField field = new CSVField();
|
||||
assertEquals(CSVConfig.FILLNONE, field.getFill());
|
||||
assertEquals(false, field.overrideFill());
|
||||
field.setFill(CSVConfig.FILLLEFT);
|
||||
assertEquals(true, field.overrideFill());
|
||||
assertEquals(CSVConfig.FILLLEFT, field.getFill());
|
||||
}
|
||||
}
|
@ -1,165 +0,0 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.commons.csv.writer;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* The testcase for the csv writer.
|
||||
*
|
||||
* @author Martin van den Bemt
|
||||
* @version $Id: $
|
||||
*/
|
||||
public class CSVWriterTest extends TestCase {
|
||||
|
||||
private Map map;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
map = new HashMap();
|
||||
map.put("field1", "12345");
|
||||
map.put("field2", "1234");
|
||||
}
|
||||
|
||||
public void testCSVConfig() {
|
||||
CSVWriter writer = new CSVWriter();
|
||||
assertEquals(null, writer.getConfig());
|
||||
CSVConfig config = new CSVConfig();
|
||||
writer.setConfig(config);
|
||||
assertEquals(config, writer.getConfig());
|
||||
writer = new CSVWriter(config);
|
||||
assertEquals(config, writer.getConfig());
|
||||
}
|
||||
|
||||
public void testWriterDefaults() throws Exception {
|
||||
CSVWriter writer = new CSVWriter();
|
||||
CSVConfig config = getConfig();
|
||||
writer.setConfig(config);
|
||||
StringWriter sw = new StringWriter();
|
||||
writer.setWriter(sw);
|
||||
writer.writeRecord(map);
|
||||
assertEquals("12345,1234\n", sw.toString());
|
||||
}
|
||||
|
||||
public void testWriterWithExplicitDelimiter() throws Exception {
|
||||
CSVWriter writer = new CSVWriter();
|
||||
CSVConfig config = getConfig();
|
||||
config.setDelimiter(';');
|
||||
config.setIgnoreDelimiter(false);
|
||||
writer.setConfig(config);
|
||||
StringWriter sw = new StringWriter();
|
||||
writer.setWriter(sw);
|
||||
writer.writeRecord(map);
|
||||
assertEquals("12345;1234\n", sw.toString());
|
||||
}
|
||||
|
||||
public void testWriterIgnoringDelimiter() throws Exception {
|
||||
CSVWriter writer = new CSVWriter();
|
||||
CSVConfig config = getConfig();
|
||||
config.setDelimiter(';');
|
||||
config.setIgnoreDelimiter(true);
|
||||
writer.setConfig(config);
|
||||
StringWriter sw = new StringWriter();
|
||||
writer.setWriter(sw);
|
||||
writer.writeRecord(map);
|
||||
assertEquals("123451234\n", sw.toString());
|
||||
}
|
||||
|
||||
public void testWriterWithExplicitValueDelimiter() throws Exception {
|
||||
CSVWriter writer = new CSVWriter();
|
||||
CSVConfig config = getConfig();
|
||||
config.setValueDelimiter('"');
|
||||
config.setIgnoreValueDelimiter(false);
|
||||
writer.setConfig(config);
|
||||
StringWriter sw = new StringWriter();
|
||||
writer.setWriter(sw);
|
||||
writer.writeRecord(map);
|
||||
assertEquals("\"12345\",\"1234\"\n", sw.toString());
|
||||
}
|
||||
|
||||
public void testWriterIgnoringValueDelimiter() throws Exception {
|
||||
CSVWriter writer = new CSVWriter();
|
||||
CSVConfig config = getConfig();
|
||||
config.setValueDelimiter('"');
|
||||
config.setIgnoreValueDelimiter(true);
|
||||
writer.setConfig(config);
|
||||
StringWriter sw = new StringWriter();
|
||||
writer.setWriter(sw);
|
||||
writer.writeRecord(map);
|
||||
assertEquals("12345,1234\n", sw.toString());
|
||||
}
|
||||
|
||||
public void testWriterWithoutHeader() throws Exception {
|
||||
CSVWriter writer = new CSVWriter();
|
||||
CSVConfig config = getConfig();
|
||||
config.setFieldHeader(false);
|
||||
writer.setConfig(config);
|
||||
StringWriter sw = new StringWriter();
|
||||
writer.setWriter(sw);
|
||||
writer.writeRecord(map);
|
||||
assertEquals("12345,1234\n", sw.toString());
|
||||
}
|
||||
|
||||
// TODO: SANDBOX-324
|
||||
public void todoTestWriterWithHeader() throws Exception {
|
||||
CSVWriter writer = new CSVWriter();
|
||||
CSVConfig config = getConfig();
|
||||
config.setFieldHeader(true);
|
||||
writer.setConfig(config);
|
||||
StringWriter sw = new StringWriter();
|
||||
writer.setWriter(sw);
|
||||
writer.writeRecord(map);
|
||||
assertEquals("field1,field2\n12345,1234\n", sw.toString());
|
||||
}
|
||||
|
||||
public void testWriterWithExplicitRowDelimiterLF() throws Exception {
|
||||
CSVWriter writer = new CSVWriter();
|
||||
CSVConfig config = getConfig();
|
||||
config.setRowDelimiter("\n");
|
||||
writer.setConfig(config);
|
||||
StringWriter sw = new StringWriter();
|
||||
writer.setWriter(sw);
|
||||
writer.writeRecord(map);
|
||||
assertEquals("12345,1234\n", sw.toString());
|
||||
}
|
||||
|
||||
public void testWriterWithExplicitRowDelimiterCRLF() throws Exception {
|
||||
CSVWriter writer = new CSVWriter();
|
||||
CSVConfig config = getConfig();
|
||||
config.setRowDelimiter("\r\n");
|
||||
writer.setConfig(config);
|
||||
StringWriter sw = new StringWriter();
|
||||
writer.setWriter(sw);
|
||||
writer.writeRecord(map);
|
||||
assertEquals("12345,1234\r\n", sw.toString());
|
||||
}
|
||||
|
||||
private CSVConfig getConfig() {
|
||||
CSVConfig config = new CSVConfig();
|
||||
config.addField(new CSVField("field1", 5));
|
||||
config.addField(new CSVField("field2", 4));
|
||||
|
||||
return config;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user