Search in sources :

Example 41 with IDOMNode

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);
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) Node(org.w3c.dom.Node) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)

Example 42 with IDOMNode

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();
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) Node(org.w3c.dom.Node) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)

Example 43 with IDOMNode

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;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) InsertEdit(org.eclipse.text.edits.InsertEdit) NamedNodeMap(org.w3c.dom.NamedNodeMap) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) ArrayList(java.util.ArrayList) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 44 with IDOMNode

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;
}
Also used : IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) NamedNodeMap(org.w3c.dom.NamedNodeMap) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) ISourceGenerator(org.eclipse.wst.xml.core.internal.provisional.document.ISourceGenerator) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 45 with IDOMNode

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;
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) NodeList(org.w3c.dom.NodeList) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Aggregations

IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)250 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)91 Node (org.w3c.dom.Node)63 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)57 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)44 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)43 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)38 List (java.util.List)35 ArrayList (java.util.ArrayList)34 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)30 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)30 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)27 Element (org.w3c.dom.Element)27 NodeList (org.w3c.dom.NodeList)23 BadLocationException (org.eclipse.jface.text.BadLocationException)22 CustomCompletionProposal (org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)22 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)20 IDOMElement (org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)19 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)18 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)18