
net.sf.jsefa.csv.CsvIOFactory Maven / Gradle / Ivy
Show all versions of jsefa-android Show documentation
/*
* Copyright 2007 the original author or authors.
*
* Licensed 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 net.sf.jsefa.csv;
import java.lang.reflect.Method;
import net.sf.jsefa.IOFactory;
import net.sf.jsefa.IOFactoryException;
import net.sf.jsefa.common.config.InitialConfiguration;
import net.sf.jsefa.common.util.ReflectionUtil;
import net.sf.jsefa.csv.annotation.CsvEntryPointFactory;
import net.sf.jsefa.csv.annotation.CsvTypeMappingFactory;
import net.sf.jsefa.csv.config.CsvConfiguration;
import net.sf.jsefa.csv.config.CsvInitialConfigurationParameters;
/**
* Factory for creating {@link CsvSerializer}s and {@link CsvDeserializer}s.
*
* This is the abstract base class for concrete factories. Each subclass must provide a static method
* create(CsvConfiguration config)
as well as implement the abstract methods.
*
* This class provides a static factory method {@link #createFactory(CsvConfiguration)} to create an instance of a
* concrete CsvIOFactory
.
*
* This class also provides static facade methods hiding the details of creating entry points based on annotated
* object types.
*
* @author Norman Lahme-Huetig
*/
public abstract class CsvIOFactory implements IOFactory {
/**
* Creates a new CsvIOFactory
for CsvSerializer
s and
* CsvDeserializer
s using the given configuration.
*
* Note that the configuration should provide a non empty collection of entry points.
* You can use the methods {@link #createFactory(Class...)} or
* {@link #createFactory(CsvConfiguration, Class...)} if you want to get the entry points automatically created
* from annotated classes.
*
* @param config the configuration object. It will be copied so that the given one can be modified or reused.
* @return an CsvIOFactory
factory
* @throws IOFactoryException
*/
public static CsvIOFactory createFactory(CsvConfiguration config) {
Class factoryClass = InitialConfiguration.get(
CsvInitialConfigurationParameters.IO_FACTORY_CLASS, CsvIOFactoryImpl.class);
Method createMethod = ReflectionUtil.getMethod(factoryClass, "createFactory", CsvConfiguration.class);
if (createMethod == null) {
throw new IOFactoryException("Failed to create a CsvIOFactory. The factory " + factoryClass
+ " does not contain the required static createFactory method.");
}
try {
return ReflectionUtil.callMethod(null, createMethod, config);
} catch (Exception e) {
throw new IOFactoryException("Failed to create a CsvIOFactory", e);
}
}
/**
* Creates a new CsvIOFactory
for CsvSerializer
s and
* CsvDeserializer
s which can handle objects of the given object types.
*
*
* It creates a new {@link CsvConfiguration} with entry points generated from the annotations found in the
* given object types.
*
* @param objectTypes object types for which entry points should be created from annotations
* @return a CsvIOFactory
factory
* @throws IOFactoryException
*/
public static CsvIOFactory createFactory(Class>... objectTypes) {
return createFactory(new CsvConfiguration(), objectTypes);
}
/**
* Creates a new CsvIOFactory
for CsvSerializer
s and
* CsvDeserializer
s which can handle objects of the given object types as well as those object
* types for which entry points are defined in the config
.
*
* @param config the configuration object. It will be copied so that the given one can be modified or reused.
* @param objectTypes object types for which entry points should be created from annotations
* @return a a CsvIOFactory
factory
* @throws IOFactoryException
*/
public static CsvIOFactory createFactory(CsvConfiguration config, Class>... objectTypes) {
CsvConfiguration newConfig = config.createCopy();
try {
CsvTypeMappingFactory typeMappingFactory = new CsvTypeMappingFactory(newConfig
.getTypeMappingRegistry(), newConfig.getSimpleTypeConverterProvider(), newConfig
.getValidatorProvider(), newConfig.getObjectAccessorProvider(), newConfig
.getDefaultQuoteMode(), newConfig.getDefaultNoValueString());
newConfig.getEntryPoints().addAll(
CsvEntryPointFactory.createEntryPoints(typeMappingFactory, objectTypes));
return createFactory(newConfig);
} catch (IOFactoryException e) {
throw e;
} catch (Exception e) {
throw new IOFactoryException("Failed to create an CsvIOFactory", e);
}
}
/**
* {@inheritDoc}
*/
public abstract CsvSerializer createSerializer();
/**
* {@inheritDoc}
*/
public abstract CsvDeserializer createDeserializer();
}