Search in sources :

Example 1 with IJSPTranslation

use of org.eclipse.jst.jsp.core.internal.java.IJSPTranslation in project webtools.sourceediting by eclipse.

the class JSPJavaValidator method performValidation.

void performValidation(IFile f, IReporter reporter, IStructuredModel model) {
    for (int i = 0; i < DEPEND_ONs.length; i++) {
        addDependsOn(f.getProject().getFile(DEPEND_ONs[i]));
    }
    if (model instanceof IDOMModel) {
        IDOMModel domModel = (IDOMModel) model;
        ModelHandlerForJSP.ensureTranslationAdapterFactory(domModel);
        IDOMDocument xmlDoc = domModel.getDocument();
        JSPTranslationAdapter translationAdapter = (JSPTranslationAdapter) xmlDoc.getAdapterFor(IJSPTranslation.class);
        IJSPTranslation translation = translationAdapter.getJSPTranslation();
        if (!reporter.isCancelled()) {
            loadPreferences(f);
            // only update task markers if the model is the same as what's on disk
            boolean updateJavaTasks = UPDATE_JAVA_TASKS && !domModel.isDirty() && f != null && f.isAccessible();
            if (updateJavaTasks) {
                // remove old Java task markers
                try {
                    IMarker[] foundMarkers = f.findMarkers(JAVA_TASK_MARKER_TYPE, true, IResource.DEPTH_ONE);
                    for (int i = 0; i < foundMarkers.length; i++) {
                        foundMarkers[i].delete();
                    }
                } catch (CoreException e) {
                    Logger.logException(e);
                }
            }
            translation.setProblemCollectingActive(true);
            translation.reconcileCompilationUnit();
            List problems = translation.getProblems();
            // add new messages
            for (int i = 0; i < problems.size() && !reporter.isCancelled(); i++) {
                IProblem problem = (IProblem) problems.get(i);
                /*
					 * Possible error in problem collection; EL translation is
					 * extensible, so we must be paranoid about this.
					 */
                if (problem == null)
                    continue;
                IMessage m = createMessageFromProblem(problem, f, translation, domModel.getStructuredDocument());
                if (m != null) {
                    if (problem.getID() == IProblem.Task) {
                        if (updateJavaTasks) {
                            // add new Java task marker
                            try {
                                IMarker task = f.createMarker(JAVA_TASK_MARKER_TYPE);
                                task.setAttribute(IMarker.LINE_NUMBER, new Integer(m.getLineNumber()));
                                task.setAttribute(IMarker.CHAR_START, new Integer(m.getOffset()));
                                task.setAttribute(IMarker.CHAR_END, new Integer(m.getOffset() + m.getLength()));
                                task.setAttribute(IMarker.MESSAGE, m.getText());
                                task.setAttribute(IMarker.USER_EDITABLE, Boolean.FALSE);
                                switch(m.getSeverity()) {
                                    case IMessage.HIGH_SEVERITY:
                                        {
                                            task.setAttribute(IMarker.PRIORITY, new Integer(IMarker.PRIORITY_HIGH));
                                            task.setAttribute(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
                                        }
                                        break;
                                    case IMessage.LOW_SEVERITY:
                                        {
                                            task.setAttribute(IMarker.PRIORITY, new Integer(IMarker.PRIORITY_LOW));
                                            task.setAttribute(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
                                        }
                                        break;
                                    default:
                                        {
                                            task.setAttribute(IMarker.PRIORITY, new Integer(IMarker.PRIORITY_NORMAL));
                                            task.setAttribute(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
                                        }
                                }
                            } catch (CoreException e) {
                                Logger.logException(e);
                            }
                        }
                    } else {
                        reporter.addMessage(fMessageOriginator, m);
                    }
                }
            }
        }
    }
    unloadPreferences();
}
Also used : IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IMessage(org.eclipse.wst.validation.internal.provisional.core.IMessage) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslationAdapter(org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter) IProblem(org.eclipse.jdt.core.compiler.IProblem) CoreException(org.eclipse.core.runtime.CoreException) List(java.util.List) IMarker(org.eclipse.core.resources.IMarker)

Example 2 with IJSPTranslation

use of org.eclipse.jst.jsp.core.internal.java.IJSPTranslation in project webtools.sourceediting by eclipse.

the class JSPJavaHyperlinkDetector method createHyperlink.

private IHyperlink createHyperlink(IJavaElement element, IRegion region, IDocument document) {
    IHyperlink link = null;
    if (region != null) {
        // open local variable in the JSP file...
        boolean isInTranslationCU = false;
        if (element instanceof ISourceReference) {
            IFile file = null;
            int jspOffset = 0;
            // try to locate the file in the workspace
            ITextFileBuffer textFileBuffer = FileBuffers.getTextFileBufferManager().getTextFileBuffer(document);
            if (textFileBuffer != null && textFileBuffer.getLocation() != null) {
                file = getFile(textFileBuffer.getLocation().toString());
            }
            // get Java range and translate to JSP range
            try {
                ISourceRange range = null;
                IJSPTranslation jspTranslation = getJSPTranslation(document);
                if (jspTranslation != null) {
                    // link to local variable definitions
                    if (element instanceof ILocalVariable) {
                        range = ((ILocalVariable) element).getNameRange();
                        Object cu = ((ILocalVariable) element).getAncestor(IJavaElement.COMPILATION_UNIT);
                        if (cu != null && cu.equals(jspTranslation.getCompilationUnit()))
                            isInTranslationCU = true;
                    } else // linking to fields of the same compilation unit
                    if (element.getElementType() == IJavaElement.FIELD) {
                        Object cu = ((IField) element).getCompilationUnit();
                        if (cu != null && cu.equals(jspTranslation.getCompilationUnit())) {
                            range = ((ISourceReference) element).getSourceRange();
                            isInTranslationCU = true;
                        }
                    } else // linking to methods of the same compilation unit
                    if (element.getElementType() == IJavaElement.METHOD) {
                        Object cu = ((IMethod) element).getCompilationUnit();
                        if (cu != null && cu.equals(jspTranslation.getCompilationUnit())) {
                            range = ((ISourceReference) element).getSourceRange();
                            isInTranslationCU = true;
                        }
                    }
                }
                if (jspTranslation != null && range != null && file != null) {
                    jspOffset = jspTranslation.getJspOffset(range.getOffset());
                    if (jspOffset >= 0) {
                        link = new WorkspaceFileHyperlink(region, file, new Region(jspOffset, range.getLength()));
                    }
                }
            } catch (JavaModelException jme) {
                Logger.log(Logger.WARNING_DEBUG, jme.getMessage(), jme);
            }
        }
        if (link == null && !isInTranslationCU) {
            // Don't try to open the translation CU
            link = new JSPJavaHyperlink(region, element);
        }
    }
    return link;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) ILocalVariable(org.eclipse.jdt.core.ILocalVariable) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IMethod(org.eclipse.jdt.core.IMethod) ISourceReference(org.eclipse.jdt.core.ISourceReference) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 3 with IJSPTranslation

use of org.eclipse.jst.jsp.core.internal.java.IJSPTranslation in project webtools.sourceediting by eclipse.

the class JSPJavaValidator method createMessageFromProblem.

/**
 * Creates an IMessage from asn IProblem
 *
 * @param problem
 * @param f
 * @param translation
 * @param structuredDoc
 * @return message representation of the problem, or null if it could not
 *         create one
 */
private IMessage createMessageFromProblem(IProblem problem, IFile f, IJSPTranslation translation, IStructuredDocument structuredDoc) {
    int sev = -1;
    int sourceStart = -1;
    int sourceEnd = -1;
    if (problem instanceof IJSPProblem) {
        sourceStart = problem.getSourceStart();
        sourceEnd = problem.getSourceEnd();
        switch(((IJSPProblem) problem).getEID()) {
            case IJSPProblem.TEIClassNotFound:
                sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_TRANSLATION_TEI_CLASS_NOT_FOUND);
                break;
            case IJSPProblem.TEIValidationMessage:
                sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_TRANSLATION_TEI_VALIDATION_MESSAGE);
                break;
            case IJSPProblem.TEIClassNotInstantiated:
                sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_TRANSLATION_TEI_CLASS_NOT_INSTANTIATED);
                break;
            case IJSPProblem.TEIClassMisc:
                sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_TRANSLATION_TEI_CLASS_RUNTIME_EXCEPTION);
                break;
            case IJSPProblem.TagClassNotFound:
                sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_TRANSLATION_TAG_HANDLER_CLASS_NOT_FOUND);
                break;
            case IJSPProblem.UseBeanInvalidID:
                sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_TRANSLATION_USEBEAN_INVALID_ID);
                break;
            case IJSPProblem.UseBeanMissingTypeInfo:
                sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_TRANSLATION_USBEAN_MISSING_TYPE_INFO);
                break;
            case IJSPProblem.UseBeanAmbiguousType:
                sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_TRANSLATION_USEBEAN_AMBIGUOUS_TYPE_INFO);
                break;
            default:
                sev = problem.isError() ? IMessage.HIGH_SEVERITY : (problem.isWarning() ? IMessage.NORMAL_SEVERITY : ValidationMessage.IGNORE);
        }
    } else {
        sourceStart = translation.getJspOffset(problem.getSourceStart());
        sourceEnd = translation.getJspOffset(problem.getSourceEnd());
        switch(problem.getID()) {
            case IProblem.LocalVariableIsNeverUsed:
                {
                    sev = getSourceSeverity(JSPCorePreferenceNames.VALIDATION_JAVA_LOCAL_VARIABLE_NEVER_USED, sourceStart, sourceEnd);
                }
                break;
            case IProblem.NullLocalVariableReference:
                {
                    sev = getSourceSeverity(JSPCorePreferenceNames.VALIDATION_JAVA_NULL_LOCAL_VARIABLE_REFERENCE, sourceStart, sourceEnd);
                }
                break;
            case IProblem.ArgumentIsNeverUsed:
                {
                    sev = getSourceSeverity(JSPCorePreferenceNames.VALIDATION_JAVA_ARGUMENT_IS_NEVER_USED, sourceStart, sourceEnd);
                }
                break;
            case IProblem.PotentialNullLocalVariableReference:
                {
                    sev = getSourceSeverity(JSPCorePreferenceNames.VALIDATION_JAVA_POTENTIAL_NULL_LOCAL_VARIABLE_REFERENCE, sourceStart, sourceEnd);
                }
                break;
            case IProblem.UnusedImport:
                {
                    sev = getSourceSeverity(JSPCorePreferenceNames.VALIDATION_JAVA_UNUSED_IMPORT, sourceStart, sourceEnd);
                }
                break;
            case IProblem.UnusedPrivateField:
            case IProblem.MissingSerialVersion:
                {
                    // JSP files don't get serialized...right?
                    sev = ValidationMessage.IGNORE;
                }
                break;
            case IProblem.UnresolvedVariable:
                {
                    try {
                        // If the unresolved variable is in a fragment, post as a warning. The fragment may be included in a page that has declared the variable
                        IContentType contentType = f.getContentDescription().getContentType();
                        IContentType fragmentType = Platform.getContentTypeManager().getContentType(ContentTypeIdForJSP.ContentTypeID_JSPFRAGMENT);
                        if (contentType != null && fragmentType != null && contentType.isKindOf(fragmentType)) {
                            sev = IMessage.NORMAL_SEVERITY;
                            break;
                        }
                    }// Doesn't matter, we'll just fall back to the default
                     catch (CoreException e) {
                    }
                }
            default:
                {
                    if (problem.isError()) {
                        sev = IMessage.HIGH_SEVERITY;
                    } else if (problem.isWarning()) {
                        sev = IMessage.NORMAL_SEVERITY;
                    } else {
                        sev = IMessage.LOW_SEVERITY;
                    }
                }
                if (sev == ValidationMessage.IGNORE) {
                    return null;
                }
                /* problems without JSP positions are in generated code */
                if (sourceStart == -1) {
                    int problemID = problem.getID();
                    /*
						 * Quoting IProblem doc: "When a problem is tagged as
						 * Internal, it means that no change other than a
						 * local source code change can fix the corresponding
						 * problem." Assuming that our generated code is
						 * correct, that should reduce the reported problems
						 * to those the user can correct.
						 */
                    if (((problemID & IProblem.Internal) != 0) && ((problemID & IProblem.Syntax) != 0) && translation instanceof JSPTranslation) {
                        // Attach to the last code scripting section
                        JSPTranslation jspTranslation = ((JSPTranslation) translation);
                        Position[] jspPositions = (Position[]) jspTranslation.getJsp2JavaMap().keySet().toArray(new Position[jspTranslation.getJsp2JavaMap().size()]);
                        for (int i = 0; i < jspPositions.length; i++) {
                            sourceStart = Math.max(sourceStart, jspPositions[i].getOffset());
                        }
                        IMessage m = new LocalizedMessage(sev, problem.getMessage(), f);
                        m.setOffset(sourceStart);
                        m.setLength(1);
                        return m;
                    } else {
                        return null;
                    }
                }
        }
    }
    if (sev == ValidationMessage.IGNORE) {
        return null;
    }
    final boolean isIndirect = translation.isIndirect(problem.getSourceStart());
    if (isIndirect && !FragmentValidationTools.shouldValidateFragment(f)) {
        return null;
    }
    // line number for marker starts @ 1
    // line number from document starts @ 0
    int lineNo = structuredDoc.getLineOfOffset(sourceStart) + 1;
    IMessage m = new LocalizedMessage(sev, problem.getMessage(), f);
    m.setLineNo(lineNo);
    m.setOffset(sourceStart);
    m.setLength((sourceEnd >= sourceStart) ? (sourceEnd - sourceStart + 1) : 0);
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=119633
    if (isIndirect) {
        adjustIndirectPosition(m, translation);
    }
    return m;
}
Also used : IJSPProblem(org.eclipse.jst.jsp.core.internal.java.IJSPProblem) CoreException(org.eclipse.core.runtime.CoreException) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslation(org.eclipse.jst.jsp.core.internal.java.JSPTranslation) Position(org.eclipse.jface.text.Position) IMessage(org.eclipse.wst.validation.internal.provisional.core.IMessage) IContentType(org.eclipse.core.runtime.content.IContentType)

Example 4 with IJSPTranslation

use of org.eclipse.jst.jsp.core.internal.java.IJSPTranslation in project webtools.sourceediting by eclipse.

the class JSPJavaTranslatorCoreTest method test_codas.

public void test_codas() throws Exception {
    String testName = "testPreludeAndCodas";
    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(testName);
    synchronized (creationLock) {
        if (!project.isAccessible()) {
            // Create new project
            project = BundleResourceUtil.createSimpleProject(testName, null, null);
            assertTrue(project.exists());
            BundleResourceUtil.copyBundleEntriesIntoWorkspace("/testfiles/" + testName, "/" + testName);
        }
    }
    IFile main = project.getFile("/web stuff/coda-user/test.jsp");
    assertTrue("sample test file not accessible", main.isAccessible());
    IDOMModel model = null;
    try {
        model = (IDOMModel) StructuredModelManager.getModelManager().getModelForEdit(main);
        ModelHandlerForJSP.ensureTranslationAdapterFactory(model);
        JSPTranslationAdapter translationAdapter = (JSPTranslationAdapter) model.getDocument().getAdapterFor(IJSPTranslation.class);
        IJSPTranslation translation = translationAdapter.getJSPTranslation();
        assertNotNull("no Java translation found", translation);
        assertTrue("coda0 contents not included", translation.getJavaText().indexOf("int coda0") > 0);
        assertTrue("coda1 contents not included", translation.getJavaText().indexOf("int coda1") > 0);
        assertTrue("import statement not found", translation.getJavaText().indexOf("import java.lang.ref.Reference") > 0);
    } finally {
        if (model != null)
            model.releaseFromEdit();
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslationAdapter(org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter) IProject(org.eclipse.core.resources.IProject)

Example 5 with IJSPTranslation

use of org.eclipse.jst.jsp.core.internal.java.IJSPTranslation in project webtools.sourceediting by eclipse.

the class JSPJavaTranslatorCoreTest method test_prelude_and_coda.

public void test_prelude_and_coda() throws Exception {
    String testName = "testPreludeAndCodas";
    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(testName);
    synchronized (creationLock) {
        if (!project.isAccessible()) {
            // Create new project
            project = BundleResourceUtil.createSimpleProject(testName, null, null);
            assertTrue(project.exists());
            BundleResourceUtil.copyBundleEntriesIntoWorkspace("/testfiles/" + testName, "/" + testName);
        }
    }
    IFile main = project.getFile("/web stuff/both/test.jsp");
    assertTrue("sample test file not accessible", main.isAccessible());
    IDOMModel model = null;
    try {
        model = (IDOMModel) StructuredModelManager.getModelManager().getModelForEdit(main);
        ModelHandlerForJSP.ensureTranslationAdapterFactory(model);
        JSPTranslationAdapter translationAdapter = (JSPTranslationAdapter) model.getDocument().getAdapterFor(IJSPTranslation.class);
        IJSPTranslation translation = translationAdapter.getJSPTranslation();
        assertNotNull("no Java translation found", translation);
        assertTrue("prelude0 contents not included", translation.getJavaText().indexOf("int prelude0") > 0);
        assertTrue("prelude1 contents included", translation.getJavaText().indexOf("int prelude1") < 0);
        assertTrue("coda0 contents not included", translation.getJavaText().indexOf("int coda0") > 0);
        assertTrue("coda1 contents included", translation.getJavaText().indexOf("int coda1") < 0);
        assertTrue("import statement not found", translation.getJavaText().indexOf("import java.lang.ref.Reference") > 0);
    } finally {
        if (model != null)
            model.releaseFromEdit();
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslationAdapter(org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter) IProject(org.eclipse.core.resources.IProject)

Aggregations

IJSPTranslation (org.eclipse.jst.jsp.core.internal.java.IJSPTranslation)8 JSPTranslationAdapter (org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter)6 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)6 IFile (org.eclipse.core.resources.IFile)5 IProject (org.eclipse.core.resources.IProject)4 CoreException (org.eclipse.core.runtime.CoreException)2 IMessage (org.eclipse.wst.validation.internal.provisional.core.IMessage)2 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)2 List (java.util.List)1 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)1 IMarker (org.eclipse.core.resources.IMarker)1 IContentType (org.eclipse.core.runtime.content.IContentType)1 ILocalVariable (org.eclipse.jdt.core.ILocalVariable)1 IMethod (org.eclipse.jdt.core.IMethod)1 ISourceRange (org.eclipse.jdt.core.ISourceRange)1 ISourceReference (org.eclipse.jdt.core.ISourceReference)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 IProblem (org.eclipse.jdt.core.compiler.IProblem)1 IRegion (org.eclipse.jface.text.IRegion)1 Position (org.eclipse.jface.text.Position)1