IDOMAttr eachAttr = (IDOMAttr) attributes.item(i);
//ITextRegion oldAttrValueRegion =
// eachAttr.getValueRegion();
String oldAttrValue = eachAttr.getValueRegionText();
if (oldAttrValue == null) {
IDOMModel structuredModel = node.getModel();
if (isXMLType(structuredModel)) {
String newAttrValue = "\"" + eachAttr.getNameRegionText() + "\""; //$NON-NLS-1$ //$NON-NLS-2$
IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
if (eachAttr.getEqualRegion() != null)
// equal region exists
structuredDocument.replaceText(structuredDocument, eachAttr.getEndOffset(), 0, newAttrValue);
else
// no equal region
structuredDocument.replaceText(structuredDocument, eachAttr.getNameRegionTextEndOffset(), 0, "=".concat(newAttrValue)); //$NON-NLS-1$
newNode = (IDOMNode) structuredModel.getIndexedRegion(node.getStartOffset()); // save
// new
// node
}
} else {
//String oldAttrValue = oldAttrValueRegion.getText();
char quote = StringUtils.isQuoted(oldAttrValue) ? oldAttrValue.charAt(0) : DOUBLE_QUOTE;
String newAttrValue = generator.generateAttrValue(eachAttr, quote);
// There is a problem in
// StructuredDocumentRegionUtil.getAttrValue(ITextRegion)
// when the region is instanceof ContextRegion.
// 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);
newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset); // save
// new
// node
}
}
}