Programmatic HTMLDocument generation using Java

2020-06-12 04:19发布

Does anyone know how to generate an HTMLDocument object programmatically in Java without resorting to generating a String externally and then using HTMLEditorKit#read to parse it? Two reasons I ask:

Firstly my HTML generation routine needs to be very fast and I assume that parsing a string into an internal model is more costly than directly constructing this model.

Secondly, an object-oriented approach would likely result in cleaner code.

I should also mention that, for licensing reasons, I can't resort to using any libraries other than those shipped with the JVM.

Thanks, Tom

9条回答
Rolldiameter
2楼-- · 2020-06-12 04:40

You may want to build some Element object with a render() method, and then assemble them in a tree structure; with a visit algorhytm you may then proceed to set the values and then render the whole thing.

PS: have you considered some templating engine like freemarker?

查看更多
Animai°情兽
3楼-- · 2020-06-12 04:40

It appears that you can accomplish what you are attempting using direct construction of HTMLDocument.BlockElement and HTMLDocument.BlockElement objects. Theses constructors have a signature that suggests direct use is possible, at least.

I would suggest examining the Swing sources in OpenJDK to see how the parser handles this, and derive your logic from there.

I would also suggest that this optimization may be premature, and perhaps this should be a speed-optimized replacement for a simpler approach (i.e. generating the HTML text) only introduced if this really does become a performance hotspot in the application.

查看更多
甜甜的少女心
4楼-- · 2020-06-12 04:45

One object-oriented approach is to use a library called ECS.

It is quite simple library, and has not changed for ages. Then again, the HTML 4.01 spec has not changed either ;) I've used ECS and consider it far better than generating large HTML fragments with just Strings or StringBuffers/StringBuilders.

Small example:

Option optionElement = new Option();
optionElement.setTagText("bar");
optionElement.setValue("foo");
optionElement.setSelected(false);   

optionElement.toString() would now yield:

<option value='foo'>bar</option>

The library supports both HTML 4.0 and XHTML. The only thing that initially bothered me a lot was that names of classes related to the XHTML version started with a lowercase letter: option, input, a, tr, and so on, which goes against the most basic Java conventions. But that's something you can get used to if you want to use XHTML; at least I did, surprisingly fast.

查看更多
三岁会撩人
5楼-- · 2020-06-12 04:47

Basically you can insert html into your HTMLDocument using one of the insert methods, insertBeforeEnd(), insertAfterEnd(), insertBeforeStart(), insertAfterStart(). You supply the method with the html you want to insert and the position in the document tree that you want the html inserted.

eg.

doc.insertBeforeEnd(element, html);

The HTMLDocument class also provided methods for traversing the document tree.

查看更多
我欲成王,谁敢阻挡
6楼-- · 2020-06-12 04:49

When dealing with XHTML, I have had much success using Java 6's XMLStreamWriter interface.

OutputStream destination = ...;
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter xml = outputFactory.createXMLStreamWriter(destination);

xml.writeStartDocument();
xml.writeStartElement("html");
xml.writeDefaultNamespace("http://www.w3.org/1999/xhtml");

xml.writeStartElement("head");
xml.writeStartElement("title");
xml.writeCharacters("The title of the page");
xml.writeEndElement();
xml.writeEndElement();

xml.writeEndElement();
xml.writeEndDocument();
查看更多
▲ chillily
7楼-- · 2020-06-12 04:50

You can use any decent xml library like JDom or Xom or XStream. Html is just a special case of XML.

Or, you can use one of the existing templating engines for server side java like jsp or velocity.

查看更多
登录 后发表回答