Search in sources :

Example 21 with LocalizedMessage

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

the class MarkupValidator method checkForAttributeValue.

private void checkForAttributeValue(IStructuredDocumentRegion structuredDocumentRegion, IReporter reporter) {
    if (structuredDocumentRegion.isDeleted()) {
        return;
    }
    // check for attributes without a value
    // track the attribute/equals/value sequence using a state of 0, 1 ,2
    // representing the name, =, and value, respectively
    int attrState = 0;
    ITextRegionList textRegions = structuredDocumentRegion.getRegions();
    // ReconcileAnnotationKey key = createKey(structuredDocumentRegion,
    // getScope());
    int errorCount = 0;
    for (int i = 0; (i < textRegions.size()) && (errorCount < ELEMENT_ERROR_LIMIT); i++) {
        ITextRegion textRegion = textRegions.get(i);
        if ((textRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) || isTagCloseTextRegion(textRegion)) {
            // dangling name and '='
            if ((attrState == 2) && (i >= 2)) {
                // create annotation
                ITextRegion nameRegion = textRegions.get(i - 2);
                if (!(nameRegion instanceof ITextRegionContainer)) {
                    Object[] args = { structuredDocumentRegion.getText(nameRegion) };
                    String messageText = NLS.bind(XMLCoreMessages.Attribute__is_missing_a_value, args);
                    int start = structuredDocumentRegion.getStartOffset(nameRegion);
                    int end = structuredDocumentRegion.getEndOffset();
                    int lineNo = getLineNumber(start);
                    int textLength = structuredDocumentRegion.getText(nameRegion).trim().length();
                    LocalizedMessage message = new LocalizedMessage(getPluginPreference().getInt(XMLCorePreferenceNames.ATTRIBUTE_HAS_NO_VALUE), messageText);
                    message.setOffset(start);
                    message.setLength(textLength);
                    message.setLineNo(lineNo);
                    // quick fix info
                    ITextRegion equalsRegion = textRegions.get(i - 2 + 1);
                    int insertOffset = structuredDocumentRegion.getTextEndOffset(equalsRegion) - end;
                    Object[] additionalFixInfo = { structuredDocumentRegion.getText(nameRegion), new Integer(insertOffset) };
                    getAnnotationMsg(reporter, ProblemIDsXML.MissingAttrValue, message, additionalFixInfo, textLength);
                    errorCount++;
                }
            } else // name but no '=' (XML only)
            if ((attrState == 1) && (i >= 1)) {
                // create annotation
                ITextRegion previousRegion = textRegions.get(i - 1);
                if (!(previousRegion instanceof ITextRegionContainer)) {
                    Object[] args = { structuredDocumentRegion.getText(previousRegion) };
                    String messageText = NLS.bind(XMLCoreMessages.Attribute__has_no_value, args);
                    int start = structuredDocumentRegion.getStartOffset(previousRegion);
                    int textLength = structuredDocumentRegion.getText(previousRegion).trim().length();
                    int lineNo = getLineNumber(start);
                    LocalizedMessage message = new LocalizedMessage(getPluginPreference().getInt(XMLCorePreferenceNames.ATTRIBUTE_HAS_NO_VALUE), messageText);
                    message.setOffset(start);
                    message.setLength(textLength);
                    message.setLineNo(lineNo);
                    getAnnotationMsg(reporter, ProblemIDsXML.NoAttrValue, message, structuredDocumentRegion.getText(previousRegion), textLength);
                    errorCount++;
                }
            }
            attrState = 1;
        } else if (textRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS) {
            attrState = 2;
        } else if (textRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) {
            attrState = 0;
        }
    }
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ITextRegionContainer(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer) LocalizedMessage(org.eclipse.wst.validation.internal.operations.LocalizedMessage)

Example 22 with LocalizedMessage

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

the class JSONSyntaxValidatorHelper method checkExpectedRegion.

private static boolean checkExpectedRegion(Token current, Token previous, Stack<Token> tagStack, IReporter reporter, IValidator validator, ISeverityProvider provider) {
    if (previous == null || isIgnoreRegion(current.type)) {
        return false;
    }
    if (previous.type == JSONRegionContexts.JSON_OBJECT_OPEN) {
        if (current.type != JSONRegionContexts.JSON_OBJECT_CLOSE && current.type != JSONRegionContexts.JSON_OBJECT_KEY) {
            String messageText = "Expected object key but found " + current.type;
            LocalizedMessage message = createMessage(current, messageText, JSONCorePreferenceNames.MISSING_BRACKET, provider);
            getAnnotationMsg(reporter, ProblemIDsJSON.MissingEndBracket, message, null, current.length, validator);
            return true;
        }
    } else if (previous.type == JSONRegionContexts.JSON_OBJECT_KEY) {
        if (current.type != JSONRegionContexts.JSON_COLON) {
            String messageText = "Expected colon but found " + current.type;
            LocalizedMessage message = createMessage(current, messageText, JSONCorePreferenceNames.MISSING_BRACKET, provider);
            getAnnotationMsg(reporter, ProblemIDsJSON.MissingEndBracket, message, null, current.length, validator);
            return true;
        }
    } else if (previous.type == JSONRegionContexts.JSON_COLON) {
        if (!JSONUtil.isJSONSimpleValue(current.type) && !JSONUtil.isStartJSONStructure(current.type)) {
            String messageText = "Expected JSON value but found " + current.type;
            LocalizedMessage message = createMessage(current, messageText, JSONCorePreferenceNames.MISSING_BRACKET, provider);
            getAnnotationMsg(reporter, ProblemIDsJSON.MissingEndBracket, message, null, current.length, validator);
            return true;
        }
    } else if (previous.type == JSONRegionContexts.JSON_COMMA) {
        if (tagStack.isEmpty()) {
            String messageText = "Unexpected comma";
            LocalizedMessage message = createMessage(current, messageText, JSONCorePreferenceNames.MISSING_BRACKET, provider);
            getAnnotationMsg(reporter, ProblemIDsJSON.MissingEndBracket, message, null, current.length, validator);
            return true;
        } else {
            if (tagStack.peek().type == JSONRegionContexts.JSON_ARRAY_OPEN) {
                // inside array, previous token must be a JSON value
                if (!JSONUtil.isJSONSimpleValue(current.type) && current.type != JSONRegionContexts.JSON_ARRAY_OPEN && current.type != JSONRegionContexts.JSON_OBJECT_OPEN) {
                    String messageText = "Expected JSON value but found " + current.type;
                    LocalizedMessage message = createMessage(current, messageText, JSONCorePreferenceNames.MISSING_BRACKET, provider);
                    getAnnotationMsg(reporter, ProblemIDsJSON.MissingEndBracket, message, null, current.length, validator);
                    return true;
                }
            } else {
                // inside object, previous token must be a JSON key
                if (current.type != JSONRegionContexts.JSON_OBJECT_KEY) {
                    String messageText = "Expected JSON key but found " + current.type;
                    LocalizedMessage message = createMessage(current, messageText, JSONCorePreferenceNames.MISSING_BRACKET, provider);
                    getAnnotationMsg(reporter, ProblemIDsJSON.MissingEndBracket, message, null, current.length, validator);
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : LocalizedMessage(org.eclipse.wst.validation.internal.operations.LocalizedMessage)

Example 23 with LocalizedMessage

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

the class JSONSyntaxValidatorHelper method createMessage.

private static LocalizedMessage createMessage(Token token, String messageText, String severityPreference, ISeverityProvider provider) {
    LocalizedMessage message = new LocalizedMessage(provider.getSeverity(severityPreference), messageText);
    message.setOffset(token.offset);
    message.setLength(token.length);
    message.setLineNo(getLine(token));
    return message;
}
Also used : LocalizedMessage(org.eclipse.wst.validation.internal.operations.LocalizedMessage)

Example 24 with LocalizedMessage

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

the class JSONSyntaxValidatorHelper method validate.

public static void validate(JSONLineTokenizer tokenizer, IReporter reporter, IValidator validator, ISeverityProvider provider) {
    List previousRegions = null;
    String type = null;
    Stack<Token> tagStack = new Stack<Token>();
    List<Token> region = null;
    boolean isClosed = true;
    int tagErrorCount = 0;
    Token previousRegion = null;
    while ((type = getNextToken(tokenizer)) != null) {
        // System.err.println(type);
        Token token = new Token(type, tokenizer.yytext(), tokenizer.getOffset(), tokenizer.yylength(), tokenizer.getLine());
        isClosed = false;
        boolean hasError = checkExpectedRegion(token, previousRegion, tagStack, reporter, validator, provider);
        if (type == JSONRegionContexts.JSON_OBJECT_OPEN || type == JSONRegionContexts.JSON_ARRAY_OPEN) {
            tagStack.push(token);
        } else if (type == JSONRegionContexts.JSON_OBJECT_CLOSE) {
            if (tagStack.isEmpty()) {
                createMissingTagError(token, false, reporter, tagErrorCount, tagStack, validator, provider);
            } else {
                Token lastToken = tagStack.peek();
                if (lastToken.type == JSONRegionContexts.JSON_OBJECT_OPEN) {
                    tagStack.pop();
                }
            }
        } else if (type == JSONRegionContexts.JSON_ARRAY_CLOSE) {
            if (tagStack.isEmpty()) {
                createMissingTagError(token, false, reporter, tagErrorCount, tagStack, validator, provider);
            } else {
                Token lastToken = tagStack.peek();
                if (lastToken.type == JSONRegionContexts.JSON_ARRAY_OPEN) {
                    if (!tagStack.isEmpty())
                        tagStack.pop();
                }
            }
        } else if (type.equalsIgnoreCase(JSONRegionContexts.UNDEFINED)) {
            if ("{".equals(token.text)) {
                tagStack.push(token);
            } else if ("}".equals(token.text)) {
                if (!tagStack.isEmpty()) {
                    tagStack.pop();
                }
            } else if ("[".equals(token.text)) {
                tagStack.push(token);
            } else if ("]".equals(token.text)) {
                if (!tagStack.isEmpty()) {
                    tagStack.pop();
                }
            } else {
                if (!hasError) {
                    String messageText = "Unexpected token";
                    LocalizedMessage message = createMessage(token, messageText, JSONCorePreferenceNames.MISSING_BRACKET, provider);
                    getAnnotationMsg(reporter, ProblemIDsJSON.MissingEndBracket, message, null, token.length, validator);
                }
            }
        }
        /*
			 * else if (check &&
			 * type.equalsIgnoreCase(JSONRegionContexts.UNDEFINED)) { String
			 * messageText = "Unexpected token"; LocalizedMessage message =
			 * createMessage(token, messageText,
			 * JSONCorePreferenceNames.MISSING_BRACKET, provider);
			 * getAnnotationMsg(reporter, ProblemIDsJSON.MissingEndBracket,
			 * message, null, token.length, validator);
			 * 
			 * }
			 */
        if (!isIgnoreRegion(type)) {
            previousRegion = token;
        }
    }
    if (!tagStack.isEmpty()) {
        while (!tagStack.isEmpty()) {
            createMissingTagError(tagStack.pop(), true, reporter, tagErrorCount, tagStack, validator, provider);
        }
    }
}
Also used : List(java.util.List) Stack(java.util.Stack) LocalizedMessage(org.eclipse.wst.validation.internal.operations.LocalizedMessage)

Example 25 with LocalizedMessage

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

the class MarkupValidator method checkClosingBracket.

private void checkClosingBracket(IStructuredDocumentRegion structuredDocumentRegion, IReporter reporter) {
    if (structuredDocumentRegion.isDeleted()) {
        return;
    }
    ITextRegionList regions = structuredDocumentRegion.getRegions();
    ITextRegion r = null;
    boolean closed = false;
    for (int i = 0; (i < regions.size()) && !structuredDocumentRegion.isDeleted(); i++) {
        r = regions.get(i);
        if ((r.getType() == DOMRegionContext.XML_TAG_CLOSE) || (r.getType() == DOMRegionContext.XML_EMPTY_TAG_CLOSE)) {
            closed = true;
        }
    }
    if (!closed) {
        String messageText = XMLCoreMessages.ReconcileStepForMarkup_6;
        int start = structuredDocumentRegion.getStartOffset();
        int length = structuredDocumentRegion.getText().trim().length();
        int lineNo = getLineNumber(start);
        LocalizedMessage message = new LocalizedMessage(getPluginPreference().getInt(XMLCorePreferenceNames.MISSING_CLOSING_BRACKET), messageText);
        message.setOffset(start);
        message.setLength(length);
        message.setLineNo(lineNo);
        getAnnotationMsg(reporter, ProblemIDsXML.MissingClosingBracket, message, null, length);
    }
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) LocalizedMessage(org.eclipse.wst.validation.internal.operations.LocalizedMessage)

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