Search in sources :

Example 11 with IMessage

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

the class JSPDirectiveValidatorTest method testBug265710Expression.

public void testBug265710Expression() throws Exception {
    JSPDirectiveValidator validator = new JSPDirectiveValidator();
    IReporter reporter = new ReporterForTest();
    ValidationContextForTest helper = new ValidationContextForTest();
    String filePath = "/" + PROJECT_NAME + "/WebContent/bug265710expression.jsp";
    assertTrue("unable to find file: " + filePath, ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(filePath)).exists());
    helper.setURI(filePath);
    validator.validate(helper, reporter);
    if (reporter.getMessages().size() > 0) {
        Iterator it = reporter.getMessages().iterator();
        while (it.hasNext()) {
            IMessage message = (IMessage) it.next();
            if (message.getLineNumber() == 14 && message.getSeverity() == IMessage.HIGH_SEVERITY) {
                fail("JSP Directive Validator flagged a JSP expression in the import directive");
            }
        }
    }
}
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)

Example 12 with IMessage

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

the class MarkupValidator method checkStartEndTagPairs.

private void checkStartEndTagPairs(IStructuredDocumentRegion sdRegion, IReporter reporter) {
    if (sdRegion.isDeleted()) {
        return;
    }
    // check start/end tag pairs
    IDOMNode xmlNode = getXMLNode(sdRegion);
    if (xmlNode == null) {
        return;
    }
    boolean selfClosed = false;
    String tagName = null;
    /**
     * For tags that aren't meant to be EMPTY, make sure it's empty or has an end tag
     */
    if (xmlNode.isContainer()) {
        IStructuredDocumentRegion endRegion = xmlNode.getEndStructuredDocumentRegion();
        if (endRegion == null) {
            IStructuredDocumentRegion startRegion = xmlNode.getStartStructuredDocumentRegion();
            if (startRegion != null && !startRegion.isDeleted() && DOMRegionContext.XML_TAG_OPEN.equals(startRegion.getFirstRegion().getType())) {
                // analyze the tag (check self closing)
                ITextRegionList regions = startRegion.getRegions();
                ITextRegion r = null;
                int start = sdRegion.getStart();
                int length = sdRegion.getTextLength();
                for (int i = 0; i < regions.size(); i++) {
                    r = regions.get(i);
                    if (r.getType() == DOMRegionContext.XML_TAG_NAME) {
                        tagName = sdRegion.getText(r);
                        start = sdRegion.getStartOffset(r);
                        length = r.getTextLength();
                    } else if (r.getType() == DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
                        selfClosed = true;
                    }
                }
                if (!selfClosed && (tagName != null)) {
                    Object[] args = { tagName };
                    String messageText = NLS.bind(XMLCoreMessages.Missing_end_tag_, args);
                    int lineNumber = getLineNumber(start);
                    LocalizedMessage message = new LocalizedMessage(getPluginPreference().getInt(XMLCorePreferenceNames.MISSING_END_TAG), messageText);
                    message.setOffset(start);
                    message.setLength(length);
                    message.setLineNo(lineNumber);
                    Object[] additionalFixInfo = getStartEndFixInfo(xmlNode, tagName, r);
                    getAnnotationMsg(reporter, ProblemIDsXML.MissingEndTag, message, additionalFixInfo, length);
                }
            }
        } else {
            IStructuredDocumentRegion startRegion = xmlNode.getStartStructuredDocumentRegion();
            if (startRegion == null || startRegion.isDeleted()) {
                // analyze the tag (check self closing)
                ITextRegionList regions = endRegion.getRegions();
                ITextRegion r = null;
                int start = sdRegion.getStart();
                int length = sdRegion.getTextLength();
                for (int i = 0; i < regions.size(); i++) {
                    r = regions.get(i);
                    if (r.getType() == DOMRegionContext.XML_TAG_NAME) {
                        tagName = sdRegion.getText(r);
                        start = sdRegion.getStartOffset(r);
                        length = r.getTextLength();
                    }
                }
                if (tagName != null) {
                    Object[] args = { tagName };
                    String messageText = NLS.bind(XMLCoreMessages.Missing_start_tag_, args);
                    int lineNumber = getLineNumber(start);
                    LocalizedMessage message = new LocalizedMessage(getPluginPreference().getInt(XMLCorePreferenceNames.MISSING_START_TAG), messageText);
                    message.setOffset(start);
                    message.setLength(length);
                    message.setLineNo(lineNumber);
                    Object[] additionalFixInfo = getStartEndFixInfo(xmlNode, tagName, r);
                    getAnnotationMsg(reporter, ProblemIDsXML.MissingStartTag, message, additionalFixInfo, length);
                }
            }
        }
    } else /*
		 * Check for an end tag that has no start tag
		 */
    {
        IStructuredDocumentRegion startRegion = xmlNode.getStartStructuredDocumentRegion();
        if (startRegion == null) {
            IStructuredDocumentRegion endRegion = xmlNode.getEndStructuredDocumentRegion();
            if (!endRegion.isDeleted()) {
                // get name
                ITextRegionList regions = endRegion.getRegions();
                ITextRegion r = null;
                for (int i = 0; i < regions.size(); i++) {
                    r = regions.get(i);
                    if (r.getType() == DOMRegionContext.XML_TAG_NAME) {
                        tagName = sdRegion.getText(r);
                    }
                }
                if (!selfClosed && (tagName != null)) {
                    String messageText = XMLCoreMessages.Indicate_no_grammar_specified_severities_error;
                    int start = sdRegion.getStart();
                    int lineNumber = getLineNumber(start);
                    // SEVERITY_STRUCTURE == IMessage.HIGH_SEVERITY
                    IMessage message = new LocalizedMessage(IMessage.HIGH_SEVERITY, messageText);
                    message.setOffset(start);
                    message.setLength(sdRegion.getTextLength());
                    message.setLineNo(lineNumber);
                    reporter.addMessage(this, message);
                }
            }
        }
    }
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IMessage(org.eclipse.wst.validation.internal.provisional.core.IMessage) LocalizedMessage(org.eclipse.wst.validation.internal.operations.LocalizedMessage)

Example 13 with IMessage

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

the class DelegatingSourceValidator method updateValidationMessages.

/**
 * iterates through the messages and calculates a "better" offset and
 * length
 *
 * @param messages -
 *            a List of IMessages
 * @param document -
 *            the document
 * @param reporter -
 *            the reporter the messages are to be added to
 */
protected void updateValidationMessages(List messages, IDOMDocument document, IReporter reporter) {
    for (int i = 0; i < messages.size(); i++) {
        IMessage message = (IMessage) messages.get(i);
        try {
            if (message.getAttribute(COLUMN_NUMBER_ATTRIBUTE) != null) {
                int column = ((Integer) message.getAttribute(COLUMN_NUMBER_ATTRIBUTE)).intValue();
                if (message.getAttribute(AnnotationMsg.PROBMLEM_ID) != null && reporter instanceof IncrementalReporter) {
                    Integer problemId = (Integer) message.getAttribute(AnnotationMsg.PROBMLEM_ID);
                    MarkupQuickAssistProcessor processor = new MarkupQuickAssistProcessor();
                    processor.setProblemId(problemId.intValue());
                    message.setOffset(column);
                    Integer length = (Integer) message.getAttribute(AnnotationMsg.LENGTH);
                    message.setLength(length.intValue());
                    Object attrValue = message.getAttribute(AnnotationMsg.ATTRVALUETEXT);
                    if (attrValue != null)
                        processor.setAdditionalFixInfo(attrValue);
                    else {
                        Object attrValueNo = message.getAttribute(AnnotationMsg.ATTRVALUENO);
                        if (attrValueNo != null) {
                            int len = ((Integer) attrValueNo).intValue();
                            Object[] objArray = new Object[len];
                            for (int j = 0; j < len; j++) {
                                objArray[j] = message.getAttribute(AnnotationMsg.ATTRNO + j);
                            }
                            processor.setAdditionalFixInfo(objArray);
                        }
                    }
                    message.setAttribute(QUICKASSISTPROCESSOR, processor);
                    AnnotationInfo info = new AnnotationInfo(message);
                    ((IncrementalReporter) reporter).addAnnotationInfo(this, info);
                } else {
                    String selectionStrategy = (String) message.getAttribute(SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE);
                    String nameOrValue = (String) message.getAttribute(SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE);
                    // convert the line and Column numbers to an offset:
                    int start = document.getStructuredDocument().getLineOffset(message.getLineNumber() - 1) + column - 1;
                    int[] result = computeStartAndEndLocation(start, selectionStrategy, getErrorSide(message), nameOrValue, document);
                    if (result != null) {
                        message.setOffset(result[0]);
                        message.setLength(result[1] - result[0]);
                        reporter.addMessage(this, message);
                    }
                }
            }
        } catch (BadLocationException e) {
        // this exception should not
        // occur - it is thrown if
        // trying to convert an
        // invalid line number to and
        // offset
        }
    }
}
Also used : IncrementalReporter(org.eclipse.wst.sse.ui.internal.reconcile.validator.IncrementalReporter) IMessage(org.eclipse.wst.validation.internal.provisional.core.IMessage) BadLocationException(org.eclipse.jface.text.BadLocationException) AnnotationInfo(org.eclipse.wst.sse.ui.internal.reconcile.validator.AnnotationInfo)

Example 14 with IMessage

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

the class TLDValidator method validate.

public ValidationResult validate(IResource resource, int kind, ValidationState state, IProgressMonitor monitor) {
    if (resource.getType() != IResource.FILE)
        return null;
    ValidationResult result = new ValidationResult();
    IFile file = (IFile) resource;
    if (file.isAccessible()) {
        // TAGX
        if (fTagXexts.contains(file.getFileExtension()) || fTagXnames.contains(file.getName())) {
            monitor.beginTask("", 4);
            org.eclipse.wst.xml.core.internal.validation.eclipse.Validator xmlValidator = new org.eclipse.wst.xml.core.internal.validation.eclipse.Validator();
            ValidationResult result3 = new MarkupValidator().validate(resource, kind, state, new SubProgressMonitor(monitor, 1));
            if (monitor.isCanceled())
                return result;
            ValidationResult result2 = xmlValidator.validate(resource, kind, state, new SubProgressMonitor(monitor, 1));
            if (monitor.isCanceled())
                return result;
            ValidationResult result1 = new JSPActionValidator().validate(resource, kind, state, new SubProgressMonitor(monitor, 1));
            List messages = new ArrayList(result1.getReporter(new NullProgressMonitor()).getMessages());
            messages.addAll(result2.getReporter(new NullProgressMonitor()).getMessages());
            messages.addAll(result3.getReporter(new NullProgressMonitor()).getMessages());
            messages.addAll(new JSPDirectiveValidator().validate(resource, kind, state, new SubProgressMonitor(monitor, 1)).getReporter(new NullProgressMonitor()).getMessages());
            for (int i = 0; i < messages.size(); i++) {
                IMessage message = (IMessage) messages.get(i);
                if (message.getText() != null && message.getText().length() > 0) {
                    ValidatorMessage vmessage = ValidatorMessage.create(message.getText(), resource);
                    if (message.getAttributes() != null) {
                        Map attrs = message.getAttributes();
                        Iterator it = attrs.entrySet().iterator();
                        while (it.hasNext()) {
                            Map.Entry entry = (Map.Entry) it.next();
                            if (!(entry.getValue() instanceof String || entry.getValue() instanceof Integer || entry.getValue() instanceof Boolean)) {
                                it.remove();
                            }
                        }
                        vmessage.setAttributes(attrs);
                    }
                    vmessage.setAttribute(IMarker.LINE_NUMBER, message.getLineNumber());
                    vmessage.setAttribute(IMarker.MESSAGE, message.getText());
                    if (message.getOffset() > -1) {
                        vmessage.setAttribute(IMarker.CHAR_START, message.getOffset());
                        vmessage.setAttribute(IMarker.CHAR_END, message.getOffset() + message.getLength());
                    }
                    int severity = 0;
                    switch(message.getSeverity()) {
                        case IMessage.HIGH_SEVERITY:
                            severity = IMarker.SEVERITY_ERROR;
                            break;
                        case IMessage.NORMAL_SEVERITY:
                            severity = IMarker.SEVERITY_WARNING;
                            break;
                        case IMessage.LOW_SEVERITY:
                            severity = IMarker.SEVERITY_INFO;
                            break;
                    }
                    vmessage.setAttribute(IMarker.SEVERITY, severity);
                    vmessage.setType(MARKER_TYPE);
                    result.add(vmessage);
                }
            }
            monitor.done();
        } else // TLD
        {
            try {
                final IJavaProject javaProject = JavaCore.create(file.getProject());
                if (javaProject.exists()) {
                    IScopeContext[] scopes = new IScopeContext[] { new InstanceScope(), new DefaultScope() };
                    ProjectScope projectScope = new ProjectScope(file.getProject());
                    if (projectScope.getNode(PREFERENCE_NODE_QUALIFIER).getBoolean(JSPCorePreferenceNames.VALIDATION_USE_PROJECT_SETTINGS, false)) {
                        scopes = new IScopeContext[] { projectScope, new InstanceScope(), new DefaultScope() };
                    }
                    Map[] problems = detectProblems(javaProject, file, scopes);
                    for (int i = 0; i < problems.length; i++) {
                        ValidatorMessage message = ValidatorMessage.create(problems[i].get(IMarker.MESSAGE).toString(), resource);
                        message.setType(MARKER_TYPE);
                        message.setAttributes(problems[i]);
                        result.add(message);
                    }
                }
            } catch (Exception e) {
                Logger.logException(e);
            }
        }
    }
    return result;
}
Also used : ProjectScope(org.eclipse.core.resources.ProjectScope) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) ValidationResult(org.eclipse.wst.validation.ValidationResult) IScopeContext(org.eclipse.core.runtime.preferences.IScopeContext) Iterator(java.util.Iterator) InstanceScope(org.eclipse.core.runtime.preferences.InstanceScope) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) IMessage(org.eclipse.wst.validation.internal.provisional.core.IMessage) DefaultScope(org.eclipse.core.runtime.preferences.DefaultScope) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) ValidatorMessage(org.eclipse.wst.validation.ValidatorMessage) JavaModelException(org.eclipse.jdt.core.JavaModelException) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) IJavaProject(org.eclipse.jdt.core.IJavaProject) HashMap(java.util.HashMap) Map(java.util.Map) MarkupValidator(org.eclipse.wst.xml.core.internal.validation.MarkupValidator) AbstractValidator(org.eclipse.wst.validation.AbstractValidator) MarkupValidator(org.eclipse.wst.xml.core.internal.validation.MarkupValidator)

Example 15 with IMessage

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

the class JsValidator method createMessageFromProblem.

/**
 * Creates an IMessage from an IProblem
 *
 * @param problem
 * @param f
 * @param translation
 * @param textDoc
 * @return message representation of the problem, or null if it could not
 *         create one
 */
private IMessage createMessageFromProblem(IProblem problem, IFile f, IJsTranslation translation, IDocument textDoc) {
    int sourceStart = problem.getSourceStart();
    int sourceEnd = problem.getSourceEnd();
    if (sourceStart == -1) {
        return null;
    }
    sourceStart = translation.getWebPageOffset(sourceStart);
    sourceEnd = translation.getWebPageOffset(sourceEnd);
    /*
		 * Bug 241794 - Validation shows errors when using JSP Expressions
		 * inside JavaScript code
		 */
    IStructuredDocument doc = (IStructuredDocument) textDoc;
    IStructuredDocumentRegion documentRegion = doc.getRegionAtCharacterOffset(sourceStart);
    if (documentRegion != null) {
        ITextRegion textRegion = documentRegion.getRegionAtCharacterOffset(sourceStart);
        /*
			 * Filter out problems from areas that aren't simple JavaScript,
			 * e.g. JSP.
			 */
        if (textRegion != null && textRegion instanceof ITextRegionCollection)
            return null;
    }
    int sev = problem.isError() ? IMessage.HIGH_SEVERITY : (problem.isWarning() ? IMessage.NORMAL_SEVERITY : IMessage.LOW_SEVERITY);
    IMessage m = new LocalizedMessage(sev, problem.getMessage(), f);
    // line numbers from document starts @ 0
    try {
        int lineNo = textDoc.getLineOfOffset(sourceStart) + 1;
        m.setLineNo(lineNo);
        m.setOffset(sourceStart);
        m.setLength(sourceEnd - sourceStart + 1);
    } catch (BadLocationException e) {
        Logger.logException(e);
    }
    return m;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IMessage(org.eclipse.wst.validation.internal.provisional.core.IMessage) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) ITextRegionCollection(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionCollection) BadLocationException(org.eclipse.jface.text.BadLocationException)

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