Search in sources :

Example 1 with IJarRecord

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

the class TestIndex method testWebXMLTaglibMappingsToJARs.

public void testWebXMLTaglibMappingsToJARs() throws Exception {
    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("bug_148717");
    if (!project.exists()) {
        // Create new project
        project = BundleResourceUtil.createSimpleProject("bug_148717", null, null);
        assertTrue(project.exists());
        BundleResourceUtil.copyBundleEntriesIntoWorkspace("/testfiles/bug_148717", "/bug_148717");
    }
    IFile file = project.getFile("/WebContent/WEB-INF/lib/internal.jar");
    assertTrue(file.exists());
    String uri = "http://example.com/external-uri";
    ITaglibRecord taglibRecord = TaglibIndex.resolve("/bug_148717/WebContent/", uri, false);
    assertNotNull("record not found for " + uri, taglibRecord);
    assertEquals(ITaglibRecord.JAR, taglibRecord.getRecordType());
    assertEquals(uri, ((IJarRecord) taglibRecord).getDescriptor().getURI());
    ITaglibRecord taglibRecord2 = null;
    ITaglibRecord[] records = TaglibIndex.getAvailableTaglibRecords(new Path("/bug_148717/WebContent/"));
    for (int i = 0; i < records.length; i++) {
        int type = records[i].getRecordType();
        switch(type) {
            case ITaglibRecord.JAR:
                {
                    taglibRecord2 = records[i];
                }
                break;
        }
    }
    assertNotNull("record not returned for " + uri, taglibRecord2);
    assertEquals(ITaglibRecord.JAR, taglibRecord2.getRecordType());
    assertEquals(uri, ((IJarRecord) taglibRecord2).getDescriptor().getURI());
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) ITaglibRecord(org.eclipse.jst.jsp.core.taglib.ITaglibRecord) IProject(org.eclipse.core.resources.IProject) IJarRecord(org.eclipse.jst.jsp.core.taglib.IJarRecord)

Example 2 with IJarRecord

use of org.eclipse.jst.jsp.core.taglib.IJarRecord 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)

Aggregations

IFile (org.eclipse.core.resources.IFile)2 Path (org.eclipse.core.runtime.Path)2 IJarRecord (org.eclipse.jst.jsp.core.taglib.IJarRecord)2 ITaglibRecord (org.eclipse.jst.jsp.core.taglib.ITaglibRecord)2 IProject (org.eclipse.core.resources.IProject)1 IResource (org.eclipse.core.resources.IResource)1 IResourceVisitor (org.eclipse.core.resources.IResourceVisitor)1 CoreException (org.eclipse.core.runtime.CoreException)1 IPath (org.eclipse.core.runtime.IPath)1 ITLDRecord (org.eclipse.jst.jsp.core.taglib.ITLDRecord)1 ITagDirRecord (org.eclipse.jst.jsp.core.taglib.ITagDirRecord)1 IURLRecord (org.eclipse.jst.jsp.core.taglib.IURLRecord)1 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)1