I am looking for XML/XHTML Java library/framework that can perform the following two tasks for me.
Before going on few definitions:
NodeOffset(Node node, int offset)marks some point in text node in the XML tree.nodeB,nodeI,nodePare the correspondingNodeinstances of the below mentioned XHTML tree andnodeSpanis some newly created node (whereNodeis not necessarilyorg.w3c.dom.Nodeand may be any other abstraction)
Flattering XHTML into plain text
The library should be able to produce plaintext output (e.g. by implementing CharSequence or similar) from given XHTML and provide one-to-one mapping between chars in the output and original XHTML node tree (e.g. via the function NodeOffset getNodeOffset(int plainTextOffset)).
Example: Suppose we have the following XHTML:
<p><b>GeForce</b> 9300M GS provides powerful <i>visual computing features</i> to thin and light notebooks.</p>
Then the plaintext representation will obviously be:
GeForce 9300M GS provides powerful visual computing features to thin and light notebooks.
Then e.g.
getNodeOffset(0)should return nodeNodeOffset(nodeB, 0)getNodeOffset(40)should return nodeNodeOffset(nodeI, 5)getNodeOffset(80)should return nodeNodeOffset(nodeP, 49).
I might miss the correct numbers, but I hope, you got the idea. I repeat the example, now with pseudo-markers inserted:
|GeForce 9300M GS provides powerful visua|l computing features to thin and light n|otebooks.
and
<p><b>|GeForce</b> 9300M GS provides powerful <i>visua|l computing features</i> to thin and light n|otebooks.</p>
Node manipulating
The library should provide a possibility to inject nodes into XHTML, that may span the tree possibly crossing the node boundaries e.g. via the operation NodeSet insert(Node nodeToInsert, NodeOffset start, NodeOffset end, int mode). The function works in two modes:
- mode1: Split the node to be inserted if necessary. In this case the splitted from
nodeToInsertnodes are returned as operations result. - mode2: Close the parent nodes.
nodeToInsertis returned as is.
For example: the insert(nodeSpan, NodeOffset(nodeB, 2), NodeOffset(nodeP, 9), mode1) operation should produce
<p><b>Ge<span>Force</span></b><span> 9300M GS</span> provides powerful <i>visual computing features</i> to thin and light notebooks.</p>
insert(nodeSpan, NodeOffset(nodeB, 2), NodeOffset(nodeP, 9), mode2) operation should produce:
<p><b>Ge</b><span><b>Force</b> 9300M GS</span> provides powerful <i>visual computing features</i> to thin and light notebooks.</p>
It is analogue to what users do in rich editor:
GeForce 9300M GS
I wonder, if there is anything like this in OpenSource world, as I really don’t want to re-implement the wheel… I’ve checked quickly Open Source HTML Parsers in Java without success.
When you post an answer:
- Make sure the above mentioned functions are available in library API (provide a link to JavaDoc).
- The library is Java-native (no JNI) and OpenSource.
I wrapped code that I had already, with modifications to match your requests (WIP) in an open-source project: ShtutXML. It’s pretty documented, so I doubt you’ll have a problem using it.
The first request (Finding a node and offsets from a global position) is already built in, and splitting of text nodes in the XML is already built in (so you can easily wrap them in new nodes as you wish). Therefore, adding the logic for marking areas with an element is rather trivial. I’ll try to do it later, but this is my best effort on this request for now.
On your XML, using my example program this is my output:
Asking it to split the element at the global position 4 will produce
Of course this syntactical split means nothing for the actual XML code that matches that document, but it will allow wrapping one text part at a time with any other node you wish.
Edit: The first insertion mode is already supported
Edit 2: The second insertion mode is already supported
Notes:
StrXMLclass. More documentation will be added later and you can contact me by email (see my profile page) for questions.