}
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();
}
}
}
else if (FIRST_NON_WHITESPACE_TEXT.equals(selectionStrategy)) {
// search through all child nodes and return range of
// first non-whitespace
// text node
if (node.getNodeType() == Node.ELEMENT_NODE) {
NodeList nodes = node.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
Node currentNode = nodes.item(i);
if (currentNode.getNodeType() == Node.TEXT_NODE) {
// TODO (Trung) I don't think we should call
// getNodeValue(), trim(), length()
// repeatedly.
// This is inefficient, to improve use local
// variables to store values.
IDOMText textNode = (IDOMText) currentNode;
if (textNode.getNodeValue().trim().length() > 0) {
String value = textNode.getNodeValue();
int index = 0;
int start = textNode.getStartOffset();
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();
break;
}
}
}
}
}
else if (TEXT_ENTITY_REFERENCE.equals(selectionStrategy)) {
if (node.getNodeType() == Node.ENTITY_REFERENCE_NODE) {
startEndPositions[0] = region.getStartOffset();
startEndPositions[1] = region.getEndOffset();
}
else if (node.getNodeType() == Node.ELEMENT_NODE) {
/*
* In this case the undeclared entity might be in one
* of the attribute values. Search through the
* attributes to find the range of the undeclared
* entity.
*/
String entity = "&" + nameOrValue + ";"; //$NON-NLS-1$ //$NON-NLS-2$
NamedNodeMap attributes = node.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
IDOMAttr attr = (IDOMAttr) attributes.item(i);
String nodeValue = attr.getNodeValue();
int index = nodeValue.indexOf(entity);
if (index != -1) {
startEndPositions[0] = attr.getValueRegionStartOffset() + index + 1;
startEndPositions[1] = startEndPositions[0] + entity.length();
}
}
}
}
else if (VALUE_OF_ATTRIBUTE_WITH_GIVEN_VALUE.equals(selectionStrategy)) {
// TODO (Trung) do we really need this strategy ?
// If we know the name of the name of the attribute, we
// can retrieve its value.
// Hence, we can incoperate this strategy with
// ATTRIBUTE_VALUE ?
if (node.getNodeType() == Node.ELEMENT_NODE) {
// here we will search through all attributes for the
// one with the
// with the value we want:
// TODO (Trung) I see a potential problem here.
// What happens when there is another attribute having
// the same value
// with this attribute's buggy value ?
// Need to solve when time permits.
NamedNodeMap attributes = node.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
IDOMAttr attr = (IDOMAttr) attributes.item(i);
String nodeValue = attr.getNodeValue().trim();
if (nodeValue.equals(nameOrValue)) {
startEndPositions[0] = attr.getValueRegionStartOffset() + 1;
startEndPositions[1] = startEndPositions[0] + nodeValue.length();
break;
}
}
}