Search in sources :

Example 1 with JSPCMDocument

use of org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument in project webtools.sourceediting by eclipse.

the class JSPModelQueryExtension method getAvailableElementContent.

/**
 * Originally taken from JSPContentAssistProcessor
 *
 * @see org.eclipse.wst.xml.core.internal.contentmodel.modelquery.extension.ModelQueryExtension#getAvailableElementContent(org.w3c.dom.Element, java.lang.String, int)
 */
public CMNode[] getAvailableElementContent(Element parentElement, String namespace, int includeOptions) {
    CMNode[] nodes = EMPTY_CMNODE_ARRAY;
    ArrayList nodeList = new ArrayList();
    // only returns anything if looking for child nodes
    if (((includeOptions & ModelQuery.INCLUDE_CHILD_NODES) != 0) && parentElement instanceof IDOMNode) {
        IDOMNode node = (IDOMNode) parentElement;
        // get position dependent CMDocuments and insert their tags as
        // proposals
        ModelQueryAdapter mqAdapter = null;
        if (node.getNodeType() == Node.DOCUMENT_NODE) {
            mqAdapter = (ModelQueryAdapter) node.getAdapterFor(ModelQueryAdapter.class);
        } else {
            mqAdapter = (ModelQueryAdapter) ((IDOMNode) node.getOwnerDocument()).getAdapterFor(ModelQueryAdapter.class);
        }
        if (mqAdapter != null) {
            CMDocument doc = mqAdapter.getModelQuery().getCorrespondingCMDocument(node);
            if (doc != null) {
                CMDocument jcmdoc = getDefaultJSPCMDocument(node);
                CMNamedNodeMap jspelements = jcmdoc.getElements();
                /* For a built-in JSP action the content model is properly
					 * set up, so don't just blindly add the rest--unless this
					 * will be a direct child of the document
					 */
                if (jspelements != null && (!(doc instanceof JSPCMDocument) || node.getNodeType() == Node.DOCUMENT_NODE)) {
                    List rejectElements = new ArrayList();
                    // determine if the document is in XML form
                    Document domDoc = null;
                    if (node.getNodeType() == Node.DOCUMENT_NODE) {
                        domDoc = (Document) node;
                    } else {
                        domDoc = node.getOwnerDocument();
                    }
                    // Show XML tag forms of JSP markers if jsp:root is
                    // the document element OR it's HTML but
                    // isn't really in the text.
                    // If the document isn't strictly XML, pull out the
                    // XML tag forms it is xml format
                    rejectElements.add(JSP12Namespace.ElementName.SCRIPTLET);
                    rejectElements.add(JSP12Namespace.ElementName.EXPRESSION);
                    rejectElements.add(JSP12Namespace.ElementName.DECLARATION);
                    rejectElements.add(JSP12Namespace.ElementName.DIRECTIVE_INCLUDE);
                    rejectElements.add(JSP12Namespace.ElementName.DIRECTIVE_PAGE);
                    rejectElements.add(JSP12Namespace.ElementName.TEXT);
                    rejectElements.add(JSP12Namespace.ElementName.DIRECTIVE_TAGLIB);
                    rejectElements.add(JSP20Namespace.ElementName.DIRECTIVE_TAG);
                    rejectElements.add(JSP20Namespace.ElementName.DIRECTIVE_ATTRIBUTE);
                    rejectElements.add(JSP20Namespace.ElementName.DIRECTIVE_VARIABLE);
                    if (isXMLFormat(domDoc)) {
                        // jsp actions
                        rejectElements.add(JSP12Namespace.ElementName.FALLBACK);
                        rejectElements.add(JSP12Namespace.ElementName.USEBEAN);
                        rejectElements.add(JSP12Namespace.ElementName.GETPROPERTY);
                        rejectElements.add(JSP12Namespace.ElementName.SETPROPERTY);
                        rejectElements.add(JSP12Namespace.ElementName.INCLUDE);
                        rejectElements.add(JSP12Namespace.ElementName.FORWARD);
                        rejectElements.add(JSP12Namespace.ElementName.PLUGIN);
                        rejectElements.add(JSP12Namespace.ElementName.FALLBACK);
                        rejectElements.add(JSP12Namespace.ElementName.PARAM);
                        rejectElements.add(JSP12Namespace.ElementName.PARAMS);
                    }
                    // don't show jsp:root if a document element already
                    // exists
                    Element docElement = domDoc.getDocumentElement();
                    if (docElement != null && ((docElement.getNodeName().equals(TAG_JSP_ROOT)) || ((((IDOMNode) docElement).getStartStructuredDocumentRegion() != null || ((IDOMNode) docElement).getEndStructuredDocumentRegion() != null)))) {
                        rejectElements.add(JSP12Namespace.ElementName.ROOT);
                    }
                    for (int j = 0; j < jspelements.getLength(); j++) {
                        CMElementDeclaration ed = (CMElementDeclaration) jspelements.item(j);
                        if (!rejectElements.contains(ed.getNodeName())) {
                            nodeList.add(ed);
                        }
                    }
                }
            } else // No cm document (such as for the Document (a non-Element) node itself)
            {
                CMNamedNodeMap jspElements = getDefaultJSPCMDocument(node).getElements();
                int length = jspElements.getLength();
                for (int i = 0; i < length; i++) {
                    nodeList.add(jspElements.item(i));
                }
            }
        }
        nodes = (CMNode[]) nodeList.toArray(new CMNode[nodeList.size()]);
    }
    return nodes;
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) JSPCMDocument(org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument) ModelQueryAdapter(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) JSPCMDocument(org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument) Document(org.w3c.dom.Document) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) JSPCMDocument(org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) ArrayList(java.util.ArrayList) List(java.util.List) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)

Example 2 with JSPCMDocument

use of org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument 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)

Example 3 with JSPCMDocument

use of org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument 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 4 with JSPCMDocument

use of org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument in project webtools.sourceediting by eclipse.

the class LibraryTagsCompletionProposalComputer method forciblyGetTagLibAndJSPElements.

/**
 * <p><b>NOTE: </b>This should be removed as soon as Bug 311961 is fixed</p>
 * <p>This is bad because it does not use the ModelQuery framework, it
 * access the TLDCMDocumentManager directly</p>
 * <p>This is essentially a combination of the {@link TaglibModelQueryExtension} and
 * the {@link JSPModelQueryExtension} but it means any other extensions get left
 * out when creating content assist suggestion at the document root level</p>
 *
 * @param elementDecls
 * @param node
 * @param childIndex
 * @return
 */
private List forciblyGetTagLibAndJSPElements(List elementDecls, Node node, int childIndex) {
    if (node instanceof IDOMNode) {
        /*
			 * find the location of the intended insertion as it will give us
			 * the correct offset for checking position dependent CMDocuments
			 */
        int textInsertionOffset = 0;
        NodeList children = node.getChildNodes();
        if (children.getLength() >= childIndex && childIndex >= 0) {
            Node nodeAlreadyAtIndex = children.item(childIndex);
            if (nodeAlreadyAtIndex instanceof IDOMNode)
                textInsertionOffset = ((IDOMNode) nodeAlreadyAtIndex).getEndOffset();
        } else {
            textInsertionOffset = ((IDOMNode) node).getStartOffset();
        }
        TLDCMDocumentManager mgr = TaglibController.getTLDCMDocumentManager(((IDOMNode) node).getStructuredDocument());
        if (mgr != null) {
            List moreCMDocuments = mgr.getCMDocumentTrackers(textInsertionOffset);
            if (moreCMDocuments != null) {
                for (int i = 0; i < moreCMDocuments.size(); i++) {
                    CMDocument doc = (CMDocument) moreCMDocuments.get(i);
                    CMNamedNodeMap elements = doc.getElements();
                    if (elements != null) {
                        for (int j = 0; j < elements.getLength(); j++) {
                            CMElementDeclaration ed = (CMElementDeclaration) elements.item(j);
                            elementDecls.add(ed);
                        }
                    }
                }
            }
        }
        // get position dependent CMDocuments and insert their tags as
        // proposals
        ModelQueryAdapter mqAdapter = null;
        if (node.getNodeType() == Node.DOCUMENT_NODE)
            mqAdapter = (ModelQueryAdapter) ((IDOMNode) node).getAdapterFor(ModelQueryAdapter.class);
        else
            mqAdapter = (ModelQueryAdapter) ((IDOMNode) node.getOwnerDocument()).getAdapterFor(ModelQueryAdapter.class);
        if (mqAdapter != null) {
            CMDocument doc = mqAdapter.getModelQuery().getCorrespondingCMDocument(node);
            if (doc != null) {
                CMDocument jcmdoc = getDefaultJSPCMDocument((IDOMNode) node);
                CMNamedNodeMap jspelements = jcmdoc.getElements();
                /*
					 * For a built-in JSP action the content model is properly
					 * set up, so don't just blindly add the rest--unless this
					 * will be a direct child of the document
					 */
                if (jspelements != null && (!(doc instanceof JSPCMDocument) || node.getNodeType() == Node.DOCUMENT_NODE)) {
                    List rejectElements = new ArrayList();
                    // determine if the document is in XML form
                    Document domDoc = null;
                    if (node.getNodeType() == Node.DOCUMENT_NODE)
                        domDoc = (Document) node;
                    else
                        domDoc = node.getOwnerDocument();
                    // Show XML tag forms of JSP markers if jsp:root is
                    // the document element OR it's HTML but
                    // isn't really in the text.
                    // If the document isn't strictly XML, pull out the
                    // XML tag forms it is xml format
                    rejectElements.add(JSP12Namespace.ElementName.SCRIPTLET);
                    rejectElements.add(JSP12Namespace.ElementName.EXPRESSION);
                    rejectElements.add(JSP12Namespace.ElementName.DECLARATION);
                    rejectElements.add(JSP12Namespace.ElementName.DIRECTIVE_INCLUDE);
                    rejectElements.add(JSP12Namespace.ElementName.DIRECTIVE_PAGE);
                    rejectElements.add(JSP12Namespace.ElementName.TEXT);
                    rejectElements.add(JSP12Namespace.ElementName.DIRECTIVE_TAGLIB);
                    rejectElements.add(JSP20Namespace.ElementName.DIRECTIVE_TAG);
                    rejectElements.add(JSP20Namespace.ElementName.DIRECTIVE_ATTRIBUTE);
                    rejectElements.add(JSP20Namespace.ElementName.DIRECTIVE_VARIABLE);
                    if (isXMLFormat(domDoc)) {
                        // jsp actions
                        rejectElements.add(JSP12Namespace.ElementName.FALLBACK);
                        rejectElements.add(JSP12Namespace.ElementName.USEBEAN);
                        rejectElements.add(JSP12Namespace.ElementName.GETPROPERTY);
                        rejectElements.add(JSP12Namespace.ElementName.SETPROPERTY);
                        rejectElements.add(JSP12Namespace.ElementName.INCLUDE);
                        rejectElements.add(JSP12Namespace.ElementName.FORWARD);
                        rejectElements.add(JSP12Namespace.ElementName.PLUGIN);
                        rejectElements.add(JSP12Namespace.ElementName.FALLBACK);
                        rejectElements.add(JSP12Namespace.ElementName.PARAM);
                        rejectElements.add(JSP12Namespace.ElementName.PARAMS);
                    }
                    // don't show jsp:root if a document element already
                    // exists
                    Element docElement = domDoc.getDocumentElement();
                    if (// $NON-NLS-1$
                    docElement != null && ((docElement.getNodeName().equals("jsp:root")) || ((((IDOMNode) docElement).getStartStructuredDocumentRegion() != null || ((IDOMNode) docElement).getEndStructuredDocumentRegion() != null))))
                        rejectElements.add(JSP12Namespace.ElementName.ROOT);
                    for (int j = 0; j < jspelements.getLength(); j++) {
                        CMElementDeclaration ed = (CMElementDeclaration) jspelements.item(j);
                        if (rejectElements.contains(ed.getNodeName()))
                            continue;
                        elementDecls.add(ed);
                    }
                }
            } else // No cm document (such as for the Document (a non-Element) node itself)
            {
                CMNamedNodeMap jspElements = getDefaultJSPCMDocument((IDOMNode) node).getElements();
                int length = jspElements.getLength();
                for (int i = 0; i < length; i++) {
                    elementDecls.add(jspElements.item(i));
                }
            }
        }
    }
    return elementDecls;
}
Also used : JSPCMDocument(org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) TLDCMDocumentManager(org.eclipse.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager) ModelQueryAdapter(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter) NodeList(org.w3c.dom.NodeList) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) JSPCMDocument(org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) JSPCMDocument(org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument) List(java.util.List) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)

Aggregations

ArrayList (java.util.ArrayList)4 List (java.util.List)4 JSPCMDocument (org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument)4 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)4 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)4 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)4 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)3 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)3 Document (org.w3c.dom.Document)3 NodeList (org.w3c.dom.NodeList)3 Iterator (java.util.Iterator)2 TLDCMDocumentManager (org.eclipse.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager)2 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)2 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)2 CustomCompletionProposal (org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)2 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)2 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)2 CMNodeWrapper (org.eclipse.wst.xml.core.internal.provisional.contentmodel.CMNodeWrapper)2 ModelQueryAdapter (org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter)2 Element (org.w3c.dom.Element)2