changing the behaviour of the HTMLEditorKit

R

Rogan Dawes

Hi folks,

I am trying to use the default Swing JEditorPane to display HTML.
However, I don't want to fetch any additional resources referenced in
the HTML from the network.

e.g. if the HTML contains a frameset, I don't want to fetch the
individual frames. If it contains references to Images, I don't want to
fetch the images. etc, etc, etc.

I have managed to deal with the <img> tags, and the <frame> tags with
the following code, simply not displaying them at all:

private static class MyHTMLFactory extends HTMLEditorKit.HTMLFactory {
public View create(Element elem) {

Object o = elem.getAttributes().getAttribute(
StyleConstants.NameAttribute);
if (o instanceof HTML.Tag) {
HTML.Tag kind = (HTML.Tag) o;
if (kind == HTML.Tag.FRAME || kind == HTML.Tag.FRAMESET
|| kind == HTML.Tag.OBJECT || kind == HTML.Tag.IMG
|| kind == HTML.Tag.APPLET) {
return new NoView(elem);
}
}
return super.create(elem);
}
}

private static class NoView extends View {
public NoView(Element elem) {
super(elem);
setSize(0.0f, 0.0f);
}

public int viewToModel(float fx, float fy, Shape a,
Position.Bias[] bias) {
return 0;
}

public Shape modelToView(int pos, Shape a, Position.Bias b)
throws BadLocationException {
return new Rectangle(0, 0);
}

public float getPreferredSpan(int axis) {
return 0.0f;
}

public void paint(Graphics g, Shape allocation) {
}


However, I am now struggling to deal with a <body> tag that has a
"background" attribute. I obviously want to show the body, so I can't
use the NoView approach. I tried extending BodyBlockView, but it is not
visible from my class. I could simply copy and paste BodyBlockView into
my own class, but I can't actually see anything that retrieves and
renders the background image anyway. And as you go deeper, the more
classes you have to reimplement.

I tried casting the Element to BlockElement, and checking the
AttributeSet. I found the background attribute, and tried to remove it,
but the AttributeSet is not mutable.

I have also tried overriding JEditorPane.getStream(URL):

private class NoNetEditorPane extends JEditorPane {

/**
* Fetches a stream for the given URL, which is about to
* be loaded by the <code>setPage</code> method. By
* default, this simply opens the URL and returns the
* stream. This can be reimplemented to do useful things
* like fetch the stream from a cache, monitor the progress
* of the stream, etc.
* <p>
* This method is expected to have the the side effect of
* establishing the content type, and therefore setting the
* appropriate <code>EditorKit</code> to use for loading the stream.
* <p>
* If this the stream was an http connection, redirects
* will be followed and the resulting URL will be set as
* the <code>Document.StreamDescriptionProperty</code> so that
* relative URL's can be properly resolved.
*
* @param page the URL of the page
*/

protected InputStream getStream(URL page) throws IOException {
throw new IOException("We do not support network traffic");
}
}

but this doesn't seem to have any effect at all.

Does anyone have any suggestions on how one can (to provide a more
likely justification for wanting to do this) properly implement a
caching strategy for JEditorPane and HTMLEditorKit? In fact, this is
what I'd prefer if at all possible, rather than just not showing the
content at all.

Or is it just so badly designed/implemented that such a thing is impossible?

Many thanks.

Rogan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top