Search in sources :

Example 16 with IMessage

use of org.eclipse.wst.validation.internal.provisional.core.IMessage 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 17 with IMessage

use of org.eclipse.wst.validation.internal.provisional.core.IMessage in project webtools.sourceediting by eclipse.

the class AbstractNestedValidator method validate.

/**
 * Perform the validation using version 2 of the validation framework.
 */
@Override
public ValidationResult validate(IResource resource, int kind, ValidationState state, IProgressMonitor monitor) {
    ValidationResult result = new ValidationResult();
    IFile file = null;
    if (resource instanceof IFile)
        file = (IFile) resource;
    if (file != null && shouldValidate(file)) {
        IReporter reporter = result.getReporter(monitor);
        NestedValidatorContext nestedcontext = getNestedContext(state, false);
        boolean teardownRequired = false;
        if (nestedcontext == null) {
            // validationstart was not called, so manually setup and tear
            // down
            nestedcontext = getNestedContext(state, true);
            nestedcontext.setProject(file.getProject());
            setupValidation(nestedcontext);
            teardownRequired = true;
        } else {
            nestedcontext.setProject(file.getProject());
        }
        InputStream inputStream = null;
        try {
            inputStream = file.getContents();
            validate(file, inputStream, result, reporter, nestedcontext);
            List messages = reporter.getMessages();
            for (Object object : messages) {
                if (object instanceof IMessage) {
                    IMessage message = (IMessage) object;
                    message.setLineNo(message.getLineNumber() + 1);
                }
            }
        } catch (CoreException e) {
            Logger.logException(e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                }
            }
        }
        if (teardownRequired)
            teardownValidation(nestedcontext);
    }
    return result;
}
Also used : IFile(org.eclipse.core.resources.IFile) IReporter(org.eclipse.wst.validation.internal.provisional.core.IReporter) CoreException(org.eclipse.core.runtime.CoreException) InputStream(java.io.InputStream) IMessage(org.eclipse.wst.validation.internal.provisional.core.IMessage) List(java.util.List) IOException(java.io.IOException) ValidationResult(org.eclipse.wst.validation.ValidationResult)

Example 18 with IMessage

use of org.eclipse.wst.validation.internal.provisional.core.IMessage in project webtools.sourceediting by eclipse.

the class HTMLValidationReporter method translateMessage.

/**
 * Translate ValidationMessage to IMessage and generate result log
 */
private IMessage translateMessage(ValidationMessage message) {
    int severity = IMessage.LOW_SEVERITY;
    switch(message.getSeverity()) {
        case ValidationMessage.ERROR:
            severity = IMessage.HIGH_SEVERITY;
            break;
        case ValidationMessage.WARNING:
            severity = IMessage.NORMAL_SEVERITY;
            break;
        case ValidationMessage.INFORMATION:
            break;
        default:
            break;
    }
    IMessage mes = new LocalizedMessage(severity, message.getMessage(), this.file);
    mes.setOffset(message.getOffset());
    mes.setLength(message.getLength());
    if (this.model != null) {
        IStructuredDocument flatModel = this.model.getStructuredDocument();
        if (flatModel != null) {
            int line = flatModel.getLineOfOffset(message.getOffset());
            mes.setLineNo(line + 1);
        }
    }
    return mes;
}
Also used : IMessage(org.eclipse.wst.validation.internal.provisional.core.IMessage) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)

Example 19 with IMessage

use of org.eclipse.wst.validation.internal.provisional.core.IMessage in project webtools.sourceediting by eclipse.

the class JSPActionValidatorTest method errorMessageFound.

private boolean errorMessageFound(IReporter reporter, String errorMessage, int errorLineNumber) {
    boolean foundError = false;
    List messages = reporter.getMessages();
    Iterator iter = messages.iterator();
    while (iter.hasNext() && !foundError) {
        IMessage message = (IMessage) iter.next();
        int lineNumber = message.getLineNumber();
        String messageText = message.getText();
        if (lineNumber == errorLineNumber && messageText.startsWith(errorMessage))
            foundError = true;
    }
    return foundError;
}
Also used : IMessage(org.eclipse.wst.validation.internal.provisional.core.IMessage) Iterator(java.util.Iterator) List(java.util.List)

Example 20 with IMessage

use of org.eclipse.wst.validation.internal.provisional.core.IMessage in project webtools.sourceediting by eclipse.

the class JSPDirectiveValidatorTest method testIncludeDirective.

public void testIncludeDirective() throws Exception {
    JSPDirectiveValidator validator = new JSPDirectiveValidator();
    IReporter reporter = new ReporterForTest();
    ValidationContextForTest helper = new ValidationContextForTest();
    String filePath = "/" + PROJECT_NAME + "/WebContent/testinclude.jsp";
    assertTrue("unable to find file: " + filePath, ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(filePath)).exists());
    helper.setURI(filePath);
    validator.validate(helper, reporter);
    boolean foundMissingInclude = false;
    boolean foundIncludeWithError = false;
    if (reporter.getMessages().size() > 0) {
        Iterator it = reporter.getMessages().iterator();
        boolean foundError = false;
        while (it.hasNext() && !foundError) {
            IMessage message = (IMessage) it.next();
            if (message.getLineNumber() == 11)
                foundIncludeWithError = true;
            else if (message.getLineNumber() == 12) {
                String expectedMsg = NLS.bind(JSPCoreMessages.JSPDirectiveValidator_4, new String[] { FRAGMENT_NAME, "/" + PROJECT_NAME + "/WebContent/" + FRAGMENT_NAME });
                if (!expectedMsg.equals(message.getText()))
                    fail("Error found on line 12, but was not a missing fragment error.");
                foundMissingInclude = true;
                break;
            }
        }
    }
    assertFalse("JSP Directive Validator reported an error for a fragment that should be locatable.", foundIncludeWithError);
    assertTrue("JSP Directive Validator did not report the missing fragment.", foundMissingInclude);
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IReporter(org.eclipse.wst.validation.internal.provisional.core.IReporter) IMessage(org.eclipse.wst.validation.internal.provisional.core.IMessage) Iterator(java.util.Iterator) JSPDirectiveValidator(org.eclipse.jst.jsp.core.internal.validation.JSPDirectiveValidator)

Aggregations

IMessage (org.eclipse.wst.validation.internal.provisional.core.IMessage)25 CoreException (org.eclipse.core.runtime.CoreException)7 List (java.util.List)6 Iterator (java.util.Iterator)5 IPath (org.eclipse.core.runtime.IPath)5 Path (org.eclipse.core.runtime.Path)5 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)4 ArrayList (java.util.ArrayList)3 IFile (org.eclipse.core.resources.IFile)3 BadLocationException (org.eclipse.jface.text.BadLocationException)3 Position (org.eclipse.jface.text.Position)3 JSPDirectiveValidator (org.eclipse.jst.jsp.core.internal.validation.JSPDirectiveValidator)3 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)3 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)3 AnnotationInfo (org.eclipse.wst.sse.ui.internal.reconcile.validator.AnnotationInfo)3 IncrementalReporter (org.eclipse.wst.sse.ui.internal.reconcile.validator.IncrementalReporter)3 IReporter (org.eclipse.wst.validation.internal.provisional.core.IReporter)3 IOException (java.io.IOException)2 IMarker (org.eclipse.core.resources.IMarker)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2