Search in sources :

Example 66 with IDOMNode

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;
}
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)

Example 67 with IDOMNode

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

Example 68 with IDOMNode

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;
}
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)

Example 69 with IDOMNode

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;
}
Also used : 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)

Example 70 with IDOMNode

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());
    }
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)

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