offset += 1; // restore offset.
}
}
}
if (region == null) {
return new DOMPosition(model.getDocument(), 0);
}
IDOMNode node = (IDOMNode) region;
int start = node.getStartOffset();
if (offset <= start) {
return new DOMRefPosition(node, false);
}
int end = node.getEndOffset();
if (offset >= end) {
return new DOMRefPosition(node, true);
}
if (node instanceof CharacterData) {
String data = ((CharacterData) node).getData();
String source = node.getSource();
if (data.equals(source)) {
return new DOMPosition(node, offset - start);
}
IStructuredDocumentRegion r = node
.getFirstStructuredDocumentRegion();
int countedData = 0;
// TODO: dead? int offsetInNode = offset - start;
while (r != null) {
if (DOMRegionContext.XML_CHAR_REFERENCE.equals(r.getType())
|| DOMRegionContext.XML_ENTITY_REFERENCE.equals(r
.getType())) {
countedData += 1; // FIXME: what if the entity reference's
// corresponding data is more than 1
// char?
// where can we get that information?
if (r.getEnd() >= offset) {
return new DOMPosition(node, countedData);
}
} else {
if (r.getEnd() >= offset) {
return new DOMPosition(node, countedData + offset
- r.getStart());
}
countedData += r.getLength();
}
r = r.getNext();