Search in sources :

Example 1 with ITLDRecord

use of org.eclipse.jst.jsp.core.taglib.ITLDRecord 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 2 with ITLDRecord

use of org.eclipse.jst.jsp.core.taglib.ITLDRecord in project webtools.sourceediting by eclipse.

the class JSPTaglibCompletionProposalComputer method isTaglibForURI.

private boolean isTaglibForURI(String uri, IPath basePath, ITaglibRecord record) {
    final ITaglibDescriptor descriptor = record.getDescriptor();
    boolean matches = false;
    if (descriptor != null) {
        if (record.getRecordType() == ITaglibRecord.TLD && (descriptor.getURI() == null || "".equals(descriptor.getURI().trim()))) {
            matches = ((ITLDRecord) record).getPath().equals(FacetModuleCoreSupport.resolve(basePath, uri));
        } else {
            matches = descriptor.getURI().toLowerCase(Locale.US).equals(uri.toLowerCase(Locale.US));
        }
    }
    return matches;
}
Also used : ITaglibDescriptor(org.eclipse.jst.jsp.core.taglib.ITaglibDescriptor) ITLDRecord(org.eclipse.jst.jsp.core.taglib.ITLDRecord)

Example 3 with ITLDRecord

use of org.eclipse.jst.jsp.core.taglib.ITLDRecord in project webtools.sourceediting by eclipse.

the class JSPDirectiveValidator method processTaglibDirective.

private void processTaglibDirective(IReporter reporter, IFile file, IStructuredDocument sDoc, ITextRegionCollection documentRegion) {
    ITextRegion prefixValueRegion = null;
    ITextRegion uriValueRegion = getAttributeValueRegion(documentRegion, JSP11Namespace.ATTR_NAME_URI);
    ITextRegion tagdirValueRegion = getAttributeValueRegion(documentRegion, JSP20Namespace.ATTR_NAME_TAGDIR);
    if (uriValueRegion != null) {
        // URI is specified
        String uri = documentRegion.getText(uriValueRegion);
        if (file != null) {
            uri = StringUtils.stripQuotes(uri);
            if (uri.length() > 0) {
                ITaglibRecord reference = TaglibIndex.resolve(file.getFullPath().toString(), uri, false);
                if (reference != null) {
                    switch(reference.getRecordType()) {
                        case (ITaglibRecord.TLD):
                            {
                                ITLDRecord record = (ITLDRecord) reference;
                                IResource tldfile = ResourcesPlugin.getWorkspace().getRoot().getFile(record.getPath());
                                addDependsOn(tldfile);
                            }
                            break;
                        case (ITaglibRecord.JAR):
                            {
                                IJarRecord record = (IJarRecord) reference;
                                IFile[] foundFilesForLocation = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(record.getLocation());
                                for (int i = 0; i < foundFilesForLocation.length; i++) {
                                    addDependsOn(foundFilesForLocation[i]);
                                }
                            }
                            break;
                        case (ITaglibRecord.TAGDIR):
                            {
                                ITagDirRecord record = (ITagDirRecord) reference;
                                IPath path = record.getPath();
                                IResource found = ResourcesPlugin.getWorkspace().getRoot().findMember(path, false);
                                try {
                                    found.accept(new IResourceVisitor() {

                                        public boolean visit(IResource resource) throws CoreException {
                                            if (resource.getType() == IResource.FILE) {
                                                addDependsOn(resource);
                                            }
                                            return true;
                                        }
                                    });
                                } catch (CoreException e) {
                                    Logger.logException(e);
                                }
                            }
                            break;
                        case (ITaglibRecord.URL):
                            {
                                IURLRecord record = (IURLRecord) reference;
                                String baseLocation = record.getBaseLocation();
                                if (baseLocation != null && baseLocation.indexOf("://") < 0) {
                                    // $NON-NLS-1$
                                    IResource found = ResourcesPlugin.getWorkspace().getRoot().findMember(baseLocation, false);
                                    if (found != null) {
                                        try {
                                            found.accept(new IResourceVisitor() {

                                                public boolean visit(IResource resource) throws CoreException {
                                                    if (resource.getType() == IResource.FILE) {
                                                        addDependsOn(resource);
                                                    }
                                                    return true;
                                                }
                                            });
                                        } catch (CoreException e) {
                                            Logger.logException(e);
                                        }
                                    } else {
                                        IFile externalJar = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(new Path(baseLocation));
                                        if (externalJar != null) {
                                            addDependsOn(externalJar);
                                        }
                                    }
                                }
                            }
                            break;
                    }
                }
                if (reference == null && fSeverityTaglibUnresolvableURI != ValidationMessage.IGNORE) {
                    // URI specified but does not resolve
                    String msgText = null;
                    // provide better messages for typical "http:*" URIs
                    final float version = DeploymentDescriptorPropertyCache.getInstance().getJSPVersion(file.getFullPath());
                    if (uri.startsWith("http:") && version < 1.2) {
                        // $NON-NLS-1$
                        if (FacetModuleCoreSupport.isDynamicWebProject(file.getProject())) {
                            msgText = NLS.bind(JSPCoreMessages.JSPDirectiveValidator_9, new Object[] { uri, new Float(version) });
                        } else {
                            msgText = NLS.bind(JSPCoreMessages.JSPDirectiveValidator_10, uri);
                        }
                    } else {
                        msgText = NLS.bind(JSPCoreMessages.JSPDirectiveValidator_1, uri);
                    }
                    LocalizedMessage message = new LocalizedMessage(fSeverityTaglibUnresolvableURI, msgText, file);
                    int start = documentRegion.getStartOffset(uriValueRegion);
                    int length = uriValueRegion.getTextLength();
                    int lineNo = sDoc.getLineOfOffset(start);
                    message.setLineNo(lineNo + 1);
                    message.setOffset(start);
                    message.setLength(length);
                    // $NON-NLS-1$
                    message.setAttribute("PROBLEM_ID", new Integer(611));
                    reporter.addMessage(fMessageOriginator, message);
                }
            } else if (fSeverityTaglibMissingURI != ValidationMessage.IGNORE) {
                // URI specified but empty string
                String msgText = NLS.bind(JSPCoreMessages.JSPDirectiveValidator_3, JSP11Namespace.ATTR_NAME_URI);
                LocalizedMessage message = new LocalizedMessage(fSeverityTaglibMissingURI, msgText, file);
                int start = documentRegion.getStartOffset(uriValueRegion);
                int length = uriValueRegion.getTextLength();
                int lineNo = sDoc.getLineOfOffset(start);
                message.setLineNo(lineNo + 1);
                message.setOffset(start);
                message.setLength(length);
                reporter.addMessage(fMessageOriginator, message);
            }
        }
    } else if (tagdirValueRegion != null) {
        // URI is specified
        String tagdir = documentRegion.getText(tagdirValueRegion);
        if (file != null) {
            tagdir = StringUtils.stripQuotes(tagdir);
            if (tagdir.length() <= 0 && fSeverityTaglibMissingURI != ValidationMessage.IGNORE) {
                // tagdir specified but empty string
                String msgText = NLS.bind(JSPCoreMessages.JSPDirectiveValidator_3, JSP20Namespace.ATTR_NAME_TAGDIR);
                LocalizedMessage message = new LocalizedMessage(fSeverityTaglibMissingURI, msgText, file);
                int start = documentRegion.getStartOffset(tagdirValueRegion);
                int length = tagdirValueRegion.getTextLength();
                int lineNo = sDoc.getLineOfOffset(start);
                message.setLineNo(lineNo + 1);
                message.setOffset(start);
                message.setLength(length);
                reporter.addMessage(fMessageOriginator, message);
            } else if (TaglibIndex.resolve(file.getFullPath().toString(), tagdir, false) == null && fSeverityTagdirUnresolvableURI != ValidationMessage.IGNORE) {
                // URI specified but does not resolve
                String msgText = NLS.bind(JSPCoreMessages.JSPDirectiveValidator_11, tagdir);
                LocalizedMessage message = new LocalizedMessage(fSeverityTaglibUnresolvableURI, msgText, file);
                int start = documentRegion.getStartOffset(tagdirValueRegion);
                int length = tagdirValueRegion.getTextLength();
                int lineNo = sDoc.getLineOfOffset(start);
                message.setLineNo(lineNo + 1);
                message.setOffset(start);
                message.setLength(length);
                reporter.addMessage(fMessageOriginator, message);
            }
        }
    } else if (fSeverityTaglibMissingURI != ValidationMessage.IGNORE) {
        // URI not specified or empty string
        String msgText = NLS.bind(JSPCoreMessages.JSPDirectiveValidator_7, new String[] { JSP20Namespace.ATTR_NAME_TAGDIR, JSP11Namespace.ATTR_NAME_URI });
        LocalizedMessage message = new LocalizedMessage(fSeverityTaglibMissingURI, msgText, file);
        int start = documentRegion.getStartOffset();
        int length = documentRegion.getTextLength();
        int lineNo = sDoc.getLineOfOffset(start);
        message.setLineNo(lineNo + 1);
        message.setOffset(start);
        message.setLength(length);
        reporter.addMessage(fMessageOriginator, message);
    }
    prefixValueRegion = getAttributeValueRegion(documentRegion, JSP11Namespace.ATTR_NAME_PREFIX);
    if (prefixValueRegion != null) {
        // prefix specified
        String taglibPrefix = documentRegion.getText(prefixValueRegion);
        taglibPrefix = StringUtils.stripQuotes(taglibPrefix);
        collectTaglibPrefix(documentRegion, prefixValueRegion, taglibPrefix);
        if (isReservedTaglibPrefix(taglibPrefix)) {
            // prefix is a reserved prefix
            String msgText = NLS.bind(JSPCoreMessages.JSPDirectiveValidator_0, taglibPrefix);
            int sev = IMessage.HIGH_SEVERITY;
            LocalizedMessage message = (file == null ? new LocalizedMessage(sev, msgText) : new LocalizedMessage(sev, msgText, file));
            int start = documentRegion.getStartOffset(prefixValueRegion);
            int length = prefixValueRegion.getTextLength();
            int lineNo = sDoc.getLineOfOffset(start);
            message.setLineNo(lineNo + 1);
            message.setOffset(start);
            message.setLength(length);
            reporter.addMessage(fMessageOriginator, message);
        }
        if (taglibPrefix.length() == 0 && fSeverityTaglibMissingPrefix != ValidationMessage.IGNORE) {
            // prefix is specified but empty
            String msgText = NLS.bind(JSPCoreMessages.JSPDirectiveValidator_3, JSP11Namespace.ATTR_NAME_PREFIX);
            LocalizedMessage message = new LocalizedMessage(fSeverityTaglibMissingPrefix, msgText, file);
            int start = documentRegion.getStartOffset(prefixValueRegion);
            int length = prefixValueRegion.getTextLength();
            int lineNo = sDoc.getLineOfOffset(start);
            message.setLineNo(lineNo + 1);
            message.setOffset(start);
            message.setLength(length);
            reporter.addMessage(fMessageOriginator, message);
        }
    } else if (fSeverityTaglibMissingPrefix != ValidationMessage.IGNORE) {
        // prefix is not specified
        String msgText = NLS.bind(JSPCoreMessages.JSPDirectiveValidator_3, JSP11Namespace.ATTR_NAME_PREFIX);
        LocalizedMessage message = new LocalizedMessage(fSeverityTaglibMissingPrefix, msgText, file);
        int start = documentRegion.getStartOffset();
        int length = documentRegion.getTextLength();
        int lineNo = sDoc.getLineOfOffset(start);
        message.setLineNo(lineNo + 1);
        message.setOffset(start);
        message.setLength(length);
        reporter.addMessage(fMessageOriginator, message);
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IResourceVisitor(org.eclipse.core.resources.IResourceVisitor) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) ITaglibRecord(org.eclipse.jst.jsp.core.taglib.ITaglibRecord) ITLDRecord(org.eclipse.jst.jsp.core.taglib.ITLDRecord) CoreException(org.eclipse.core.runtime.CoreException) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ITagDirRecord(org.eclipse.jst.jsp.core.taglib.ITagDirRecord) IURLRecord(org.eclipse.jst.jsp.core.taglib.IURLRecord) IResource(org.eclipse.core.resources.IResource) IJarRecord(org.eclipse.jst.jsp.core.taglib.IJarRecord)

Example 4 with ITLDRecord

use of org.eclipse.jst.jsp.core.taglib.ITLDRecord in project webtools.sourceediting by eclipse.

the class TLDCMDocumentManager method getModificationStamp.

private long getModificationStamp(String reference) {
    IPath currentParserPath = getCurrentParserPath();
    if (currentParserPath == null) {
        return IResource.NULL_STAMP;
    }
    ITaglibRecord record = TaglibIndex.resolve(currentParserPath.toString(), reference, false);
    long modificationStamp = IResource.NULL_STAMP;
    if (record != null) {
        switch(record.getRecordType()) {
            case (ITaglibRecord.TLD):
                {
                    IFile tldfile = ResourcesPlugin.getWorkspace().getRoot().getFile(((ITLDRecord) record).getPath());
                    if (tldfile.isAccessible()) {
                        modificationStamp = tldfile.getModificationStamp();
                    }
                }
                break;
            case (ITaglibRecord.JAR):
                {
                    File jarfile = new File(((IJarRecord) record).getLocation().toOSString());
                    if (jarfile.exists()) {
                        try {
                            modificationStamp = jarfile.lastModified();
                        } catch (SecurityException e) {
                            modificationStamp = IResource.NULL_STAMP;
                        }
                    }
                }
                break;
            case (ITaglibRecord.TAGDIR):
                {
                    IFolder tagFolder = ResourcesPlugin.getWorkspace().getRoot().getFolder(((ITagDirRecord) record).getPath());
                    if (tagFolder.isAccessible()) {
                        IResource[] members;
                        try {
                            members = tagFolder.members();
                            for (int i = 0; i < members.length; i++) {
                                modificationStamp = Math.max(modificationStamp, members[i].getModificationStamp());
                            }
                        } catch (CoreException e) {
                            modificationStamp = IResource.NULL_STAMP;
                        }
                    }
                }
                break;
            case (ITaglibRecord.URL):
                {
                    String loc = ((IURLRecord) record).getBaseLocation();
                    if (loc != null && loc.endsWith(".jar")) {
                        // $NON-NLS-1$
                        File jarfile = new File(loc);
                        if (jarfile.exists()) {
                            try {
                                modificationStamp = jarfile.lastModified();
                            } catch (SecurityException e) {
                                modificationStamp = IResource.NULL_STAMP;
                            }
                        }
                    }
                }
                break;
            default:
                break;
        }
    }
    return modificationStamp;
}
Also used : IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) CoreException(org.eclipse.core.runtime.CoreException) ITaglibRecord(org.eclipse.jst.jsp.core.taglib.ITaglibRecord) ITLDRecord(org.eclipse.jst.jsp.core.taglib.ITLDRecord) ITagDirRecord(org.eclipse.jst.jsp.core.taglib.ITagDirRecord) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IFolder(org.eclipse.core.resources.IFolder)

Aggregations

ITLDRecord (org.eclipse.jst.jsp.core.taglib.ITLDRecord)4 ITaglibRecord (org.eclipse.jst.jsp.core.taglib.ITaglibRecord)3 IFile (org.eclipse.core.resources.IFile)2 CoreException (org.eclipse.core.runtime.CoreException)2 IPath (org.eclipse.core.runtime.IPath)2 ITagDirRecord (org.eclipse.jst.jsp.core.taglib.ITagDirRecord)2 File (java.io.File)1 List (java.util.List)1 IFolder (org.eclipse.core.resources.IFolder)1 IResource (org.eclipse.core.resources.IResource)1 IResourceVisitor (org.eclipse.core.resources.IResourceVisitor)1 Path (org.eclipse.core.runtime.Path)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IDocument (org.eclipse.jface.text.IDocument)1 IRegion (org.eclipse.jface.text.IRegion)1 ITypedRegion (org.eclipse.jface.text.ITypedRegion)1 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)1 CMElementDeclarationImpl (org.eclipse.jst.jsp.core.internal.contentmodel.tld.CMElementDeclarationImpl)1 TLDCMDocumentManager (org.eclipse.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager)1 TaglibTracker (org.eclipse.jst.jsp.core.internal.contentmodel.tld.TaglibTracker)1