Search in sources :

Example 16 with CMNamedNodeMapImpl

use of org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl 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 17 with CMNamedNodeMapImpl

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

the class InferredGrammarFactory method createCMContent.

public void createCMContent(CMDocument parentCMDocument, CMElementDeclaration parentEd, CMDocument childCMDocument, CMElementDeclaration childEd, boolean isLocal, String uri) {
    // add element to the parent's content
    // consider all content to be of the form (A | B | C)*
    // 
    CMGroupImpl group = (CMGroupImpl) parentEd.getContent();
    CMNodeListImpl groupChildNodeList = (CMNodeListImpl) group.getChildNodes();
    if (parentCMDocument == childCMDocument) {
        if (!groupChildNodeList.contains(childEd)) {
            groupChildNodeList.add(childEd);
        }
        if (isLocal) {
            CMNamedNodeMapImpl localElementMap = (CMNamedNodeMapImpl) parentEd.getLocalElements();
            localElementMap.put(childEd);
        }
    } else {
        CMAnyElement cmAnyElement = lookupOrCreateCMAnyElement((CMDocumentImpl) parentCMDocument, uri);
        if (!groupChildNodeList.contains(cmAnyElement)) {
            groupChildNodeList.add(cmAnyElement);
        }
    }
}
Also used : CMNamedNodeMapImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl) CMNodeListImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNodeListImpl) CMAnyElement(org.eclipse.wst.xml.core.internal.contentmodel.CMAnyElement) CMGroupImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMGroupImpl)

Example 18 with CMNamedNodeMapImpl

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

the class InferredGrammarFactory method lookupOrCreateCMAnyElement.

protected CMAnyElement lookupOrCreateCMAnyElement(CMDocumentImpl parentCMDocument, String uri) {
    CMNamedNodeMapImpl anyElementMap = parentCMDocument.getAnyElements();
    CMAnyElementImpl anyElement = (CMAnyElementImpl) anyElementMap.getNamedItem(CMAnyElementImpl.computeNodeName(uri));
    if (anyElement == null) {
        // System.out.println("create anyElement " + uri);
        anyElement = new CMAnyElementImpl(uri);
        anyElement.setInferred(true);
        anyElementMap.put(anyElement);
    }
    return anyElement;
}
Also used : CMNamedNodeMapImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl) CMAnyElementImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMAnyElementImpl)

Example 19 with CMNamedNodeMapImpl

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

the class XMLPropertySource method updatePropertyDescriptors.

protected void updatePropertyDescriptors() {
    if ((fDescriptors == null) || (fDescriptors.length == 0)) {
        // Nothing to update
        return;
    }
    // List of all names encountered in the tag and defined by the element
    List declaredNames = new ArrayList();
    // New descriptor list that will become fDescriptors after all
    // processing is done
    List descriptors = new ArrayList();
    // Names of the descriptors in the above List
    List descriptorNames = new ArrayList();
    // Update any descriptors derived from the metainfo
    CMElementDeclaration ed = getDeclaration();
    CMNamedNodeMap attrMap = null;
    if (ed != null) {
        attrMap = ed.getAttributes();
        CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attrMap);
        ModelQuery modelQuery = ModelQueryUtil.getModelQuery(fNode.getOwnerDocument());
        if (modelQuery != null) {
            List nodes = modelQuery.getAvailableContent((Element) fNode, ed, 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;
    }
    // Update exiting descriptors; not added to the final list here
    if (attrMap != null) {
        // Update existing descriptor types based on metainfo
        CMAttributeDeclaration attrDecl = null;
        for (int i = 0; i < attrMap.getLength(); i++) {
            attrDecl = (CMAttributeDeclaration) attrMap.item(i);
            String attrName = DOMNamespaceHelper.computeName(attrDecl, fNode, null);
            if (!declaredNames.contains(attrName)) {
                declaredNames.add(attrName);
            }
            for (int j = 0; j < fDescriptors.length; j++) {
                boolean sameName = (fCaseSensitive && fDescriptors[j].getId().equals(attrDecl.getNodeName())) || (!fCaseSensitive && attrDecl.getNodeName().equals(fDescriptors[j].getId().toString()));
                if (sameName) {
                    String[] validValues = getValidValues(attrDecl);
                    // updated for now)
                    if (fDescriptors[j] instanceof EnumeratedStringPropertyDescriptor) {
                        ((EnumeratedStringPropertyDescriptor) fDescriptors[j]).updateValues(validValues);
                    } else // Replace with better descriptor
                    if ((validValues != null) && (validValues.length > 0)) {
                        fDescriptors[j] = createPropertyDescriptor(attrDecl, null);
                    }
                }
            }
        }
    } else {
        // Update existing descriptors based on not having any metainfo
        for (int j = 0; j < fDescriptors.length; j++) {
            // Replace with basic descriptor
            if (!(fDescriptors[j] instanceof TextPropertyDescriptor)) {
                fDescriptors[j] = createDefaultPropertyDescriptor((String) fDescriptors[j].getId());
            }
        }
    }
    NamedNodeMap attributes = fNode.getAttributes();
    // are present or declared
    for (int i = 0; i < fDescriptors.length; i++) {
        if (fDescriptors[i] != null) {
            String descriptorName = fDescriptors[i].getId().toString();
            if ((declaredNames.contains(descriptorName) || (attributes.getNamedItem(descriptorName) != null)) && !descriptorNames.contains(descriptorName)) {
                descriptorNames.add(descriptorName);
                descriptors.add(fDescriptors[i]);
            }
        }
    }
    // Add descriptors for declared attributes that don't already have one
    if (attrMap != null) {
        // Update existing descriptor types based on metainfo
        CMAttributeDeclaration attrDecl = null;
        for (int i = 0; i < attrMap.getLength(); i++) {
            attrDecl = (CMAttributeDeclaration) attrMap.item(i);
            String attrName = DOMNamespaceHelper.computeName(attrDecl, fNode, null);
            if (fCaseSensitive) {
                if (!descriptorNames.contains(attrName)) {
                    IPropertyDescriptor descriptor = createPropertyDescriptor(attrDecl, null);
                    if (descriptor != null) {
                        descriptorNames.add(attrName);
                        descriptors.add(descriptor);
                    }
                }
            } else {
                boolean exists = false;
                for (int j = 0; j < descriptorNames.size(); j++) {
                    exists = (descriptorNames.get(j).toString().equalsIgnoreCase(attrName)) || exists;
                }
                if (!exists) {
                    descriptorNames.add(attrName);
                    IPropertyDescriptor descriptor = createPropertyDescriptor(attrDecl, null);
                    if (descriptor != null) {
                        descriptorNames.add(attrName);
                        descriptors.add(descriptor);
                    }
                }
            }
        }
    }
    // Add descriptors for existing attributes that don't already have one
    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            Attr attr = (Attr) attributes.item(i);
            String attrName = attr.getName();
            if (fCaseSensitive) {
                if (!descriptorNames.contains(attrName)) {
                    descriptorNames.add(attrName);
                    descriptors.add(createDefaultPropertyDescriptor(attrName));
                }
            } else {
                boolean exists = false;
                for (int j = 0; j < descriptorNames.size(); j++) {
                    exists = (descriptorNames.get(j).toString().equalsIgnoreCase(attrName)) || exists;
                }
                if (!exists) {
                    descriptorNames.add(attrName);
                    descriptors.add(createDefaultPropertyDescriptor(attrName));
                }
            }
        }
    }
    // Update fDescriptors
    IPropertyDescriptor[] newDescriptors = new IPropertyDescriptor[descriptors.size()];
    for (int i = 0; i < newDescriptors.length; i++) {
        newDescriptors[i] = (IPropertyDescriptor) descriptors.get(i);
    }
    fDescriptors = newDescriptors;
}
Also used : CMNamedNodeMapImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl) NamedNodeMap(org.w3c.dom.NamedNodeMap) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) ArrayList(java.util.ArrayList) Attr(org.w3c.dom.Attr) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) 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) TextPropertyDescriptor(org.eclipse.ui.views.properties.TextPropertyDescriptor) IPropertyDescriptor(org.eclipse.ui.views.properties.IPropertyDescriptor) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)

Example 20 with CMNamedNodeMapImpl

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

the class XMLPropertySource method createPropertyDescriptors.

/**
 * Returns the current collection of property descriptors.
 *
 * @return all valid descriptors.
 */
private IPropertyDescriptor[] createPropertyDescriptors() {
    CMNamedNodeMap attrMap = null;
    CMElementDeclaration ed = getDeclaration();
    if (ed != null) {
        attrMap = ed.getAttributes();
        CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attrMap);
        List nodes = ModelQueryUtil.getModelQuery(fNode.getOwnerDocument()).getAvailableContent((Element) fNode, ed, 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;
    }
    List descriptorList = new ArrayList();
    List names = new ArrayList();
    IPropertyDescriptor descriptor;
    CMAttributeDeclaration attrDecl = null;
    // add descriptors for existing attributes
    NamedNodeMap attributes = fNode.getAttributes();
    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            Attr attr = (Attr) attributes.item(i);
            // CMAttributeDeclaration to derive a descriptor
            if (attrMap != null) {
                String attrName = attr.getName();
                if (fCaseSensitive) {
                    attrDecl = (CMAttributeDeclaration) attrMap.getNamedItem(attrName);
                } else {
                    attrDecl = null;
                    for (int j = 0; j < attrMap.getLength(); j++) {
                        if (!fCaseSensitive && attrMap.item(j).getNodeName().equalsIgnoreCase(attrName)) {
                            attrDecl = (CMAttributeDeclaration) attrMap.item(j);
                            break;
                        }
                    }
                }
            }
            // be consistent: if there's metainfo, use *that* as the
            // descriptor ID
            descriptor = null;
            if (attrDecl != null) {
                String attrName = DOMNamespaceHelper.computeName(attrDecl, fNode, null);
                if (!names.contains(attrName)) {
                    descriptor = createPropertyDescriptor(attrDecl, attr);
                    if (descriptor != null)
                        names.add(attrName);
                }
            } else {
                if (!names.contains(attr.getName())) {
                    descriptor = createDefaultPropertyDescriptor(attr.getName());
                    if (descriptor != null)
                        names.add(attr.getName());
                }
            }
            if (descriptor != null) {
                descriptorList.add(descriptor);
            }
        }
    }
    // add descriptors from the metainfo that are not yet listed
    if (attrMap != null) {
        for (int i = 0; i < attrMap.getLength(); i++) {
            attrDecl = (CMAttributeDeclaration) attrMap.item(i);
            String attrName = DOMNamespaceHelper.computeName(attrDecl, fNode, null);
            if (!names.contains(attrName)) {
                IPropertyDescriptor holdDescriptor = createPropertyDescriptor(attrDecl, null);
                if (holdDescriptor != null) {
                    names.add(attrName);
                    descriptorList.add(holdDescriptor);
                }
            }
        }
    }
    // add MQE-based descriptors
    if (ed != null && fNode.getNodeType() == Node.ELEMENT_NODE) {
        List nodes = ModelQueryUtil.getModelQuery(fNode.getOwnerDocument()).getAvailableContent((Element) fNode, ed, ModelQuery.INCLUDE_ATTRIBUTES);
        for (int i = 0; i < nodes.size(); i++) {
            CMNode node = (CMNode) nodes.get(i);
            if (node.getNodeType() == CMNode.ATTRIBUTE_DECLARATION) {
                attrDecl = (CMAttributeDeclaration) node;
                String attrName = DOMNamespaceHelper.computeName(attrDecl, fNode, null);
                if (!names.contains(attrName)) {
                    IPropertyDescriptor holdDescriptor = createPropertyDescriptor(attrDecl, null);
                    if (holdDescriptor != null) {
                        names.add(attrName);
                        descriptorList.add(holdDescriptor);
                    }
                }
            }
        }
    }
    IPropertyDescriptor[] descriptors = new IPropertyDescriptor[descriptorList.size()];
    for (int i = 0; i < descriptors.length; i++) {
        descriptors[i] = (IPropertyDescriptor) descriptorList.get(i);
    }
    return descriptors;
}
Also used : CMNamedNodeMapImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl) NamedNodeMap(org.w3c.dom.NamedNodeMap) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) ArrayList(java.util.ArrayList) Attr(org.w3c.dom.Attr) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) ArrayList(java.util.ArrayList) List(java.util.List) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) IPropertyDescriptor(org.eclipse.ui.views.properties.IPropertyDescriptor) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)

Aggregations

CMNamedNodeMapImpl (org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl)21 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)16 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)15 List (java.util.List)14 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)14 CMAttributeDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)13 ArrayList (java.util.ArrayList)11 NamedNodeMap (org.w3c.dom.NamedNodeMap)11 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)7 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)6 Attr (org.w3c.dom.Attr)6 Iterator (java.util.Iterator)5 Image (org.eclipse.swt.graphics.Image)4 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)4 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)4 CustomCompletionProposal (org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)4 CMNodeList (org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList)4 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)4 Element (org.w3c.dom.Element)4 NodeList (org.w3c.dom.NodeList)4