All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.etlunit.io.file.DataFileManagerImpl Maven / Gradle / Ivy

There is a newer version: 1.6.9
Show newest version
package org.etlunit.io.file;

import org.etlunit.DiffManager;
import org.etlunit.parser.ETLTestOperation;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class DataFileManagerImpl implements DataFileManager
{
	private String defaultColumnDelimiter = DEFAULT_COLUMN_DELIMITER;
	private String defaultRowDelimiter = DEFAULT_ROW_DELIMITER;
	private String defaultNullToken = DEFAULT_NULL_TOKEN;
	private DataFileSchema.format_type defaultFormatType = DEFAULT_FORMAT_TYPE;

	@Override
	public DataFileSchema.format_type getDefaultFormatType()
	{
		return defaultFormatType;
	}

	@Override
	public void setDefaultFormatType(DataFileSchema.format_type defaultFormatType)
	{
		this.defaultFormatType = defaultFormatType;
	}

	@Override
	public String getDefaultColumnDelimiter()
	{
		return defaultColumnDelimiter;
	}

	@Override
	public void setDefaultColumnDelimiter(String defaultColumnDelimiter)
	{
		this.defaultColumnDelimiter = defaultColumnDelimiter;
	}

	@Override
	public String getDefaultRowDelimiter()
	{
		return defaultRowDelimiter;
	}

	@Override
	public void setDefaultRowDelimiter(String defaultRowDelimiter)
	{
		this.defaultRowDelimiter = defaultRowDelimiter;
	}

	@Override
	public String getDefaultNullToken()
	{
		return defaultNullToken;
	}

	@Override
	public DataFileSchema loadDataFileSchemaFromResource(String resourcePath)
	{
		return loadDataFileSchemaFromResource(resourcePath, this);
	}

	@Override
	public DataFileSchema loadDataFileSchemaFromResource(String resourcePath, Object classLoaderContext)
	{
		return FlatFileSchema.loadFromResource(resourcePath, classLoaderContext.getClass().getClassLoader(), this);
	}

	@Override
	public DataFileSchema createDataFileSchema(String id)
	{
		return defaultFormatType == DataFileSchema.format_type.delimited ?
				new FlatFileSchema(id, defaultFormatType, defaultRowDelimiter, defaultColumnDelimiter, defaultNullToken, this)
				:
						new FlatFileSchema(id, defaultFormatType, defaultRowDelimiter, null, defaultNullToken, this)
				;
	}

	@Override
	public void setDefaultNullToken(String defaultNullToken)
	{
		this.defaultNullToken = defaultNullToken;
	}

	@Override
	public DataFileSchema loadDataFileSchema(File schema)
	{
		return FlatFileSchema.loadFromFile(schema, schema.getName(), this);
	}

	@Override
	public DataFile loadDataFile(File file, DataFileSchema schema)
	{
		return new FlatFile(schema, file);
	}

	@Override
	public List diff(DataFile lfile, DataFile rfile) throws IOException
	{
		return diff(lfile, rfile, null);
	}

	@Override
	public List diff(DataFile lfile, DataFile rfile, List columns) throws IOException
	{
		FlatFileDiff fdiff = new FlatFileDiff(lfile);

		return fdiff.diffFile(rfile, columns);
	}

	@Override
	public void report(DiffManager dManager, ETLTestOperation operation, String failureId, List diffs)
	{
		FlatFileDiff.report(dManager, operation, failureId, diffs);
	}

	@Override
	public DataFormatValidator resolveValidatorById(String id)
	{
		// first try the SupportedColumnTypes interface,
		// then try the classpath
		if (SupportedColumnTypes.TYPES_MAP.containsKey(id))
		{
			return SupportedColumnTypes.TYPES_MAP.get(id);
		}

		return FlatFileSchema.resolveValidatorForType(id);
	}

	@Override
	public void copyDataFile(DataFile source, DataFile dest, Map defaultValues) throws IOException
	{
		DataFile.FileData read = source.getFileData();

		try
		{
			Iterator it = read.iterator();

			DataFileWriter writer = dest.getWriter();

			try
			{
				while (it.hasNext())
				{
					writer.addRow((Map) it.next().getData());
				}
			}
			finally
			{
				writer.close();
			}
		}
		finally
		{
			read.dispose();
		}
	}

	@Override
	public void copyDataFile(DataFile source, DataFile dest) throws IOException
	{
		copyDataFile(source, dest, Collections.EMPTY_MAP);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy