söndag 14 september 2014

Low Cost Jaxb Validator

       

package nap.util.jaxb;

import java.lang.reflect.Field;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import org.apache.log4j.Logger;


/**
 * minimal consideration of JAXB annotation
 */
public class JaxbValidator
{

    private static final Logger      LOG       = Logger.getLogger(JaxbValidator.class);
    private static StringBuilder     errors    = new StringBuilder();
    private static String            message;

    /**
     * Enforce 'required' attibute.
     *
     * Requires either no security manager is used or the default security
     * manager is employed.
     * 
     * @throws SchemaValidationException
     * @see {@link Field#setAccessible(boolean)}.
     */
    public static  void validateRequired(T target) throws SchemaValidationException
    {
        try
        {
            Class targetClass = target.getClass();
            Field[] fields = targetClass.getDeclaredFields();

            for (Field field : fields)
            {
                XmlElement annotation = field.getAnnotation(XmlElement.class);
                if (annotation != null && annotation.required())
                {
                    try
                    {
                        field.setAccessible(true);
                        if (field.get(target) == null)
                        {
                            if (errors.length() != 0)
                            {
                                errors.append(" ");
                            }
                            message = String.format("%s: required field '%s' is null.", targetClass.getCanonicalName(), field.getName());
                            LOG.warn(message);
                            errors.append(message);
                        }
                    }
                    catch (IllegalArgumentException e)
                    {
                        LOG.warn(field.getName(), e);
                    }
                    catch (IllegalAccessException e)
                    {
                        LOG.warn(field.getName(), e);
                    }
                }
                if (field.getType().getCanonicalName().equals(List.class.getCanonicalName()))
                {
                    field.setAccessible(true);
                    List list = (List) field.get(target);
                    for (Object obj : list)
                    {
                        validateRequired(obj);
                    }

                }
            }
            if (errors.length() != 0)
            {
                throw new SchemaValidationException(message);
            }
        }
        catch (IllegalArgumentException e)
        {
         throw new SchemaValidationException(message);
        }
        catch (IllegalAccessException iae)
        {
         throw new SchemaValidationException(message);
        }

    }

    static class SchemaValidationException extends Exception
    {
        private static final long    serialVersionUID    = 1787869622725094010L;

        SchemaValidationException(String msg)
        {
            super(msg);
        }
    }

}

       
 

Inga kommentarer:

Skicka en kommentar