XHTML manipulation library in Java
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
,nodeP
are the corr开发者_运维问答espondingNode
instances of the below mentioned XHTML tree andnodeSpan
is some newly created node (whereNode
is not necessarilyorg.w3c.dom.Node
and 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
nodeToInsert
nodes are returned as operations result. - mode2: Close the parent nodes.
nodeToInsert
is 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:
************* BASE DOCUMENT *****************
DOCUMENT ROOT
|-<p >
| |-<b >
| | |-Text: GeForce
| |-Text: 9300M GS provides powerful
| |-<i >
| | |-Text: visual computing features
| |-Text: to thin and light notebooks.
*** Text ***
"GeForce 9300M GS provides powerful visual computing features to thin and light notebooks."
*** Node of each text segment ***
[b: null]: GeForce
[p: null]: 9300M GS provides powerful
[i: null]: visual computing features
[p: null]: to thin and light notebooks.
*** Offset testing ***
offset 0 is at [b: null] at 0
offset 40 is at [i: null] at 5
offset 80 is at [p: null] at 48
Asking it to split the element at the global position 4 will produce
*********** Split(4) DOCUMENT *****************
DOCUMENT ROOT
|-<p >
| |-<b >
| | |-Text: GeFo
| | |-Text: rce
| |-Text: 9300M GS provides powerful
| |-<i >
| | |-Text: visual computing features
| |-Text: to thin and light notebooks.
*** Node of each text segment ***
[b: null]: GeFo
[b: null]: rce
[p: null]: 9300M GS provides powerful
[i: null]: visual computing features
[p: null]: to thin and light notebooks.
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:
- Any document modification you may do, will make all the offsetts invalid. Using them later will cause corruption of the entire document. So, after each modification you must do GetOffset to retreive the offsetts again.
- I know some of the functions are not documented. Basically the only functions that should be used outside of the package are the ones you requested from the
StrXML
class. More documentation will be added later and you can contact me by email (see my profile page) for questions.
Maybe you could try jsoup - http://jsoup.org.
It is an open source Java library distributed under the MIT license. Its source code is available at GitHub.
From the home page:
jsoup is a Java library for working with real-world HTML. It provides a very convenient API for extracting and manipulating data, using the best of DOM, CSS, and jquery-like methods.
With jsoup you can:
- find and extract data, using DOM traversal or CSS selectors
- manipulate the HTML elements, attributes, and text
Here is its Javadoc: http://jsoup.org/apidocs/
I tried Jericho couple of years back it it was deceptively simple to use its API for parsing. I used it for logging into yahoo mail and fetching the contacts from the address book. I sure it can do much more than. The home page mentions one of your requirement "Flattering XHTML into plain text" as one of its features. Some of the features which might be relevant to your questions are
- Built-in functionality to extract all text from HTML markup
The begin and end positions in the source document of all parsed segments are accessible, allowing modification of only selected segments of the document without having to reconstruct the entire document from a tree.
And its Free open source. (Quoting the site :You are therefore free to use it in commercial applications subject to the terms detailed in either one of these licence documents.)
精彩评论