/*
* Copyright 2002-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 org.internna.iwebmvc.core.i18n;
import java.lang.annotation.Annotation;
import org.internna.iwebmvc.core.visitor.AbstractCloneVisitor;
import org.springframework.validation.FieldError;
/**
* A recursive Translator implementation based on a
* {@link org.internna.iwebmvc.core.visitor.AnnotationBasedCloneVisitor}.
*
* @author Jose Noheda
* @since 1.0
*/
public abstract class AbstractTranslator extends AbstractCloneVisitor implements Translator {
/**
* Works with {@link Translate}.
*
* @return Translate.class
*/
public Class<? extends Annotation> baseAnnotation() {
return Translate.class;
}
/**
* Translate any object using a
* {@link org.internna.iwebmvc.core.visitor.CloneVisitor}.
* @param object any object
* @return a copy of the object translated
*/
@SuppressWarnings("unchecked")
public <T> T translate(final T object) {
return (T) visit(object);
}
@Override
@SuppressWarnings("unchecked")
public <T> T visit(Object element) {
try {
return super.<T>visit(element);
} catch (IllegalArgumentException ia) {
if (FieldError.class.isAssignableFrom(element.getClass())) {
FieldError e = (FieldError) element;
FieldError t = new FieldError(e.getObjectName(), e.getField(), e.getRejectedValue(), false, e.getCodes(), null, this.visit(e.getCode()));
return (T) t;
} else {
throw ia;
}
}
}
}