Search in sources :

Example 1 with DefaultProblem

use of org.eclipse.wst.jsdt.internal.compiler.problem.DefaultProblem in project webtools.sourceediting by eclipse.

the class JSDTSourceValidator method validateFile.

/**
 * @param result
 */
private void validateFile(IValidationContext helper, IReporter reporter, IFile file, ValidationResult result) {
    if ((reporter != null) && (reporter.isCancelled() == true)) {
        throw new OperationCanceledException();
    }
    if (!shouldValidate(file)) {
        return;
    }
    char[] source = fDocument.get().toCharArray();
    if (source == null) {
        return;
    }
    parser.setSource(source);
    parser.setProject(JavaScriptCore.create(file.getProject()));
    JavaScriptUnit unit = (JavaScriptUnit) parser.createAST(new NullProgressMonitor());
    if (unit == null) {
        Message valMessage = new // $NON-NLS-1$
        Message(// $NON-NLS-1$
        JavaScriptUI.ID_PLUGIN, // $NON-NLS-1$
        IMessage.HIGH_SEVERITY, // $NON-NLS-1$
        JavaScriptUI.ID_PLUGIN + ".problem") {

            /**
             * @see IMessage#getText(Locale, ClassLoader)
             */
            public java.lang.String getText(Locale locale, ClassLoader classLoader) {
                // $NON-NLS-1$
                return "AST couldn't be created due to the fatal error";
            }
        };
        valMessage.setOffset(0);
        valMessage.setLength(0);
        valMessage.setLineNo(0);
        reporter.addMessage(this, valMessage);
    } else if (unit.getProblems().length > 0) {
        for (IProblem problem : unit.getProblems()) {
            final String msg = problem.getMessage();
            String[] arguments = problem.getArguments();
            int severity = problem.isError() ? IMessage.HIGH_SEVERITY : IMessage.ERROR_AND_WARNING;
            int lineNumber = problem.getSourceLineNumber();
            int sourceStart = problem.getSourceStart();
            int sourceEnd = problem.getSourceEnd();
            int columnNumber = (problem instanceof DefaultProblem) ? ((DefaultProblem) problem).getSourceColumnNumber() : -1;
            Message valMessage = new Message(JavaScriptUI.ID_PLUGIN, severity, JavaScriptUI.ID_PLUGIN + ".problem") {

                /**
                 * @see IMessage#getText(Locale, ClassLoader)
                 */
                public java.lang.String getText(Locale locale, ClassLoader classLoader) {
                    return msg;
                }
            };
            Position position = sourceEnd == -1 ? calcPosition(source, sourceStart) : new Position(sourceStart, sourceEnd - sourceStart);
            valMessage.setOffset(position.getOffset());
            valMessage.setLength(position.getLength());
            valMessage.setLineNo(lineNumber);
            // System.out.println(getClass().getName() + ": " + valMessage.getLineNumber() +
            // "[" + valMessage.getOffset() + ":" + valMessage.getLength() + "] : " +
            // valMessage.getText() +
            // "==>>>" + String.copyValueOf(source, position.getOffset(), position.getLength()) + "<<<==");
            reporter.addMessage(this, valMessage);
        }
    }
}
Also used : Locale(java.util.Locale) JavaScriptUnit(org.eclipse.wst.jsdt.core.dom.JavaScriptUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IMessage(org.eclipse.wst.validation.internal.provisional.core.IMessage) Message(org.eclipse.wst.validation.internal.core.Message) Position(org.eclipse.jface.text.Position) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IProblem(org.eclipse.wst.jsdt.core.compiler.IProblem) DefaultProblem(org.eclipse.wst.jsdt.internal.compiler.problem.DefaultProblem)

Aggregations

Locale (java.util.Locale)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 Position (org.eclipse.jface.text.Position)1 IProblem (org.eclipse.wst.jsdt.core.compiler.IProblem)1 JavaScriptUnit (org.eclipse.wst.jsdt.core.dom.JavaScriptUnit)1 DefaultProblem (org.eclipse.wst.jsdt.internal.compiler.problem.DefaultProblem)1 Message (org.eclipse.wst.validation.internal.core.Message)1 IMessage (org.eclipse.wst.validation.internal.provisional.core.IMessage)1