* @param element The element to update, must not be <code>null</code>.
* @param value The text string to set, must not be <code>null</code>.
*/
private void rewriteValue( Element element, String value )
{
Text text = null;
if ( element.getContent() != null )
{
for ( Iterator<?> it = element.getContent().iterator(); it.hasNext(); )
{
Object content = it.next();
if ( ( content instanceof Text ) && ( (Text) content ).getTextTrim().length() > 0 )
{
text = (Text) content;
while ( it.hasNext() )
{
content = it.next();
if ( content instanceof Text )
{
text.append( (Text) content );
it.remove();
}
else
{
break;
}
}
break;
}
}
}
if ( text == null )
{
element.addContent( value );
}
else
{
String chars = text.getText();
String trimmed = text.getTextTrim();
int idx = chars.indexOf( trimmed );
String leadingWhitespace = chars.substring( 0, idx );
String trailingWhitespace = chars.substring( idx + trimmed.length() );
text.setText( leadingWhitespace + value + trailingWhitespace );
}
}