use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr in project webtools.sourceediting by eclipse.
the class TaglibHyperlinkDetector method getHyperlinkRegion.
// the below methods were copied from URIHyperlinkDetector
private IRegion getHyperlinkRegion(Node node, IRegion boundingRegion) {
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);
}
}
if (nodeType == Node.ELEMENT_NODE) {
// Handle doc type node
IDOMNode docNode = (IDOMNode) node;
hyperRegion = getNameRegion(docNode.getFirstStructuredDocumentRegion());
if (hyperRegion == null) {
hyperRegion = new Region(docNode.getStartOffset(), docNode.getFirstStructuredDocumentRegion().getTextLength());
}
}
}
/**
* Only return a hyperlink region that overlaps the search region.
* This will help us to not underline areas not under the cursor.
*/
if (hyperRegion != null && intersects(hyperRegion, boundingRegion))
return hyperRegion;
return null;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr in project webtools.sourceediting by eclipse.
the class ElementNodeCleanupHandler method quoteAttrValue.
protected IDOMNode quoteAttrValue(IDOMNode node) {
IDOMElement element = (IDOMElement) node;
if (element.isCommentTag())
// do nothing
return node;
boolean quoteAttrValues = getCleanupPreferences().getQuoteAttrValues();
IDOMNode newNode = node;
if (quoteAttrValues) {
NamedNodeMap attributes = newNode.getAttributes();
int attributesLength = attributes.getLength();
ISourceGenerator generator = node.getModel().getGenerator();
for (int i = 0; i < attributesLength; i++) {
attributes = newNode.getAttributes();
attributesLength = attributes.getLength();
IDOMAttr eachAttr = (IDOMAttr) attributes.item(i);
// ITextRegion oldAttrValueRegion = eachAttr.getValueRegion();
String oldAttrValue = eachAttr.getValueRegionText();
if (oldAttrValue == null) {
IDOMModel structuredModel = node.getModel();
if (isXMLType(structuredModel)) {
// TODO: Kit, please check. Is there any way to not
// rely on getting regions from attributes?
// $NON-NLS-1$ //$NON-NLS-2$
String newAttrValue = "=\"" + eachAttr.getNameRegionText() + "\"";
IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
replaceSource(structuredModel, structuredDocument, eachAttr.getNameRegionEndOffset(), 0, newAttrValue);
// save
newNode = (IDOMNode) structuredModel.getIndexedRegion(node.getStartOffset());
// new
// node
}
} else {
char quote = StringUtils.isQuoted(oldAttrValue) ? oldAttrValue.charAt(0) : DOUBLE_QUOTE;
String newAttrValue = generator.generateAttrValue(eachAttr, quote);
// Workaround for now...
if (oldAttrValue.length() == 1) {
char firstChar = oldAttrValue.charAt(0);
if (firstChar == SINGLE_QUOTE)
newAttrValue = SINGLE_QUOTES;
else if (firstChar == DOUBLE_QUOTE)
newAttrValue = DOUBLE_QUOTES;
}
if (newAttrValue != null) {
if (newAttrValue.compareTo(oldAttrValue) != 0) {
int attrValueStartOffset = eachAttr.getValueRegionStartOffset();
int attrValueLength = oldAttrValue.length();
int startTagStartOffset = node.getStartOffset();
IDOMModel structuredModel = node.getModel();
IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
replaceSource(structuredModel, structuredDocument, attrValueStartOffset, attrValueLength, newAttrValue);
// save
newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
// new
// node
}
}
}
}
}
return newNode;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr in project webtools.sourceediting by eclipse.
the class BasicCommentElementHandler method createElement.
public Element createElement(Document document, String data, boolean isJSPTag) {
Element element = null;
String str = data.trim();
CommentElementFactory factory = new CommentElementFactory(document, isJSPTag, this);
if (str.charAt(0) == '/') {
// end tag
// skip '/'
TagScanner scanner = new TagScanner(str, 1);
String name = scanner.nextName();
if (name.equals(elementName)) {
element = factory.create(name, CommentElementFactory.IS_END);
}
} else {
// start tag
TagScanner scanner = new TagScanner(str, 0);
String name = scanner.nextName();
if (name.equals(elementName)) {
element = factory.create(name, (isEmpty) ? CommentElementFactory.IS_EMPTY : CommentElementFactory.IS_START);
// set attributes
String attrName = scanner.nextName();
while (attrName != null) {
String attrValue = scanner.nextValue();
Attr attr = document.createAttribute(attrName);
if (attr != null) {
if (attrValue != null)
((IDOMAttr) attr).setValueSource(attrValue);
element.setAttributeNode(attr);
}
attrName = scanner.nextName();
}
}
}
return element;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr in project webtools.sourceediting by eclipse.
the class BaseHyperlinkDetector method isLinkable.
/**
* Determines whether a node is "linkable" that is, the component it refers to
* can be the target of a "go to definition" navigation.
*
* @param node the node to test, must not be null;
* @return true if the node is linkable, false otherwise.
*/
private boolean isLinkable(IDOMNode node) {
if (node == null) {
return false;
}
short nodeType = node.getNodeType();
boolean isLinkable = false;
if (nodeType == Node.ATTRIBUTE_NODE) {
IDOMAttr attr = (IDOMAttr) node;
String name = attr.getName();
// isLinkableAttribute is a template method. Derived classes should
// override.
isLinkable = isLinkableAttribute(name);
}
return isLinkable;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr in project webtools.sourceediting by eclipse.
the class ElementNodeCleanupHandler method quoteAttrValue.
private IDOMNode quoteAttrValue(IDOMNode node) {
IDOMNode newNode = node;
// XMLElement element = (XMLElement) node;
if (isCommentTag(node))
// do nothing
return node;
boolean quoteAttrValues = getCleanupPreferences().getQuoteAttrValues();
if (quoteAttrValues) {
NamedNodeMap attributes = newNode.getAttributes();
if (attributes != null) {
int attributesLength = attributes.getLength();
ISourceGenerator generator = node.getModel().getGenerator();
for (int i = 0; i < attributesLength; i++) {
attributes = newNode.getAttributes();
attributesLength = attributes.getLength();
IDOMAttr eachAttr = (IDOMAttr) attributes.item(i);
// ITextRegion oldAttrValueRegion =
// eachAttr.getValueRegion();
String oldAttrValue = eachAttr.getValueRegionText();
if (oldAttrValue == null) {
IDOMModel structuredModel = node.getModel();
if (isXMLType(structuredModel)) {
// $NON-NLS-1$ //$NON-NLS-2$
String newAttrValue = "\"" + eachAttr.getNameRegionText() + "\"";
IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
if (eachAttr.getEqualRegion() != null)
// equal region exists
structuredDocument.replaceText(structuredDocument, eachAttr.getEndOffset(), 0, newAttrValue);
else
// no equal region
// $NON-NLS-1$
structuredDocument.replaceText(structuredDocument, eachAttr.getNameRegionTextEndOffset(), 0, "=".concat(newAttrValue));
// save
newNode = (IDOMNode) structuredModel.getIndexedRegion(node.getStartOffset());
// new
// node
}
} else {
// String oldAttrValue = oldAttrValueRegion.getText();
char quote = StringUtils.isQuoted(oldAttrValue) ? oldAttrValue.charAt(0) : DOUBLE_QUOTE;
String newAttrValue = generator.generateAttrValue(eachAttr, quote);
// Workaround for now...
if (oldAttrValue.length() == 1) {
char firstChar = oldAttrValue.charAt(0);
if (firstChar == SINGLE_QUOTE)
newAttrValue = SINGLE_QUOTES;
else if (firstChar == DOUBLE_QUOTE)
newAttrValue = DOUBLE_QUOTES;
}
if (newAttrValue != null) {
if (newAttrValue.compareTo(oldAttrValue) != 0) {
int attrValueStartOffset = eachAttr.getValueRegionStartOffset();
int attrValueLength = oldAttrValue.length();
int startTagStartOffset = node.getStartOffset();
IDOMModel structuredModel = node.getModel();
IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
structuredDocument.replaceText(structuredDocument, attrValueStartOffset, attrValueLength, newAttrValue);
// save
newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
// new
// node
}
}
}
}
}
}
return newNode;
}
Aggregations