Can't get hold of Content object

Having trouble installing Oxygen? Got a bug to report? Post it all here.
sijomon
Posts: 83
Joined: Wed May 20, 2009 1:18 pm

Can't get hold of Content object

Post by sijomon »

Hi,

I am attempting to select the text content of the current node's text nodes and only the current nodes, so no text content from child nodes is required. I assumed I could get the node's children using AuthorNode.getContentNodes() and then collect together the text content of all the direct children where child.getType() == AuthorNode.NODE_TYPE_CDATA, however the API doc for getContentNodes() says:
Returns:
The list that contains all children of this node without text nodes.
A bit more reading finds that for any given node you can get that node's text content by asking for the offset into a mysterious 'Content' object. This makes sense, but there is no documentation on how to get access to this Content object. The Content interface docs don't give any implementing classes, and going through the API docs from AuthorAccess down I can't find any methods that create or return an Object of this type.

Can you let me know how I can access ther Content object.

Thanks,

Simon.
Radu
Posts: 9439
Joined: Fri Jul 09, 2004 5:18 pm

Re: Can't get hold of Content object

Post by Radu »

Hi Simon,

The Content object can only be obtained for an AuthorDocumentFragment.
For Oxygen 11 we added a new method in the API for getting the text between two offsets like:

Code: Select all


  /**
* The content represents the entire text content of the Author page + additional markers/sentinels
* at offsets which are pointed to by the AuthorNodes.
* Each AuthorNode points to specific start and end character markers in the content.
* The start and end offsets pointed to by the AuthorNode can be retrieved using the
* AuthorNode.getStartOffset() and AuthorNode.getEndOffset()
*
* <br/>
* <img src="node/AuthorDocumentFragmentArchitecture.gif"/>
* <br/>
*
* Retrieves a portion of the content into the specified {@link Segment}.
*
* @param where The starting position >= 0, where + len <= length()
* @param len The number of characters to be retrieved >= 0
* @param chars The {@link Segment} object to return the characters int.o
* @exception BadLocationException If the specified position or length are invalid.
*/
public void getChars(int where, int len, Segment chars) throws BadLocationException;
For now, one way to proceed is to do the following:

Code: Select all


	/**
* Get the text content of this element + the text between its children.
* @param authorAccess The author access
* @param node The node
* @return The text content
* @throws BadLocationException
*/
public static String getElementFirstLevelText(AuthorAccess authorAccess, AuthorNode node) throws BadLocationException {
String text = "";
if (node.getType() == AuthorNode.NODE_TYPE_ELEMENT) {
AuthorElement elem = (AuthorElement) node;
int startText = elem.getStartOffset() + 1;
int endText = elem.getEndOffset() - 1;
List<AuthorNode> contentNodes = elem.getContentNodes();
for (int i = 0; i < contentNodes.size(); i++) {
// Pick text between the child siblings
AuthorNode child = contentNodes.get(i);
if (startText < child.getStartOffset() - 1) {
// Text between last offset and start of child offset
AuthorDocumentFragment contentFrag = authorAccess
.getDocumentController().createDocumentFragment(
startText, child.getStartOffset() - 1);
text+= contentFrag.getContent().getString(0,
contentFrag.getContent().getLength());
}
startText = child.getEndOffset() + 1;
}

// Maybe some text after the last child
if (startText < endText) {
// Text between last offset and start of child offset
AuthorDocumentFragment contentFrag = authorAccess
.getDocumentController().createDocumentFragment(
startText, endText);
text += contentFrag.getContent().getString(0,
contentFrag.getContent().getLength());
}
} else {
int so = node.getStartOffset();
int eo = node.getEndOffset();
// Comment or CDATA or PI, its whole content is text.
if (so + 1 < eo) {
AuthorDocumentFragment contentFrag = authorAccess
.getDocumentController().createDocumentFragment(so + 1,
eo - 1);
text+= contentFrag.getContent().getString(0,
contentFrag.getContent().getLength());
}
}
return text;
}
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
sijomon
Posts: 83
Joined: Wed May 20, 2009 1:18 pm

Re: Can't get hold of Content object

Post by sijomon »

Thanks for the response - fast and helpful - as ever :)

I'll try using the method you suggest, I think with slight modification it will do exactly what I'm after.

Many Thanks,

Simon.
Post Reply