I wrote a class extending JEditorPane that I thought should allow for
"safer" HTML rendering, but on occasions I still get an
ArrayIndexOutOfBoundsException: -1 or EmptyStackException when trying
to use setText().
Here is the exception stack:
java.util.EmptyStackException
at java.util.Stack.peek(Stack.java:85)
at javax.swing.text.DefaultStyledDocument
$ElementBuffer.insertElement(DefaultStyledDocument.java:1667)
at javax.swing.text.DefaultStyledDocument
$ElementBuffer.insertUpdate(DefaultStyledDocument.java:1405)
at javax.swing.text.DefaultStyledDocument
$ElementBuffer.insert(DefaultStyledDocument.java:1275)
at
javax.swing.text.DefaultStyledDocument.insert(DefaultStyledDocument.java:
199)
at javax.swing.text.html.HTMLDocument.insert(HTMLDocument.java:
242)
at javax.swing.text.html.HTMLDocument
$HTMLReader.flushBuffer(HTMLDocument.java:3325)
at javax.swing.text.html.HTMLDocument
$HTMLReader.addContent(HTMLDocument.java:3265)
at javax.swing.text.html.HTMLDocument
$HTMLReader.addContent(HTMLDocument.java:3230)
at javax.swing.text.html.HTMLDocument
$HTMLReader.handleText(HTMLDocument.java:2180)
at
javax.swing.text.html.parser.DocumentParser.handleText(DocumentParser.java:
241)
at javax.swing.text.html.parser.Parser.handleText(Parser.java:
345)
at javax.swing.text.html.parser.Parser.endTag(Parser.java:427)
at javax.swing.text.html.parser.Parser.parseTag(Parser.java:
1807)
at
javax.swing.text.html.parser.Parser.parseContent(Parser.java:2044)
at javax.swing.text.html.parser.Parser.parse(Parser.java:2211)
at
javax.swing.text.html.parser.DocumentParser.parse(DocumentParser.java:
105)
at
javax.swing.text.html.parser.ParserDelegator.parse(ParserDelegator.java:
73)
at javax.swing.text.html.HTMLEditorKit.read(HTMLEditorKit.java:
230)
at javax.swing.JEditorPane.read(JEditorPane.java:556)
at javax.swing.JEditorPane.read(JEditorPane.java:584)
at javax.swing.JEditorPane$PageLoader.run(JEditorPane.java:
647)
The page loads just fine (albeit very VERY slowly), but these annoying
exceptions occur frequently and I have no way of catching them.
All this to ask: Is Java reliable for HTML rendering or should I bag
the idea that Java can do this?
Phil
The problem is not with Java the language, but with the component
JEditorPane. More specifically, the problem is in the default
HTMLEditorKit that is used by JEditorPane to render the HTML. As it
says in the docs, the HTMLEditorKit only supports HTML 3.2, which is
pretty inadequate nowadays.
Instead of extending JEditorPane, you should probably look into third-
party packages that are meant to handle this problem. I can't
personally recommend any good free ones, but you can take a look at
this website, which reviews a few: http://today.java.net/pub/a/today/2004/05/24/html-pt1.html.
That website is pretty old (3 years) so you should just do a Google
search for "Java HTML renderer" and take a look at what comes up.
Java, the programming language, is just as suitable as any other
programming language for HTML rendering (see
http://en.wikipedia.org/wiki/Turing_complete).
One specific piece of code written in Java (e.g. JEditorPane) may be
bad at rendering HTML, just like one specific piece of code written in any
language might be bad at rendering HTML.
You may be interested in http://jrex.mozdev.org/ among other Java HTML
projects.
- Oliver