use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr in project webtools.sourceediting by eclipse.
the class DelegatingSourceValidator method computeStartAndEndLocation.
/**
* Calculates the "better" offsets.
*
* @param startOffset -
* the offset given by Xerces
* @param errorMessage -
* the Xerces error Message
* @param selectionStrategy -
* the selectionStrategy
* @param document -
* the document
* @return int[] - position 0 has the start offset of the squiggle range,
* position 1 has the endOffset
*/
/*
* The way the offsets is calculated is: - find the indexed region
* (element) closest to the given offset - if we are between two elements,
* choosing left or right element will depend on parameter 'errorSide' -
* based on the selectionStrategy choose the underlining strategy (eg
* START_TAG means underline the start tag of that element) - use
* information from nameOrValue and the DOM to get better offsets
*
*/
protected int[] computeStartAndEndLocation(int startOffset, String selectionStrategy, String errorSide, String nameOrValue, IDOMDocument document) {
try {
int[] startEndPositions = new int[2];
IndexedRegion region = document.getModel().getIndexedRegion(startOffset);
IndexedRegion prevRegion = document.getModel().getIndexedRegion(startOffset - 1);
if (prevRegion != region) {
// exactly where we need to be.
if (ERROR_SIDE_LEFT.equals(errorSide)) {
region = prevRegion;
}
}
// the start of the region where the error was
if (region != null) {
startEndPositions[0] = region.getStartOffset();
startEndPositions[1] = startEndPositions[0];
} else {
// this will message will not get added to the IReporter
// since the length is 0
startEndPositions[0] = 0;
startEndPositions[1] = 0;
}
if (region instanceof Node) {
Node node = (Node) region;
if (START_TAG.equals(selectionStrategy)) {
// 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)) {
// 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)) {
// 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);
// 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)) {
// 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);
// 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.
*/
// $NON-NLS-1$ //$NON-NLS-2$
String entity = "&" + nameOrValue + ";";
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)) {
// 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;
}
}
}
} else if (ATTRIBUTE_NAME_LAST.equals(selectionStrategy)) {
// underline the last attribute's name
if (node.getNodeType() == Node.ELEMENT_NODE) {
NamedNodeMap attributeMap = node.getAttributes();
final int length = attributeMap.getLength();
Node tempNode = null;
Node attrNode = null;
for (int i = 0; i < length; i++) {
tempNode = attributeMap.item(i);
if (tempNode != null && tempNode.getNodeName().equals(nameOrValue)) {
attrNode = tempNode;
}
}
IDOMNode attributeNode = (IDOMNode) (attrNode);
if (attributeNode != null) {
startEndPositions[0] = attributeNode.getStartOffset();
startEndPositions[1] = attributeNode.getStartOffset() + nameOrValue.length();
}
}
}
}
return startEndPositions;
} finally // catch (Exception e) { // e.printStackTrace();
// }
{
}
// return null;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr in project webtools.sourceediting by eclipse.
the class BaseHyperlinkDetector method getHyperlinkRegion.
/**
* Get the text region corresponding to an IDOMNode.
*
* @param node the node for which we want the text region. Must not be null.
* @return an IRegion for the node, or null if the node is not recognized.
*/
protected IRegion getHyperlinkRegion(IDOMNode node) {
if (node == null) {
return null;
}
IRegion hyperRegion = null;
short nodeType = node.getNodeType();
switch(nodeType) {
case Node.ELEMENT_NODE:
{
hyperRegion = new Region(node.getStartOffset(), node.getEndOffset() - node.getStartOffset());
}
break;
case Node.ATTRIBUTE_NODE:
{
IDOMAttr att = (IDOMAttr) node;
int regOffset = att.getValueRegionStartOffset();
// ISSUE: We are using a deprecated method here. Is there
// a better way to get what we need?
ITextRegion valueRegion = att.getValueRegion();
if (valueRegion != null) {
int regLength = valueRegion.getTextLength();
String attValue = att.getValueRegionText();
if (StringUtils.isQuoted(attValue)) {
regLength = regLength - 2;
regOffset++;
}
hyperRegion = new Region(regOffset, regLength);
}
}
break;
default:
// Do nothing.
break;
}
return hyperRegion;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr in project webtools.sourceediting by eclipse.
the class BaseHyperlinkDetector method getAttributeNode.
/**
* Locates the attribute node under the cursor.
*
* @param offset the cursor offset.
* @param parent the parent node
* @return an IDOMNode representing the attribute if one is found at the
* offset or null otherwise.
*/
protected IDOMNode getAttributeNode(int offset, IDOMNode parent) {
IDOMAttr attrNode = null;
NamedNodeMap map = parent.getAttributes();
for (int index = 0; index < map.getLength(); index++) {
attrNode = (IDOMAttr) map.item(index);
boolean located = attrNode.contains(offset);
if (located) {
if (attrNode.hasNameOnly()) {
attrNode = null;
}
break;
}
}
if (attrNode == null) {
return parent;
}
return attrNode;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr in project webtools.sourceediting by eclipse.
the class XMLHyperlinkDetector method getHyperlinkRegion.
private IRegion getHyperlinkRegion(Node node) {
IRegion hyperRegion = null;
if (node != null) {
short nodeType = node.getNodeType();
if (nodeType == Node.DOCUMENT_TYPE_NODE) {
// handle doc type node
IDOMNode docNode = (IDOMNode) node;
hyperRegion = new Region(docNode.getStartOffset(), docNode.getEndOffset() - docNode.getStartOffset());
} else if (nodeType == Node.ATTRIBUTE_NODE) {
// handle attribute nodes
IDOMAttr att = (IDOMAttr) node;
// do not include quotes in attribute value region
int regOffset = att.getValueRegionStartOffset();
ITextRegion valueRegion = att.getValueRegion();
if (valueRegion != null) {
int regLength = valueRegion.getTextLength();
String attValue = att.getValueRegionText();
if (StringUtils.isQuoted(attValue)) {
++regOffset;
regLength = regLength - 2;
}
hyperRegion = new Region(regOffset, regLength);
}
}
}
return hyperRegion;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr in project webtools.sourceediting by eclipse.
the class SelectAttributeContentAssist method adjustXPathStart.
/**
* This needs to setup the content assistance correctly. Here is what needs
* to happen: 1. Adjust the matchString (This should have been calculated
* earlier) 2. Get the current tokens offset position..this will be the
* starting offset. 3. Get the replacement length...this is the difference
* between the token offset and the next token or end of the string
*
* @param attrName
* The name of the attribute to use as the starting node.
*/
protected void adjustXPathStart(String attrName) {
IDOMElement elem = (IDOMElement) getNode();
IDOMAttr xpathNode = (IDOMAttr) elem.getAttributeNode(attrName);
if (xpathNode == null) {
return;
}
String xpathString = xpathNode.getValue();
if (xpathString.length() == 0) {
return;
}
int startOffset = xpathNode.getValueRegionStartOffset();
replacementLength = getReplacementBeginPosition() - startOffset;
}
Aggregations