Search in sources :

Example 6 with TaglibTracker

use of org.eclipse.jst.jsp.core.internal.contentmodel.tld.TaglibTracker in project webtools.sourceediting by eclipse.

the class TaglibHyperlinkDetector method detectHyperlinks.

public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
    IHyperlink hyperlink = null;
    if (textViewer != null && region != null) {
        IDocument doc = textViewer.getDocument();
        if (doc != null) {
            try {
                // check if jsp tag/directive first
                ITypedRegion partition = TextUtilities.getPartition(doc, IStructuredPartitioning.DEFAULT_STRUCTURED_PARTITIONING, region.getOffset(), false);
                if (partition != null && partition.getType() == IJSPPartitions.JSP_DIRECTIVE) {
                    IStructuredModel sModel = null;
                    try {
                        sModel = StructuredModelManager.getModelManager().getExistingModelForRead(doc);
                        // check if jsp taglib directive
                        Node currentNode = getCurrentNode(sModel, region.getOffset());
                        if (currentNode != null && currentNode.getNodeType() == Node.ELEMENT_NODE) {
                            String baseLocationForTaglib = getBaseLocationForTaglib(doc);
                            if (baseLocationForTaglib != null && JSP11Namespace.ElementName.DIRECTIVE_TAGLIB.equalsIgnoreCase(currentNode.getNodeName())) {
                                /**
                                 * The taglib directive itself
                                 */
                                // get the uri attribute
                                Attr taglibURINode = ((Element) currentNode).getAttributeNode(JSP11Namespace.ATTR_NAME_URI);
                                if (taglibURINode != null) {
                                    ITaglibRecord reference = TaglibIndex.resolve(baseLocationForTaglib, taglibURINode.getValue(), false);
                                    // there's nothing to link to
                                    if (reference != null) {
                                        // handle taglibs
                                        switch(reference.getRecordType()) {
                                            case (ITaglibRecord.TLD):
                                                {
                                                    ITLDRecord record = (ITLDRecord) reference;
                                                    String uriString = record.getPath().toString();
                                                    IRegion hyperlinkRegion = getHyperlinkRegion(taglibURINode, region);
                                                    if (hyperlinkRegion != null) {
                                                        hyperlink = createHyperlink(uriString, hyperlinkRegion, doc, null);
                                                    }
                                                }
                                                break;
                                            case (ITaglibRecord.JAR):
                                            case (ITaglibRecord.URL):
                                                {
                                                    IRegion hyperlinkRegion = getHyperlinkRegion(taglibURINode, region);
                                                    if (hyperlinkRegion != null) {
                                                        hyperlink = new TaglibJarUriHyperlink(hyperlinkRegion, reference);
                                                    }
                                                }
                                        }
                                    }
                                }
                            } else if (baseLocationForTaglib != null && JSP12Namespace.ElementName.ROOT.equalsIgnoreCase(currentNode.getNodeName())) {
                                /**
                                 * The jsp:root element
                                 */
                                NamedNodeMap attrs = currentNode.getAttributes();
                                for (int i = 0; i < attrs.getLength(); i++) {
                                    Attr attr = (Attr) attrs.item(i);
                                    if (attr.getNodeName().startsWith(XMLNS)) {
                                        String uri = StringUtils.strip(attr.getNodeValue());
                                        if (uri.startsWith(URN_TLD)) {
                                            uri = uri.substring(URN_TLD.length());
                                        }
                                        ITaglibRecord reference = TaglibIndex.resolve(baseLocationForTaglib, uri, false);
                                        // there's nothing to link to
                                        if (reference != null) {
                                            // handle taglibs
                                            switch(reference.getRecordType()) {
                                                case (ITaglibRecord.TLD):
                                                    {
                                                        ITLDRecord record = (ITLDRecord) reference;
                                                        String uriString = record.getPath().toString();
                                                        IRegion hyperlinkRegion = getHyperlinkRegion(attr, region);
                                                        if (hyperlinkRegion != null) {
                                                            hyperlink = createHyperlink(uriString, hyperlinkRegion, doc, null);
                                                        }
                                                    }
                                                    break;
                                                case (ITaglibRecord.JAR):
                                                case (ITaglibRecord.URL):
                                                    {
                                                        IRegion hyperlinkRegion = getHyperlinkRegion(attr, region);
                                                        if (hyperlinkRegion != null) {
                                                            hyperlink = new TaglibJarUriHyperlink(hyperlinkRegion, reference);
                                                        }
                                                    }
                                            }
                                        }
                                    }
                                }
                            } else {
                                /**
                                 * Hyperlink custom tag to its TLD or tag file
                                 */
                                TLDCMDocumentManager documentManager = TaglibController.getTLDCMDocumentManager(doc);
                                if (documentManager != null) {
                                    List documentTrackers = documentManager.getCMDocumentTrackers(currentNode.getPrefix(), region.getOffset());
                                    for (int i = 0; i < documentTrackers.size(); i++) {
                                        TaglibTracker tracker = (TaglibTracker) documentTrackers.get(i);
                                        CMElementDeclaration decl = (CMElementDeclaration) tracker.getElements().getNamedItem(currentNode.getNodeName());
                                        if (decl != null) {
                                            decl = (CMElementDeclaration) ((CMNodeWrapper) decl).getOriginNode();
                                            if (decl instanceof CMElementDeclarationImpl) {
                                                String base = ((CMElementDeclarationImpl) decl).getLocationString();
                                                IRegion hyperlinkRegion = getHyperlinkRegion(currentNode, region);
                                                if (hyperlinkRegion != null) {
                                                    hyperlink = createHyperlink(base, hyperlinkRegion, doc, currentNode);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    } finally {
                        if (sModel != null)
                            sModel.releaseFromRead();
                    }
                }
            } catch (BadLocationException e) {
                Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
            }
        }
    }
    if (hyperlink != null)
        return new IHyperlink[] { hyperlink };
    return null;
}
Also used : TLDCMDocumentManager(org.eclipse.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager) NamedNodeMap(org.w3c.dom.NamedNodeMap) ITaglibRecord(org.eclipse.jst.jsp.core.taglib.ITaglibRecord) ITLDRecord(org.eclipse.jst.jsp.core.taglib.ITLDRecord) TaglibTracker(org.eclipse.jst.jsp.core.internal.contentmodel.tld.TaglibTracker) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Attr(org.w3c.dom.Attr) IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) IRegion(org.eclipse.jface.text.IRegion) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) ITypedRegion(org.eclipse.jface.text.ITypedRegion) CMElementDeclarationImpl(org.eclipse.jst.jsp.core.internal.contentmodel.tld.CMElementDeclarationImpl) List(java.util.List) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) NodeList(org.w3c.dom.NodeList) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 7 with TaglibTracker

use of org.eclipse.jst.jsp.core.internal.contentmodel.tld.TaglibTracker in project webtools.sourceediting by eclipse.

the class JSPELContentAssistProcessor method getFunctionProposals.

protected List getFunctionProposals(String prefix, StructuredTextViewer viewer, int offset) {
    TLDCMDocumentManager docMgr = TaglibController.getTLDCMDocumentManager(viewer.getDocument());
    ArrayList completionList = new ArrayList();
    if (docMgr == null)
        return null;
    Iterator taglibs = docMgr.getCMDocumentTrackers(offset).iterator();
    while (taglibs.hasNext()) {
        TaglibTracker tracker = (TaglibTracker) taglibs.next();
        if (tracker.getPrefix().equals(prefix)) {
            CMDocumentImpl doc = (CMDocumentImpl) tracker.getDocument();
            List functions = doc.getFunctions();
            for (Iterator it = functions.iterator(); it.hasNext(); ) {
                TLDFunction function = (TLDFunction) it.next();
                CustomCompletionProposal proposal = new // $NON-NLS-1$
                CustomCompletionProposal(// $NON-NLS-1$
                function.getName() + "()", offset, 0, function.getName().length() + 1, null, function.getName() + " - " + function.getSignature(), null, null, // $NON-NLS-1$
                1);
                completionList.add(proposal);
            }
        }
    }
    return completionList;
}
Also used : TLDCMDocumentManager(org.eclipse.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager) TaglibTracker(org.eclipse.jst.jsp.core.internal.contentmodel.tld.TaglibTracker) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) ArrayList(java.util.ArrayList) List(java.util.List) TLDFunction(org.eclipse.jst.jsp.core.internal.contentmodel.tld.provisional.TLDFunction) CMDocumentImpl(org.eclipse.jst.jsp.core.internal.contentmodel.tld.CMDocumentImpl)

Example 8 with TaglibTracker

use of org.eclipse.jst.jsp.core.internal.contentmodel.tld.TaglibTracker in project webtools.sourceediting by eclipse.

the class ELGeneratorVisitor method genFunction.

/**
 * Generate a function invocation.
 *
 * @param fullFunctionName
 * @return
 */
protected String genFunction(String fullFunctionName) {
    TLDCMDocumentManager docMgr = TaglibController.getTLDCMDocumentManager(fDocument);
    int colonIndex = fullFunctionName.indexOf(':');
    String prefix = fullFunctionName.substring(0, colonIndex);
    String functionName = fullFunctionName.substring(colonIndex + 1);
    if (docMgr == null)
        return null;
    Iterator taglibs = docMgr.getCMDocumentTrackers(fCurrentNode.getStartOffset()).iterator();
    while (taglibs.hasNext()) {
        TaglibTracker tracker = (TaglibTracker) taglibs.next();
        if (tracker.getPrefix().equals(prefix)) {
            CMDocumentImpl doc = (CMDocumentImpl) tracker.getDocument();
            List functions = doc.getFunctions();
            for (Iterator it = functions.iterator(); it.hasNext(); ) {
                TLDFunction function = (TLDFunction) it.next();
                if (function.getName().equals(functionName)) {
                    String javaFuncName = getFunctionNameFromSignature(function.getSignature());
                    if (javaFuncName == null)
                        javaFuncName = functionName;
                    // $NON-NLS-1$
                    return function.getClassName() + "." + javaFuncName;
                }
            }
        }
    }
    return null;
}
Also used : TLDCMDocumentManager(org.eclipse.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager) TaglibTracker(org.eclipse.jst.jsp.core.internal.contentmodel.tld.TaglibTracker) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) TLDFunction(org.eclipse.jst.jsp.core.internal.contentmodel.tld.provisional.TLDFunction) CMDocumentImpl(org.eclipse.jst.jsp.core.internal.contentmodel.tld.CMDocumentImpl)

Aggregations

List (java.util.List)8 TLDCMDocumentManager (org.eclipse.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager)8 TaglibTracker (org.eclipse.jst.jsp.core.internal.contentmodel.tld.TaglibTracker)8 ArrayList (java.util.ArrayList)6 Iterator (java.util.Iterator)6 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)4 CMDocumentImpl (org.eclipse.jst.jsp.core.internal.contentmodel.tld.CMDocumentImpl)3 TLDFunction (org.eclipse.jst.jsp.core.internal.contentmodel.tld.provisional.TLDFunction)3 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)3 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)3 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)3 TLDElementDeclaration (org.eclipse.jst.jsp.core.internal.contentmodel.tld.provisional.TLDElementDeclaration)2 CustomCompletionProposal (org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)2 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)2 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)2 CMNodeWrapper (org.eclipse.wst.xml.core.internal.provisional.contentmodel.CMNodeWrapper)2 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 HashSet (java.util.HashSet)1