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

org.ow2.opensuit.xmlmap.BindWithAnnotations Maven / Gradle / Ivy

The newest version!
/**
 * Open SUIT - Simple User Interface Toolkit
 * 
 * Copyright (c) 2009 France Telecom, http://www.francetelecom.com/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.ow2.opensuit.xmlmap;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Vector;

import org.ow2.opensuit.xmlmap.annotations.XmlAncestor;
import org.ow2.opensuit.xmlmap.annotations.XmlAttribute;
import org.ow2.opensuit.xmlmap.annotations.XmlChild;
import org.ow2.opensuit.xmlmap.annotations.XmlChildren;
import org.ow2.opensuit.xmlmap.annotations.XmlContent;
import org.ow2.opensuit.xmlmap.annotations.XmlContentAssist;
import org.ow2.opensuit.xmlmap.annotations.XmlDoc;
import org.ow2.opensuit.xmlmap.annotations.XmlElement;
import org.ow2.opensuit.xmlmap.annotations.XmlEnumeration;
import org.ow2.opensuit.xmlmap.annotations.XmlSimpleType;
import org.ow2.opensuit.xmlmap.annotations.XmlSubstitutionGroup;
import org.ow2.opensuit.xmlmap.interfaces.IContentAssist;
import org.ow2.opensuit.xmlmap.mapping.FieldMapping;
import org.ow2.opensuit.xmlmap.mapping.IMappingMethod;
import org.ow2.opensuit.xmlmap.mapping.MappingError;

/**
 * This class implements IMappingMethod interface.
 * This implementation allow to generated the Java & XML data binding,
 * thank to Java annotations. It's the default mapping method of
 * OpenSUIT Framework.
 * At the moment just three {@link IMappingMethod} exsist:
 * 
    *
  • method-annotations
  • *
  • method-declare
  • *
  • method-naming
  • *
* @author Adrien Ruffie / Pierre Smeyers * @author http://opensuit.ow2.org/ * @version 1.0 */ public class BindWithAnnotations extends IMappingMethod { // ===================================================================================== // === Private Stuff // ===================================================================================== private static int jvmFieldEnumerationDirection = 0;// unknown private static class TestClass { private Object field1; private Object field2; } /** * Returns whether the JVM enumerates the declared fields in the same order * than they were declared, or in the opposite order. * @return boolean */ private static boolean fieldEnumFollowsDeclaration() { if(jvmFieldEnumerationDirection == 0) { // --- have to init Field[] fields = TestClass.class.getDeclaredFields(); jvmFieldEnumerationDirection = fields[0].getName().equals("field1") ? 1 : -1; } return jvmFieldEnumerationDirection == 1; } // ===================================================================================== // === Type Methods // ===================================================================================== @Override /** * Allow to check if the provided class parameter is an simple type. * Returns true if an XmlSimpleType annotation for the specified * class is present on this element, else false. * @param c the Class object which must be checked * @return boolean * @see org.ow2.opensuit.xmlmap.mapping.IMappingMethod#isSimpleType(Class) */ public boolean isSimpleType(Class c) { return c.isAnnotationPresent(XmlSimpleType.class); } /** * Allow to check if the provided class parameter is an enumeration. * Returns true if an XmlEnumeration annotation for the specified * class is present on this element, else false. * @param c the Class object which must be checked * @return boolean * @see org.ow2.opensuit.xmlmap.mapping.IMappingMethod#isEnumeration(Class) */ public boolean isEnumeration(Class c) { return c.isAnnotationPresent(XmlEnumeration.class); } /** * Allow to check if the provided class parameter importable by * checking substitution group annotation. Returns true if an * XmlSubstitutionGroup annotation for the specified class is * present on this element, else false. * @param c the Class object which must be checked * @return boolean * @see org.ow2.opensuit.xmlmap.mapping.IMappingMethod#isImportable(Class) */ public boolean isImportable(Class c) { XmlSubstitutionGroup annot = (XmlSubstitutionGroup)c.getAnnotation(XmlSubstitutionGroup.class); if(annot == null){ return false; } return annot.importable(); } /** * Allow to check if the provided class parameter is marked as a * substitution group. Returns true if an XmlSubstitutionGroup * annotation for the specified class is present on this element, * else false. * @param c the Class object which must be checked * @return boolean * @see org.ow2.opensuit.xmlmap.mapping.IMappingMethod#isSubstitutionGroup(Class) */ public boolean isSubstitutionGroup(Class c) { return c.isAnnotationPresent(XmlSubstitutionGroup.class); } /** * Allow to check if the provided class parameter is marked as a * XmlElement element. Returns true if an XmlElement * annotation for the specified class is present on this element, * else false. * @param c the Class object which must be checked * @return boolean * @see org.ow2.opensuit.xmlmap.mapping.IMappingMethod#isXmlElement(Class) */ public boolean isXmlElement(Class c) { return c.isAnnotationPresent(XmlElement.class); } // ===================================================================================== // === Simple Type Methods // ===================================================================================== @Override /** * Allow to retrieve the base type of the specified class function * parameter. This provided parameter must be annotated with * XmlSimpleType annotation, in order to retrieve the base * type * @param simpleType the Class object which must be checked * @return Class the base class of the specified simple type * @see org.ow2.opensuit.xmlmap.mapping.IMappingMethod#getBaseSimpleType(Class) */ public Class getBaseSimpleType(Class simpleType) { XmlSimpleType annot = (XmlSimpleType)simpleType.getAnnotation(XmlSimpleType.class); if(annot == null){ return null; } return annot.base(); } // ===================================================================================== // === Element Methods // ===================================================================================== /** * Allow populate the mapping list provided in function parameter. * This populating is recursivly performed for super class. * @param mappedFields fields list which must be populate. * @param mappingErrors list where occured errors are stored. * @param iClass Class to extract Xml Mappings from. * @param iExcludeFrom Class object allowing to check if the * super class isn't an exclude type. * @see org.ow2.opensuit.xmlmap.mapping.IMappingMethod#populateMappings(List, List , Class, Class); */ protected void populateMappings(List mappedFields, List mappingErrors, Class iClass, Class iExcludeFrom) { if(iClass.getSuperclass() != null && iClass.getSuperclass() != iExcludeFrom){ populateMappings(mappedFields, mappingErrors, iClass.getSuperclass(), iExcludeFrom); } Field[] fields = iClass.getDeclaredFields(); boolean fieldsInDeclOrder = fieldEnumFollowsDeclaration(); for(int i=0; i, Field[]> class2Fields = new HashMap, Field[]>(); /** * Returns the number of elements in the enumeration. * @param Class represent the enumeration class. * @return int The number of elements. * @see org.ow2.opensuit.xmlmap.mapping.IMappingMethod#getNbOfEnumItems(Class) */ public int getNbOfEnumItems(Class iEnumClass) { if(iEnumClass.isEnum()){ return iEnumClass.getEnumConstants().length; } return getFakeEnumItems(iEnumClass).length; } /** * Returns an enumeration element from its name. * @param Class represent the enumeration class. * @param iName The enumeration element name. * @return Object The enumeration element value. * @see org.ow2.opensuit.xmlmap.mapping.IMappingMethod#getEnumItem(Class, String) */ public Object getEnumItem(Class iEnumClass, String iName) { if(iEnumClass.isEnum()){ return Enum.valueOf(iEnumClass, iName); } Field[] elements = getFakeEnumItems(iEnumClass); for(int i=0; i= elements.length){ return null; } try { return elements[iIndex].get(null); } catch(Exception e) { System.out.println("Enum Error:"); e.printStackTrace(); return null; } } /** * Returns an enumeration element name from its index. * @param iIndex The enumeration element index. * @return String The enumeration element name. * @see org.ow2.opensuit.xmlmap.mapping.IMappingMethod#getEnumItemName(Class, int) */ public String getEnumItemName(Class iEnumClass, int iIndex) { if(iEnumClass.isEnum()){ return ((Enum)iEnumClass.getEnumConstants()[iIndex]).name(); } Field[] elements = getFakeEnumItems(iEnumClass); if(iIndex >= elements.length){ return null; } return elements[iIndex].getName(); } /** * Returns an enumeration name. * @param iEnumValue The enumeration element. * @return String The enumeration element name. * @see org.ow2.opensuit.xmlmap.mapping.IMappingMethod#getEnumItemName(Object) */ public String getEnumItemName(Object iEnumValue) { if(iEnumValue instanceof Enum){ return ((Enum)iEnumValue).name(); } Field[] elements = getFakeEnumItems(iEnumValue.getClass()); for(int i=0; i iEnumClass) { Field[] elements = (Field[])class2Fields.get(iEnumClass); if(elements == null) { Vector elementFields = new Vector(); Field[] fields = iEnumClass.getDeclaredFields(); for(int i=0; i holderClass = annot.value(); Constructor cs = holderClass.getDeclaredConstructor(); // IContentAssistHolder holder = annot.value().newInstance(); cs.setAccessible(true); IContentAssistHolder holder = cs.newInstance(); holder.setObject(obj); return holder; } catch (Exception e) { System.err.println("error while instanatiating content assist holder: "); e.printStackTrace(); return null; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy