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 [Profil de fix] OverBlog - Le blog des blogs Fix Your Money Screw-Ups - Kiplinger.com frontline: the wall street fix PBS Portail Internet de la Haute Autorité de santé - H-FIX PDS Your Freebie Fix - All the latest Freebies, Coupons and Online Deals はてなブックマーク - FIX Bug Fix Weekend finished :: pnCommunity :: Support at your fingertips Windows Mobile 5.0 Fix Site - Home Tena Fix - Incontinence Google to fix blog noise problem The Register BBC/OU Open2.net - Can Gerry Robinson Fix The NHS? Two charged with hacking PeopleSoft to fix grades - Network World Bike Shop Frederick, Maryland Bike Repairs, Bike Sales --BICYCLE ... Object Fix Zip - Freeware for repairing damaged ZIP archives with ... Ajaxian » IE’s Memory Leak Fix Greatly Exaggerated IPython fix for Leopard - O'Reilly ONLamp Blog Fix all Ajax cross-browser problems then deploy Wiki Autrans - Fix FIX-IT - bedrijvengids - handelsgids - webdesign - Pc repair ... PC Fix Error Doctor Registry Cleaner PC Diagnostics PC Checkup SF Gate: Columnists: Mark Morford Archive PNG in Internet Explorer: How to Use Registry Repair, Clean Up & File Fix for Windows Markdown Fix Histoire philosophique et politique des établissemens et du ... - Résultats Google Recherche de Livres How not to fix HTML ¶ Personal Weblog of Joe Clark, Toronto Madeleine Fix-Hansen :: Design :: Illustration :: Media ... MacNN Apple updates iMac fix for Tiger users MacNN Apple updates iMac fix for Tiger users Acheter Housse pouf Sit Fix... avec eco-SAPIENS Mr. Fix It (2006) KompoZer - Easy web authoring Rob Galbraith DPI: EOS-1D Mark III sub-mirror fix announced in USA ... Indonesia's three divas fix the nation's finances International ... Objet Publicitaire : Magnet Magic fix - ALB01.com Windows Vista Team Blog : Partners helping fix Vista Software ... FOSSwire » Fix a Frozen System with the Magic SysRq Keys Why Blog Post Frequency Does Not Matter Anymore Marketing Profs ... serious fix 4.1 How to Fix CGI - majordojo GRC CIH Virus Recovery You receive an access violation error and the system may appear to ... FIX: Update to enable DirectX Video Acceleration (DXVA) of Windows ... Video Coldplay - Fix You - coldplay, fix, you, clip ... Free Registry Fix 3.9 for Windows Solar shield could be quick fix for global warming - earth - 05 ... adaptive path » 8 quick ways to fix your search engine Fix for COM Surrogate Has Stopped Working Error in Vista :: the ... Oral Fixation Mints / Get Your Fix Nail Fungus Onychomycosis - Fix My Fungus Pierre Fix-Masseau affiches sur AllPosters.fr Fixit Guide Series - DIY Mac & iPod Repair Dura Fix Aluminum Welding Aluminum Brazing Aluminum Soldering ... The Simple Dollar » 31 Days To Fix Your Finances: A Wrapup Gallery 2.2.3 Security Fix Release Gallery What To Fix VCOM: V Communications. Security, Web, OS Management, Partitioning ... Fix for securityd hogging RAM when reauthorizing apps' Keychain ... Cafe Hayek: Just Fix It DriverAgent.com Fix Your Driver Problems Instantly with Driver Agent