Search in sources :

Example 11 with ModelQuery

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

the class ElementNodeCleanupHandler 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();
            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 : CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)

Example 12 with ModelQuery

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

the class CommentElementHandlerForSSI method isCommentElement.

public boolean isCommentElement(IDOMElement element) {
    if (element == null) {
        return false;
    }
    Document document = element.getOwnerDocument();
    ModelQuery modelQuery = ModelQueryUtil.getModelQuery(document);
    if (modelQuery == null) {
        return false;
    }
    CMDocument cm = modelQuery.getCorrespondingCMDocument(document);
    if (cm == null) {
        return false;
    }
    CMNamedNodeMap map = cm.getElements();
    if (map == null) {
        return false;
    }
    String prefix = element.getPrefix();
    if (prefix == null || !prefix.equals(SSI_PREFIX)) {
        return false;
    }
    String tagName = element.getTagName();
    if (tagName.length() <= 4) {
        return false;
    }
    if (map.getNamedItem(tagName) == null) {
        return false;
    } else {
        return true;
    }
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) Document(org.w3c.dom.Document) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)

Example 13 with ModelQuery

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

the class TestModelClone method checkEmbeddedModelQuery.

private void checkEmbeddedModelQuery(IStructuredModel model, Class outerQueryAdapter, Class outerQueryClass, Class embeddedQueryClass) {
    Document doc = ((IDOMModel) model).getDocument();
    ModelQueryAdapter modelQueryAdapter = (ModelQueryAdapter) ((INodeNotifier) doc).getExistingAdapter(ModelQueryAdapter.class);
    assertNotNull("model did not have modelQueryAdapter", modelQueryAdapter);
    Class expected = outerQueryAdapter;
    Class actual = modelQueryAdapter.getClass();
    assertEquals("document's model query is not as expected", expected, actual);
    ModelQuery modelQuery = modelQueryAdapter.getModelQuery();
    expected = outerQueryClass;
    actual = modelQuery.getClass();
    assertEquals("model query adapter's model query is not as expected", expected, actual);
    ModelQuery nodeQuery = getEmbeddedModelQuery(doc);
    assertNotNull("node does not have a modelQueryAdapter", nodeQuery);
    expected = embeddedQueryClass;
    actual = nodeQuery.getClass();
    assertEquals("documents model query is not as expected", expected, actual);
}
Also used : ModelQueryAdapter(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) Document(org.w3c.dom.Document)

Example 14 with ModelQuery

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

the class XMLLabelProvider method getText.

public String getText(Object o) {
    StringBuffer text = new StringBuffer(super.getText(o));
    if (o instanceof Node) {
        Node node = (Node) o;
        if ((node.getNodeType() == Node.ELEMENT_NODE) && fShowAttributes) {
            // https://bugs.eclipse.org/bugs/show_bug.cgi?id=88444
            if (node.hasAttributes()) {
                Element element = (Element) node;
                NamedNodeMap attributes = element.getAttributes();
                Node idTypedAttribute = null;
                Node requiredAttribute = null;
                boolean hasId = false;
                boolean hasName = false;
                Node shownAttribute = null;
                // try to get content model element
                // declaration
                CMElementDeclaration elementDecl = null;
                ModelQuery mq = ModelQueryUtil.getModelQuery(element.getOwnerDocument());
                if (mq != null) {
                    elementDecl = mq.getCMElementDeclaration(element);
                }
                // ID
                if (elementDecl != null) {
                    int i = 0;
                    while ((i < attributes.getLength()) && (idTypedAttribute == null)) {
                        Node attr = attributes.item(i);
                        String attrName = attr.getNodeName();
                        CMAttributeDeclaration attrDecl = (CMAttributeDeclaration) elementDecl.getAttributes().getNamedItem(attrName);
                        if (attrDecl != null) {
                            if ((attrDecl.getAttrType() != null) && (CMDataType.ID.equals(attrDecl.getAttrType().getDataTypeName()))) {
                                idTypedAttribute = attr;
                            } else if ((attrDecl.getUsage() == CMAttributeDeclaration.REQUIRED) && (requiredAttribute == null)) {
                                // as a backup, keep tabs on
                                // any required
                                // attributes
                                requiredAttribute = attr;
                            } else {
                                // $NON-NLS-1$
                                hasId = hasId || attrName.equals("id");
                                // $NON-NLS-1$
                                hasName = hasName || attrName.equals("name");
                            }
                        }
                        ++i;
                    }
                }
                /*
					 * If no suitable attribute was found, try using a
					 * required attribute, if none, then prefer "id" or
					 * "name", otherwise just use first attribute
					 */
                if (idTypedAttribute != null) {
                    shownAttribute = idTypedAttribute;
                } else if (requiredAttribute != null) {
                    shownAttribute = requiredAttribute;
                } else if (hasId) {
                    // $NON-NLS-1$
                    shownAttribute = attributes.getNamedItem("id");
                } else if (hasName) {
                    // $NON-NLS-1$
                    shownAttribute = attributes.getNamedItem("name");
                }
                if (shownAttribute == null) {
                    shownAttribute = attributes.item(0);
                }
                // display the attribute and value (without quotes)
                String attributeName = shownAttribute.getNodeName();
                if ((attributeName != null) && (attributeName.length() > 0)) {
                    // $NON-NLS-1$
                    text.append(" " + attributeName);
                    String attributeValue = shownAttribute.getNodeValue();
                    if ((attributeValue != null) && (attributeValue.length() > 0)) {
                        // $NON-NLS-1$
                        text.append("=" + StringUtils.strip(attributeValue));
                    }
                }
            }
        }
    }
    return text.toString();
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)

Example 15 with ModelQuery

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

the class JSPContentAssistProcessor method addAttributeValueProposals.

/**
 * add proposals for tags in attribute values
 */
protected void addAttributeValueProposals(ContentAssistRequest contentAssistRequest) {
    addTemplates(contentAssistRequest, TemplateContextTypeIdsJSP.ATTRIBUTE_VALUE);
    IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
    // add JSP extra proposals from JSPBeanInfoContentAssistProcessor
    // JSPPropertyContentAssistProcessor
    // 2.1
    // get results from JSPUseBean and JSPProperty here
    // (look up processor in a map based on node name)
    JSPDummyContentAssistProcessor extraProcessor = (JSPDummyContentAssistProcessor) fNameToProcessorMap.get(node.getNodeName());
    if (extraProcessor != null && contentAssistRequest != null) {
        extraProcessor.addAttributeValueProposals(contentAssistRequest);
    }
    ModelQuery mq = ModelQueryUtil.getModelQuery(node.getOwnerDocument());
    if (mq != null) {
        CMDocument doc = mq.getCorrespondingCMDocument(node);
        // this shouldn't have to have the prefix coded in
        if (// $NON-NLS-1$
        doc instanceof JSPCMDocument || doc instanceof CMNodeWrapper || node.getNodeName().startsWith("jsp:"))
            return;
    }
    // Find the attribute name for which this position should have a value
    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;
    }
    // on an empty value, add all the JSP and taglib tags
    CMElementDeclaration elementDecl = getCMElementDeclaration(node);
    if (nameRegion != null && elementDecl != null) {
        String attributeName = open.getText(nameRegion);
        if (attributeName != null) {
            String currentValue = node.getAttributes().getNamedItem(attributeName).getNodeValue();
            if (currentValue == null || currentValue.length() == 0) {
                // $NON-NLS-1$
                List additionalElements = ModelQueryUtil.getModelQuery(node.getOwnerDocument()).getAvailableContent((Element) node, elementDecl, ModelQuery.INCLUDE_ATTRIBUTES);
                for (i = 0; i < additionalElements.size(); i++) {
                    Object additionalElement = additionalElements.get(i);
                    if (additionalElement instanceof CMElementDeclaration) {
                        CMElementDeclaration ed = (CMElementDeclaration) additionalElement;
                        String tagname = getContentGenerator().getRequiredName(node, ed);
                        // $NON-NLS-1$
                        StringBuffer contents = new StringBuffer("\"");
                        getContentGenerator().generateTag(node, ed, contents);
                        // $NON-NLS-1$
                        contents.append('"');
                        CustomCompletionProposal proposal = new CustomCompletionProposal(contents.toString(), contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), contents.length(), JSPEditorPluginImageHelper.getInstance().getImage(JSPEditorPluginImages.IMG_OBJ_TAG_GENERIC), tagname, null, null, XMLRelevanceConstants.R_JSP_ATTRIBUTE_VALUE);
                        contentAssistRequest.addProposal(proposal);
                    }
                }
            }
        }
    } else if (contentAssistRequest.getRegion().getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) {
        try {
            // Create a new model for Content Assist to operate on. This
            // will simulate
            // a full Document and then adjust the offset numbers in the
            // list of results.
            IStructuredModel internalModel = null;
            IModelManager mmanager = StructuredModelManager.getModelManager();
            internalModel = mmanager.createUnManagedStructuredModelFor(ContentTypeIdForJSP.ContentTypeID_JSP);
            IDOMNode xmlNode = null;
            IDOMModel xmlOuterModel = null;
            if (contentAssistRequest.getNode() instanceof IDOMNode) {
                xmlNode = (IDOMNode) contentAssistRequest.getNode();
                xmlOuterModel = xmlNode.getModel();
                internalModel.setResolver(xmlOuterModel.getResolver());
                internalModel.setBaseLocation(xmlOuterModel.getBaseLocation());
            }
            String contents = StringUtils.strip(contentAssistRequest.getText());
            if (xmlNode != null && contents != null) {
                int additionalShifts = 0;
                // Be sure that custom tags from taglibs also show up
                // by
                // adding taglib declarations to the internal model.
                TLDCMDocumentManager mgr = TaglibController.getTLDCMDocumentManager(xmlOuterModel.getStructuredDocument());
                if (mgr != null) {
                    List trackers = mgr.getCMDocumentTrackers(contentAssistRequest.getReplacementBeginPosition());
                    if (trackers != null) {
                        for (i = 0; i < trackers.size(); i++) {
                            CMDocumentTracker tracker = (CMDocumentTracker) trackers.get(i);
                            String declaration = tracker.getStructuredDocumentRegion().getText();
                            if (declaration != null) {
                                contents = declaration + contents;
                                additionalShifts += declaration.length();
                            }
                        }
                    }
                }
                // Also copy any jsp:useBean tags so that
                // jsp:[gs]etProperty will function
                Document doc = null;
                if (contentAssistRequest.getNode().getNodeType() == Node.DOCUMENT_NODE)
                    doc = (Document) node;
                else
                    doc = node.getOwnerDocument();
                NodeList useBeans = doc.getElementsByTagName(JSP12Namespace.ElementName.USEBEAN);
                for (int k = 0; k < useBeans.getLength(); k++) {
                    IDOMNode useBean = (IDOMNode) useBeans.item(k);
                    if (useBean.getStartOffset() < contentAssistRequest.getReplacementBeginPosition()) {
                        // $NON-NLS-1$
                        StringBuffer useBeanText = new StringBuffer("<jsp:useBean");
                        for (int j = 0; j < useBean.getAttributes().getLength(); j++) {
                            Attr attr = (Attr) useBean.getAttributes().item(j);
                            useBeanText.append(' ');
                            useBeanText.append(attr.getName());
                            // $NON-NLS-1$
                            useBeanText.append("=\"");
                            useBeanText.append(attr.getValue());
                            useBeanText.append('"');
                        }
                        // $NON-NLS-1$
                        useBeanText.append("/>");
                        additionalShifts += useBeanText.length();
                        contents = useBeanText.toString() + contents;
                    }
                }
                internalModel.getStructuredDocument().set(contents);
                int internalOffset = 0;
                boolean quoted = false;
                // if quoted, use position inside and shift by one
                if (contentAssistRequest.getMatchString().length() > 0 && (contentAssistRequest.getMatchString().charAt(0) == '\'' || contentAssistRequest.getMatchString().charAt(0) == '"')) {
                    internalOffset = contentAssistRequest.getMatchString().length() - 1 + additionalShifts;
                    quoted = true;
                } else // if unquoted, use position inside
                if (contentAssistRequest.getMatchString().length() > 0 && contentAssistRequest.getMatchString().charAt(0) == '<')
                    internalOffset = contentAssistRequest.getMatchString().length() + additionalShifts;
                else
                    internalOffset = contentAssistRequest.getReplacementBeginPosition() - contentAssistRequest.getStartOffset() + additionalShifts;
                depthCount++;
                IndexedRegion internalNode = null;
                int tmpOffset = internalOffset;
                while (internalNode == null && tmpOffset >= 0) internalNode = internalModel.getIndexedRegion(tmpOffset--);
                if (internalModel.getFactoryRegistry() != null) {
                    // set up the internal model
                    if (internalModel.getFactoryRegistry().getFactoryFor(PageDirectiveAdapter.class) == null) {
                        internalModel.getFactoryRegistry().addFactory(new PageDirectiveAdapterFactory());
                    }
                    PageDirectiveAdapter outerEmbeddedTypeAdapter = (PageDirectiveAdapter) xmlOuterModel.getDocument().getAdapterFor(PageDirectiveAdapter.class);
                    PageDirectiveAdapter internalEmbeddedTypeAdapter = (PageDirectiveAdapter) ((INodeNotifier) ((Node) internalNode).getOwnerDocument()).getAdapterFor(PageDirectiveAdapter.class);
                    internalEmbeddedTypeAdapter.setEmbeddedType(outerEmbeddedTypeAdapter.getEmbeddedType());
                }
                AdapterFactoryRegistry adapterRegistry = JSPUIPlugin.getDefault().getAdapterFactoryRegistry();
                Iterator adapterList = adapterRegistry.getAdapterFactories();
                // of content
                while (adapterList.hasNext()) {
                    try {
                        AdapterFactoryProvider provider = (AdapterFactoryProvider) adapterList.next();
                        if (provider.isFor(internalModel.getModelHandler())) {
                            provider.addAdapterFactories(internalModel);
                        }
                    } catch (Exception e) {
                        Logger.logException(e);
                    }
                }
                /**
                 * the internal adapter does all the real work of using
                 * the JSP content model to form proposals
                 */
                ICompletionProposal[] results = null;
                depthCount--;
                if (results != null) {
                    for (i = 0; i < results.length; i++) {
                        contentAssistRequest.addProposal(new CustomCompletionProposal(((CustomCompletionProposal) results[i]).getReplacementString(), ((CustomCompletionProposal) results[i]).getReplacementOffset() - additionalShifts + contentAssistRequest.getStartOffset() + (quoted ? 1 : 0), ((CustomCompletionProposal) results[i]).getReplacementLength(), ((CustomCompletionProposal) results[i]).getCursorPosition(), results[i].getImage(), results[i].getDisplayString(), ((CustomCompletionProposal) results[i]).getContextInformation(), ((CustomCompletionProposal) results[i]).getAdditionalProposalInfo(), (results[i] instanceof IRelevanceCompletionProposal) ? ((IRelevanceCompletionProposal) results[i]).getRelevance() : IRelevanceConstants.R_NONE));
                    }
                }
            }
        } catch (Exception e) {
            // $NON-NLS-1$
            Logger.logException("Error in embedded JSP Content Assist", e);
        }
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) CMNodeWrapper(org.eclipse.wst.xml.core.internal.provisional.contentmodel.CMNodeWrapper) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) PageDirectiveAdapterFactory(org.eclipse.jst.jsp.core.internal.document.PageDirectiveAdapterFactory) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) PageDirectiveAdapter(org.eclipse.jst.jsp.core.internal.document.PageDirectiveAdapter) Document(org.w3c.dom.Document) JSPCMDocument(org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument) IDocument(org.eclipse.jface.text.IDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) Attr(org.w3c.dom.Attr) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) CMDocumentTracker(org.eclipse.wst.xml.core.internal.provisional.contentmodel.CMDocumentTracker) IRelevanceCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.IRelevanceCompletionProposal) Iterator(java.util.Iterator) List(java.util.List) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier) JSPCMDocument(org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) AdapterFactoryProvider(org.eclipse.wst.sse.ui.internal.provisional.registry.AdapterFactoryProvider) TLDCMDocumentManager(org.eclipse.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager) NodeList(org.w3c.dom.NodeList) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) BadLocationException(org.eclipse.jface.text.BadLocationException) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) JSPCMDocument(org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) AdapterFactoryRegistry(org.eclipse.wst.sse.ui.internal.provisional.registry.AdapterFactoryRegistry)

Aggregations

ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)76 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)38 List (java.util.List)22 ArrayList (java.util.ArrayList)19 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)18 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)16 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)15 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)15 Element (org.w3c.dom.Element)15 Document (org.w3c.dom.Document)14 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)13 Node (org.w3c.dom.Node)12 Iterator (java.util.Iterator)10 CMAttributeDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)10 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)10 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)10 NodeList (org.w3c.dom.NodeList)10 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)7 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)7 IFile (org.eclipse.core.resources.IFile)6