
net.sf.jsefa.flr.FlrIOFactory 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.flr;
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.flr.annotation.FlrEntryPointFactory;
import net.sf.jsefa.flr.annotation.FlrTypeMappingFactory;
import net.sf.jsefa.flr.config.FlrConfiguration;
import net.sf.jsefa.flr.config.FlrInitialConfigurationParameters;
/**
* Factory for creating {@link FlrSerializer}s and {@link FlrDeserializer}s.
*
* This is the abstract base class for concrete factories. Each subclass must provide a static method
* create(FlrConfiguration config)
as well as implement the abstract methods.
*
* This class provides a static factory method {@link #createFactory(FlrConfiguration)} to create an instance of a
* concrete FlrIOFactory
.
*
* 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 FlrIOFactory implements IOFactory {
/**
* Creates a new FlrIOFactory
for FlrSerializer
s and
* FlrDeserializer
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(FlrConfiguration, 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 FlrIOFactory
factory
* @throws IOFactoryException
*/
public static FlrIOFactory createFactory(FlrConfiguration config) {
Class factoryClass = InitialConfiguration.get(
FlrInitialConfigurationParameters.IO_FACTORY_CLASS, FlrIOFactoryImpl.class);
Method createMethod = ReflectionUtil.getMethod(factoryClass, "createFactory", FlrConfiguration.class);
if (createMethod == null) {
throw new IOFactoryException("Failed to create a FlrIOFactory. 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 FlrIOFactory", e);
}
}
/**
* Creates a new FlrIOFactory
for FlrSerializer
s and
* FlrDeserializer
s which can handle objects of the given object types.
*
*
* It creates a new {@link FlrConfiguration} 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 FlrIOFactory
factory
* @throws IOFactoryException
*/
public static FlrIOFactory createFactory(Class>... objectTypes) {
return createFactory(new FlrConfiguration(), objectTypes);
}
/**
* Creates a new FlrIOFactory
for FlrSerializer
s and
* FlrDeserializer
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 FlrIOFactory
factory
* @throws IOFactoryException
*/
public static FlrIOFactory createFactory(FlrConfiguration config, Class>... objectTypes) {
FlrConfiguration newConfig = config.createCopy();
try {
FlrTypeMappingFactory typeMappingFactory = new FlrTypeMappingFactory(newConfig
.getTypeMappingRegistry(), newConfig.getSimpleTypeConverterProvider(), newConfig
.getValidatorProvider(), newConfig.getObjectAccessorProvider(), newConfig
.getDefaultPadCharacter());
newConfig.getEntryPoints().addAll(
FlrEntryPointFactory.createEntryPoints(typeMappingFactory, objectTypes));
return createFactory(newConfig);
} catch (IOFactoryException e) {
throw e;
} catch (Exception e) {
throw new IOFactoryException("Failed to create an FlrIOFactory", e);
}
}
/**
* {@inheritDoc}
*/
public abstract FlrSerializer createSerializer();
/**
* {@inheritDoc}
*/
public abstract FlrDeserializer createDeserializer();
}