}
}
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();
}
return new DOMRefPosition(node, true);
} else if (node instanceof Element) {
CMElementDeclaration cm = CMUtil
.getElementDeclaration((Element) node);
if (cm != null && cm.getContentType() == CMElementDeclaration.EMPTY) {
// this node can't have children.
return new DOMRefPosition(node, true);
}
IStructuredDocumentRegion startRegion = node
.getStartStructuredDocumentRegion();
if (startRegion == null) {
return new DOMRefPosition(node, true);
}
int startRegionEnd = node.getStartStructuredDocumentRegion()
.getEnd();
if (offset <= startRegionEnd) {
// it is in the start tag region. So put position at first
// child position.
return new DOMRefPosition2(node, false);