 |
HTML Builder
Posted by evanx on January 25, 2007 at 05:31 AM | Comments (2)
Having defined style objects in the
Hyper Style prequel,
we build an "HTML builder"
to generate HTML/CSS, eg. for this series
of articles using quitehyper.dev.java.net.
Click here to read "HTML Builder"
from the "Hyper Beans" part of a trilogy in 42 parts"
Code Snippet
public class QHyperArticleHtmlBuilder extends QHtmlBuilder {
QStyle anchorStyle = createStyle("anchorStyle", a);
QStyle sectionStyle = createStyle("sectionStyle", div);
QStyle subsectionStyle = createStyle("subsectionStyle", div);
...
public String buildSubSectionHeading(String anchorName, String text) {
return buildAnchorHeading(subsectionStyle, anchorName, text);
}
protected String buildAnchorHeading(QStyle divStyle,
String anchorName, String text) {
QMutableElement element = create(p);
element.add(br);
element.add(create(a, anchorStyle, name.create(anchorName)))
.add(create(div, divStyle, text));
return element.buildHtml();
}
public String buildLink(String url, String label) {
QMutableElement element = create(a, anchorStyle, href.create(url));
element.add(create(span, underlineStyle))
.add(create(span, linkStyle, label));
return element.buildHtml();
}
}
where QHtmlBuilder defines the "immutable HTML elements" eg. pre, div, p, et al,
from which we create mutable elements, with attributes (eg. name, href, et al),
a given style ie. CSS class, child elements, and/or text content.
Besides HTML fragments for sections headings and what-not,
we get out the CSS text as follows.
<style>
pre.javaStyle {
font-family: courier new, courier, mono;
background-color: #fbfbfb;
font-size: 11pt;
width: 800px;
border: dashed 1px;
border-color: lightgray;
padding-left: 4px;
}
...
</style>
Bookmark blog post: del.icio.us Digg DZone Furl Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment
-
few months ago I worked in a project that generates HTML contents through servlets. It was an old project, without JSP or other templates, just a set of classes assembling the HTML directly into the OutputStream of the servlet request. It works fine, and the code presented an excellent performance, but, the maintenance of that was a totally boring task. So, I downloaded the W3C Schema of the XHTML and I created the its respective Java classes using JAXB 2 (xjc). All classes were created without problems, and I created also a small test project to generate HTML programatically.It worked fine, the HTML generated was compliance with the W3C specification and the way to program was a bit more elegant.The problems of my approach started when I needed to produce huge HTML files, i.e., Html pages with more than a hundred of tags.. each tag with several attributes...As you can imagine, it exploded the memory in seconds - the amount of memory required to produce such HTML files removed my enthusiasm about good design in the HTML generation by code.
So, just a tip: copy and paste hundred of tags in your generator code and do some memory evaluation.. just to check if it is feasible :)
Posted by: felipegaucho on January 26, 2007 at 01:11 AM
-
thanks felipe, that's a good tip, to check the performance. Hey that's a neat trick to use JAXB like that :)
where i am going with this, is defining styled documents in a format-neutral and programmable way namely in java, eg. for reports from database queries, and then you can generate HTML, PDF and/or Excel.
the advantage i found doing this before (in a heavy BI production environment), is that java is comfortable and convenient, ie. using IDE with auto-completion, error highlighting and such. But the reports were typically only a few pages long.
The user captures query details in HTML form (eg. products, stores, et al in a retail stock environment), we run the query (sometimes against multiple databases, in parallel threads, collating the results).
Interestingly the SQL queries used similar approach, where we define the SQL using java objects, and generate SQL, like we define HTML tags here as java objects, and generate HTML.
I wrote about this approach for "native queries" in "Dog House" article aka "Bean Curd 1"
Anyway, so user wants to see the query result as a draft webpage first, then maybe tweak the query parameters. Finally, they might also want a printable HTML version, PDF version, and/or Excel version of that report that they are previewing as web page with editable query parameters. This might mean customising the style, eg. where one can extend the java class and customise it in an OO fashion.
but i never got that far - i got as far as outputting either the HTML webpage, and alternatively a PDF version using iText. It was running on a "big" server eg. with 4Gb, and i never worried about memory usage. Actually the big bottleneck was running queries across over 100 remote databases before i could collate and present the results!
ps. another option to get PDF would be to generate XHTML and use flying saucer to emit PDF.
ps. i see that wicket web framework uses java objects to generate HTML - so that would be my choice if i was a web developer. Actually i lie, i would use echo2 cos its like Swing ;)
Posted by: evanx on January 26, 2007 at 02:16 AM
|