Node node = (Node) region;
if (START_TAG.equals(selectionStrategy)) {// then we want to
// underline the opening tag
if (node.getNodeType() == Node.ELEMENT_NODE) {
IDOMElement element = (IDOMElement) node;
startEndPositions[0] = element.getStartOffset() + 1;
startEndPositions[1] = startEndPositions[0] + element.getTagName().length();
}
}
else if (END_TAG.equals(selectionStrategy)) {// then we want to
// underline the end tag
if (node.getNodeType() == Node.ELEMENT_NODE) {
IDOMElement element = (IDOMElement) node;
startEndPositions[0] = element.getEndStartOffset();
startEndPositions[1] = element.getEndOffset();
}
}
else if (ATTRIBUTE_NAME.equals(selectionStrategy)) { // in
// underline the attribute's name
if (node.getNodeType() == Node.ELEMENT_NODE) {
IDOMElement element = (IDOMElement) node;
IDOMNode attributeNode = (IDOMNode) (element.getAttributeNode(nameOrValue));
if (attributeNode != null) {
startEndPositions[0] = attributeNode.getStartOffset();
startEndPositions[1] = attributeNode.getStartOffset() + nameOrValue.length();
}
}
}
else if (ATTRIBUTE_VALUE.equals(selectionStrategy)) {
// underline the attribute's value
if (node.getNodeType() == Node.ELEMENT_NODE) {
IDOMElement element = (IDOMElement) node;
IDOMAttr attributeNode = (IDOMAttr) (element.getAttributeNode(nameOrValue));
if (attributeNode != null) {
startEndPositions[0] = attributeNode.getValueRegionStartOffset();
String valueRegionText = attributeNode.getValueRegionText();
int valueRegionLength = valueRegionText == null ? 0 : valueRegionText.length();
startEndPositions[1] = startEndPositions[0] + valueRegionLength;
}
}
}
else if (ALL_ATTRIBUTES.equals(selectionStrategy)) {
// underline all attributes
if (node.getNodeType() == Node.ELEMENT_NODE) {
IDOMElement element = (IDOMElement) node;
NamedNodeMap attributes = element.getAttributes();
if (attributes != null) {
IDOMNode first = (IDOMNode) attributes.item(0);
IDOMNode last = (IDOMNode) attributes.item(attributes.getLength() - 1);
if ((first != null) && (last != null)) {
startEndPositions[0] = first.getStartOffset();
startEndPositions[1] = last.getEndOffset();
}
}
}
}
else if (TEXT.equals(selectionStrategy)) {
// underline the text between the tags
if (node.getNodeType() == Node.TEXT_NODE) {
IDOMText textNode = (IDOMText) node;
int start = textNode.getStartOffset();
String value = textNode.getNodeValue();
int index = 0;
char curChar = value.charAt(index);
// here we are finding start offset by skipping over
// whitespace:
while ((curChar == '\n') || (curChar == '\t') || (curChar == '\r') || (curChar == ' ')) {
curChar = value.charAt(index);
index++;
}
if (index > 0) {
index--;
}
start = start + index;
startEndPositions[0] = start;
startEndPositions[1] = start + value.trim().length();
}
else if (node.getNodeType() == Node.ELEMENT_NODE) {
IDOMElement element = (IDOMElement) node;
Node child = element.getFirstChild();
if (child instanceof IDOMNode) {
IDOMNode xmlChild = ((IDOMNode) child);
startEndPositions[0] = xmlChild.getStartOffset();
startEndPositions[1] = xmlChild.getEndOffset();
}