*
*/
void normalize()
{
XmlCursor curs = newCursor();
TokenType tt = curs.currentTokenType();
// Walk through the tokens removing empty text nodes and merging adjacent text nodes.
if (tt.isStartdoc())
{
tt = curs.toFirstContentToken();
}
if (tt.isContainer())
{
int nestLevel = 1;
String previousText = null;
while (nestLevel > 0)
{
tt = curs.toNextToken();
if (tt == XmlCursor.TokenType.TEXT)
{
String currentText = curs.getChars().trim();
if (currentText.trim().length() == 0)
{
// Empty text node, remove.
removeToken(curs);
curs.toPrevToken();
}
else if (previousText == null)
{
// No previous text node, reset to trimmed version
previousText = currentText;
}
else
{
// It appears that this case never happens with XBeans.
// Previous text node exists, concatenate
String newText = previousText + currentText;
curs.toPrevToken();
removeToken(curs);
removeToken(curs);
curs.insertChars(newText);
}
}
else
{
previousText = null;
}
if (tt.isStart())
{
nestLevel++;
}
else if (tt.isEnd())
{
nestLevel--;
}
else if (tt.isEnddoc())
{
// Shouldn't get here, but just in case.
break;
}
}