org.exolab.castor.mapping.loader
Class AbstractMappingLoader

java.lang.Object
  extended by org.exolab.castor.mapping.loader.AbstractMappingLoader2
      extended by org.exolab.castor.mapping.loader.AbstractMappingLoader
All Implemented Interfaces:
MappingLoader
Direct Known Subclasses:
XMLMappingLoader

public abstract class AbstractMappingLoader
extends AbstractMappingLoader2

Assists in the construction of descriptors. Can be used as a mapping resolver to the engine. Engines will implement their own mapping scheme typically by extending this class.

Version:
$Revision: 6784 $ $Date: 2006-04-10 16:39:24 -0600 (Mon, 10 Apr 2006) $
Author:
Assaf Arkin, Keith Visco

Nested Class Summary
 class AbstractMappingLoader.TypeInfoReference
          A class used to by the createFieldHandler method in order to save the reference of the TypeInfo that was used.
 
Field Summary
protected static java.lang.Class[] EMPTY_ARGS
          Empty array of class types used for reflection.
protected static java.lang.Class[] STRING_ARG
          The string argument for the valueOf method, used for introspection when searching for type-safe enumeration style classes.
protected static java.lang.String VALUE_OF
          Factory method name for type-safe enumerations.
 
Constructor Summary
protected AbstractMappingLoader(java.lang.ClassLoader loader)
          Constructs a new mapping helper.
 
Method Summary
protected  void checkFieldNameDuplicates(FieldDescriptor[] fields, java.lang.Class cls)
          Checks all given fields for name equality and throws a MappingException if at least two fields have the same name.
protected abstract  ClassDescriptor createClassDescriptor(ClassMapping clsMap)
           
protected  void createClassDescriptors(MappingRoot mapping)
           
protected  AbstractFieldDescriptor createFieldDesc(java.lang.Class javaClass, FieldMapping fieldMap)
          Creates a single field descriptor.
protected  AbstractFieldDescriptor[] createFieldDescriptors(ClassMapping clsMap, java.lang.Class javaClass)
          Create field descriptors.
protected  FieldHandler createFieldHandler(java.lang.Class javaClass, java.lang.Class fldType, FieldMapping fldMap, AbstractMappingLoader.TypeInfoReference typeInfoRef)
          Creates the FieldHandler for the given FieldMapping.
protected  FieldDescriptor[] divideFieldDescriptors(FieldDescriptor[] fields, java.lang.String[] ids, FieldDescriptor[] identities)
           
static java.lang.reflect.Method findAccessor(java.lang.Class javaClass, java.lang.String methodName, java.lang.Class fieldType, boolean getMethod)
          Returns the named accessor.
protected  ClassDescriptor getDepended(ClassMapping clsMap, java.lang.Class javaClass)
          Gets the ClassDescriptor the given classMapping depends on.
protected  ClassDescriptor getExtended(ClassMapping clsMap, java.lang.Class javaClass)
          Gets the ClassDescriptor the given classMapping extends.
protected  int getIdColumnIndex(FieldDescriptor field, java.lang.String[] ids)
          Finds the index in the given idColumnNames that has the same name as the given field.
static java.lang.String[] getIdentityColumnNames(java.lang.String[] ids, ClassMapping clsMap)
          Returns a list of column names that are part of the identity.
protected  ClassMapping getOrigin(ClassMapping clsMap)
          Gets the top-most (i.e.
 java.lang.String getSourceType()
          
protected  TypeInfo getTypeInfo(java.lang.Class fieldType, CollectionHandler colHandler, FieldMapping fieldMap)
           
protected static boolean isPrimitive(java.lang.Class type)
          Returns true if the given class should be treated as a primitive type
abstract  void loadMapping(MappingRoot mapping, java.lang.Object param)
          Loads the mapping from the specified mapping object if not loaded previously.
protected abstract  void resolveRelations(ClassDescriptor clsDesc)
           
protected  java.lang.Class resolveType(java.lang.String typeName)
          Returns the Java class for the named type.
 
Methods inherited from class org.exolab.castor.mapping.loader.AbstractMappingLoader2
addDescriptor, clear, descriptorIterator, getClassLoader, getDescriptor, isAllowRedefinition, loadMapping, setAllowRedefinitions, setClassLoader
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.exolab.castor.mapping.MappingLoader
getBindingType
 

Field Detail

EMPTY_ARGS

protected static final java.lang.Class[] EMPTY_ARGS
Empty array of class types used for reflection.


STRING_ARG

protected static final java.lang.Class[] STRING_ARG
The string argument for the valueOf method, used for introspection when searching for type-safe enumeration style classes.


VALUE_OF

protected static final java.lang.String VALUE_OF
Factory method name for type-safe enumerations.

See Also:
Constant Field Values
Constructor Detail

AbstractMappingLoader

protected AbstractMappingLoader(java.lang.ClassLoader loader)
Constructs a new mapping helper. This constructor is used by a derived class.

Parameters:
loader - The class loader to use, null for the default
Method Detail

getSourceType

public final java.lang.String getSourceType()


loadMapping

public abstract void loadMapping(MappingRoot mapping,
                                 java.lang.Object param)
                          throws MappingException
Loads the mapping from the specified mapping object if not loaded previously.

Parameters:
mapping - The mapping information.
param - Arbitrary parameter that can be used by subclasses.
Throws:
MappingException - The mapping file is invalid.

createClassDescriptors

protected final void createClassDescriptors(MappingRoot mapping)
                                     throws MappingException
Throws:
MappingException

createClassDescriptor

protected abstract ClassDescriptor createClassDescriptor(ClassMapping clsMap)
                                                  throws MappingException
Throws:
MappingException

getExtended

protected final ClassDescriptor getExtended(ClassMapping clsMap,
                                            java.lang.Class javaClass)
                                     throws MappingException
Gets the ClassDescriptor the given classMapping extends.

Parameters:
clsMap - The ClassMapping to find the required descriptor for.
javaClass - The name of the class that is checked (this is used for generating the exception).
Returns:
The ClassDescriptor the given ClassMapping extends or null if the given ClassMapping does not extend any.
Throws:
MappingException - If the given ClassMapping extends another ClassMapping but its descriptor could not be found.

getDepended

protected final ClassDescriptor getDepended(ClassMapping clsMap,
                                            java.lang.Class javaClass)
                                     throws MappingException
Gets the ClassDescriptor the given classMapping depends on.

Parameters:
clsMap - The ClassMapping to find the required ClassDescriptor for.
javaClass - The name of the class that is checked (this is used for generating the exception).
Returns:
The ClassDescriptor the given ClassMapping depends on or null if the given ClassMapping does not depend on any.
Throws:
MappingException - If the given ClassMapping depends on another ClassMapping but its descriptor could not be found.

checkFieldNameDuplicates

protected final void checkFieldNameDuplicates(FieldDescriptor[] fields,
                                              java.lang.Class cls)
                                       throws MappingException
Checks all given fields for name equality and throws a MappingException if at least two fields have the same name.

Parameters:
fields - The fields to be checked.
cls - Class that is checked (this is used for generating the exception).
Throws:
MappingException - If at least two fields have the same name.

resolveRelations

protected abstract void resolveRelations(ClassDescriptor clsDesc)

resolveType

protected final java.lang.Class resolveType(java.lang.String typeName)
                                     throws MappingException
Returns the Java class for the named type. The type name can be one of the accepted short names (e.g. integer) or the full Java class name (e.g. java.lang.Integer). If the short name is used, the primitive type might be returned.

Throws:
MappingException

createFieldDescriptors

protected final AbstractFieldDescriptor[] createFieldDescriptors(ClassMapping clsMap,
                                                                 java.lang.Class javaClass)
                                                          throws MappingException
Create field descriptors. The class mapping information is used to create descriptors for all the fields in the class, except for container fields. Implementations may extend this method to create more suitable descriptors, or create descriptors only for a subset of the fields.

Parameters:
clsMap - The class to which the fields belong.
javaClass - The field mappings.
Throws:
MappingException - An exception indicating why mapping for the class cannot be created.

getOrigin

protected final ClassMapping getOrigin(ClassMapping clsMap)
Gets the top-most (i.e. without any further 'extends') extends of the given classMapping.

Parameters:
clsMap - The ClassMapping to get the origin for.
Returns:
The top-most extends of the given ClassMapping or the ClassMapping itself if it does not extend any other ClassMapping.

divideFieldDescriptors

protected final FieldDescriptor[] divideFieldDescriptors(FieldDescriptor[] fields,
                                                         java.lang.String[] ids,
                                                         FieldDescriptor[] identities)

getIdColumnIndex

protected int getIdColumnIndex(FieldDescriptor field,
                               java.lang.String[] ids)
Finds the index in the given idColumnNames that has the same name as the given field.

Parameters:
field - The FieldDescriptor to find the column index for.
ids - The id columnNames available.
Returns:
The index of the id column name that matches the given field's name or -1 if no such id column name exists.

createFieldDesc

protected AbstractFieldDescriptor createFieldDesc(java.lang.Class javaClass,
                                                  FieldMapping fieldMap)
                                           throws MappingException
Creates a single field descriptor. The field mapping is used to create a new stock FieldDescriptor. Implementations may extend this class to create a more suitable descriptor.

Parameters:
javaClass - The class to which the field belongs.
fieldMap - The field mapping information.
Returns:
The field descriptor.
Throws:
MappingException - The field or its accessor methods are not found, not accessible, not of the specified type, etc.

createFieldHandler

protected final FieldHandler createFieldHandler(java.lang.Class javaClass,
                                                java.lang.Class fldType,
                                                FieldMapping fldMap,
                                                AbstractMappingLoader.TypeInfoReference typeInfoRef)
                                         throws MappingException
Creates the FieldHandler for the given FieldMapping.

Parameters:
javaClass - the class type of the parent of the field.
fldType - the Java class type for the field.
fldMap - the field mapping.
Returns:
the newly created FieldHandler.
Throws:
MappingException

getTypeInfo

protected TypeInfo getTypeInfo(java.lang.Class fieldType,
                               CollectionHandler colHandler,
                               FieldMapping fieldMap)
                        throws MappingException
Throws:
MappingException

findAccessor

public static final java.lang.reflect.Method findAccessor(java.lang.Class javaClass,
                                                          java.lang.String methodName,
                                                          java.lang.Class fieldType,
                                                          boolean getMethod)
                                                   throws MappingException
Returns the named accessor. Uses reflection to return the named accessor and check the return value or parameter type, if specified.

Parameters:
javaClass - The class to which the field belongs.
methodName - The name of the accessor method.
fieldType - The type of the field if known, or null.
getMethod - True if get method, false if set method.
Returns:
The method, null if not found.
Throws:
MappingException - The method is not accessible or is not of the specified type.

getIdentityColumnNames

public static final java.lang.String[] getIdentityColumnNames(java.lang.String[] ids,
                                                              ClassMapping clsMap)
Returns a list of column names that are part of the identity.

Parameters:
ids - Known identity names.
clsMap - Class mapping.
Returns:
List of identity column names.

isPrimitive

protected static final boolean isPrimitive(java.lang.Class type)
Returns true if the given class should be treated as a primitive type

Returns:
true if the given class should be treated as a primitive type


Intalio Inc. (C) 1999-2007. All rights reserved http://www.intalio.com