Search in sources :

Example 16 with CMAttributeDeclaration

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

the class AttrImpl method getDeclaration.

/**
 */
protected CMAttributeDeclaration getDeclaration() {
    ElementImpl element = (ElementImpl) getOwnerElement();
    if (element == null)
        return null;
    CMElementDeclaration elementDecl = element.getDeclaration();
    if (elementDecl == null)
        return null;
    List nodes = ModelQueryUtil.getModelQuery(getOwnerDocument()).getAvailableContent(getOwnerElement(), elementDecl, ModelQuery.INCLUDE_ATTRIBUTES);
    String name = getName();
    for (int k = 0; k < nodes.size(); k++) {
        CMNode cmnode = (CMNode) nodes.get(k);
        if (cmnode.getNodeType() == CMNode.ATTRIBUTE_DECLARATION && name.equals(cmnode.getNodeName())) {
            return (CMAttributeDeclaration) cmnode;
        }
    }
    return null;
}
Also used : CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) List(java.util.List) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)

Example 17 with CMAttributeDeclaration

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

the class AbstractContentAssistProcessor method addAttributeValueProposals.

protected void addAttributeValueProposals(ContentAssistRequest contentAssistRequest) {
    IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
    // Find the attribute region and name for which this position should
    // have a value proposed
    IStructuredDocumentRegion open = node.getFirstStructuredDocumentRegion();
    ITextRegionList openRegions = open.getRegions();
    int i = openRegions.indexOf(contentAssistRequest.getRegion());
    if (i < 0) {
        return;
    }
    ITextRegion nameRegion = null;
    while (i >= 0) {
        nameRegion = openRegions.get(i--);
        if (nameRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) {
            break;
        }
    }
    // the name region is REQUIRED to do anything useful
    if (nameRegion != null) {
        // Retrieve the declaration
        CMElementDeclaration elementDecl = getCMElementDeclaration(node);
        // String attributeName = nameRegion.getText();
        String attributeName = open.getText(nameRegion);
        CMAttributeDeclaration attrDecl = null;
        // declaration for the attribute otherwise
        if (elementDecl != null) {
            CMNamedNodeMap attributes = elementDecl.getAttributes();
            CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attributes) {

                private Map caseInsensitive;

                private Map getCaseInsensitiveMap() {
                    if (caseInsensitive == null)
                        caseInsensitive = new HashMap();
                    return caseInsensitive;
                }

                public CMNode getNamedItem(String name) {
                    CMNode node = super.getNamedItem(name);
                    if (node == null) {
                        node = (CMNode) getCaseInsensitiveMap().get(name.toLowerCase(Locale.US));
                    }
                    return node;
                }

                public void put(CMNode cmNode) {
                    super.put(cmNode);
                    getCaseInsensitiveMap().put(cmNode.getNodeName().toLowerCase(Locale.US), cmNode);
                }
            };
            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 noprefixName = DOMNamespaceHelper.getUnprefixedName(attributeName);
            if (attributes != null) {
                attrDecl = (CMAttributeDeclaration) attributes.getNamedItem(noprefixName);
                if (attrDecl == null) {
                    attrDecl = (CMAttributeDeclaration) attributes.getNamedItem(attributeName);
                }
            }
            if (attrDecl == null) {
                setErrorMessage(UNKNOWN_ATTR, attributeName);
            }
        }
        String currentValue = node.getAttributes().getNamedItem(attributeName).getNodeValue();
        String proposedInfo = null;
        Image image = CMImageUtil.getImage(attrDecl);
        if (image == null) {
            if ((attrDecl != null) && (attrDecl.getUsage() == CMAttributeDeclaration.REQUIRED)) {
                image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATT_REQ_OBJ);
            } else {
                image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATTRIBUTE);
            }
        }
        if ((attrDecl != null) && (attrDecl.getAttrType() != null)) {
            // attribute is known, prompt with values from the declaration
            proposedInfo = getAdditionalInfo(elementDecl, attrDecl);
            List possibleValues = getPossibleDataTypeValues(node, attrDecl);
            String defaultValue = attrDecl.getAttrType().getImpliedValue();
            if (possibleValues.size() > 0 || defaultValue != null) {
                // ENUMERATED VALUES
                String matchString = contentAssistRequest.getMatchString();
                if (matchString == null) {
                    // $NON-NLS-1$
                    matchString = "";
                }
                if ((matchString.length() > 0) && (matchString.startsWith("\"") || matchString.startsWith("'"))) {
                    matchString = matchString.substring(1);
                }
                boolean currentValid = false;
                // d210858, if the region's a container, don't suggest the
                // enumerated values as they probably won't help
                boolean existingComplicatedValue = (contentAssistRequest.getRegion() != null) && (contentAssistRequest.getRegion() instanceof ITextRegionContainer);
                if (!existingComplicatedValue) {
                    int rOffset = contentAssistRequest.getReplacementBeginPosition();
                    int rLength = contentAssistRequest.getReplacementLength();
                    for (Iterator j = possibleValues.iterator(); j.hasNext(); ) {
                        String possibleValue = (String) j.next();
                        if (!possibleValue.equals(defaultValue)) {
                            currentValid = currentValid || possibleValue.equals(currentValue);
                            if ((matchString.length() == 0) || possibleValue.startsWith(matchString)) {
                                // $NON-NLS-2$//$NON-NLS-1$
                                String rString = "\"" + possibleValue + "\"";
                                CustomCompletionProposal proposal = new CustomCompletionProposal(rString, rOffset, rLength, possibleValue.length() + 1, XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ENUM), rString, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
                                contentAssistRequest.addProposal(proposal);
                            }
                        }
                    }
                    if (defaultValue != null && ((matchString.length() == 0) || defaultValue.startsWith(matchString))) {
                        // $NON-NLS-2$//$NON-NLS-1$
                        String rString = "\"" + defaultValue + "\"";
                        CustomCompletionProposal proposal = new CustomCompletionProposal(rString, rOffset, rLength, defaultValue.length() + 1, XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_DEFAULT), rString, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
                        contentAssistRequest.addProposal(proposal);
                    }
                }
            } else if (((attrDecl.getUsage() == CMAttributeDeclaration.FIXED) || (attrDecl.getAttrType().getImpliedValueKind() == CMDataType.IMPLIED_VALUE_FIXED)) && (attrDecl.getAttrType().getImpliedValue() != null)) {
                // FIXED values
                String value = attrDecl.getAttrType().getImpliedValue();
                if ((value != null) && (value.length() > 0)) {
                    // $NON-NLS-2$//$NON-NLS-1$
                    String rValue = "\"" + value + "\"";
                    CustomCompletionProposal proposal = new CustomCompletionProposal(rValue, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), rValue.length() + 1, image, rValue, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
                    contentAssistRequest.addProposal(proposal);
                    if ((currentValue.length() > 0) && !value.equals(currentValue)) {
                        // $NON-NLS-2$//$NON-NLS-1$
                        rValue = "\"" + currentValue + "\"";
                        proposal = new CustomCompletionProposal(rValue, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), rValue.length() + 1, image, rValue, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
                        contentAssistRequest.addProposal(proposal);
                    }
                }
            }
        } else {
            // unknown attribute, so supply nice empty values
            proposedInfo = getAdditionalInfo(null, elementDecl);
            CustomCompletionProposal proposal = null;
            if ((currentValue != null) && (currentValue.length() > 0)) {
                // $NON-NLS-2$//$NON-NLS-1$
                String rValue = "\"" + currentValue + "\"";
                proposal = new CustomCompletionProposal(rValue, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), 1, image, rValue, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
                contentAssistRequest.addProposal(proposal);
            }
        }
    } else {
        setErrorMessage(UNKNOWN_CONTEXT);
    }
}
Also used : CMNamedNodeMapImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) HashMap(java.util.HashMap) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) ITextRegionContainer(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer) Image(org.eclipse.swt.graphics.Image) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) 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) Iterator(java.util.Iterator) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) 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) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) Map(java.util.Map) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) HashMap(java.util.HashMap) NamedNodeMap(org.w3c.dom.NamedNodeMap)

Example 18 with CMAttributeDeclaration

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

the class XMLQuickAssistProcessor method getInsertRequiredAttrs.

private void getInsertRequiredAttrs(List proposals, ISourceViewer viewer, int offset) {
    IDOMNode node = (IDOMNode) getNodeAt(viewer, offset);
    if ((node != null) && (node.getNodeType() == Node.ELEMENT_NODE)) {
        IStructuredDocumentRegion startStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
        if ((startStructuredDocumentRegion != null) && startStructuredDocumentRegion.containsOffset(offset)) {
            IDOMNode cursorNode = (IDOMNode) getNodeAt(viewer, offset);
            List requiredAttrs = getRequiredAttrs(cursorNode);
            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) {
                    proposals.add(new InsertRequiredAttrsQuickAssistProposal(insertAttrs));
                }
            }
        }
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) NamedNodeMap(org.w3c.dom.NamedNodeMap) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)

Example 19 with CMAttributeDeclaration

use of org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration 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 20 with CMAttributeDeclaration

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

the class XMLTableTreePropertyDescriptorFactory method createAttributePropertyDescriptor.

public IPropertyDescriptor createAttributePropertyDescriptor(Attr attr) {
    IPropertyDescriptor result = null;
    String attributeName = attr.getName();
    ModelQuery mq = ModelQueryUtil.getModelQuery(attr.getOwnerDocument());
    CMAttributeDeclaration ad = null;
    if (mq != null) {
        ad = mq.getCMAttributeDeclaration(attr);
    }
    if (ad != null) {
        result = createPropertyDescriptorHelper(attributeName, attr.getOwnerElement(), ad);
    }
    if (result == null) {
        result = new TextPropertyDescriptor(attributeName, attributeName);
    }
    return result;
}
Also used : ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) TextPropertyDescriptor(org.eclipse.ui.views.properties.TextPropertyDescriptor) IPropertyDescriptor(org.eclipse.ui.views.properties.IPropertyDescriptor)

Aggregations

CMAttributeDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)57 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)30 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)29 List (java.util.List)24 ArrayList (java.util.ArrayList)18 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)17 NamedNodeMap (org.w3c.dom.NamedNodeMap)17 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)14 CMNamedNodeMapImpl (org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl)13 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)12 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)12 CMNodeList (org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList)11 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)11 Attr (org.w3c.dom.Attr)11 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)10 Element (org.w3c.dom.Element)10 Iterator (java.util.Iterator)9 NodeList (org.w3c.dom.NodeList)8 CMDataType (org.eclipse.wst.xml.core.internal.contentmodel.CMDataType)7 HashMap (java.util.HashMap)4