use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode in project webtools.sourceediting by eclipse.
the class ElementNodeCleanupHandler method compressEmptyElementTag.
private IDOMNode compressEmptyElementTag(IDOMNode node) {
boolean compressEmptyElementTags = getCleanupPreferences().getCompressEmptyElementTags();
IDOMNode newNode = node;
IStructuredDocumentRegion startTagStructuredDocumentRegion = newNode.getFirstStructuredDocumentRegion();
IStructuredDocumentRegion endTagStructuredDocumentRegion = newNode.getLastStructuredDocumentRegion();
if (compressEmptyElementTags && startTagStructuredDocumentRegion != endTagStructuredDocumentRegion && startTagStructuredDocumentRegion != null) {
ITextRegionList regions = startTagStructuredDocumentRegion.getRegions();
ITextRegion lastRegion = regions.get(regions.size() - 1);
// format children and end tag if not empty element tag
if (lastRegion.getType() != DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
NodeList childNodes = newNode.getChildNodes();
if (childNodes == null || childNodes.getLength() == 0 || (childNodes.getLength() == 1 && (childNodes.item(0)).getNodeType() == Node.TEXT_NODE && ((childNodes.item(0)).getNodeValue().trim().length() == 0))) {
IDOMModel structuredModel = newNode.getModel();
IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
int startTagStartOffset = newNode.getStartOffset();
int offset = endTagStructuredDocumentRegion.getStart();
int length = endTagStructuredDocumentRegion.getLength();
// $NON-NLS-1$
structuredDocument.replaceText(structuredDocument, offset, length, "");
// save
newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
offset = startTagStructuredDocumentRegion.getStart() + lastRegion.getStart();
// $NON-NLS-1$
structuredDocument.replaceText(structuredDocument, offset, 0, "/");
// save
newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
}
}
}
return newNode;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode in project webtools.sourceediting by eclipse.
the class ElementNodeCleanupHandler method cleanup.
public Node cleanup(Node node) {
Node newNode = cleanupChildren(node);
IDOMNode renamedNode = newNode instanceof IDOMNode ? (IDOMNode) newNode : null;
// call quoteAttrValue() first so it will close any unclosed attr
// quoteAttrValue() will return the new start tag if there is a
// structure change
renamedNode = quoteAttrValue(renamedNode);
// and not implicit tag
if (!isCommentTag(renamedNode) && !isImplicitTag(renamedNode)) {
IDOMModel structuredModel = renamedNode.getModel();
// save start offset before insertTagClose()
// or else renamedNode.getStartOffset() will be zero if
// renamedNode replaced by insertTagClose()
int startTagStartOffset = renamedNode.getStartOffset();
// for start tag
IStructuredDocumentRegion startTagStructuredDocumentRegion = renamedNode.getStartStructuredDocumentRegion();
insertTagClose(structuredModel, startTagStructuredDocumentRegion);
// update renamedNode and startTagStructuredDocumentRegion after
// insertTagClose()
renamedNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
startTagStructuredDocumentRegion = renamedNode.getStartStructuredDocumentRegion();
// for end tag
IStructuredDocumentRegion endTagStructuredDocumentRegion = renamedNode.getEndStructuredDocumentRegion();
if (endTagStructuredDocumentRegion != startTagStructuredDocumentRegion)
insertTagClose(structuredModel, endTagStructuredDocumentRegion);
}
// call insertMissingTags() next, it will generate implicit tags if
// there are any
// insertMissingTags() will return the new missing start tag if one is
// missing
renamedNode = insertMissingTags(renamedNode);
renamedNode = compressEmptyElementTag(renamedNode);
renamedNode = insertRequiredAttrs(renamedNode);
return renamedNode;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode 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;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode in project webtools.sourceediting by eclipse.
the class ElementNodeCleanupHandler method insertStartTag.
private IDOMNode insertStartTag(IDOMNode node) {
IDOMNode newNode = node;
if (isCommentTag(node))
// do nothing
return node;
String tagName = node.getNodeName();
String startTag = START_TAG_OPEN.concat(tagName).concat(TAG_CLOSE);
int startTagStartOffset = node.getStartOffset();
IDOMModel structuredModel = node.getModel();
IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
structuredDocument.replaceText(structuredDocument, startTagStartOffset, 0, startTag);
// save
newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
return newNode;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode in project webtools.sourceediting by eclipse.
the class XMLFormatterFormatProcessor method formatNode.
public void formatNode(Node node) {
if (node instanceof IDOMNode) {
IDOMNode domNode = (IDOMNode) node;
formatModel(domNode.getModel(), domNode.getStartOffset(), domNode.getLength());
}
}
Aggregations