Does anyone have any suggestions as to how I could go about modelling html as java object(s)? The problem is this: I have some objects which I would like to render as a set of html pages. I would like to pass the objects to a renderer that would then take information from the objects and build html pages linking from one page to another where appropriate. My initial thought was to write some set of java objects to represent the various html elements with the ability to show themselves as html (ie toString()?). However, it seems like that may be a lot to hand code if I want to cover all (or most of) the elements supported by html. My next thought was along the lines of the first but to use some xml schema/java generation tool (ie jaxb or castor?) Any suggestions or help would be welcome. Also, is there anything like this out there already? I have tried a few searches and have not found anything. thanks
you may want to take a look at xslt and xpath. Those tools can render any xml (for instance html) differently depending on the stylesheet and make it either a java class or other. I am not sure if this is what you mean...
I'd go with rendering the objects as XML and then translating from there, perhaps with XSLT. Generated XML is more meaningful than HTML - it's logical markup rather than presentational markup. Later on in the project the XML may be useful - for persistence or translating into another format like PDF. In terms of system architecture, it depends on the structure of the objects you're translating from. If you have a composite structure (I'm thinking like composite pattern...), the visitor pattern is ideal generating a particular output. There are probably other good (better?) options but thats the way I'd approach it. Cheers,
posted 16 years ago
Thanks for the input. I defintely think I want the output to be XML but I think maybe I didn't explain clearly enough ... or don't completely understand the answer. The task is to generate HTML reports based on java objects. What I would like to do is pass the java object to some type of renderer which will know how and what to pull off of the object and build an HTML object. Not necessarily an HTML page or xml file but a java object that has members such as body whcih in turn might have member collections of other HTML 'element objects' such as <a href... If I could define a XML Schema for these HTML objects using something like Casstor, I could then simply marshall the objects to an XML file and then process it using XSLT. Does it make sense? Is this a crazy idea? thanks again
Again I think I have not gotten my ont across accurately. I understand XHTML and HTML. I do not intend to reinvent XHTML. What I would like to do is use the XHTML schema to generate java objects which represent HTML elements. Then I can marshall these objects to XML which would be actually be XHTML. I have tweaked the XHTML schema enough to generate the HTML Java objects. So now I have runtime objects such as Html and Body which I can load with data ... but Iam not sure the marshall method is going to give me the pages I want.
You might also want to take a look at some of the html-templating languages for Java, like Velocity or Freemarker. These are especially tailored for creating html representations of java objects, the advantages over XML/XSL being simpler to use and no need to convert your java objects to XML first (a disadvantage is that they are more java-platform dependent). You could f.e. just have a generic renderer object that takes a javabean (the model) and a template string/stream (the view) and generates the html.
Have you looked at JAXB? It's sole purpose in life is to be able to take XML and map it to an object graph, or vice versa. After that consider something like Jakarta Cacoon that can take an XML data document and using XSLT in it's framework, with a set of renderers, pump out all sorts of views/formats (e.g. wml, hdml, pdf, html etc.). [ March 26, 2004: Message edited by: Byron Estes ]