Search in sources :

Example 76 with CMNode

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

the class AbstractContentAssistProcessor method getAvailableChildElementDeclarations.

// returns a list of CMNodes that are available within this parent context
// Given the grammar shown below and a snippet of XML code (where the '|'
// indicated the cursor position)
// the list would return all of the element declarations that are
// potential child elements of Foo.
// 
// grammar : Foo -> (A, B, C)
// snippet : <Foo><A>|
// result : {A, B, C}
// 
// TODO cs... do we need to pass in the 'kindOfAction'? Seems to me we
// could assume it's always an insert.
protected List getAvailableChildElementDeclarations(Element parent, int childPosition, int kindOfAction) {
    List modelQueryActions = getAvailableChildrenAtIndex(parent, childPosition, ModelQuery.VALIDITY_NONE);
    Iterator iterator = modelQueryActions.iterator();
    List cmnodes = new Vector();
    while (iterator.hasNext()) {
        ModelQueryAction action = (ModelQueryAction) iterator.next();
        if ((childPosition < 0) || (((action.getStartIndex() <= childPosition) && (childPosition <= action.getEndIndex())) && (action.getKind() == kindOfAction))) {
            CMNode actionCMNode = action.getCMNode();
            if ((actionCMNode != null) && !cmnodes.contains(actionCMNode)) {
                cmnodes.add(actionCMNode);
            }
        }
    }
    return cmnodes;
}
Also used : ModelQueryAction(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQueryAction) 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) Vector(java.util.Vector)

Example 77 with CMNode

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

the class AbstractContentAssistProcessor method addAttributeNameProposals.

protected void addAttributeNameProposals(ContentAssistRequest contentAssistRequest) {
    IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
    IStructuredDocumentRegion sdRegion = contentAssistRequest.getDocumentRegion();
    // retrieve the list of attributes
    CMElementDeclaration elementDecl = getCMElementDeclaration(node);
    if (elementDecl != null) {
        CMNamedNodeMap attributes = elementDecl.getAttributes();
        CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attributes);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            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);
                }
            }
        }
        attributes = allAttributes;
        String matchString = contentAssistRequest.getMatchString();
        // check whether an attribute really exists for the replacement
        // offsets AND if it possesses a value
        boolean attrAtLocationHasValue = false;
        NamedNodeMap attrs = node.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            AttrImpl existingAttr = (AttrImpl) attrs.item(i);
            ITextRegion name = existingAttr.getNameRegion();
            if ((sdRegion.getStartOffset(name) <= contentAssistRequest.getReplacementBeginPosition()) && (sdRegion.getStartOffset(name) + name.getLength() >= contentAssistRequest.getReplacementBeginPosition() + contentAssistRequest.getReplacementLength()) && (existingAttr.getValueRegion() != null)) {
                attrAtLocationHasValue = true;
                break;
            }
        }
        // the matchstring
        if (attributes != null) {
            for (int i = 0; i < attributes.getLength(); i++) {
                CMAttributeDeclaration attrDecl = (CMAttributeDeclaration) attributes.item(i);
                int isRequired = 0;
                if (attrDecl.getUsage() == CMAttributeDeclaration.REQUIRED) {
                    isRequired = XMLRelevanceConstants.R_REQUIRED;
                }
                boolean showAttribute = true;
                showAttribute = showAttribute && beginsWith(getRequiredName(node, attrDecl), matchString.trim());
                AttrImpl attr = (AttrImpl) node.getAttributes().getNamedItem(getRequiredName(node, attrDecl));
                ITextRegion nameRegion = attr != null ? attr.getNameRegion() : null;
                // nameRegion.getEndOffset() + 1 is required to allow for
                // matches against the full name of an existing Attr
                showAttribute = showAttribute && (attr == null || nameRegion == null || (nameRegion != null && (sdRegion.getStartOffset(nameRegion) <= contentAssistRequest.getReplacementBeginPosition()) && (sdRegion.getStartOffset(nameRegion) + nameRegion.getLength() >= contentAssistRequest.getReplacementBeginPosition() + contentAssistRequest.getReplacementLength())));
                if (showAttribute) {
                    Image attrImage = CMImageUtil.getImage(attrDecl);
                    if (attrImage == null) {
                        if (isRequired > 0) {
                            attrImage = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATT_REQ_OBJ);
                        } else {
                            attrImage = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATTRIBUTE);
                        }
                    }
                    String proposedText = null;
                    String proposedInfo = getAdditionalInfo(elementDecl, attrDecl);
                    CustomCompletionProposal proposal = null;
                    // attribute is at this location and already exists
                    if (attrAtLocationHasValue) {
                        // only propose the name
                        proposedText = getRequiredName(node, attrDecl);
                        proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), proposedText.length(), attrImage, proposedText, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_NAME + isRequired, true);
                    } else // no attribute exists or is elsewhere, generate
                    // minimally
                    {
                        Attr existingAttrNode = (Attr) node.getAttributes().getNamedItem(getRequiredName(node, attrDecl));
                        String value = null;
                        if (existingAttrNode != null && existingAttrNode.getSpecified()) {
                            value = existingAttrNode.getNodeValue();
                        }
                        if ((value != null) && (value.length() > 0)) {
                            proposedText = getRequiredName(node, attrDecl);
                        } else {
                            proposedText = getRequiredText(node, attrDecl);
                        }
                        proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), attrDecl.getNodeName().length() + 2, attrImage, // and there is no single quote that may be encasing a double quote
                        (showValues && (proposedText.lastIndexOf('\"') - proposedText.indexOf('\"') == 1 && proposedText.indexOf('\'') == -1)) ? getRequiredName(node, attrDecl) : proposedText, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_NAME + isRequired);
                    }
                    contentAssistRequest.addProposal(proposal);
                }
            }
        }
    } else {
        setErrorMessage(NLS.bind(XMLUIMessages.Element__is_unknown, (new Object[] { node.getNodeName() })));
    }
}
Also used : CMNamedNodeMapImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) AttrImpl(org.eclipse.wst.xml.core.internal.document.AttrImpl) Image(org.eclipse.swt.graphics.Image) Attr(org.w3c.dom.Attr) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) 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) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)

Example 78 with CMNode

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

the class AbstractContentAssistProcessor method attributeInList.

/**
 * This method determines if any of the attributes in the proposed XMLNode
 * node, are possible values of attributes from possible Elements at this
 * point in the document according to the Content Model.
 *
 * @param node
 *            the element with attributes that you would like to test if
 *            are possible for possible Elements at this point
 * @param cmnode
 *            possible element at this point in the document (depending on
 *            what 'node' is) true if any attributes of 'node' match any
 *            possible attributes from 'cmnodes' list.
 */
protected boolean attributeInList(IDOMNode node, Node parent, CMNode cmnode) {
    if ((node == null) || (parent == null) || (cmnode == null)) {
        return false;
    }
    String elementMatchString = node.getNodeName();
    // cmnode.getNodeName();
    String cmnodeName = getRequiredName(parent, cmnode);
    if (node instanceof Element) {
        NamedNodeMap map = ((Element) node).getAttributes();
        // $NON-NLS-1$
        String attrMatchString = "";
        CMNamedNodeMap cmattrMap = null;
        // iterate attribute possibilities for partially started node
        for (int i = 0; (map != null) && (i < map.getLength()); i++) {
            attrMatchString = map.item(i).getNodeName();
            // filter on whatever user typed for element name already
            if (beginsWith(cmnodeName, elementMatchString)) {
                if (cmnode.getNodeType() == CMNode.ELEMENT_DECLARATION) {
                    cmattrMap = ((CMElementDeclaration) cmnode).getAttributes();
                    CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(cmattrMap);
                    List nodes = ModelQueryUtil.getModelQuery(node.getOwnerDocument()).getAvailableContent((Element) node, (CMElementDeclaration) cmnode, ModelQuery.INCLUDE_ATTRIBUTES);
                    for (int k = 0; k < nodes.size(); k++) {
                        CMNode adnode = (CMNode) nodes.get(k);
                        if (adnode.getNodeType() == CMNode.ATTRIBUTE_DECLARATION) {
                            allAttributes.put(adnode);
                        }
                    }
                    cmattrMap = allAttributes;
                    // proposal list
                    for (int k = 0; (cmattrMap != null) && (k < cmattrMap.getLength()); k++) {
                        // check if name matches
                        if (cmattrMap.item(k).getNodeName().equals(attrMatchString)) {
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}
Also used : CMNamedNodeMapImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) Element(org.w3c.dom.Element) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) 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) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)

Example 79 with CMNode

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

the class AttributeContextInformationProvider method getInfoForElement.

/**
 * @param node
 */
private IContextInformation[] getInfoForElement(IDOMNode node) {
    IContextInformation[] results = EMPTY_CONTEXT_INFO;
    CMElementDeclaration decl = fModelUtil.getModelQuery().getCMElementDeclaration((Element) node);
    if (decl != null) {
        CMNamedNodeMap attributes = decl.getAttributes();
        CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attributes);
        List nodes = ModelQueryUtil.getModelQuery(node.getOwnerDocument()).getAvailableContent((Element) node, decl, 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);
            }
        }
        attributes = allAttributes;
        String attrContextString = node.getNodeName();
        // $NON-NLS-1$
        StringBuffer attrInfo = new StringBuffer(" ");
        // $NON-NLS-1$
        String name = "";
        HashMap attrPosMap = new HashMap();
        int pos = 0;
        int length = 0;
        int numPerLine = 8;
        CMAttributeDeclaration[] sortedAttrs = getSortedAttributes(attributes);
        for (int i = 0; i < sortedAttrs.length; i++) {
            name = sortedAttrs[i].getAttrName();
            length = name.length();
            pos = attrInfo.length();
            attrInfo.append(name);
            if (sortedAttrs[i].getUsage() == CMAttributeDeclaration.REQUIRED) {
                // $NON-NLS-1$
                attrInfo.append("*");
                length++;
            }
            if (i < attributes.getLength() - 1) {
                // $NON-NLS-1$
                attrInfo.append(" ");
                if ((i != 0) && (i % numPerLine == 0)) {
                    // $NON-NLS-1$
                    attrInfo.append("\n ");
                }
            }
            attrPosMap.put(name, new Position(pos, length));
        }
        if (!attrInfo.toString().trim().equals("")) {
            return new IContextInformation[] { new AttributeContextInformation(attrContextString, attrInfo.toString(), attrPosMap) };
        }
    }
    return results;
}
Also used : CMNamedNodeMapImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl) HashMap(java.util.HashMap) Position(org.eclipse.jface.text.Position) IContextInformation(org.eclipse.jface.text.contentassist.IContextInformation) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) CMNodeList(org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList) List(java.util.List) 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 80 with CMNode

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

the class AttributeContextInformationProvider method getInfoForText.

/**
 * @param node
 */
IContextInformation[] getInfoForText(IDOMNode node) {
    Node parent = node.getParentNode();
    String contextString = node.getNodeName();
    // $NON-NLS-1$
    StringBuffer info = new StringBuffer(" ");
    if ((parent != null) && (parent.getNodeType() == Node.ELEMENT_NODE)) {
        CMElementDeclaration decl = fModelUtil.getModelQuery().getCMElementDeclaration((Element) parent);
        CMContent content = decl.getContent();
        if (content instanceof CMGroup) {
            CMGroup cmGroup = (CMGroup) content;
            CMNodeList children = cmGroup.getChildNodes();
            CMNode cmNode = null;
            for (int i = 0; i < children.getLength(); i++) {
                cmNode = children.item(i);
                contextString = cmNode.getNodeName();
                if (contextString != null) {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    info.append("<" + cmNode.getNodeName() + ">");
                    if (i < children.getLength() - 1) {
                        // $NON-NLS-1$
                        info.append(" ");
                    }
                }
            }
        }
    }
    if (!info.toString().trim().equals("")) {
        return new IContextInformation[] { new ContextInformation(contextString, info.toString()) };
    } else {
        return EMPTY_CONTEXT_INFO;
    }
}
Also used : CMGroup(org.eclipse.wst.xml.core.internal.contentmodel.CMGroup) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IContextInformation(org.eclipse.jface.text.contentassist.IContextInformation) ContextInformation(org.eclipse.jface.text.contentassist.ContextInformation) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) IContextInformation(org.eclipse.jface.text.contentassist.IContextInformation) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) CMContent(org.eclipse.wst.xml.core.internal.contentmodel.CMContent) CMNodeList(org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList)

Aggregations

CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)133 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)37 List (java.util.List)36 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)35 ArrayList (java.util.ArrayList)28 Iterator (java.util.Iterator)23 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)20 CMNodeList (org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList)19 CMAttributeDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)17 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)17 Element (org.w3c.dom.Element)16 NodeList (org.w3c.dom.NodeList)15 CMNamedNodeMapImpl (org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl)14 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)13 Node (org.w3c.dom.Node)13 NamedNodeMap (org.w3c.dom.NamedNodeMap)10 ModelQueryAction (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQueryAction)9 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)9 Image (org.eclipse.swt.graphics.Image)8 CustomCompletionProposal (org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)8