Search in sources :

Example 1 with CMElementDeclarationImpl

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

the class InferredGrammarFactory method createCMElementDeclaration.

public CMElementDeclaration createCMElementDeclaration(CMDocument cmDocument, Element element, boolean isLocal) {
    String localName = element.getLocalName();
    CMDocumentImpl cmDocumentImpl = (CMDocumentImpl) cmDocument;
    CMNamedNodeMapImpl elementMap = isLocal ? (CMNamedNodeMapImpl) cmDocumentImpl.getLocalElementPool() : (CMNamedNodeMapImpl) cmDocumentImpl.getElements();
    CMElementDeclarationImpl ed = (CMElementDeclarationImpl) elementMap.getNamedItem(localName);
    if (ed == null) {
        // System.out.println("create ed " + localName);
        ed = new CMElementDeclarationImpl(cmDocument, localName);
        ed.setInferred(true);
        ed.setLocal(isLocal);
        ed.setMaxOccur(1);
        CMGroupImpl group = new CMGroupImpl(new CMNodeListImpl(), CMGroup.CHOICE);
        group.setInferred(true);
        group.setMinOccur(0);
        group.setMaxOccur(-1);
        ed.setContent(group);
        elementMap.put(ed);
    }
    // lookup or create the attributes
    // 
    NamedNodeMap attributeMap = element.getAttributes();
    int attributeMapLength = attributeMap.getLength();
    for (int i = 0; i < attributeMapLength; i++) {
        Attr attr = (Attr) attributeMap.item(i);
        CMAttributeDeclarationImpl ad = (CMAttributeDeclarationImpl) ed.getAttributeMap().getNamedItem(attr.getNodeName());
        if (ad == null) {
            // todo... use an attribute pool to be more efficient?
            ad = new CMAttributeDeclarationImpl(attr.getNodeName(), CMAttributeDeclaration.OPTIONAL);
            ad.setInferred(true);
            ed.getAttributeMap().put(ad);
        }
    }
    return ed;
}
Also used : CMNamedNodeMapImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl) CMAttributeDeclarationImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMAttributeDeclarationImpl) CMNodeListImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNodeListImpl) NamedNodeMap(org.w3c.dom.NamedNodeMap) CMElementDeclarationImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMElementDeclarationImpl) CMDocumentImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMDocumentImpl) Attr(org.w3c.dom.Attr) CMGroupImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMGroupImpl)

Example 2 with CMElementDeclarationImpl

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

the class ElementImplTests method testCMAttrWithNullImpliedValue.

public void testCMAttrWithNullImpliedValue() {
    IDOMModel model = null;
    try {
        model = (IDOMModel) getModelForRead("testfiles/time.xml");
        if (model != null) {
            IDOMDocument document = model.getDocument();
            final String ATTR_NAME = "second";
            // Setup a ModelQueryAdapter whose sole purpose it to provide a attribute declaration with a null implied value
            document.addAdapter(new ModelQueryAdapter() {

                public boolean isAdapterForType(Object type) {
                    return type.equals(ModelQueryAdapter.class);
                }

                public void notifyChanged(INodeNotifier notifier, int eventType, Object changedFeature, Object oldValue, Object newValue, int pos) {
                }

                public CMDocumentCache getCMDocumentCache() {
                    return null;
                }

                public org.eclipse.wst.common.uriresolver.internal.provisional.URIResolver getIdResolver() {
                    return null;
                }

                public ModelQuery getModelQuery() {
                    return new ModelQueryImpl(null) {

                        /* (non-Javadoc)
							 * @see org.eclipse.wst.xml.core.internal.contentmodel.modelqueryimpl.ModelQueryImpl#getCMElementDeclaration(org.w3c.dom.Element)
							 */
                        public CMElementDeclaration getCMElementDeclaration(Element element) {
                            final CMElementDeclaration decl = new CMElementDeclarationImpl(null, null);
                            CMNamedNodeMapImpl map = (CMNamedNodeMapImpl) decl.getAttributes();
                            map.put(new CMAttributeDeclarationImpl(ATTR_NAME, CMAttributeDeclaration.OPTIONAL, new CMDataTypeImpl(ATTR_NAME, (String) null)));
                            return decl;
                        }
                    };
                }

                public void release() {
                }

                public void setIdResolver(org.eclipse.wst.common.uriresolver.internal.provisional.URIResolver newIdResolver) {
                }
            });
            Element element = document.getDocumentElement();
            assertNotNull(element);
            // Default value should be 0
            assertEquals("", element.getAttribute(ATTR_NAME));
        }
    } finally {
        if (model != null) {
            model.releaseFromRead();
        }
    }
}
Also used : CMNamedNodeMapImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl) CMAttributeDeclarationImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMAttributeDeclarationImpl) ModelQueryAdapter(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Element(org.w3c.dom.Element) URIResolver(org.eclipse.wst.sse.core.internal.util.URIResolver) CMDataTypeImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMDataTypeImpl) CMElementDeclarationImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMElementDeclarationImpl) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier) ModelQueryImpl(org.eclipse.wst.xml.core.internal.contentmodel.modelqueryimpl.ModelQueryImpl) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) CMDocumentCache(org.eclipse.wst.xml.core.internal.contentmodel.util.CMDocumentCache) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)

Example 3 with CMElementDeclarationImpl

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

the class HTMLTagsCompletionProposalComputer method validModelQueryNode.

/**
 * <p>Filter out all {@link CMNode}s except those specific to HTML documents</p>
 *
 * @see org.eclipse.wst.xml.ui.internal.contentassist.AbstractXMLModelQueryCompletionProposalComputer#validModelQueryNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode)
 */
protected boolean validModelQueryNode(CMNode node) {
    boolean isValid = false;
    // $NON-NLS-1$
    Object cmdoc = node.getProperty("CMDocument");
    if (cmdoc instanceof CMNode) {
        String name = ((CMNode) cmdoc).getNodeName();
        // $NON-NLS-1$ //$NON-NLS-2$
        isValid = name != null && name.endsWith(".dtd") && name.indexOf("html") != -1;
    } else if (node.supports(HTMLAttributeDeclaration.IS_HTML)) {
        Boolean isHTML = (Boolean) node.getProperty(HTMLAttributeDeclaration.IS_HTML);
        isValid = isHTML == null || isHTML.booleanValue();
    } else if (node instanceof HTMLPropertyDeclaration) {
        HTMLPropertyDeclaration propDec = (HTMLPropertyDeclaration) node;
        isValid = !propDec.isJSP();
    } else if (node instanceof CMAttributeDeclaration || node instanceof CMElementDeclarationImpl) {
        isValid = true;
    } else if (node instanceof CMElementDeclaration) {
        Boolean isXHTML = ((Boolean) node.getProperty(HTMLCMProperties.IS_XHTML));
        isValid = isXHTML != null && isXHTML.booleanValue();
    }
    // Do not propose obsolete tags, regardless
    if (isValid && node.supports(HTMLCMProperties.IS_OBSOLETE)) {
        Boolean isObsolete = ((Boolean) node.getProperty(HTMLCMProperties.IS_OBSOLETE));
        isValid = !(isObsolete != null && isObsolete.booleanValue());
    }
    return isValid;
}
Also used : HTMLPropertyDeclaration(org.eclipse.wst.html.core.internal.contentmodel.HTMLPropertyDeclaration) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) CMElementDeclarationImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMElementDeclarationImpl) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)

Aggregations

CMElementDeclarationImpl (org.eclipse.wst.xml.core.internal.contentmodel.basic.CMElementDeclarationImpl)3 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)2 CMAttributeDeclarationImpl (org.eclipse.wst.xml.core.internal.contentmodel.basic.CMAttributeDeclarationImpl)2 CMNamedNodeMapImpl (org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl)2 HTMLPropertyDeclaration (org.eclipse.wst.html.core.internal.contentmodel.HTMLPropertyDeclaration)1 INodeNotifier (org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)1 URIResolver (org.eclipse.wst.sse.core.internal.util.URIResolver)1 CMAttributeDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)1 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)1 CMDataTypeImpl (org.eclipse.wst.xml.core.internal.contentmodel.basic.CMDataTypeImpl)1 CMDocumentImpl (org.eclipse.wst.xml.core.internal.contentmodel.basic.CMDocumentImpl)1 CMGroupImpl (org.eclipse.wst.xml.core.internal.contentmodel.basic.CMGroupImpl)1 CMNodeListImpl (org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNodeListImpl)1 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)1 ModelQueryImpl (org.eclipse.wst.xml.core.internal.contentmodel.modelqueryimpl.ModelQueryImpl)1 CMDocumentCache (org.eclipse.wst.xml.core.internal.contentmodel.util.CMDocumentCache)1 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)1 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)1 ModelQueryAdapter (org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter)1 Attr (org.w3c.dom.Attr)1