org.exolab.castor.mapping.loader
Class MappingLoader

java.lang.Object
  extended by org.exolab.castor.mapping.loader.MappingLoader
All Implemented Interfaces:
MappingResolver
Direct Known Subclasses:
JDOMappingLoader, MappingTool.InternalLoader, XMLMappingLoader

public abstract class MappingLoader
extends java.lang.Object
implements MappingResolver

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: 1.4 $ $Date: 2003/04/23 08:50:51 $
Author:
Assaf Arkin, Keith Visco

Nested Class Summary
(package private)  class MappingLoader.TypeInfoReference
          A class used to by the createFieldHandler method in order to save the reference of the TypeInfo that was used.
 
Field Summary
static ClassDescriptor NoDescriptor
           
 
Constructor Summary
protected MappingLoader(java.lang.ClassLoader loader, java.io.PrintWriter logWriter)
          Constructs a new mapping helper.
 
Method Summary
protected  void addDescriptor(ClassDescriptor clsDesc)
          Adds a class descriptor.
protected  ClassDescriptor createDescriptor(ClassMapping clsMap)
          Creates a new descriptor.
protected  FieldDescriptor createFieldDesc(java.lang.Class javaClass, FieldMapping fieldMap)
          Creates a single field descriptor.
protected  FieldDescriptor[] createFieldDescs(java.lang.Class javaClass, FieldMapping[] fieldMaps)
          Create field descriptors.
protected  FieldHandler createFieldHandler(java.lang.Class javaClass, java.lang.Class fieldType, FieldMapping fieldMap, MappingLoader.TypeInfoReference typeInfoRef)
          Creates the FieldHandler for the given FieldMapping
protected static java.lang.reflect.Method findAccessor(java.lang.Class javaClass, java.lang.String methodName, java.lang.Class fieldType, boolean getMethod)
          Returns the named accessor.
 java.lang.ClassLoader getClassLoader()
          Returns the class loader associated with this mapping resolver if one was specified.
 ClassDescriptor getDescriptor(java.lang.Class type)
          Returns the class descriptor for the specified Java class.
protected  java.io.PrintWriter getLogWriter()
          Returns the log writer.
protected  TypeInfo getTypeInfo(java.lang.Class fieldType, CollectionHandler colHandler, FieldMapping fieldMap)
           
 java.util.Enumeration listDescriptors()
          Returns an enumeration of all the known descriptors.
 java.util.Enumeration listJavaClasses()
          Returns an enumeration of all the supported Java classes.
protected  ClassDescriptor loadClassDescriptor(java.lang.String clsName)
          Loads a class descriptor from a compiled class.
 void loadMapping(MappingRoot mapping, java.lang.Object param)
          Loads the mapping from the specified mapping object.
protected  void resolveRelations(ClassDescriptor clsDesc)
           
protected  java.lang.Class resolveType(java.lang.String typeName)
          Returns the Java class for the named type.
 void setAllowRedefinitions(boolean allow)
          Enables or disables the ability to allow the redefinition of class mappings.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NoDescriptor

public static final ClassDescriptor NoDescriptor
Constructor Detail

MappingLoader

protected MappingLoader(java.lang.ClassLoader loader,
                        java.io.PrintWriter logWriter)
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

getDescriptor

public ClassDescriptor getDescriptor(java.lang.Class type)
Description copied from interface: MappingResolver
Returns the class descriptor for the specified Java class. In no such descriptor exists, returns null.

Specified by:
getDescriptor in interface MappingResolver
Parameters:
type - The Java class
Returns:
A suitable class descriptor or null

listDescriptors

public java.util.Enumeration listDescriptors()
Description copied from interface: MappingResolver
Returns an enumeration of all the known descriptors. Each element is of type ClassDescriptor.

Specified by:
listDescriptors in interface MappingResolver

listJavaClasses

public java.util.Enumeration listJavaClasses()
Description copied from interface: MappingResolver
Returns an enumeration of all the supported Java classes. Each element is of type java.lang.Class, and for each such class a suitable descriptor exists.

Specified by:
listJavaClasses in interface MappingResolver

getClassLoader

public java.lang.ClassLoader getClassLoader()
Description copied from interface: MappingResolver
Returns the class loader associated with this mapping resolver if one was specified. This is the class loader used to load all the classes mapped by this mapping resolver. May be null if no class loader was specified or in certain JVMs.

Specified by:
getClassLoader in interface MappingResolver

getLogWriter

protected java.io.PrintWriter getLogWriter()
Returns the log writer. If not null, errors and other messages should be directed to the log writer.


resolveType

protected java.lang.Class resolveType(java.lang.String typeName)
                               throws java.lang.ClassNotFoundException
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:
java.lang.ClassNotFoundException

loadMapping

public void loadMapping(MappingRoot mapping,
                        java.lang.Object param)
                 throws MappingException
Loads the mapping from the specified mapping object. Calls createDescriptor(org.exolab.castor.mapping.xml.ClassMapping) to create each descriptor and addDescriptor(org.exolab.castor.mapping.ClassDescriptor) to store it. Also loads all the included mapping files.

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

setAllowRedefinitions

public void setAllowRedefinitions(boolean allow)
Enables or disables the ability to allow the redefinition of class mappings.

Parameters:
allow - a boolean that when true enables redefinitions.

addDescriptor

protected void addDescriptor(ClassDescriptor clsDesc)
                      throws MappingException
Adds a class descriptor. Will throw a mapping exception if a descriptor for this class already exists.

Parameters:
clsDesc - The descriptor to add
Throws:
MappingException - A descriptor for this class already exists

resolveRelations

protected void resolveRelations(ClassDescriptor clsDesc)
                         throws MappingException
Throws:
MappingException

createDescriptor

protected ClassDescriptor createDescriptor(ClassMapping clsMap)
                                    throws MappingException
Creates a new descriptor. The class mapping information is used to create a new stock ClassDescriptor. Implementations may extend this class to create a more suitable descriptor.

Parameters:
clsMap - The class mapping information
Throws:
MappingException - An exception indicating why mapping for the class cannot be created

createFieldDescs

protected FieldDescriptor[] createFieldDescs(java.lang.Class javaClass,
                                             FieldMapping[] fieldMaps)
                                      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:
javaClass - The class to which the fields belong
fieldMaps - The field mappings
Throws:
MappingException - An exception indicating why mapping for the class cannot be created

createFieldDesc

protected FieldDescriptor 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 FieldHandler createFieldHandler(java.lang.Class javaClass,
                                          java.lang.Class fieldType,
                                          FieldMapping fieldMap,
                                          MappingLoader.TypeInfoReference typeInfoRef)
                                   throws MappingException
Creates the FieldHandler for the given FieldMapping

Parameters:
javaClass - the class type of the parent of the field
fieldType - the Java class type for the field.
fieldMap - 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

protected 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
isGetMethod - 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

loadClassDescriptor

protected ClassDescriptor loadClassDescriptor(java.lang.String clsName)
Loads a class descriptor from a compiled class.

Parameters:
clsName - The class for which the descriptor is loaded
Returns:
An instance of the class descriptor or null if not found


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