Search in sources :

Example 51 with CMElementDeclaration

use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration in project webtools.sourceediting by eclipse.

the class AbstractContentAssistProcessor method getAvailableChildrenAtIndex.

// returns a list of ModelQueryActions
protected List getAvailableChildrenAtIndex(Element parent, int index, int validityChecking) {
    List list = new ArrayList();
    CMElementDeclaration parentDecl = getCMElementDeclaration(parent);
    if (parentDecl != null) {
        ModelQuery modelQuery = ModelQueryUtil.getModelQuery(parent.getOwnerDocument());
        // taken from ActionManagers
        // int editMode = modelQuery.getEditMode();
        int editMode = ModelQuery.EDIT_MODE_UNCONSTRAINED;
        int ic = (editMode == ModelQuery.EDIT_MODE_CONSTRAINED_STRICT) ? ModelQuery.INCLUDE_CHILD_NODES | ModelQuery.INCLUDE_SEQUENCE_GROUPS : ModelQuery.INCLUDE_CHILD_NODES;
        modelQuery.getInsertActions(parent, parentDecl, index, ic, validityChecking, list);
    }
    return list;
}
Also used : CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) ArrayList(java.util.ArrayList) CMNodeList(org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList) List(java.util.List) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)

Example 52 with CMElementDeclaration

use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration in project webtools.sourceediting by eclipse.

the class AbstractContentAssistProcessor method addTagNameProposals.

protected void addTagNameProposals(ContentAssistRequest contentAssistRequest, int childPosition) {
    List cmnodes = null;
    Node parent = contentAssistRequest.getParent();
    IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
    String error = null;
    String matchString = contentAssistRequest.getMatchString();
    if (parent.getNodeType() == Node.ELEMENT_NODE) {
        // retrieve the list of children
        // validActions = getAvailableChildrenAtIndex((Element) parent,
        // childPosition);
        cmnodes = getAvailableChildElementDeclarations((Element) parent, childPosition, ModelQueryAction.INSERT);
        Iterator nodeIterator = cmnodes.iterator();
        // chop off any leading <'s and whitespace from the matchstring
        while ((matchString.length() > 0) && (Character.isWhitespace(matchString.charAt(0)) || beginsWith(matchString, "<"))) {
            // $NON-NLS-1$
            matchString = matchString.substring(1);
        }
        if (!nodeIterator.hasNext()) {
            error = NLS.bind(XMLUIMessages.__Has_no_known_child, (new Object[] { parent.getNodeName() }));
        }
        while (nodeIterator.hasNext()) {
            CMNode elementDecl = (CMNode) nodeIterator.next();
            if (elementDecl != null) {
                // only add proposals for the child element's that begin
                // with the matchstring
                String proposedText = null;
                int cursorAdjustment = 0;
                // names are in list
                if (((node != null) && (node.getAttributes() != null) && (node.getAttributes().getLength() > 0) && attributeInList(node, parent, elementDecl)) || ((node.getNodeType() != Node.TEXT_NODE) && node.getFirstStructuredDocumentRegion().isEnded())) {
                    proposedText = getRequiredName(parent, elementDecl);
                    cursorAdjustment = proposedText.length();
                } else {
                    proposedText = getRequiredName(parent, elementDecl);
                    cursorAdjustment = proposedText.length();
                    if (elementDecl instanceof CMElementDeclaration) {
                        CMElementDeclaration ed = (CMElementDeclaration) elementDecl;
                        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=89811
                        StringBuffer sb = new StringBuffer();
                        getContentGenerator().generateTag(parent, ed, sb);
                        // since it's a name proposal, assume '<' is
                        // already there
                        // only return the rest of the tag
                        proposedText = sb.toString().substring(1);
                        cursorAdjustment = getCursorPositionForProposedText(proposedText);
                    // cursorAdjustment = proposedText.length() +
                    // 1;
                    // proposedText += "></" +
                    // getRequiredName(parent, elementDecl) + ">";
                    // //$NON-NLS-2$//$NON-NLS-1$
                    }
                }
                if (beginsWith(proposedText, matchString)) {
                    Image image = CMImageUtil.getImage(elementDecl);
                    if (image == null) {
                        image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_TAG_GENERIC);
                    }
                    String proposedInfo = getAdditionalInfo(getCMElementDeclaration(parent), elementDecl);
                    CustomCompletionProposal proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), cursorAdjustment, image, getRequiredName(parent, elementDecl), null, proposedInfo, XMLRelevanceConstants.R_TAG_NAME);
                    contentAssistRequest.addProposal(proposal);
                }
            }
        }
        if (contentAssistRequest.getProposals().size() == 0) {
            if (error != null) {
                setErrorMessage(error);
            } else if ((contentAssistRequest.getMatchString() != null) && (contentAssistRequest.getMatchString().length() > 0)) {
                setErrorMessage(NLS.bind(XMLUIMessages.No_known_child_tag_names, (new Object[] { parent.getNodeName(), contentAssistRequest.getMatchString() })));
            // $NON-NLS-1$ = "No known child tag names of <{0}> begin with \"{1}\""
            } else {
                setErrorMessage(NLS.bind(XMLUIMessages.__Has_no_known_child, (new Object[] { parent.getNodeName() })));
            }
        }
    } else if (parent.getNodeType() == Node.DOCUMENT_NODE) {
        List childElements = getAvailableRootChildren((Document) parent, childPosition);
        for (int i = 0; i < childElements.size(); i++) {
            CMNode ed = (CMNode) childElements.get(i);
            if (ed == null) {
                continue;
            }
            String proposedText = null;
            int cursorAdjustment = 0;
            if (ed instanceof CMElementDeclaration) {
                // proposedText = getRequiredName(parent, ed);
                StringBuffer sb = new StringBuffer();
                getContentGenerator().generateTag(parent, (CMElementDeclaration) ed, sb);
                // tag starts w/ '<', but we want to compare to name
                proposedText = sb.toString().substring(1);
                if (!beginsWith(proposedText, matchString)) {
                    continue;
                }
                cursorAdjustment = getCursorPositionForProposedText(proposedText);
                String proposedInfo = getAdditionalInfo(null, ed);
                Image image = CMImageUtil.getImage(ed);
                if (image == null) {
                    image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_TAG_GENERIC);
                }
                CustomCompletionProposal proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), cursorAdjustment, image, getRequiredName(parent, ed), null, proposedInfo, XMLRelevanceConstants.R_TAG_NAME);
                contentAssistRequest.addProposal(proposal);
            }
        }
    }
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) Image(org.eclipse.swt.graphics.Image) Document(org.w3c.dom.Document) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Iterator(java.util.Iterator) CMNodeList(org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList) List(java.util.List) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode)

Example 53 with CMElementDeclaration

use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration in project webtools.sourceediting by eclipse.

the class XMLQuickAssistProcessor method getRequiredAttrs.

private List getRequiredAttrs(Node node) {
    List result = new ArrayList();
    ModelQuery modelQuery = getModelQuery(node);
    if (modelQuery != null) {
        CMElementDeclaration elementDecl = modelQuery.getCMElementDeclaration((Element) node);
        if (elementDecl != null) {
            CMNamedNodeMap attrMap = elementDecl.getAttributes();
            CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attrMap);
            List nodes = ModelQueryUtil.getModelQuery(node.getOwnerDocument()).getAvailableContent((Element) node, elementDecl, ModelQuery.INCLUDE_ATTRIBUTES);
            for (int k = 0; k < nodes.size(); k++) {
                CMNode cmnode = (CMNode) nodes.get(k);
                if (cmnode.getNodeType() == CMNode.ATTRIBUTE_DECLARATION) {
                    allAttributes.put(cmnode);
                }
            }
            attrMap = allAttributes;
            Iterator it = attrMap.iterator();
            CMAttributeDeclaration attr = null;
            while (it.hasNext()) {
                attr = (CMAttributeDeclaration) it.next();
                if (attr.getUsage() == CMAttributeDeclaration.REQUIRED) {
                    result.add(attr);
                }
            }
        }
    }
    return result;
}
Also used : CMNamedNodeMapImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)

Example 54 with CMElementDeclaration

use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration in project webtools.sourceediting by eclipse.

the class ContentModelWorkbenchAdapter method getChildren.

public Object[] getChildren(Object o) {
    if (o instanceof Element) {
        Element node = (Element) o;
        ModelQuery mq = ModelQueryUtil.getModelQuery(node.getOwnerDocument());
        if (mq != null) {
            CMElementDeclaration decl = mq.getCMElementDeclaration(node);
            CMListWorkbenchAdapter adapter = new CMListWorkbenchAdapter(decl);
            return new Object[] { adapter };
        }
    }
    return EMPTY;
}
Also used : CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) Element(org.w3c.dom.Element) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)

Example 55 with CMElementDeclaration

use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration in project webtools.sourceediting by eclipse.

the class ElementNodeFormatter method formatEndTag.

protected void formatEndTag(IDOMNode node, IStructuredFormatContraints formatContraints) {
    if (!isEndTagMissing(node)) {
        // end tag exists
        IStructuredDocument structuredDocument = node.getModel().getStructuredDocument();
        String lineDelimiter = structuredDocument.getLineDelimiter();
        String nodeIndentation = getNodeIndent(node);
        IDOMNode lastChild = (IDOMNode) node.getLastChild();
        if (lastChild != null && lastChild.getNodeType() != Node.TEXT_NODE) {
            if (isEndTagMissing(lastChild)) {
                // find deepest child
                IDOMNode deepestChild = (IDOMNode) lastChild.getLastChild();
                while (deepestChild != null && deepestChild.getLastChild() != null && isEndTagMissing(deepestChild)) {
                    lastChild = deepestChild;
                    deepestChild = (IDOMNode) deepestChild.getLastChild();
                }
                if (deepestChild != null) {
                    if (deepestChild.getNodeType() == Node.TEXT_NODE) {
                        // Special indentation handling if lastChild's end
                        // tag is missing and deepestChild is a text node.
                        String nodeText = deepestChild.getNodeValue();
                        if (!nodeText.endsWith(lineDelimiter + nodeIndentation)) {
                            nodeText = StringUtils.appendIfNotEndWith(nodeText, lineDelimiter);
                            nodeText = StringUtils.appendIfNotEndWith(nodeText, nodeIndentation);
                        }
                        replaceNodeValue(deepestChild, nodeText);
                    } else
                        insertAfterNode(lastChild, lineDelimiter + nodeIndentation);
                }
            } else
                // indent end tag
                insertAfterNode(lastChild, lineDelimiter + nodeIndentation);
        } else if (lastChild == null && firstStructuredDocumentRegionContainsLineDelimiters(node)) {
            // BUG174243 do not indent end tag if node has empty content
            // (otherwise new text node would be introduced)
            ModelQueryAdapter adapter = (ModelQueryAdapter) ((IDOMDocument) node.getOwnerDocument()).getAdapterFor(ModelQueryAdapter.class);
            CMElementDeclaration elementDeclaration = (CMElementDeclaration) adapter.getModelQuery().getCMNode(node);
            if ((elementDeclaration == null) || (elementDeclaration.getContentType() != CMElementDeclaration.EMPTY)) {
                // indent end tag
                replace(structuredDocument, node.getFirstStructuredDocumentRegion().getEndOffset(), 0, lineDelimiter + nodeIndentation);
            }
        }
        // format end tag name
        IStructuredDocumentRegion endTagStructuredDocumentRegion = node.getLastStructuredDocumentRegion();
        if (endTagStructuredDocumentRegion.getRegions().size() >= 3) {
            ITextRegion endTagNameRegion = endTagStructuredDocumentRegion.getRegions().get(1);
            removeRegionSpaces(node, endTagStructuredDocumentRegion, endTagNameRegion);
        }
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ModelQueryAdapter(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)

Aggregations

CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)147 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)53 List (java.util.List)46 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)43 Element (org.w3c.dom.Element)41 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)38 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)37 ArrayList (java.util.ArrayList)35 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)33 Node (org.w3c.dom.Node)32 CMAttributeDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)30 NodeList (org.w3c.dom.NodeList)28 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)24 Iterator (java.util.Iterator)19 CMNodeList (org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList)19 CustomCompletionProposal (org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)17 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)17 NamedNodeMap (org.w3c.dom.NamedNodeMap)17 Image (org.eclipse.swt.graphics.Image)15 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)15