Search in sources :

Example 56 with ModelQuery

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

the class HTMLTagInfoTest method getCMElementDeclaration.

/**
 * Retreives CMElementDeclaration for given node
 *
 * @return CMElementDeclaration - CMElementDeclaration of node or
 *         <code>null</code> if not possible
 */
private CMElementDeclaration getCMElementDeclaration(Element element) {
    CMElementDeclaration result = null;
    ModelQuery modelQuery = ModelQueryUtil.getModelQuery(element.getOwnerDocument());
    if (modelQuery != null)
        result = modelQuery.getCMElementDeclaration(element);
    return result;
}
Also used : CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)

Example 57 with ModelQuery

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

the class CommentElementHandlerForSSI method createElement.

public Element createElement(Document document, String data, boolean isJSPTag) {
    ModelQuery modelQuery = ModelQueryUtil.getModelQuery(document);
    if (modelQuery == null) {
        return null;
    }
    CMDocument cm = modelQuery.getCorrespondingCMDocument(document);
    if (cm == null) {
        return null;
    }
    CMNamedNodeMap map = cm.getElements();
    if (map == null) {
        return null;
    }
    TagScanner scanner = new TagScanner(data, 1);
    String name = scanner.nextName();
    if (name == null) {
        return null;
    }
    StringBuffer buffer = new StringBuffer(name.length() + 4);
    buffer.append(SSI_PREFIX);
    buffer.append(':');
    buffer.append(name);
    String tagName = buffer.toString();
    // check if valid (defined) SSI tag or not
    if (map.getNamedItem(tagName) == null) {
        return null;
    }
    CommentElementFactory factory = new CommentElementFactory(document, isJSPTag, this);
    Element element = factory.create(tagName, CommentElementFactory.IS_START);
    // set attributes
    String attrName = scanner.nextName();
    while (attrName != null) {
        String attrValue = scanner.nextValue();
        Attr attr = document.createAttribute(attrName);
        if (attr != null) {
            if (attrValue != null)
                attr.setValue(attrValue);
            element.setAttributeNode(attr);
        }
        attrName = scanner.nextName();
    }
    return element;
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) CommentElementFactory(org.eclipse.wst.xml.core.internal.commentelement.util.CommentElementFactory) Element(org.w3c.dom.Element) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) TagScanner(org.eclipse.wst.xml.core.internal.commentelement.util.TagScanner) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) Attr(org.w3c.dom.Attr)

Example 58 with ModelQuery

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

the class ElementNodeCleanupHandler method isEmptyElement.

private boolean isEmptyElement(IDOMElement element) {
    Document document = element.getOwnerDocument();
    if (document == null)
        // undefined tag, return default
        return false;
    ModelQuery modelQuery = ModelQueryUtil.getModelQuery(document);
    if (modelQuery == null)
        // undefined tag, return default
        return false;
    CMElementDeclaration decl = modelQuery.getCMElementDeclaration(element);
    if (decl == null)
        // undefined tag, return default
        return false;
    return (decl.getContentType() == CMElementDeclaration.EMPTY);
}
Also used : CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) Document(org.w3c.dom.Document) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)

Example 59 with ModelQuery

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

the class LibraryTagsCompletionProposalComputer method addAttributeValueProposals.

/**
 * @see org.eclipse.wst.xml.ui.internal.contentassist.DefaultXMLCompletionProposalComputer#addAttributeValueProposals(org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest, org.eclipse.wst.sse.ui.contentassist.CompletionProposalInvocationContext)
 */
protected void addAttributeValueProposals(ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context) {
    if (!this.isXHTML) {
        IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
        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 (doc instanceof JSPCMDocument || doc instanceof CMNodeWrapper || node.getNodeName().startsWith("jsp:")) {
                // $NON-NLS-1$
                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 = AbstractXMLModelQueryCompletionProposalComputer.getCMElementDeclaration(node);
        if (nameRegion != null && elementDecl != null) {
            String attributeName = open.getText(nameRegion);
            if (attributeName != null) {
                Node parent = contentAssistRequest.getParent();
                // ignore start quote in match string
                String matchString = contentAssistRequest.getMatchString().trim();
                if (matchString.startsWith("'") || matchString.startsWith("\"")) {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    matchString = matchString.substring(1);
                }
                // get all the proposals
                List additionalElements = ModelQueryUtil.getModelQuery(node.getOwnerDocument()).getAvailableContent((Element) node, elementDecl, ModelQuery.INCLUDE_ALL);
                Iterator nodeIterator = additionalElements.iterator();
                // check each suggestion
                while (nodeIterator.hasNext()) {
                    CMNode additionalElementDecl = (CMNode) nodeIterator.next();
                    if (additionalElementDecl != null && additionalElementDecl instanceof CMElementDeclaration && validModelQueryNode(additionalElementDecl)) {
                        CMElementDeclaration ed = (CMElementDeclaration) additionalElementDecl;
                        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=89811
                        StringBuffer sb = new StringBuffer();
                        getContentGenerator().generateTag(parent, ed, sb);
                        String proposedText = sb.toString();
                        // filter out any proposals that dont match matchString
                        if (beginsWith(proposedText, matchString)) {
                            // wrap with ' because JSP attributes are warped with "
                            // $NON-NLS-1$
                            proposedText = "'" + proposedText;
                            // don't want to risk injecting an extra
                            if (!(contentAssistRequest.getRegion() instanceof ITextRegionContainer)) {
                                // $NON-NLS-1$
                                proposedText += "'";
                            }
                            // get the image
                            Image image = CMImageUtil.getImage(elementDecl);
                            if (image == null) {
                                image = this.getGenericTagImage();
                            }
                            // create the proposal
                            int cursorAdjustment = getCursorPositionForProposedText(proposedText);
                            String proposedInfo = AbstractXMLModelQueryCompletionProposalComputer.getAdditionalInfo(AbstractXMLModelQueryCompletionProposalComputer.getCMElementDeclaration(parent), elementDecl);
                            String tagname = getContentGenerator().getRequiredName(node, ed);
                            CustomCompletionProposal proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), cursorAdjustment, image, tagname, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
                            contentAssistRequest.addProposal(proposal);
                        }
                    }
                }
            }
        }
    }
}
Also used : JSPCMDocument(org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) CMNodeWrapper(org.eclipse.wst.xml.core.internal.provisional.contentmodel.CMNodeWrapper) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) Node(org.w3c.dom.Node) 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) JSPCMDocument(org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) List(java.util.List) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode)

Example 60 with ModelQuery

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

the class HTMLTagsCompletionProposalComputer method addHTMLTagProposal.

/**
 * <p>adds HTML tag proposal for empty document</p>
 *
 * @param contentAssistRequest request to add proposal too
 * @param context context of the completion request
 */
private void addHTMLTagProposal(ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context) {
    IStructuredModel model = null;
    try {
        if (context.getDocument() instanceof IStructuredDocument) {
            model = StructuredModelManager.getModelManager().getModelForRead((IStructuredDocument) context.getDocument());
        }
        if (model != null) {
            IDOMDocument doc = ((IDOMModel) model).getDocument();
            ModelQuery mq = ModelQueryUtil.getModelQuery(doc);
            if (mq != null) {
                // XHTML requires lowercase tagname for lookup
                CMDocument correspondingCMDocument = mq.getCorrespondingCMDocument(doc);
                if (correspondingCMDocument != null) {
                    CMElementDeclaration htmlDecl = (CMElementDeclaration) correspondingCMDocument.getElements().getNamedItem(HTML40Namespace.ElementName.HTML.toLowerCase());
                    if (htmlDecl != null) {
                        StringBuffer proposedTextBuffer = new StringBuffer();
                        getContentGenerator().generateTag(doc, htmlDecl, proposedTextBuffer);
                        String proposedText = proposedTextBuffer.toString();
                        String requiredName = getContentGenerator().getRequiredName(doc, htmlDecl);
                        IStructuredDocumentRegion region = contentAssistRequest.getDocumentRegion();
                        if (region != null) {
                            if (region.getFirstRegion() != null && region.getFirstRegion().getType().equals(DOMRegionContext.XML_TAG_OPEN)) {
                                // in order to differentiate between content assist on
                                // completely empty document and the one with xml open tag
                                proposedText = proposedText.substring(1);
                            }
                        }
                        if (!beginsWith(proposedText, contentAssistRequest.getMatchString())) {
                            return;
                        }
                        int cursorAdjustment = getCursorPositionForProposedText(proposedText);
                        CustomCompletionProposal proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), cursorAdjustment, HTMLEditorPluginImageHelper.getInstance().getImage(HTMLEditorPluginImages.IMG_OBJ_TAG_GENERIC), requiredName, null, null, XMLRelevanceConstants.R_TAG_NAME);
                        contentAssistRequest.addProposal(proposal);
                    }
                }
            }
        }
    } finally {
        if (model != null)
            model.releaseFromRead();
    }
}
Also used : HTMLCMDocument(org.eclipse.wst.html.core.internal.contentmodel.HTMLCMDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)

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