use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode in project webtools.sourceediting by eclipse.
the class CSSModelImpl method beginRecording.
/**
*/
public void beginRecording(Object requester, String label, String description) {
getStyleNotifier().beginRecording();
Node node = getOwnerDOMNode();
if (node != null && node instanceof IDOMNode) {
IStructuredModel model = ((IDOMNode) node).getModel();
if (model != null) {
model.beginRecording(requester, label, description);
return;
}
}
super.beginRecording(requester, label, description);
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode in project webtools.sourceediting by eclipse.
the class CSSModelImpl method endRecording.
/**
*/
public void endRecording(Object requester) {
Node node = getOwnerDOMNode();
if (node != null && node instanceof IDOMNode) {
IStructuredModel model = ((IDOMNode) node).getModel();
if (model != null) {
model.endRecording(requester);
return;
}
}
super.endRecording(requester);
getStyleNotifier().endRecording();
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode in project webtools.sourceediting by eclipse.
the class ElementNodeCleanupHandler method insertRequiredAttrs.
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)
// $NON-NLS-1$
defaultValue = "";
// $NON-NLS-1$
String nameAndDefaultValue = " ";
if (i == 0 && lastRegion.getLength() > lastRegion.getTextLength())
// $NON-NLS-1$
nameAndDefaultValue = "";
// $NON-NLS-1$ //$NON-NLS-2$
nameAndDefaultValue += requiredAttributeName + "=\"" + defaultValue + "\"";
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 or now, unless we find reason not to
Logger.log(Logger.INFO, e.getMessage());
}
}
}
}
return newNode;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode 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.IDOMNode in project webtools.sourceediting by eclipse.
the class ElementNodeCleanupHandler method compressEmptyElementTag.
/**
* <p>Compress empty element tags if the prefence is set to do so</p>
*
* @copyof org.eclipse.wst.xml.core.internal.cleanup.ElementNodeCleanupHandler#compressEmptyElementTag
*
* @param node the {@link IDOMNode} to possible compress
* @return the compressed node if the given node should be compressed, else the node as it was given
*/
private IDOMNode compressEmptyElementTag(IDOMNode node) {
boolean compressEmptyElementTags = getCleanupPreferences().getCompressEmptyElementTags();
IDOMNode newNode = node;
IStructuredDocumentRegion startTagStructuredDocumentRegion = newNode.getFirstStructuredDocumentRegion();
IStructuredDocumentRegion endTagStructuredDocumentRegion = newNode.getLastStructuredDocumentRegion();
// only compress tags if they are empty
if ((compressEmptyElementTags && startTagStructuredDocumentRegion != endTagStructuredDocumentRegion && startTagStructuredDocumentRegion != null)) {
// only compress end tags if its XHTML or not a container
if (isXMLTag((IDOMElement) newNode) || !newNode.isContainer()) {
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;
}
Aggregations