The most basic part of JaxMeXS is its generic parser. This part is suited for arbitrary XML document types, for example configuration files and the like. The actual XML Schema parsers (the syntax parser and the logical parser) are sitting on top of the generic parser. If you aren't interested in the generic part, you may safely skip to the next section, which is basically self contained. However, some details are best described here.
The generic parser follows an idea from the Ant project: A SAX parser is responsible for the configuration of a bean. Any XML element is mapped to a Java bean. The XML elements attributes are mapped to properties of the bean and the child elements are mapped to other beans, which are part of the parent bean. If you know how to write an Ant task, you know how the generic parser works.
The generic parser is implemented by the interface XsSAXParser and implemented by the class XsSAXParserImpl. There's few things to know about that. The most important parts are some other interfaces and classes which you might like to implement or extend:
The AttributeSetter interface consists of a single method:
public void setAttribute(String pQName, String pNamespaceURI, String pLocalName, String pValue) throws SAXException;This method is called for any attribute that the SAX parser finds.
The AttributeSetters main idea is as follows: Assuming that the attributes local
name is foo, a method setFoo(SomeClass pValue)
is searched.
If such a method is found, the attribute value is converted into an instance of
SomeClass
and the method is invoked. More formally, the default
implementation
AttributeSetterImpl behaves as follows:
setAttribute(String, String, String, String)
,
it is invoked with the same arguments pQName
, pNamespaceURI
,
pLocalName
, pValue
. If this method does not have
the result types boolean
or Boolean
, or if the result
is true, then the AttributeSetterImpl
assumes that the
property is set.setProperty(String)
, then this method is invoked
with the attribute value.setProperty(T)
, and
the class T
has either of a method
public static T valueOf(String)
or a constructor
public T(String)
(in that order), then the method
setProperty(T)
is invoked with the value obtained
by an invocation of the method valueOf()
, or
the constructor, respectively. Note, that this applies in
particular to the classes Long, Integer,
Short, Byte, Double, Float,
java.math.BigInteger, java.math.BigDecimal,
java.io.File}, and StringBuffer.setProperty(boolean)
,
the method will be invoked with the value true
(the value specified in the XML file is either of
true
, or 1
) or false
(the attribute has any other value).setProperty(char)
,
or setProperty(Character)
, the method will be
invoked with the first character of the value specified in
the XML file. If the value contains zero or multiple characters,
an IllegalArgumentException is thrown.setProperty(long)
setProperty(int)
setProperty(short)
setProperty(byte)
setProperty(double)
setProperty(float)
java.lang.Class
, the
XsSAXParser
will interpret the value given in the
XML file as a Java class name and load the named class from its
class loader. If the class cannot be loaded, it will also try
to use the current threads context class loader. An
exception is thrown, if neither of the class loaders can
load the class.The TextSetter
interface is invoked from within the SAX ContentHandlers method
characters(char[] pBuffer, int pOffset, int pLen)
.
It's task is to fill the bean with character data. Note, that the latter
method may very well be called multiple times, even for a single
character sequence in the XML file. For example, if the XML reader
loads the XML file in blocks of 1024 characters and a block stops
right within an elements character data, then it is valid behaviour
to call the character(char[], int, int)
method twice:
Once for the first part, which sits at the end of the 1024 characters
and once for the remaining part. The same holds for the TextSetter
method
public void addText(String pValue);
The default implementation is TextSetterImpl, which behaves as follows:
public void addText(String)
, then the method is invoked.Embedded child elements are handled by the interface ChildSetter and its default implementation ChildSetterImpl. The interface exposes a single method:
public ContentHandler getChildHandler(String pQName, String pNamespaceURI, String pLocalName) throws SAXException;The purpose of the method is to create a SAX handler for this element. If such a handler is returned, then it receives the SAX events generated for the element. The default implementation works as follows:
public ContentHandler getChildHandler(String, String, String, String)
,
then this method is invoked. A non-null result will be used as a ContentHandler
for the child element.public T createFoo()
is searched, with an arbitrary result type T.
If there is such a method it is invoked and a new instance of
XsSAXParserImpl
is created to configure the bean.Back to Contents |