GXPARSE hosted by SourceForge.net Logo

ca.gorman.xml.parse.util
Class SimpleElementMapper<T extends Parser>

java.lang.Object
  extended by ca.gorman.xml.parse.util.SimpleElementMapper<T>
All Implemented Interfaces:
ElementListener<T>, ElementMapper<T>

public class SimpleElementMapper<T extends Parser>
extends Object
implements ElementMapper<T>

Maps element names to Java method names and invokes the appropriate element method for each element.

This class gets the name of an XML element and transforms that name to a Java identitifier by:

If a method can be found with the constructed name, and the method takes a single parameter of type CurrentElement, that method is invoked on the element. Otherwise, a specified default method is invoked on the element.


Nested Class Summary
 
Nested classes/interfaces inherited from interface ca.gorman.xml.parse.ElementMapper
ElementMapper.InvalidHandlerException, ElementMapper.NotFoundException
 
Field Summary
 
Fields inherited from interface ca.gorman.xml.parse.ElementMapper
ESCAPE_CENT, ESCAPE_CURRENCY, ESCAPE_DOLLAR, ESCAPE_POUND, ESCAPE_YEN
 
Constructor Summary
SimpleElementMapper(String methodNamePrefix, Object defaultClassInstance, String defaultMethodName)
           Construct an element mapper to access public element methods in a defaultClassInstance.
 
Method Summary
 void doElement(T parser, CurrentElement element)
           Receive a request to handle a element that is being parsed, and invoke the appropriate public element method for that element.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleElementMapper

public SimpleElementMapper(String methodNamePrefix,
                           Object defaultClassInstance,
                           String defaultMethodName)
                    throws NoSuchMethodException,
                           IllegalAccessException

Construct an element mapper to access public element methods in a defaultClassInstance. When an element name cannot be mapped to the name of a public element method in the defaultClassInstance, public method defaultMethodName will invoked on the element.

Warning: Element method names must be public and non-static. Static methods will cause an exception. Private and protected methods will be treated as if they do not exist.

Note:The constructor makes a check to ensure that a non-empty methodNamePrefix is not used as a prefix in any static or non-public method.

Parameters:
methodNamePrefix - String, which can be empty, used as the prefix to the name of every public element method except the public default method
defaultClassInstance - the class instance that contains the default element method and all other element methods.
defaultMethodName - the name of the public default element method
Throws:
IllegalAccessException - if the default method is static or if methodNamePrefix is a non-empty prefix and the name of any static or non-public method in defaultClassInstance begins with the methodNamePrefix
NoSuchMethodException - when the named default method does not exist in the default class instance, or exists but is not public
NullPointerException - if any parameter is null
IllegalArgumentException - if methodNamePrefix is not empty and is also invalid as a Java identifier.
Method Detail

doElement

public void doElement(T parser,
                      CurrentElement element)
               throws ListenerException,
                      IOException

Receive a request to handle a element that is being parsed, and invoke the appropriate public element method for that element.

Warning: Element method names must be public and non-static. Static methods will cause an exception. Private and protected methods will be treated as if they do not exist, and the default element method will be invoked instead of any private or protected method

Specified by:
doElement in interface ElementListener<T extends Parser>
Specified by:
doElement in interface ElementMapper<T extends Parser>
Parameters:
parser - The Parser that invoked this method
element - CurrentElement instance with a content handler to parse the centent of the element.
Throws:
ListenerException - if the element method throws ListenerException
IOException - if the element method throws IOException
ListenerRuntimeException - with IllegalAccessException as the cause if the element method is static
RuntimeException - if the element method throws RuntimeException
NullPointerException - if element is null

GXPARSE download

GXPARSE Generic XML Stream Parser API and supporting tools.   Release $Name: gxparse-sf-alpha-2_0 $
Copyright 2003-2004 Ian E. Gorman
Released under GNU Lesser General Public License