Search in sources :

Example 31 with CMNamedNodeMap

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

the class AbstractXMLModelQueryCompletionProposalComputer method computeEntityReferenceProposals.

/**
 * return all possible EntityReferenceProposals (according to current
 * position in doc)
 */
private ICompletionProposal[] computeEntityReferenceProposals(ITextRegion completionRegion, IDOMNode treeNode, CompletionProposalInvocationContext context) {
    // only handle XML content for now
    int documentPosition = context.getInvocationOffset();
    // ICompletionProposals
    Vector proposals = new Vector();
    IStructuredDocumentRegion sdRegion = ContentAssistUtils.getStructuredDocumentRegion(context.getViewer(), context.getInvocationOffset());
    if ((completionRegion != null) && (completionRegion.getType() == DOMRegionContext.XML_CONTENT)) {
        int nodeOffset = documentPosition - sdRegion.getStartOffset(completionRegion);
        String regionText = sdRegion.getFullText(completionRegion);
        // the previous region...there might be a better way to do this
        if ((regionText != null) && regionText.trim().equals("") && (documentPosition > 0)) {
            // $NON-NLS-1$
            IStructuredDocumentRegion prev = treeNode.getStructuredDocument().getRegionAtCharacterOffset(documentPosition - 1);
            if ((prev != null) && prev.getText().equals("&")) {
                // $NON-NLS-1$
                // https://bugs.eclipse.org/bugs/show_bug.cgi?id=206680
                // examine previous region
                sdRegion = prev;
                completionRegion = prev.getLastRegion();
                regionText = prev.getFullText();
                nodeOffset = 1;
            }
        }
        // string must start w/ &
        if ((regionText != null) && regionText.startsWith("&")) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            String key = (nodeOffset > 0) ? regionText.substring(1, nodeOffset) : "";
            // get entity proposals, passing in the appropriate start
            // string
            ModelQuery mq = ModelQueryUtil.getModelQuery(((Node) treeNode).getOwnerDocument());
            if (mq != null) {
                CMDocument xmlDoc = mq.getCorrespondingCMDocument(treeNode);
                CMNamedNodeMap cmmap = null;
                Properties entities = null;
                if (xmlDoc != null) {
                    cmmap = xmlDoc.getEntities();
                }
                if (cmmap != null) {
                    entities = mapToProperties(cmmap);
                } else // 224787 in absence of content model, just use
                // minimal 5 entities
                {
                    entities = new Properties();
                    // $NON-NLS-1$ //$NON-NLS-2$
                    entities.put("quot", "\"");
                    // $NON-NLS-1$ //$NON-NLS-2$
                    entities.put("apos", "'");
                    // $NON-NLS-1$ //$NON-NLS-2$
                    entities.put("amp", "&");
                    // $NON-NLS-1$ //$NON-NLS-2$
                    entities.put("lt", "<");
                    // $NON-NLS-1$ //$NON-NLS-2$
                    entities.put("gt", ">");
                    // $NON-NLS-1$ //$NON-NLS-2$
                    entities.put("nbsp", " ");
                }
                addEntityProposals(proposals, entities, key, nodeOffset, sdRegion, completionRegion, context);
            }
        }
    }
    return (ICompletionProposal[]) ((proposals.size() > 0) ? proposals.toArray(new ICompletionProposal[proposals.size()]) : null);
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) Properties(java.util.Properties) Vector(java.util.Vector)

Example 32 with CMNamedNodeMap

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

the class QuickAssistProcessorXML method getRequiredAttrs.

protected 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 33 with CMNamedNodeMap

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

the class XMLHyperlinkDetector method isLinkableAttr.

/**
 * Checks to see if the given attribute is openable. Attribute is openable
 * if it is a namespace declaration attribute or if the attribute value is
 * of type URI.
 *
 * @param attr
 *            cannot be null
 * @param cmElement
 *            CMElementDeclaration associated with the attribute (can be
 *            null)
 * @return true if this attribute is "openOn-able" false otherwise
 */
private boolean isLinkableAttr(Attr attr, CMElementDeclaration cmElement) {
    String attrName = attr.getName();
    String prefix = DOMNamespaceHelper.getPrefix(attrName);
    String unprefixedName = DOMNamespaceHelper.getUnprefixedName(attrName);
    // determine if attribute is namespace declaration
    if ((XMLNS.equals(prefix)) || (XMLNS.equals(unprefixedName))) {
        return true;
    }
    // determine if attribute contains schema location
    if ((XSI_NAMESPACE_URI.equals(DOMNamespaceHelper.getNamespaceURI(attr))) && ((SCHEMA_LOCATION.equals(unprefixedName)) || (NO_NAMESPACE_SCHEMA_LOCATION.equals(unprefixedName)))) {
        return true;
    }
    // determine if attribute value is of type URI
    if (cmElement != null) {
        CMNamedNodeMap attrDecls = cmElement.getAttributes();
        CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attrDecls);
        List nodes = ModelQueryUtil.getModelQuery(attr.getOwnerDocument()).getAvailableContent(attr.getOwnerElement(), cmElement, 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);
            }
        }
        attrDecls = allAttributes;
        CMAttributeDeclaration attrDecl = (CMAttributeDeclaration) attrDecls.getNamedItem(attrName);
        if ((attrDecl != null) && (attrDecl.getAttrType() != null) && (CMDataType.URI.equals(attrDecl.getAttrType().getDataTypeName()))) {
            return true;
        }
    }
    return false;
}
Also used : CMNamedNodeMapImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl) 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) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)

Example 34 with CMNamedNodeMap

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

the class XMLPropertySource method resetPropertyValue.

/**
 * Resets the specified property's value to its default value.
 */
public void resetPropertyValue(Object propertyObject) {
    String property = propertyObject.toString();
    CMNamedNodeMap attrDecls = null;
    CMElementDeclaration ed = getDeclaration();
    if (ed != null) {
        attrDecls = ed.getAttributes();
        CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attrDecls);
        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);
            }
        }
        attrDecls = allAttributes;
    }
    NamedNodeMap attrMap = fNode.getAttributes();
    if (attrDecls != null) {
        CMAttributeDeclaration attrDecl = (CMAttributeDeclaration) attrDecls.getNamedItem(property);
        String defValue = null;
        if (attrDecl != null) {
            if (attrDecl.getAttrType() != null) {
                CMDataType helper = attrDecl.getAttrType();
                if ((helper.getImpliedValueKind() != CMDataType.IMPLIED_VALUE_NONE) && (helper.getImpliedValue() != null)) {
                    defValue = helper.getImpliedValue();
                }
            }
        }
        if ((defValue != null) && (defValue.length() > 0)) {
            // implied values will be in the DOM, but not the source
            attrMap.removeNamedItem(property);
        } else {
            // $NON-NLS-1$
            ((Attr) attrMap.getNamedItem(property)).setValue("");
        }
    } else {
        // remember, this method is for reset, not remove
        // $NON-NLS-1$
        ((Attr) attrMap.getNamedItem(property)).setValue("");
    }
}
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) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) CMDataType(org.eclipse.wst.xml.core.internal.contentmodel.CMDataType) 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) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) Attr(org.w3c.dom.Attr)

Example 35 with CMNamedNodeMap

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

the class BugFixesTest method testXSITypeVsTypeAttr.

public void testXSITypeVsTypeAttr() {
    // See bug 225447, 225819
    // Load the XSD file
    String XSD_FILE_NAME = "XSITypeTest.xsd";
    String fileURI = FILE_PROTOCOL + PLUGIN_ABSOLUTE_PATH + SAMPLES_DIR + XSD_FILE_NAME;
    CMDocumentFactoryXSD factory = new CMDocumentFactoryXSD();
    assertNotNull("Assert factory is not null", factory);
    CMDocument cmDocument = factory.createCMDocument(fileURI);
    assertNotNull("Assert CMDocument is not null", cmDocument);
    // Check and obtain the two global elements (elementA and elementB)
    CMNamedNodeMap elements = cmDocument.getElements();
    assertEquals(elements.getLength(), 2);
    CMElementDeclaration cmElementDeclaration = (CMElementDeclaration) elements.item(0);
    CMElementDeclaration cmElementDeclarationA = null;
    CMElementDeclaration cmElementDeclarationB = null;
    if ("elementA".equals(cmElementDeclaration.getElementName())) {
        cmElementDeclarationA = cmElementDeclaration;
        cmElementDeclarationB = (CMElementDeclaration) elements.item(1);
    } else {
        cmElementDeclarationB = cmElementDeclaration;
        cmElementDeclarationA = (CMElementDeclaration) elements.item(1);
    }
    // elementA has a "type" attribute with "X" enumerated value, make sure it appears in the model
    CMNamedNodeMap attributesA = cmElementDeclarationA.getAttributes();
    assertEquals(attributesA.getLength(), 1);
    CMAttributeDeclaration cmAttributeDeclarationA = (CMAttributeDeclaration) attributesA.item(0);
    assertEquals("type", cmAttributeDeclarationA.getAttrName());
    CMDataType attrTypeA = cmAttributeDeclarationA.getAttrType();
    String[] enumeratedValuesA = attrTypeA.getEnumeratedValues();
    assertEquals(1, enumeratedValuesA.length);
    assertEquals("X", enumeratedValuesA[0]);
    // elementB does not have a "type" attribute, make sure the xsi:type appears in the model
    CMNamedNodeMap attributesB = cmElementDeclarationB.getAttributes();
    assertEquals(attributesB.getLength(), 1);
    CMAttributeDeclaration cmAttributeDeclarationB = (CMAttributeDeclaration) attributesB.item(0);
    assertEquals("type", cmAttributeDeclarationB.getAttrName());
    CMDataType attrTypeB = cmAttributeDeclarationB.getAttrType();
    assertEquals("typeNames", attrTypeB.getDataTypeName());
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) CMDataType(org.eclipse.wst.xml.core.internal.contentmodel.CMDataType) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) CMDocumentFactoryXSD(org.eclipse.wst.xsd.contentmodel.internal.CMDocumentFactoryXSD)

Aggregations

CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)68 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)39 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)36 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)26 CMAttributeDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)23 List (java.util.List)21 ArrayList (java.util.ArrayList)18 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)16 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)14 Element (org.w3c.dom.Element)14 CMNamedNodeMapImpl (org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl)13 NamedNodeMap (org.w3c.dom.NamedNodeMap)10 CMDataType (org.eclipse.wst.xml.core.internal.contentmodel.CMDataType)9 NodeList (org.w3c.dom.NodeList)9 Iterator (java.util.Iterator)8 Attr (org.w3c.dom.Attr)8 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)7 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)6 CMNodeList (org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList)6 IDOMElement (org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)6