fromindex = 0;
}
if (fromindex == 0 && mIsInserted) {
return;
}
OdfWhitespaceProcessor textProcessor = new OdfWhitespaceProcessor();
int nodeLength = 0;
Node node = pNode.getFirstChild();
while (node != null) {
if (fromindex <= 0 && mIsInserted) {
return;
}
if (node.getNodeType() == Node.TEXT_NODE) {
nodeLength = node.getNodeValue().length();
if ((fromindex != 0) && (nodeLength < fromindex)) {
fromindex -= nodeLength;
} else {
// insert result after node, and insert an new text node
// after
// the result node
String value = node.getNodeValue();
StringBuffer buffer = new StringBuffer();
buffer.append(value.substring(0, fromindex));
// insert the text span in appropriate position
node.setNodeValue(buffer.toString());
Node nextNode = node.getNextSibling();
Node parNode = node.getParentNode();
Node newNode = node.cloneNode(true);
newNode.setNodeValue(value.substring(fromindex, value.length()));
if (nextNode != null) {
parNode.insertBefore(textSpan, nextNode);
parNode.insertBefore(newNode, nextNode);
} else {
parNode.appendChild(textSpan);
parNode.appendChild(newNode);
}
mIsInserted = true;
return;
}
} else if (node.getNodeType() == Node.ELEMENT_NODE) {
if (node.getLocalName().equals("s")) // text:s
{
try {
nodeLength = Integer.parseInt(((Element) node).getAttributeNS(OdfDocumentNamespace.TEXT.getUri(), "c"));
} catch (Exception e) {
nodeLength = 1;
}
fromindex -= nodeLength;
} else if (node.getLocalName().equals("line-break")) {
nodeLength = 1;
fromindex--;
} else if (node.getLocalName().equals("tab")) {
nodeLength = 1;
fromindex--;
} else {
nodeLength = textProcessor.getText(node).length();
insertSpan(textSpan, fromindex, node);
fromindex -= nodeLength;
}
}