net.sf.andromedaioc.model.builder.processor.ValidationContextModelProcessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of andromeda-ioc Show documentation
Show all versions of andromeda-ioc Show documentation
Inversion of Control Framework for Android
The newest version!
package net.sf.andromedaioc.model.builder.processor;
import net.sf.andromedaioc.exception.XmlParseException;
import net.sf.andromedaioc.model.beans.*;
import java.util.Map;
/**
* This processor will catch all model errors that is not covered by xsd validation
*
* @author Alexey Mitrov
*/
public class ValidationContextModelProcessor implements ContextModelProcessor {
public ContextModel process(ContextModel input) {
for(BeanModel beanModel : input.getBeans().values()) {
validateBeanModel(beanModel);
}
return input;
}
private void validateBeanModel(BeanModel beanModel) {
if(!beanModel.getKey().isInner()) {
if(beanModel.getKey().getId() == null) {
throw new XmlParseException(String.format("Missing id for %s", printBeanIdAndType(beanModel, false, true)));
}
}
if(beanModel.getFactoryBean() != null) {
if(beanModel.getFactoryMethod() == null) {
throw new XmlParseException(String.format("If factory bean is specified then you need provide factory method for %s", printBeanIdAndType(beanModel, true, true)));
}
} else {
if(beanModel.getBeanClass() == null) {
throw new XmlParseException(String.format("Missing class or factory bean for %s", printBeanIdAndType(beanModel, true, false)));
}
}
validateConstructorArgs(beanModel);
validateProperties(beanModel);
validateItems(beanModel);
validateEntries(beanModel);
}
private void validateConstructorArgs(BeanModel beanModel) {
for(ConstructorArgumentModel constructorArgumentModel : beanModel.getConstructorArguments()) {
if(!validateValueModel(constructorArgumentModel.getValue())) {
throw new XmlParseException(String.format("Please provide one of following for constructor argument: ref, value or inner bean for %s", printBeanIdAndType(beanModel, true, true)));
}
}
}
private void validateProperties(BeanModel beanModel) {
for(Map.Entry propertyEntry : beanModel.getProperties().entrySet()) {
if(!validateValueModel(propertyEntry.getValue().getValue())) {
throw new XmlParseException(String.format("Please provide one of following for property with name %s: ref, value or inner bean for %s", propertyEntry.getKey(), printBeanIdAndType(beanModel, true, true)));
}
}
}
private void validateItems(BeanModel beanModel) {
if(!(beanModel instanceof CollectionModel)) {
return;
}
CollectionModel collectionModel = (CollectionModel) beanModel;
for(ValueModel item : collectionModel.getItems()) {
if(!validateValueModel(item)) {
throw new XmlParseException(String.format("Please provide one of following for item: ref, value or inner bean for %s", printBeanIdAndType(beanModel, true, true)));
}
}
}
private void validateEntries(BeanModel beanModel) {
if(!(beanModel instanceof MapModel)) {
return;
}
MapModel mapModel = (MapModel) beanModel;
for(EntryModel entryModel : mapModel.getEntries()) {
if(!validateValueModel(entryModel.getKey())) {
throw new XmlParseException(String.format("Please provide one of following for key of entry: ref, value or inner bean for %s", printBeanIdAndType(beanModel, true, true)));
}
if(!validateValueModel(entryModel.getValue())) {
throw new XmlParseException(String.format("Please provide one of following for value of entry: ref, value or inner bean for %s", printBeanIdAndType(beanModel, true, true)));
}
}
}
private boolean validateValueModel(ValueModel valueModel) {
return !(valueModel.getReference() == null && valueModel.getValue() == null);
}
private String printBeanIdAndType(BeanModel beanModel, boolean printId, boolean printType) {
StringBuilder builder = new StringBuilder("bean");
if(printId) {
builder.append(" with id=").append(beanModel.getKey().getId());
}
if(printType && (beanModel.getBeanClass() != null)) {
builder.append(" of type=").append(beanModel.getBeanClass().getName());
}
return builder.toString();
}
}