I have a need to export an object hierarchy to several different
formats (xml, pdf, msword, etc). Each format will contain unique
information about the obejcts. For instance, the word doc might contain
information about the object's depth in the structure while the pdf
version omits this info.
I was trying to play around with serialization (as opposed to
toString()), so that I could encapsulate the unique behaviors in
decorator classes. That way, to change the protocl, all I need to do is
change the decorator. If I need a new protocol, then the only existing
code that gets changed is the decorator factory and the client making
use of the factory. The underlying algorithm should be unaltered.
I've had somewhat limit success with Externalizable. thoughts? ideas?
am i smoking crack?
Chris Uppal - 08 Dec 2006 15:51 GMT
> I have a need to export an object hierarchy to several different
> formats (xml, pdf, msword, etc). [...]
>
> I was trying to play around with serialization
I really don't see what serialization has to do with this.
Given your stated requirements, it's not completely clear that there would be
any significant gain in sharing code between the different formats (if you have
to write different information, in a different order, in a different form then
there's not too much left that's the same ;-). If that's the case then just
write as many kinds of document writer as you need (there may be some shared
code in opening files, and so on, but not a lot).
If, there /is/ more sharable code -- say they all would transverse the object
network in the same order, then it seems reasonable to factor that out. One
way would be to have an object who's job it was to traverse the network in some
defined order, issuing callbacks to a formatting/writing object as it went.
You can make that as simple or fancy as you like. E.g the Traverser might keep
some sort of registry to previously written data, so the Writer could make
back-references to previously seen stuff. Or the API between Traverser and
Writer might be rich enough that the Writer could influence the traversal
order.
-- chris
Daniel Pitts - 08 Dec 2006 19:11 GMT
> I have a need to export an object hierarchy to several different
> formats (xml, pdf, msword, etc). Each format will contain unique
[quoted text clipped - 11 lines]
> I've had somewhat limit success with Externalizable. thoughts? ideas?
> am i smoking crack?
I say its the crack....
What you probably want to look into is MVC.
You have a model, and I assume some sort of controller, what your
stated requirements ask for is a set of views. and XML view, a PDF
view, etc... Each view should be implemented to be independant of the
Controller (Application logic manipulating the Model), and should only
rely on observable properties of the Model (the Model, in good design,
should handle domain logic, and only expose what is necessary for the
View to render.)
The Model shouldn't be dependent on either the Controller or any of the
View's.
Look into templating technology to implement your Views. If this is a
webapp, JSP's could work nicely. Also look into Velocity or
Freemarker. You might also want to look into existing PDF generating
API's.
Hope this helps.
- Daniel.
Andrew Thompson - 09 Dec 2006 01:52 GMT
> I have a need to export an object hierarchy to several different
> formats (xml, pdf, msword, etc). ....
Had you considered producing just a single XML,
then using XSLT to produce the PDF, DOC and other
(cut-down) XML files?
Andrew T.