xml.com: using xslt to fix swing
xml.com: using xslt to fix swing
articles | weblogs | newsletter | safari bookshelf
listen
print
discuss
subscribe to xml
using xslt to fix swing
by dave horlick
august 02, 2006
swing is a java framework for building cross-platform graphical user interfaces. many areas of the framework have recently enjoyed some compelling improvements. the portion that renders html hasn't been so lucky. when swing debuted, html was at a youthful version 3.2, and practices such as nestling paragraphs within tags could still be considered newfangled. today, they are familiar if not mandatory, and it's easy to consider it a bug when swing inserts an unrequested line break between a formally declared paragraph and its preceding headline.
one solution is to abandon swing's html renderer and jeditorpane in favor of another renderer, such as the javadesktop flying saucer project or a native html browser. this approach won't help if you are working with frameworks such as javahelp, which is hardwired to use swing's renderer.
luckily, there's a straightforward solution involving xslt.
screenshot of a jeditorpane incorrectly displaying html with an extra line break
extensible stylesheet language templates
xslt (extensible stylesheet language templates) is origami for xml. xslt can easily fold one kind of xml document into any other kind you can dream up. it accomplishes this by matching a set of templates to patterns of varying specificity and position in a target xml document. these templates introduce new patterns into an output document, constructed from information found in the target. this lends itself to very subtle and recursive use.
conveniently for us, java has had xslt built-in since j2se 1.4.
we'll need to format our html data as xhtml. xhtml is a flavor of xml and accordingly, is susceptible to the wiles of xslt.
<html>
<body>
line 1<br>
line 2<br>
line 3
<h3>my headline</h3>
<p>my paragraph</p>
</body>
</html>
→
<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
line 1<br/>
line 2<br/>
line 3
<h3>my headline</h3>
<p>my paragraph</p>
</body>
</html>
source.html
source.xhtml
as you can see, the only significant differences here are the xml header and the self-closing tags. if your source html has validity problems or you're just lazy, tagsoup is an excellent tool for automating the process.
using xslt, we can convert our original xhtml document into an html 3.2 representation that will look good in swing's html renderer.
the identity transform
we'll start with an identity transform that will more or less faithfully reproduce the structure of our original document.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
version="1.0">
<xsl:output method="html" version="3.2" />
<xsl:strip-space elements="*" />
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
the html method attribute in the xsl:output tag will prompt empty output tags to refrain from closing themselves. that's important, because the practice confounds swing's html renderer.
the * wildcard character in the match attribute of the first template will pull in a source element. initially, this will be the source document root. the stylesheet will copy the matched element to the output document. finally, the template will start over with another element or will release control to the next template for processing child attributes.
the @* character sequence in the second template's match attribute will target a source attribute. the stylesheet will copy the attribute into the output document.
in this manner, all elements and attributes will find their way over to the output document.
save the stylesheet above to a file named identity.xsl. pull up a command shell and type
java org.apache.xalan.xslt.process -in source.xhtml -xsl identity.xsl -out output.html
this should invoke a handy main method in xalan, java's default xslt implementation. if you're using a jdk more recent than 1.4, you may need to download a fresh copy of xalan and put it into your class path, because the more recent bundled versions seem to lack this interface on at least one platform.
review the output file and verify that your basic document structure is unchanged. in fact, it should closely resemble the pre-xhtml version.
pages: 1, 2, 3
next page
tagged articles
be the first to post this article to del.icio.us
sponsored resources
inside lightroom
contact us |
our mission |
privacy policy |
advertise with us |
| submissions guidelines
copyright © 2007
o'reilly media, inc.
Acceuil
suivante
xml.com: using xslt to fix swing Blog de giangi-fix - gian - Skyrock.com coding... and other random stuff - home of Bingo Caller Balai FIX-O-MAT Le balai - Webmarchand.com How to fix the Firefox memory leak (Firefox hack) « Internet Duct Tape FixTheLogo.com: Sports cherche board + fix pas cher ->plus2sport.com Traduction de la liste des fix 7.0.2 - Football Manager 2007 sur ... MG WinSock XP Fix 1.2 Welkom op de Homepage van C-Fix Hard Drive Data Recovery Software - Hard Drive Repair Is Easy With ... SPACE.com -- Astronauts Ready for Space Station Fix serious fix 4.1 Lispmeister.com Free advice on how to fix your bicycle いろんな言語でmemoizeとfix - メモ化と最小不動点の話題 固定電腦自由: 7 個指令- 文章 Konrad Hornschuch AG - d-c-fix, dcfix, skai Klebefolien ... BW Online March 19, 2001 How to Fix America's Schools PST repair software recovers deleted or corrupt mails of Microsoft ... A fix for Suckerfish dropdowns in IE 7 Daily Herald Officials promise quick fix to state's finance woes Fix the Future: What this site is all about. Quick Fix Meals with Robin Miller - Show List A to Z - TV - Food ... Boat Parts, Marine Engine Parts, Boat Accessories and Marine ... messin' link: Fix-up look sharp Rockentrip - COLDPLAY - Fix You The Fix Online - Comedy, Magazine, Films and Events Bug Fix: iTunes 7.5 Released, Download it Now Fix Resources on ZDNet sonnerie fix those guitarz (Sonnerie polyphonique fix those ... Annonces.com : Pc fix *neuf* acer acerpower fh - celeron d 360 ... Adobe - Photoshop : For Macintosh : Mac OS X Keyboard Shortcut Fix Mac Rumors: Apple Acknowledges iMac Freezing Issue, Fix In Works Lèvres Fix'Intense Tenue 10 h Brillance Ambre - Rouge à Lèvres ... Télécharger 433 eros fix up jamendo mp3 vbr 192k 2006 02 07 www ... Robert M. Ball - A Social Security Fix For 2008 - washingtonpost.com Decapitation Victim Tries To Fix Eyes - Health News Story - KGTV ... Blog de jan-fix - jan-fix.skyblog.com - Skyrock.com HTML FIX IT.COM: A site with free scripts, advanced scripts ... Dent Fix Equipment Daily Funny Fix / Media The Fix (Heroes) - Wikipedia, the free encyclopedia Flickr: FIX MY PIC Multi-Fix N.V. - Professional Signmaking Films signmaking vinyl ... Tom Tromey - Patch: FYI: Fix for PR 4859 Patch: fix for PR java.lang/339 Video: Your daily Tesla fix! - AutoblogGreen Traduction Garbage Fix Me Now lyrics - musique traduite Futurist: To fix education, think Web 2.0 Tech News on ZDNet Eric's Archived Thoughts: Framework Fix Clean PC Software :: Registry Fix It!, Error Scan and Fix, Free ... : : RegistrySmart : : NASA delays spacewalk to fix solar wing International Reuters NT4.0 too flawed to fix – official The Register Encyclopédie méthodique, ou par ordre de matières - Résultats Google Recherche de Livres Credit Repair by Credit Clean Help Repair Bad Debts :: SUSHI FIX RESTAURANTS :: WELCOME Fix Blogger Sitemap XML Warnings Inside Google Webmaster at ... fix tps Fix APO ? - forum Snowboard