org.apache.commons.betwixt
Class XMLIntrospector

java.lang.Object
  extended byorg.apache.commons.betwixt.XMLIntrospector

public class XMLIntrospector
extends java.lang.Object

XMLIntrospector an introspector of beans to create a XMLBeanInfo instance.

By default, XMLBeanInfo caching is switched on. This means that the first time that a request is made for a XMLBeanInfo for a particular class, the XMLBeanInfo is cached. Later requests for the same class will return the cached value.

Note :

This class makes use of the java.bean.Introspector class, which contains a BeanInfoSearchPath. To make sure betwixt can do his work correctly, this searchpath is completely ignored during processing. The original values will be restored after processing finished

Version:
$Id: XMLIntrospector.java,v 1.19 2003/01/09 22:34:07 rdonkin Exp $
Author:
James Strachan, Martin van den Bemt

Field Summary
private  NameMapper attributeNameMapper
          The strategy used to convert bean type names into attribute names It will default to the normal nameMapper.
private  boolean attributesForPrimitives
          should attributes or elements be used for primitive types
private  XMLBeanInfoDigester digester
          Digester used to parse the XML descriptor files
private  NameMapper elementNameMapper
          The strategy used to convert bean type names into element names
protected  org.apache.commons.logging.Log log
          Log used for logging (Doh!)
private  PluralStemmer pluralStemmer
          The strategy used to detect matching singular and plural properties
private  XMLBeanInfoRegistry registry
          Maps classes to XMLBeanInfo's
private  boolean useBeanInfoSearchPath
          Should the existing bean info search path for java.reflect.Introspector be used?
private  boolean wrapCollectionsInElement
          should we wrap collections in an extra element?
 
Constructor Summary
XMLIntrospector()
          Base constructor
 
Method Summary
protected  void addProperties(java.beans.BeanInfo beanInfo, java.util.List elements, java.util.List attributes)
          Loop through properties and process each one
protected  void addProperty(java.beans.BeanInfo beanInfo, java.beans.PropertyDescriptor propertyDescriptor, java.util.List elements, java.util.List attributes)
          Process a property.
protected  NameMapper createNameMapper()
          A Factory method to lazily create a strategy used to convert bean type names into element names.
protected  PluralStemmer createPluralStemmer()
          A Factory method to lazily create a new strategy to detect matching singular and plural properties.
protected  XMLBeanInfo createXMLBeanInfo(java.beans.BeanInfo beanInfo)
          Factory method to create XMLBeanInfo instances
protected  XMLBeanInfo findByXMLDescriptor(java.lang.Class aClass)
          Attempt to lookup the XML descriptor for the given class using the classname + ".betwixt" using the same ClassLoader used to load the class or return null if it could not be loaded
 void flushCache()
          Deprecated. use flushable registry instead
 NameMapper getAttributeNameMapper()
          Gets the name mapping strategy used to convert bean names into attributes.
 NameMapper getElementNameMapper()
          Gets the name mapping strategy used to convert bean names into elements.
 org.apache.commons.logging.Log getLog()
          Gets the current logging implementation.
 NameMapper getNameMapper()
          Deprecated. getNameMapper is split up in getElementNameMapper() and getAttributeNameMapper()
 PluralStemmer getPluralStemmer()
          Get singular and plural matching strategy.
 XMLBeanInfoRegistry getRegistry()
          Gets the current registry implementation.
 XMLBeanInfo introspect(java.beans.BeanInfo beanInfo)
          Create a standard XMLBeanInfo by introspection.
 XMLBeanInfo introspect(java.lang.Class aClass)
          Create a standard XMLBeanInfo by introspection.
 XMLBeanInfo introspect(java.lang.Object bean)
          Create a standard XMLBeanInfo by introspection The actual introspection depends only on the BeanInfo associated with the bean.
 boolean isAttributesForPrimitives()
          Should attributes (or elements) be used for primitive types.
 boolean isCachingEnabled()
          Deprecated. replaced by XMlBeanInfoRegistry
 boolean isLoopType(java.lang.Class type)
          Is this class a loop?
 boolean isPrimitiveType(java.lang.Class type)
          Is this class a primitive?
 boolean isWrapCollectionsInElement()
          Should collections be wrapped in an extra element?
 void setAttributeNameMapper(NameMapper nameMapper)
          Sets the strategy used to convert bean type names into attribute names
 void setAttributesForPrimitives(boolean attributesForPrimitives)
          Set whether attributes (or elements) should be used for primitive types.
 void setCachingEnabled(boolean cachingEnabled)
          Deprecated. replaced by XMlBeanInfoRegistry
 void setElementNameMapper(NameMapper nameMapper)
          Sets the strategy used to convert bean type names into element names
 void setLog(org.apache.commons.logging.Log log)
          Sets the current logging implementation.
 void setNameMapper(NameMapper nameMapper)
          Deprecated. setNameMapper is split up in setElementNameMapper(NameMapper) and setAttributeNameMapper(NameMapper)
 void setPluralStemmer(PluralStemmer pluralStemmer)
          Sets the strategy used to detect matching singular and plural properties
 void setRegistry(XMLBeanInfoRegistry registry)
          Sets the XMLBeanInfoRegistry implementation.
 void setUseBeanInfoSearchPath(boolean useBeanInfoSearchPath)
          Specifies if you want to use the beanInfoSearchPath
 void setWrapCollectionsInElement(boolean wrapCollectionsInElement)
          Sets whether we should we wrap collections in an extra element.
 boolean useBeanInfoSearchPath()
          Should the original java.reflect.Introspector bean info search path be used? By default it will be false.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected org.apache.commons.logging.Log log
Log used for logging (Doh!)


attributesForPrimitives

private boolean attributesForPrimitives
should attributes or elements be used for primitive types


wrapCollectionsInElement

private boolean wrapCollectionsInElement
should we wrap collections in an extra element?


registry

private XMLBeanInfoRegistry registry
Maps classes to XMLBeanInfo's


digester

private XMLBeanInfoDigester digester
Digester used to parse the XML descriptor files


pluralStemmer

private PluralStemmer pluralStemmer
The strategy used to detect matching singular and plural properties


elementNameMapper

private NameMapper elementNameMapper
The strategy used to convert bean type names into element names


attributeNameMapper

private NameMapper attributeNameMapper
The strategy used to convert bean type names into attribute names It will default to the normal nameMapper.


useBeanInfoSearchPath

private boolean useBeanInfoSearchPath
Should the existing bean info search path for java.reflect.Introspector be used?

Constructor Detail

XMLIntrospector

public XMLIntrospector()
Base constructor

Method Detail

getLog

public org.apache.commons.logging.Log getLog()

Gets the current logging implementation.

Returns:
the Log implementation which this class logs to

setLog

public void setLog(org.apache.commons.logging.Log log)

Sets the current logging implementation.

Parameters:
log - the Log implementation to use for logging

getRegistry

public XMLBeanInfoRegistry getRegistry()

Gets the current registry implementation. The registry is checked to see if it has an XMLBeanInfo for a class before introspecting. After standard introspection is complete, the instance will be passed to the registry.

This allows finely grained control over the caching strategy. It also allows the standard introspection mechanism to be overridden on a per class basis.

Returns:
the XMLBeanInfoRegistry currently used

setRegistry

public void setRegistry(XMLBeanInfoRegistry registry)

Sets the XMLBeanInfoRegistry implementation. The registry is checked to see if it has an XMLBeanInfo for a class before introspecting. After standard introspection is complete, the instance will be passed to the registry.

This allows finely grained control over the caching strategy. It also allows the standard introspection mechanism to be overridden on a per class basis.

Parameters:
registry - the XMLBeanInfoRegistry to use

isCachingEnabled

public boolean isCachingEnabled()
Deprecated. replaced by XMlBeanInfoRegistry

Is XMLBeanInfo caching enabled?

Returns:
true if caching is enabled

setCachingEnabled

public void setCachingEnabled(boolean cachingEnabled)
Deprecated. replaced by XMlBeanInfoRegistry

Set whether XMLBeanInfo caching should be enabled.

Parameters:
cachingEnabled - ignored

flushCache

public void flushCache()
Deprecated. use flushable registry instead

Flush existing cached XMLBeanInfo's.


introspect

public XMLBeanInfo introspect(java.lang.Object bean)
                       throws java.beans.IntrospectionException
Create a standard XMLBeanInfo by introspection The actual introspection depends only on the BeanInfo associated with the bean.

Parameters:
bean - introspect this bean
Returns:
XMLBeanInfo describing bean-xml mapping
Throws:
java.beans.IntrospectionException - when the bean introspection fails

introspect

public XMLBeanInfo introspect(java.lang.Class aClass)
                       throws java.beans.IntrospectionException
Create a standard XMLBeanInfo by introspection. The actual introspection depends only on the BeanInfo associated with the bean.

Parameters:
aClass - introspect this class
Returns:
XMLBeanInfo describing bean-xml mapping
Throws:
java.beans.IntrospectionException - when the bean introspection fails

introspect

public XMLBeanInfo introspect(java.beans.BeanInfo beanInfo)
                       throws java.beans.IntrospectionException
Create a standard XMLBeanInfo by introspection. The actual introspection depends only on the BeanInfo associated with the bean.

Parameters:
beanInfo - the BeanInfo the xml-bean mapping is based on
Returns:
XMLBeanInfo describing bean-xml mapping
Throws:
java.beans.IntrospectionException - when the bean introspection fails

isAttributesForPrimitives

public boolean isAttributesForPrimitives()
Should attributes (or elements) be used for primitive types.

Returns:
true if primitive types will be mapped to attributes in the introspection

setAttributesForPrimitives

public void setAttributesForPrimitives(boolean attributesForPrimitives)
Set whether attributes (or elements) should be used for primitive types.

Parameters:
attributesForPrimitives - pass trus to map primitives to attributes, pass false to map primitives to elements

isWrapCollectionsInElement

public boolean isWrapCollectionsInElement()
Should collections be wrapped in an extra element?

Returns:
whether we should we wrap collections in an extra element?

setWrapCollectionsInElement

public void setWrapCollectionsInElement(boolean wrapCollectionsInElement)
Sets whether we should we wrap collections in an extra element.

Parameters:
wrapCollectionsInElement - pass true if collections should be wrapped in a parent element

getPluralStemmer

public PluralStemmer getPluralStemmer()
Get singular and plural matching strategy.

Returns:
the strategy used to detect matching singular and plural properties

setPluralStemmer

public void setPluralStemmer(PluralStemmer pluralStemmer)
Sets the strategy used to detect matching singular and plural properties

Parameters:
pluralStemmer - the PluralStemmer used to match singular and plural

getNameMapper

public NameMapper getNameMapper()
Deprecated. getNameMapper is split up in getElementNameMapper() and getAttributeNameMapper()

Gets the name mapper strategy.

Returns:
the strategy used to convert bean type names into element names

setNameMapper

public void setNameMapper(NameMapper nameMapper)
Deprecated. setNameMapper is split up in setElementNameMapper(NameMapper) and setAttributeNameMapper(NameMapper)

Sets the strategy used to convert bean type names into element names

Parameters:
nameMapper - the NameMapper strategy to be used

getElementNameMapper

public NameMapper getElementNameMapper()
Gets the name mapping strategy used to convert bean names into elements.

Returns:
the strategy used to convert bean type names into element names. If no element mapper is currently defined then a default one is created.

setElementNameMapper

public void setElementNameMapper(NameMapper nameMapper)
Sets the strategy used to convert bean type names into element names

Parameters:
nameMapper - the NameMapper to use for the conversion

getAttributeNameMapper

public NameMapper getAttributeNameMapper()
Gets the name mapping strategy used to convert bean names into attributes.

Returns:
the strategy used to convert bean type names into attribute names. If no attributeNamemapper is known, it will default to the ElementNameMapper

setAttributeNameMapper

public void setAttributeNameMapper(NameMapper nameMapper)
Sets the strategy used to convert bean type names into attribute names

Parameters:
nameMapper - the NameMapper to use for the convertion

createPluralStemmer

protected PluralStemmer createPluralStemmer()
A Factory method to lazily create a new strategy to detect matching singular and plural properties.

Returns:
new defualt PluralStemmer implementation

createNameMapper

protected NameMapper createNameMapper()
A Factory method to lazily create a strategy used to convert bean type names into element names.

Returns:
new default NameMapper implementation

findByXMLDescriptor

protected XMLBeanInfo findByXMLDescriptor(java.lang.Class aClass)
Attempt to lookup the XML descriptor for the given class using the classname + ".betwixt" using the same ClassLoader used to load the class or return null if it could not be loaded

Parameters:
aClass - digester .betwixt file for this class
Returns:
XMLBeanInfo digested from the .betwixt file if one can be found. Otherwise null.

addProperties

protected void addProperties(java.beans.BeanInfo beanInfo,
                             java.util.List elements,
                             java.util.List attributes)
                      throws java.beans.IntrospectionException
Loop through properties and process each one

Parameters:
beanInfo - the BeanInfo whose properties will be processed
elements - ElementDescriptor list to which elements will be added
attributes - AttributeDescriptor list to which attributes will be added
Throws:
java.beans.IntrospectionException - if the bean introspection fails

addProperty

protected void addProperty(java.beans.BeanInfo beanInfo,
                           java.beans.PropertyDescriptor propertyDescriptor,
                           java.util.List elements,
                           java.util.List attributes)
                    throws java.beans.IntrospectionException
Process a property. Go through and work out whether it's a loop property, a primitive or a standard. The class property is ignored.

Parameters:
beanInfo - the BeanInfo whose property is being processed
propertyDescriptor - the PropertyDescriptor to process
elements - ElementDescriptor list to which elements will be added
attributes - AttributeDescriptor list to which attributes will be added
Throws:
java.beans.IntrospectionException - if the bean introspection fails

createXMLBeanInfo

protected XMLBeanInfo createXMLBeanInfo(java.beans.BeanInfo beanInfo)
Factory method to create XMLBeanInfo instances

Parameters:
beanInfo - the BeanInfo from which the XMLBeanInfo will be created
Returns:
XMLBeanInfo describing the bean-xml mapping

isLoopType

public boolean isLoopType(java.lang.Class type)
Is this class a loop?

Parameters:
type - the Class to test
Returns:
true if the type is a loop type

isPrimitiveType

public boolean isPrimitiveType(java.lang.Class type)
Is this class a primitive?

Parameters:
type - the Class to test
Returns:
true for primitive types

useBeanInfoSearchPath

public boolean useBeanInfoSearchPath()
Should the original java.reflect.Introspector bean info search path be used? By default it will be false.

Returns:
boolean if the beanInfoSearchPath should be used.

setUseBeanInfoSearchPath

public void setUseBeanInfoSearchPath(boolean useBeanInfoSearchPath)
Specifies if you want to use the beanInfoSearchPath

Parameters:
useBeanInfoSearchPath -
See Also:
for more details