Search in sources :

Example 46 with IDOMElement

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement in project webtools.sourceediting by eclipse.

the class HTMLFormatter method canInsertBreakBefore.

/**
 */
protected boolean canInsertBreakBefore(Node node) {
    if (node == null)
        return false;
    Node parent = node.getParentNode();
    if (parent == null)
        return false;
    Node prev = node.getPreviousSibling();
    // special exception if this node is a non-HTML tag (like JSP
    // elements)
    // BUG188093 - only preserve whitespace for jsp (not custom) tags
    String prefix = node.getPrefix();
    if (prefix != null && JSP.equals(prefix)) {
        boolean canInsertBreakBefore = false;
        // if a whitespace does not exist before it, do not add one
        if (prev != null && prev.getNodeType() == Node.TEXT_NODE) {
            String theText = ((Text) prev).getData();
            if (theText != null && theText.length() > 0) {
                char theChar = theText.charAt(theText.length() - 1);
                canInsertBreakBefore = Character.isWhitespace(theChar);
            }
        }
        // continue processing)
        if (!canInsertBreakBefore)
            return false;
    }
    // BUG188093 - only preserve whitespace for jsp (not custom) tags
    if (prev != null) {
        prefix = prev.getPrefix();
        if (prefix != null && JSP.equals(prefix)) {
            boolean canInsertBreakBeforeNext = false;
            // if a whitespace does not exist after it, do not add one
            if (node.getNodeType() == Node.TEXT_NODE) {
                String theText = ((Text) node).getData();
                if (theText != null && theText.length() > 0) {
                    char theChar = theText.charAt(0);
                    canInsertBreakBeforeNext = Character.isWhitespace(theChar);
                }
            }
            // continue processing)
            if (!canInsertBreakBeforeNext)
                return false;
        }
    }
    if (parent.getNodeType() == Node.DOCUMENT_NODE) {
        if (prev == null)
            return false;
        return true;
    } else if (parent.getNodeType() == Node.ELEMENT_NODE) {
        IDOMElement element = (IDOMElement) parent;
        // do not insert break after missing start tag
        if (prev == null && element.getStartStructuredDocumentRegion() == null)
            return false;
        // elements
        if (element.getPrefix() != null)
            return true;
        CMElementDeclaration decl = getElementDeclaration(element);
        if (decl != null) {
            return allowNewlineBefore(node, element, decl);
        }
    }
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        return true;
    }
    if (prev != null && prev.getNodeType() == Node.ELEMENT_NODE) {
        CMElementDeclaration decl = getElementDeclaration((Element) prev);
        if (canInsertBreakAfter(decl)) {
            // spcial for BR
            return canFormatChild(parent);
        }
    }
    return false;
}
Also used : CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) Text(org.w3c.dom.Text) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 47 with IDOMElement

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement in project webtools.sourceediting by eclipse.

the class ElementNodeCleanupHandler method insertEndTag.

private IDOMNode insertEndTag(IDOMNode node) {
    IDOMNode newNode = node;
    IDOMElement element = (IDOMElement) node;
    if (element.isCommentTag())
        // do nothing
        return node;
    int startTagStartOffset = node.getStartOffset();
    IDOMModel structuredModel = node.getModel();
    if (isEmptyElement(element)) {
        IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
        IStructuredDocumentRegion startStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
        ITextRegionList regions = startStructuredDocumentRegion.getRegions();
        ITextRegion lastRegion = regions.get(regions.size() - 1);
        structuredDocument.replaceText(structuredDocument, startStructuredDocumentRegion.getStartOffset(lastRegion), lastRegion.getLength(), EMPTY_TAG_CLOSE);
        if (regions.size() > 1) {
            ITextRegion regionBeforeTagClose = regions.get(regions.size() - 1 - 1);
            // region does not have extra spaces
            if (regionBeforeTagClose.getTextLength() == regionBeforeTagClose.getLength())
                // $NON-NLS-1$
                structuredDocument.replaceText(structuredDocument, startStructuredDocumentRegion.getStartOffset(lastRegion), 0, " ");
        }
    } else {
        String tagName = node.getNodeName();
        String endTag = END_TAG_OPEN.concat(tagName).concat(TAG_CLOSE);
        IDOMNode lastChild = (IDOMNode) node.getLastChild();
        int endTagStartOffset = 0;
        if (lastChild != null)
            // if this node has children, insert the end tag after the
            // last child
            endTagStartOffset = lastChild.getEndOffset();
        else
            // if this node does not has children, insert the end tag
            // after the start tag
            endTagStartOffset = node.getEndOffset();
        IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
        structuredDocument.replaceText(structuredDocument, endTagStartOffset, 0, endTag);
    }
    // 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) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 48 with IDOMElement

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement in project webtools.sourceediting by eclipse.

the class XMLComponentRenameParticipant method createChange.

// private RefactoringStatus createRenameChanges(final IProgressMonitor monitor) throws CoreException {
// Assert.isNotNull(monitor);
// final RefactoringStatus status= new RefactoringStatus();
// try {
// monitor.beginTask("RefactoringMessages.RenameComponentRefactoring_searching", 1);
// createRenameChanges(new SubProgressMonitor(monitor, 1));
// //updateChangeManager(new SubProgressMonitor(monitor, 1), status);
// } finally {
// monitor.done();
// }
// return status;
// }
public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    for (Iterator iter = matches.iterator(); iter.hasNext(); ) {
        SearchMatch match = (SearchMatch) iter.next();
        TextChange textChange = getChangeManager().get(match.getFile());
        String newName = getArguments().getNewName();
        String qualifier = "";
        if (getArguments() instanceof ComponentRenameArguments) {
            qualifier = ((ComponentRenameArguments) getArguments()).getQualifier();
        }
        if (match.getObject() instanceof Node) {
            Node node = (Node) match.getObject();
            if (node instanceof IDOMAttr) {
                IDOMAttr attr = (IDOMAttr) node;
                IDOMElement element = (IDOMElement) attr.getOwnerElement();
                newName = getNewQName(element, qualifier, newName);
            }
            newName = RenameComponentProcessor.quoteString(newName);
        }
        ReplaceEdit replaceEdit = new ReplaceEdit(match.getOffset(), match.getLength(), newName);
        String editName = RefactoringMessages.getString("RenameComponentProcessor.Component_Refactoring_update_reference");
        TextChangeCompatibility.addTextEdit(textChange, editName, replaceEdit);
    }
    // don't create any change now, all the changes are in changeManger variable and will be combined in RenameComponentProcessor.postCreateChange method
    return null;
}
Also used : SearchMatch(org.eclipse.wst.common.core.search.SearchMatch) IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) Node(org.w3c.dom.Node) Iterator(java.util.Iterator) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) TextChange(org.eclipse.ltk.core.refactoring.TextChange) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 49 with IDOMElement

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement in project liferay-ide by liferay.

the class LayoutTplUtil method getChildElementsByTagName.

public static List<Element> getChildElementsByTagName(IDOMElement parentElement, String childElementTag) {
    NodeList childNodes = ((Node) parentElement).getChildNodes();
    List<Element> childElements = new ArrayList<>();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);
        if ((childNode.getNodeType() == 1) && (childElementTag != null)) {
            Element element = (Element) childNode;
            if (element.getTagName().equals(childElementTag)) {
                childElements.add(element);
            }
        }
    }
    return childElements;
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) LayoutTplElement(com.liferay.ide.layouttpl.core.model.LayoutTplElement) Element(org.w3c.dom.Element) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) ArrayList(java.util.ArrayList)

Example 50 with IDOMElement

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement in project liferay-ide by liferay.

the class LayoutTplUtil method findMainContentElement.

public static IDOMElement findMainContentElement(IDOMDocument rootDocument) {
    if ((rootDocument == null) || !(rootDocument.hasChildNodes())) {
        return null;
    }
    IDOMElement mainContentElement = null;
    mainContentElement = (IDOMElement) rootDocument.getElementById("main-content");
    return mainContentElement;
}
Also used : IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Aggregations

IDOMElement (org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)59 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)20 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)19 Node (org.w3c.dom.Node)18 NodeList (org.w3c.dom.NodeList)13 Element (org.w3c.dom.Element)11 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)9 IDOMAttr (org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr)9 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)8 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)8 NamedNodeMap (org.w3c.dom.NamedNodeMap)8 ArrayList (java.util.ArrayList)7 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)7 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)7 IFile (org.eclipse.core.resources.IFile)6 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)6 List (java.util.List)5 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)5 Text (org.w3c.dom.Text)5 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)4