All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jvnet.annox.parser.XAnnotationFieldParser Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package org.jvnet.annox.parser;

import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Method;

import org.apache.commons.lang.Validate;
import org.jvnet.annox.model.XAnnotation;
import org.jvnet.annox.model.XAnnotationField;
import org.jvnet.annox.model.XAnnotationField.XBoolean;
import org.jvnet.annox.model.XAnnotationField.XBooleanArray;
import org.jvnet.annox.model.XAnnotationField.XByte;
import org.jvnet.annox.model.XAnnotationField.XByteArray;
import org.jvnet.annox.model.XAnnotationField.XChar;
import org.jvnet.annox.model.XAnnotationField.XCharArray;
import org.jvnet.annox.model.XAnnotationField.XClass;
import org.jvnet.annox.model.XAnnotationField.XClassArray;
import org.jvnet.annox.model.XAnnotationField.XDouble;
import org.jvnet.annox.model.XAnnotationField.XDoubleArray;
import org.jvnet.annox.model.XAnnotationField.XEnum;
import org.jvnet.annox.model.XAnnotationField.XEnumArray;
import org.jvnet.annox.model.XAnnotationField.XFloat;
import org.jvnet.annox.model.XAnnotationField.XFloatArray;
import org.jvnet.annox.model.XAnnotationField.XInt;
import org.jvnet.annox.model.XAnnotationField.XIntArray;
import org.jvnet.annox.model.XAnnotationField.XLong;
import org.jvnet.annox.model.XAnnotationField.XLongArray;
import org.jvnet.annox.model.XAnnotationField.XShort;
import org.jvnet.annox.model.XAnnotationField.XShortArray;
import org.jvnet.annox.model.XAnnotationField.XString;
import org.jvnet.annox.model.XAnnotationField.XStringArray;
import org.jvnet.annox.util.ClassUtils;
import org.jvnet.annox.util.ElementUtils;
import org.jvnet.annox.util.ObjectUtils;
import org.w3c.dom.Element;

public abstract class XAnnotationFieldParser {

	public abstract XAnnotationField parse(Element element, String name,
			Class type) throws Exception;

	public abstract XAnnotationField parse(Annotation annotation,
			String name, Class type) throws Exception;

	public abstract static class XFieldParser extends
			XAnnotationFieldParser {

		@Override
		public XAnnotationField parse(Element element, String name,
				Class type) throws Exception {
			Validate.notNull(element, "Element must not be null.");
			Validate.notNull(name, "Field name must not be null.");
			Validate.notNull(type, "Type must not be null.");
			final String draft = ElementUtils.getValue(element, name);

			if (draft == null) {
				return null;
			} else {
				final T value = parse(draft, type);
				return construct(name, value, type);
			}
		}

		public XAnnotationField parse(Annotation annotation, String name,
				Class type) throws Exception {

			final Class annotationClass = annotation
					.annotationType();
			final Method method = annotationClass.getMethod(name, null);

			final T value = (T) method.invoke(annotation, null);

			return construct(name, value, type);
		}

		public final T parse(String draft, Class type) throws Exception
		{
			return (T) ObjectUtils.valueOf(type, draft);
		}

		public abstract XAnnotationField construct(String name, T value,
				Class type);
	}

	public abstract static class XArrayFieldParser extends
			XAnnotationFieldParser {

		@Override
		public XAnnotationField parse(Element element, String name,
				Class type) throws Exception {
			final String[] draft = ElementUtils.getValues(element, name);
			final T[] value = parse(draft, type.getComponentType());
			return construct(name, value, type.getComponentType());
		}

		public XAnnotationField parse(Annotation annotation, String name,
				Class type) throws Exception {

			final Class annotationClass = annotation
					.annotationType();
			final Method method = annotationClass.getMethod(name, null);

			final Object values = method.invoke(annotation, null);
			final int length = Array.getLength(values);

			final T[] value = (T[]) Array.newInstance(ClassUtils.primitiveToWrapper(type.getComponentType()), length);
			for (int index = 0; index < length; index++) {
				Object object = Array.get(values, index);
				value[index] = (T) object;
			}
			return construct(name, value, type.getComponentType());
		}

		public abstract XAnnotationField construct(String name, T[] value,
				Class type);

		@SuppressWarnings("unchecked")
		public final T[] parse(String[] draft, Class type) throws Exception {
			
			return (T[]) ObjectUtils.valueOf(type, draft);
		}
	}

	public static class XBooleanFieldParser extends XFieldParser {

		@Override
		public XAnnotationField construct(String name, Boolean value,
				Class type) {
			return new XBoolean(name, value);
		}
	}

	public static class XByteFieldParser extends XFieldParser {

		@Override
		public XAnnotationField construct(String name, Byte value,
				Class type) {
			return new XByte(name, value);
		}
	}

	public static class XIntFieldParser extends XFieldParser {

		@Override
		public XAnnotationField construct(String name, Integer value,
				Class type) {
			return new XInt(name, value);
		}
	}

	public static class XLongFieldParser extends XFieldParser {

		@Override
		public XAnnotationField construct(String name, Long value,
				Class type) {
			return new XLong(name, value);
		}
	}

	public static class XShortFieldParser extends XFieldParser {

		@Override
		public XAnnotationField construct(String name, Short value,
				Class type) {
			return new XShort(name, value);
		}
	}

	public static class XCharFieldParser extends XFieldParser {


		@Override
		public XAnnotationField construct(String name,
				Character value, Class type) {
			return new XChar(name, value);
		}
	}

	public static class XClassFieldParser extends XFieldParser {


		@Override
		public XAnnotationField construct(String name, Class value,
				Class type) {
			return new XClass(name, value);
		}
	}

	public static class XDoubleFieldParser extends XFieldParser {

		@Override
		public XAnnotationField construct(String name, Double value,
				Class type) {
			return new XDouble(name, value);
		}
	}

	public static class XFloatFieldParser extends XFieldParser {

		@Override
		public XAnnotationField construct(String name, Float value,
				Class type) {
			return new XFloat(name, value);
		}
	}

	public static class XStringFieldParser extends XFieldParser {

		@Override
		public XAnnotationField construct(String name, String value,
				Class type) {
			return new XString(name, value);
		}
	}

	public static class XEnumFieldParser extends XFieldParser {

		@Override
		@SuppressWarnings("unchecked")
		public XAnnotationField construct(String name, Enum value,
				Class type) {
			return new XEnum(name, value, (Class) type);
		}
	}

	public static class XAnnotationItemFieldParser extends
			XAnnotationFieldParser {

		@Override
		@SuppressWarnings("unchecked")
		public XAnnotationField parse(Element annotationElement,
				String name, Class type) throws Exception {

			final Element element = ElementUtils.getElement(annotationElement,
					name);

			if (element == null) {
				return null;
			} else {

				final XAnnotation annotation = XAnnotationParser.GENERIC
						.parse(element);
				return new XAnnotationField.XAnnotation(name, annotation,
						(Class) type);
			}
		}

		@Override
		public XAnnotationField parse(Annotation annotation,
				String name, Class type) throws Exception {

			final Class annotationClass = annotation
					.annotationType();

			final Method method = annotationClass.getMethod(name,
					(Class[]) null);

			final Annotation value = (Annotation) method.invoke(annotation,
					(Object[]) null);

			final XAnnotation xannotation = XAnnotationParser.GENERIC
					.parse(value);
			return new XAnnotationField.XAnnotation(name, xannotation,
					(Class) type);
		}

	}

	public static final XBooleanFieldParser BOOLEAN = new XBooleanFieldParser();

	public static final XByteFieldParser BYTE = new XByteFieldParser();

	public static final XIntFieldParser INT = new XIntFieldParser();

	public static final XLongFieldParser LONG = new XLongFieldParser();

	public static final XShortFieldParser SHORT = new XShortFieldParser();

	public static final XCharFieldParser CHAR = new XCharFieldParser();

	public static final XClassFieldParser CLASS = new XClassFieldParser();

	public static final XDoubleFieldParser DOUBLE = new XDoubleFieldParser();

	public static final XFloatFieldParser FLOAT = new XFloatFieldParser();

	public static final XEnumFieldParser ENUM = new XEnumFieldParser();

	public static final XStringFieldParser STRING = new XStringFieldParser();

	public static final XAnnotationFieldParser ANNOTATION = new XAnnotationItemFieldParser();

	public static class XBooleanArrayFieldParser extends
			XArrayFieldParser {

		@Override
		public XAnnotationField construct(String name,
				Boolean[] value, Class type) {
			return new XBooleanArray(name, value);
		}
	}

	public static class XByteArrayFieldParser extends XArrayFieldParser {

		@Override
		public XAnnotationField construct(String name, Byte[] value,
				Class type) {
			return new XByteArray(name, value);
		}
	}

	public static class XIntArrayFieldParser extends XArrayFieldParser {

		@Override
		public XAnnotationField construct(String name,
				Integer[] value, Class type) {
			return new XIntArray(name, value);
		}
	}

	public static class XLongArrayFieldParser extends XArrayFieldParser {

		@Override
		public XAnnotationField construct(String name, Long[] value,
				Class type) {
			return new XLongArray(name, value);
		}
	}

	public static class XShortArrayFieldParser extends XArrayFieldParser {

		@Override
		public XAnnotationField construct(String name, Short[] value,
				Class type) {
			return new XShortArray(name, value);
		}
	}

	public static class XCharArrayFieldParser extends
			XArrayFieldParser {

		@Override
		public XAnnotationField construct(String name,
				Character[] value, Class type) {
			return new XCharArray(name, value);
		}
	}

	public static class XClassArrayFieldParser extends XArrayFieldParser {

		@Override
		public XAnnotationField construct(String name, Class[] value,
				Class type) {
			return new XClassArray(name, value);
		}
	}

	public static class XDoubleArrayFieldParser extends
			XArrayFieldParser {

		@Override
		public XAnnotationField construct(String name,
				Double[] value, Class type) {
			return new XDoubleArray(name, value);
		}
	}

	public static class XFloatArrayFieldParser extends XArrayFieldParser {

		@Override
		public XAnnotationField construct(String name, Float[] value,
				Class type) {
			return new XFloatArray(name, value);
		}
	}

	public static class XStringArrayFieldParser extends
			XArrayFieldParser {


		@Override
		public XAnnotationField construct(String name,
				String[] value, Class type) {
			return new XStringArray(name, value);
		}
	}

	public static class XEnumArrayFieldParser extends XArrayFieldParser {


		@Override
		public XAnnotationField construct(String name, Enum[] value,
				Class type) {
			return new XEnumArray(name, value, (Class) type);
		}

	}

	public static class XAnnotationItemArrayFieldParser extends
			XAnnotationFieldParser {

		@Override
		public XAnnotationField parse(Element annotationElement,
				String name, Class type) throws Exception {

			final Element[] elements = ElementUtils.getElements(
					annotationElement, name);

			final XAnnotation[] annotations = new XAnnotation[elements.length];

			for (int index = 0; index < elements.length; index++) {
				annotations[index] = XAnnotationParser.GENERIC
						.parse(elements[index]);
			}
			return new XAnnotationField.XAnnotationArray(name, annotations,
					(Class) type.getComponentType());
		}

		public XAnnotationField parse(Annotation annotation,
				String name, Class type) throws Exception {

			final Class annotationClass = annotation
					.annotationType();

			final Method method = annotationClass.getMethod(name,
					(Class[]) null);

			final Annotation[] value = (Annotation[]) method.invoke(annotation,
					(Object[]) null);

			final XAnnotation[] annotations = new XAnnotation[value.length];

			for (int index = 0; index < value.length; index++) {
				annotations[index] = XAnnotationParser.GENERIC
						.parse(value[index]);
			}
			return new XAnnotationField.XAnnotationArray(name, annotations,
					(Class) type.getComponentType());
		}
	}

	public static final XBooleanArrayFieldParser BOOLEAN_ARRAY = new XBooleanArrayFieldParser();

	public static final XByteArrayFieldParser BYTE_ARRAY = new XByteArrayFieldParser();

	public static final XIntArrayFieldParser INT_ARRAY = new XIntArrayFieldParser();

	public static final XLongArrayFieldParser LONG_ARRAY = new XLongArrayFieldParser();

	public static final XShortArrayFieldParser SHORT_ARRAY = new XShortArrayFieldParser();

	public static final XCharArrayFieldParser CHAR_ARRAY = new XCharArrayFieldParser();

	public static final XClassArrayFieldParser CLASS_ARRAY = new XClassArrayFieldParser();

	public static final XDoubleArrayFieldParser DOUBLE_ARRAY = new XDoubleArrayFieldParser();

	public static final XFloatArrayFieldParser FLOAT_ARRAY = new XFloatArrayFieldParser();

	public static final XEnumArrayFieldParser ENUM_ARRAY = new XEnumArrayFieldParser();

	public static final XStringArrayFieldParser STRING_ARRAY = new XStringArrayFieldParser();

	public static final XAnnotationItemArrayFieldParser ANNOTATION_ARRAY = new XAnnotationItemArrayFieldParser();

	public static XAnnotationFieldParser detectType(Class theClass) {
		if (theClass == null)
			throw new IllegalArgumentException("Class must not be null.");

		if (theClass.isArray()) {
			final XAnnotationFieldParser componentType = detectType(theClass
					.getComponentType());
			if (componentType == BOOLEAN)
				return BOOLEAN_ARRAY;
			else if (componentType == BYTE)
				return BYTE_ARRAY;
			else if (componentType == INT)
				return INT_ARRAY;
			else if (componentType == LONG)
				return LONG_ARRAY;
			else if (componentType == SHORT)
				return SHORT_ARRAY;
			else if (componentType == CHAR)
				return CHAR_ARRAY;
			else if (componentType == CLASS)
				return CLASS_ARRAY;
			else if (componentType == DOUBLE)
				return DOUBLE_ARRAY;
			else if (componentType == FLOAT)
				return FLOAT_ARRAY;
			else if (componentType == ENUM)
				return ENUM_ARRAY;
			else if (componentType == STRING)
				return STRING_ARRAY;
			else if (componentType == ANNOTATION)
				return ANNOTATION_ARRAY;
			else
				throw new IllegalArgumentException(
						"Unknown annotation field type.");

		} else {
			if (Boolean.TYPE.equals(theClass)) {
				return BOOLEAN;
			} else if (Byte.TYPE.equals(theClass)) {
				return BYTE;
			} else if (Integer.TYPE.equals(theClass)) {
				return INT;
			} else if (Long.TYPE.equals(theClass)) {
				return LONG;
			} else if (Short.TYPE.equals(theClass)) {
				return SHORT;
			} else if (Character.TYPE.equals(theClass)) {
				return CHAR;
			} else if (Double.TYPE.equals(theClass)) {
				return DOUBLE;
			} else if (Float.TYPE.equals(theClass)) {
				return FLOAT;
			} else if (Class.class.equals(theClass)) {
				return CLASS;
			} else if (String.class.equals(theClass)) {
				return STRING;
			} else if (Enum.class.isAssignableFrom(theClass)) {
				return ENUM;
			} else if (Annotation.class.isAssignableFrom(theClass)) {
				return ANNOTATION;
			} else {
				throw new IllegalArgumentException(
						"Unknown annotation field type [" + theClass.getName()
								+ "].");
			}
		}
	}

	public static class XGenericFieldParser extends
			XAnnotationFieldParser {

		@Override
		public XAnnotationField parse(Element element, String name,
				Class type) throws Exception {

			final XAnnotationFieldParser parser = detectType(type);
			return parser.parse(element, name, type);
		}

		@Override
		public XAnnotationField parse(Annotation annotation, String name,
				Class type) throws Exception {
			final XAnnotationFieldParser parser = detectType(type);
			return parser.parse(annotation, name, type);
		}

	}

	public static final XGenericFieldParser GENERIC = new XGenericFieldParser();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy