return newNode;
}
private IDOMNode insertRequiredAttrs(IDOMNode node) {
boolean insertRequiredAttrs = getCleanupPreferences().getInsertRequiredAttrs();
IDOMNode newNode = node;
if (insertRequiredAttrs) {
List requiredAttrs = getRequiredAttrs(newNode);
if (requiredAttrs.size() > 0) {
NamedNodeMap currentAttrs = node.getAttributes();
List insertAttrs = new ArrayList();
if (currentAttrs.getLength() == 0)
insertAttrs.addAll(requiredAttrs);
else {
for (int i = 0; i < requiredAttrs.size(); i++) {
String requiredAttrName = ((CMAttributeDeclaration) requiredAttrs.get(i)).getAttrName();
boolean found = false;
for (int j = 0; j < currentAttrs.getLength(); j++) {
String currentAttrName = currentAttrs.item(j).getNodeName();
if (requiredAttrName.compareToIgnoreCase(currentAttrName) == 0) {
found = true;
break;
}
}
if (!found)
insertAttrs.add(requiredAttrs.get(i));
}
}
if (insertAttrs.size() > 0) {
IStructuredDocumentRegion startStructuredDocumentRegion = newNode.getStartStructuredDocumentRegion();
int index = startStructuredDocumentRegion.getEndOffset();
ITextRegion lastRegion = startStructuredDocumentRegion.getLastRegion();
if (lastRegion.getType() == DOMRegionContext.XML_TAG_CLOSE) {
index--;
lastRegion = startStructuredDocumentRegion.getRegionAtCharacterOffset(index - 1);
} else if (lastRegion.getType() == DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
index = index - 2;
lastRegion = startStructuredDocumentRegion.getRegionAtCharacterOffset(index - 1);
}
MultiTextEdit multiTextEdit = new MultiTextEdit();
try {
for (int i = insertAttrs.size() - 1; i >= 0; i--) {
CMAttributeDeclaration attrDecl = (CMAttributeDeclaration) insertAttrs.get(i);
String requiredAttributeName = attrDecl.getAttrName();
String defaultValue = attrDecl.getDefaultValue();
if (defaultValue == null)
defaultValue = ""; //$NON-NLS-1$
String nameAndDefaultValue = " "; //$NON-NLS-1$
if (i == 0 && lastRegion.getLength() > lastRegion.getTextLength())
nameAndDefaultValue = ""; //$NON-NLS-1$
nameAndDefaultValue += requiredAttributeName + "=\"" + defaultValue + "\""; //$NON-NLS-1$ //$NON-NLS-2$
multiTextEdit.addChild(new InsertEdit(index, nameAndDefaultValue));
// BUG3381: MultiTextEdit applies all child
// TextEdit's basing on offsets
// in the document before the first TextEdit, not
// after each
// child TextEdit. Therefore, do not need to
// advance the index.
//index += nameAndDefaultValue.length();
}
multiTextEdit.apply(newNode.getStructuredDocument());
} catch (BadLocationException e) {
// log for now, unless we find reason not to
Logger.log(Logger.INFO, e.getMessage());
}
}