Search in sources :

Example 31 with LocalizedMessage

use of org.eclipse.wst.validation.internal.operations.LocalizedMessage in project webtools.sourceediting by eclipse.

the class StreamingMarkupValidator method createMissingTagError.

/**
 * Creates a missing tag error for the token
 * @param token the token that's missing its pair tag
 * @param isStartTag is the token a start tag
 * @param reporter  the reporter
 */
private void createMissingTagError(Token token, boolean isStartTag, IReporter reporter) {
    Object[] args = { token.text };
    String messageText = NLS.bind(isStartTag ? XMLCoreMessages.Missing_end_tag_ : XMLCoreMessages.Missing_start_tag_, args);
    LocalizedMessage message = new LocalizedMessage(getPluginPreference().getInt(XMLCorePreferenceNames.MISSING_END_TAG), messageText);
    message.setOffset(token.offset);
    message.setLength(token.length);
    message.setLineNo(getLine(token));
    Object fixInfo = isStartTag ? (Object) getStartEndFixInfo(token.text, token) : token.text;
    getAnnotationMsg(reporter, isStartTag ? ProblemIDsXML.MissingEndTag : ProblemIDsXML.MissingStartTag, message, fixInfo, token.length);
    if (++tagErrorCount > ERROR_THRESHOLD) {
        tagStack.clear();
        tagStack = null;
    }
}
Also used : LocalizedMessage(org.eclipse.wst.validation.internal.operations.LocalizedMessage)

Example 32 with LocalizedMessage

use of org.eclipse.wst.validation.internal.operations.LocalizedMessage in project webtools.sourceediting by eclipse.

the class StreamingMarkupValidator method checkAttributes.

/**
 * Checks that all the attribute in the start-tag have values and that those values are properly quoted
 * @param region the start-tag region
 * @param reporter the reporter
 */
private void checkAttributes(List region, IReporter reporter) {
    int attrState = 0;
    int errorCount = 0;
    final int regionLength = region.size();
    // Start at one, since we know the first token is an tag-open
    for (int i = 1; i < regionLength && errorCount < ERROR_THRESHOLD; i++) {
        Token t = (Token) region.get(i);
        if (t.type == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME || t.type == DOMRegionContext.XML_TAG_CLOSE || t.type == DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
            // dangling name and '='
            if ((attrState == 2) && (i >= 2)) {
                // create annotation
                Token nameRegion = (Token) region.get(i - 2);
                Object[] args = { nameRegion.text };
                String messageText = NLS.bind(XMLCoreMessages.Attribute__is_missing_a_value, args);
                int start = nameRegion.offset;
                int end = start + nameRegion.length;
                LocalizedMessage message = new LocalizedMessage(getPluginPreference().getInt(XMLCorePreferenceNames.ATTRIBUTE_HAS_NO_VALUE), messageText);
                message.setOffset(start);
                message.setLength(nameRegion.length);
                message.setLineNo(nameRegion.line + 1);
                // quick fix info
                Token equalsRegion = (Token) region.get(i - 2 + 1);
                int insertOffset = (equalsRegion.offset + equalsRegion.length) - end;
                Object[] additionalFixInfo = { nameRegion.text, new Integer(insertOffset) };
                getAnnotationMsg(reporter, ProblemIDsXML.MissingAttrValue, message, additionalFixInfo, nameRegion.text.length());
                errorCount++;
            } else // name but no '=' (XML only)
            if ((attrState == 1) && (i >= 1)) {
                // create annotation
                Token nameToken = (Token) region.get(i - 1);
                Object[] args = { nameToken.text };
                String messageText = NLS.bind(XMLCoreMessages.Attribute__has_no_value, args);
                int start = nameToken.offset;
                int textLength = nameToken.text.trim().length();
                int lineNo = nameToken.line;
                LocalizedMessage message = new LocalizedMessage(getPluginPreference().getInt(XMLCorePreferenceNames.ATTRIBUTE_HAS_NO_VALUE), messageText);
                message.setOffset(start);
                message.setLength(textLength);
                message.setLineNo(lineNo + 1);
                getAnnotationMsg(reporter, ProblemIDsXML.NoAttrValue, message, nameToken.text, textLength);
                errorCount++;
            }
            attrState = 1;
        } else if (t.type == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS) {
            attrState = 2;
        } else if (t.type == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) {
            attrState = 0;
            // Check that there are quotes around the attribute value and that they match
            final String trimmed = t.text.trim();
            if (trimmed.length() > 0) {
                final char q1 = trimmed.charAt(0), q2 = trimmed.charAt(trimmed.length() - 1);
                if ((q1 == '\'' || q1 == '"') && (q1 != q2 || trimmed.length() == 1)) {
                    // missing closing quote
                    String message = XMLCoreMessages.ReconcileStepForMarkup_0;
                    addAttributeError(message, t.text, t.offset, t.length, t.line, ProblemIDsXML.Unclassified, reporter, getPluginPreference().getInt(XMLCorePreferenceNames.MISSING_CLOSING_QUOTE));
                    errorCount++;
                } else if (q1 != '\'' && q1 != '"') {
                    // missing both
                    String message = XMLCoreMessages.ReconcileStepForMarkup_1;
                    addAttributeError(message, t.text, t.offset, t.length, t.line, ProblemIDsXML.AttrValueNotQuoted, reporter, getPluginPreference().getInt(XMLCorePreferenceNames.MISSING_CLOSING_QUOTE));
                    errorCount++;
                }
            }
        }
    }
}
Also used : LocalizedMessage(org.eclipse.wst.validation.internal.operations.LocalizedMessage)

Example 33 with LocalizedMessage

use of org.eclipse.wst.validation.internal.operations.LocalizedMessage in project webtools.sourceediting by eclipse.

the class StreamingMarkupValidator method validateFile.

/**
 * Validates the given file. It will stream the contents of the file without creating a model for the file; it will only
 * use existing
 * @param file the file to validate
 * @param reporter the reporter
 */
private void validateFile(IFile file, IReporter reporter) {
    Message message = new LocalizedMessage(IMessage.LOW_SEVERITY, file.getFullPath().toString().substring(1));
    reporter.displaySubtask(StreamingMarkupValidator.this, message);
    XMLLineTokenizer tokenizer = null;
    try {
        tagStack = new Stack();
        model = StructuredModelManager.getModelManager().getExistingModelForRead(file);
        try {
            if (model == null) {
                tokenizer = new XMLLineTokenizer(new BufferedReader(new InputStreamReader(file.getContents(true), getCharset(file))));
            } else {
                tokenizer = new XMLLineTokenizer(new BufferedReader(new DocumentReader(model.getStructuredDocument())));
            }
            checkTokens(tokenizer, reporter);
        } finally {
            if (model != null) {
                model.releaseFromRead();
                model = null;
            }
        }
    } catch (UnsupportedEncodingException e) {
    } catch (CoreException e) {
    } catch (IOException e) {
    }
}
Also used : IMessage(org.eclipse.wst.validation.internal.provisional.core.IMessage) LocalizedMessage(org.eclipse.wst.validation.internal.operations.LocalizedMessage) Message(org.eclipse.wst.validation.internal.core.Message) InputStreamReader(java.io.InputStreamReader) CoreException(org.eclipse.core.runtime.CoreException) BufferedReader(java.io.BufferedReader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) XMLLineTokenizer(org.eclipse.wst.xml.core.internal.parser.XMLLineTokenizer) DocumentReader(org.eclipse.wst.sse.core.internal.document.DocumentReader) IOException(java.io.IOException) LocalizedMessage(org.eclipse.wst.validation.internal.operations.LocalizedMessage) Stack(java.util.Stack)

Example 34 with LocalizedMessage

use of org.eclipse.wst.validation.internal.operations.LocalizedMessage in project webtools.sourceediting by eclipse.

the class StreamingMarkupValidator method checkNamespacesInProcessingInstruction.

/**
 * Checks that the processing instruction name doesn't contain a namespace
 * @param region the processing instruction region
 * @param reporter the reporter
 */
private void checkNamespacesInProcessingInstruction(List region, IReporter reporter) {
    final int regionLength = region.size();
    for (int i = 0; i < regionLength; i++) {
        Token t = (Token) region.get(i);
        if (t.type == DOMRegionContext.XML_TAG_NAME) {
            // $NON-NLS-1$
            int index = t.text.indexOf(":");
            if (index != -1) {
                String messageText = XMLCoreMessages.ReconcileStepForMarkup_4;
                int start = t.offset + index;
                int length = t.text.trim().length() - index;
                LocalizedMessage message = new LocalizedMessage(getPluginPreference().getInt(XMLCorePreferenceNames.NAMESPACE_IN_PI_TARGET), messageText);
                message.setOffset(start);
                message.setLength(length);
                message.setLineNo(t.line + 1);
                getAnnotationMsg(reporter, ProblemIDsXML.NamespaceInPI, message, null, length);
                break;
            }
        }
    }
}
Also used : LocalizedMessage(org.eclipse.wst.validation.internal.operations.LocalizedMessage)

Example 35 with LocalizedMessage

use of org.eclipse.wst.validation.internal.operations.LocalizedMessage in project webtools.sourceediting by eclipse.

the class JSONSyntaxValidatorHelperTest method missingEndArray.

@Test
public void missingEndArray() throws Exception {
    IReporter reporter = validate("[");
    List messages = reporter.getMessages();
    Assert.assertEquals(1, messages.size());
    LocalizedMessage msg = (LocalizedMessage) messages.get(0);
    assertMessage(msg, "Missing end array", 1, 1);
}
Also used : IReporter(org.eclipse.wst.validation.internal.provisional.core.IReporter) List(java.util.List) LocalizedMessage(org.eclipse.wst.validation.internal.operations.LocalizedMessage) Test(org.junit.Test)

Aggregations

LocalizedMessage (org.eclipse.wst.validation.internal.operations.LocalizedMessage)41 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)12 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)10 AnnotationInfo (org.eclipse.wst.sse.ui.internal.reconcile.validator.AnnotationInfo)9 IncrementalReporter (org.eclipse.wst.sse.ui.internal.reconcile.validator.IncrementalReporter)9 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)8 List (java.util.List)7 IMessage (org.eclipse.wst.validation.internal.provisional.core.IMessage)6 IReporter (org.eclipse.wst.validation.internal.provisional.core.IReporter)6 Test (org.junit.Test)5 CoreException (org.eclipse.core.runtime.CoreException)4 Message (org.eclipse.wst.validation.internal.core.Message)4 BufferedReader (java.io.BufferedReader)3 IOException (java.io.IOException)3 InputStreamReader (java.io.InputStreamReader)3 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Stack (java.util.Stack)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 DocumentReader (org.eclipse.wst.sse.core.internal.document.DocumentReader)2