Lately I've realised that the Analyser
code in X2J is messy, and wanted to treat it to a better design. For those numerous people out there who do not know the X2J code (yet!), I will offer some explanation:
The Analyser's task in X2J is to take a Plain Old Java Object (POJO) and analyse it into a ClassInfo
instance. This instance represents an xs:complexType
in XSD terms, and contains information regarding the type.
This information includes two collections: One for ElementInfo
instances, and one for AttributeInfo
instances (representing the xs:element
and xs:attribute
respectively).
These XXX-Info
instances have only getter methods for their properties; They are much like the Class instances Java provides, in a way.
The problem with Analyser
is that all the XXX-Info
creation logic is located inside it; Whenever new properties are added to ClassInfo
it would require changing Analyser
itself.
An idea comes to mind and it is called Visitors. Visitors allow separation between the object's inspection and the object's creation. The problem is, once an XXX-Info
is created, it can't be changed (getters only, remember?)
My proposition, and what I use in X2J, is something I call a "Mould". Internally, it's nothing more than a Properties instance.
What makes it special is that it is accompanied by a set of Visitors which change the mould information as they inspect the analysed class, and a Factory class which takes a mould and creates an XXX-Info
instance from it.
What more is that the Factory contains a "default mould", so that if visitors did not change certain values they will have a default value set in a pretty convinient way, and outside the Factory's logic.
Any remarks? Did I miss anything? I'd love to hear responses!
Related Topics
XML to POJOs Advatanges: @Converter
XML to POJOs Advantages: @PrimaryKey
Interesting Topics
XML to POJOs Example: Contacts List
Class Enum<E extends Enum<E>>