Search in sources :

Example 6 with Message

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

the class ValidatorTest method testAddInfoToMessage.

/**
 * Test the addInfoToMessage method. The following tests are performed:<br/>
 * 1. When the validation message contains a null key nothing is added to the message.<br/>
 * 2. When the message contains the key "ENTIRE_ELEMENT" the three attributes are added
 * to the method and the SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE is null.<br/>
 * 3. When the message contains the key "EntityNotDeclared" the three attributes are set.
 */
public void testAddInfoToMessage() {
    // These strings are common addition information types.
    // $NON-NLS-1$
    String COLUMN_NUMBER_ATTRIBUTE = "columnNumber";
    // $NON-NLS-1$
    String SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE = "squiggleSelectionStrategy";
    // $NON-NLS-1$
    String SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE = "squiggleNameOrValue";
    // Test that the message does not contain the attributes when the method is called
    // with a null key.
    ValidationMessage validationMessage = new ValidationMessage("", 1, 1, "");
    Message message = new Message();
    validator.addInfoToMessage(validationMessage, message);
    assertNull("COLUMN_NUMBER_ATTRIBUTE was not null for a ValidationMessage with a null key. COLUMN_NUMBER_ATTRIBUTE = " + message.getAttribute(COLUMN_NUMBER_ATTRIBUTE), message.getAttribute(COLUMN_NUMBER_ATTRIBUTE));
    assertNull("SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE was not null for a ValidationMessage with a null key. SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE = " + message.getAttribute(SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE), message.getAttribute(SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE));
    assertNull("SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE was not null for a ValidationMessage with a null key. SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE = " + message.getAttribute(SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE), message.getAttribute(SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE));
    // Test that the message contains the three attributes when the key is set to "ElementUnterminated".
    ValidationMessage validationMessage2 = new ValidationMessage("", 1, 1, "", "ElementUnterminated", null);
    Message message2 = new Message();
    validator.addInfoToMessage(validationMessage2, message2);
    assertEquals("COLUMN_NUMBER_ATTRIBUTE was correctly set to 1 for a ValidationMessage with the key ENTIRE_ELEMENT. COLUMN_NUMBER_ATTRIBUTE = " + message2.getAttribute(COLUMN_NUMBER_ATTRIBUTE), new Integer(1), message2.getAttribute(COLUMN_NUMBER_ATTRIBUTE));
    assertEquals("SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE was not ENTIRE_ELEMENT. SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE = " + message2.getAttribute(SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE), "ENTIRE_ELEMENT", message2.getAttribute(SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE));
    assertNull("SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE was not null for a ValidationMessage with the key ENTIRE_ELEMENT. SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE = " + message2.getAttribute(SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE), message2.getAttribute(SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE));
    // Test that the message contains the three attributes when the key is set to "EntityNotDeclared".
    ValidationMessage validationMessage3 = new ValidationMessage("", 1, 1, "", "EntityNotDeclared", new Object[] { "MyName" });
    Message message3 = new Message();
    validator.addInfoToMessage(validationMessage3, message3);
    assertEquals("COLUMN_NUMBER_ATTRIBUTE was correctly set to 1 for a ValidationMessage with the key EntityNotDeclared. COLUMN_NUMBER_ATTRIBUTE = " + message3.getAttribute(COLUMN_NUMBER_ATTRIBUTE), new Integer(1), message3.getAttribute(COLUMN_NUMBER_ATTRIBUTE));
    assertEquals("SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE was not TEXT_ENTITY_REFERENCE. SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE = " + message3.getAttribute(SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE), "TEXT_ENTITY_REFERENCE", message3.getAttribute(SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE));
    assertEquals("SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE was not set to MyName for a ValidationMessage with the key EntityNotDeclared. SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE = " + message3.getAttribute(SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE), "MyName", message3.getAttribute(SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE));
}
Also used : ValidationMessage(org.eclipse.wst.xml.core.internal.validation.core.ValidationMessage) Message(org.eclipse.wst.validation.internal.core.Message) ValidationMessage(org.eclipse.wst.xml.core.internal.validation.core.ValidationMessage)

Example 7 with Message

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

the class ValidatorTest method testAddInfoToMessage.

/**
 * Test the addInfoToMessage method. The following tests are performed:<br/>
 * 1. When the validation message contains a null key nothing is added to the message.<br/>
 * 2. When the message contains the key "ENTIRE_ELEMENT" the three attributes are added
 * to the method and the SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE is null.<br/>
 * 3. When the message contains the key "EntityNotDeclared" the three attributes are set.
 */
public void testAddInfoToMessage() {
    // These strings are common addition information types.
    // $NON-NLS-1$
    String COLUMN_NUMBER_ATTRIBUTE = "columnNumber";
    // $NON-NLS-1$
    String SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE = "squiggleSelectionStrategy";
    // $NON-NLS-1$
    String SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE = "squiggleNameOrValue";
    // Test that the message does not contain the attributes when the method is called
    // with a null key.
    ValidationMessage validationMessage = new ValidationMessage("", 1, 1, "");
    Message message = new Message();
    validator.addInfoToMessage(validationMessage, message);
    assertNull("COLUMN_NUMBER_ATTRIBUTE was not null for a ValidationMessage with a null key. COLUMN_NUMBER_ATTRIBUTE = " + message.getAttribute(COLUMN_NUMBER_ATTRIBUTE), message.getAttribute(COLUMN_NUMBER_ATTRIBUTE));
    assertNull("SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE was not null for a ValidationMessage with a null key. SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE = " + message.getAttribute(SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE), message.getAttribute(SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE));
    assertNull("SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE was not null for a ValidationMessage with a null key. SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE = " + message.getAttribute(SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE), message.getAttribute(SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE));
    // Test that the message contains the three attributes when the key is set to "s4s-elt-character".
    ValidationMessage validationMessage2 = new ValidationMessage("", 1, 1, "", "s4s-elt-character", null);
    Message message2 = new Message();
    validator.addInfoToMessage(validationMessage2, message2);
    assertEquals("COLUMN_NUMBER_ATTRIBUTE was correctly set to 1 for a ValidationMessage with the key s4s-elt-character. COLUMN_NUMBER_ATTRIBUTE = " + message2.getAttribute(COLUMN_NUMBER_ATTRIBUTE), new Integer(1), message2.getAttribute(COLUMN_NUMBER_ATTRIBUTE));
    assertEquals("SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE was not TEXT. SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE = " + message2.getAttribute(SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE), "TEXT", message2.getAttribute(SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE));
    assertEquals("SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE was not an empty string for a ValidationMessage with the key s4s-elt-character. SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE = " + message2.getAttribute(SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE), "", message2.getAttribute(SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE));
    // Test that the message contains the three attributes when the key is set to "src-resolve".
    ValidationMessage validationMessage3 = new ValidationMessage("'MyName'", 1, 1, "", "src-resolve", null);
    Message message3 = new Message();
    validator.addInfoToMessage(validationMessage3, message3);
    assertEquals("COLUMN_NUMBER_ATTRIBUTE was correctly set to 1 for a ValidationMessage with the key src-resolve. COLUMN_NUMBER_ATTRIBUTE = " + message3.getAttribute(COLUMN_NUMBER_ATTRIBUTE), new Integer(1), message3.getAttribute(COLUMN_NUMBER_ATTRIBUTE));
    assertEquals("SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE was not VALUE_OF_ATTRIBUTE_WITH_GIVEN_VALUE. SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE = " + message3.getAttribute(SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE), "VALUE_OF_ATTRIBUTE_WITH_GIVEN_VALUE", message3.getAttribute(SQUIGGLE_SELECTION_STRATEGY_ATTRIBUTE));
    assertEquals("SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE was not set to MyName for a ValidationMessage with the key src-resolve. SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE = " + message3.getAttribute(SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE), "MyName", message3.getAttribute(SQUIGGLE_NAME_OR_VALUE_ATTRIBUTE));
}
Also used : ValidationMessage(org.eclipse.wst.xml.core.internal.validation.core.ValidationMessage) Message(org.eclipse.wst.validation.internal.core.Message) ValidationMessage(org.eclipse.wst.xml.core.internal.validation.core.ValidationMessage)

Example 8 with Message

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

the class MarkupValidator method validateV1File.

/**
 * @param currentFile
 * @param reporter
 */
private void validateV1File(IFile currentFile, IReporter reporter) {
    Message message = new LocalizedMessage(IMessage.LOW_SEVERITY, currentFile.getFullPath().toString().substring(1));
    reporter.displaySubtask(MarkupValidator.this, message);
    IStructuredModel model = null;
    try {
        model = StructuredModelManager.getModelManager().getModelForRead(currentFile);
        IStructuredDocument document = null;
        if (model != null) {
            document = model.getStructuredDocument();
            connect(document);
            IStructuredDocumentRegion validationRegion = document.getFirstStructuredDocumentRegion();
            while (validationRegion != null) {
                validate(validationRegion, reporter);
                validationRegion = validationRegion.getNext();
            }
            disconnect(document);
        }
    } catch (Exception e) {
        Logger.logException(e);
    } finally {
        if (model != null) {
            model.releaseFromRead();
        }
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) 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) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) ValidationException(org.eclipse.wst.validation.internal.core.ValidationException) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) LocalizedMessage(org.eclipse.wst.validation.internal.operations.LocalizedMessage)

Example 9 with Message

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

the class JSPELValidator method validateXMLNode.

protected void validateXMLNode(ITextRegionCollection container, ITextRegion region, IReporter reporter, IFile file) {
    String elText = container.getText(region);
    JSPELParser elParser = JSPELParser.createParser(elText);
    int contentStart = container.getStartOffset(region);
    int contentLength = container.getLength();
    try {
        elParser.Expression();
    } catch (ParseException e) {
        int sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_EL_SYNTAX);
        if (sev != ValidationMessage.IGNORE) {
            Token curTok = e.currentToken;
            int problemStartOffset = contentStart + curTok.beginColumn;
            Message message = new LocalizedMessage(sev, JSPCoreMessages.JSPEL_Syntax);
            message.setOffset(problemStartOffset);
            message.setLength(curTok.endColumn - curTok.beginColumn + 1);
            message.setTargetObject(file);
            reporter.addMessage(fMessageOriginator, message);
        }
    } catch (TokenMgrError te) {
        int sev = getMessageSeverity(JSPCorePreferenceNames.VALIDATION_EL_LEXER);
        if (sev != ValidationMessage.IGNORE) {
            Message message = new LocalizedMessage(IMessage.NORMAL_SEVERITY, JSPCoreMessages.JSPEL_Token);
            message.setOffset(contentStart);
            message.setLength(contentLength);
            message.setTargetObject(file);
            reporter.addMessage(fMessageOriginator, message);
        }
    }
}
Also used : ValidationMessage(org.eclipse.wst.sse.core.internal.validate.ValidationMessage) IMessage(org.eclipse.wst.validation.internal.provisional.core.IMessage) Message(org.eclipse.wst.validation.internal.core.Message) JSPELParser(org.eclipse.jst.jsp.core.internal.java.jspel.JSPELParser) Token(org.eclipse.jst.jsp.core.internal.java.jspel.Token) TokenMgrError(org.eclipse.jst.jsp.core.internal.java.jspel.TokenMgrError) ParseException(org.eclipse.jst.jsp.core.internal.java.jspel.ParseException)

Example 10 with Message

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

the class JSPValidator method validate.

public void validate(IValidationContext helper, IReporter reporter) throws ValidationException {
    String[] uris = helper.getURIs();
    IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot();
    if (uris.length > 0) {
        IFile currentFile = null;
        for (int i = 0; i < uris.length && !reporter.isCancelled(); i++) {
            // might be called with just project path?
            currentFile = wsRoot.getFile(new Path(uris[i]));
            if (currentFile != null && currentFile.exists()) {
                if (shouldValidate(currentFile) && fragmentCheck(currentFile)) {
                    Message message = new LocalizedMessage(IMessage.LOW_SEVERITY, uris[i]);
                    reporter.displaySubtask(this, message);
                    validateFile(currentFile, reporter);
                }
                if (DEBUG)
                    // $NON-NLS-1$ //$NON-NLS-2$
                    System.out.println("validating: [" + uris[i] + "]");
            }
        }
    } else {
        // if uris[] length 0 -> validate() gets called for each project
        if (helper instanceof IWorkbenchContext) {
            IProject project = ((IWorkbenchContext) helper).getProject();
            JSPFileVisitor visitor = new JSPFileVisitor(reporter);
            try {
                // collect all jsp files for the project
                project.accept(visitor, IResource.DEPTH_INFINITE);
            } catch (CoreException e) {
                if (DEBUG)
                    e.printStackTrace();
            }
            IFile[] files = visitor.getFiles();
            for (int i = 0; i < files.length && !reporter.isCancelled(); i++) {
                if (shouldValidate(files[i]) && fragmentCheck(files[i])) {
                    int percent = (i * 100) / files.length + 1;
                    Message message = new LocalizedMessage(IMessage.LOW_SEVERITY, percent + "% " + files[i].getFullPath().toString());
                    reporter.displaySubtask(this, message);
                    validateFile(files[i], reporter);
                }
                if (DEBUG)
                    // $NON-NLS-1$ //$NON-NLS-2$
                    System.out.println("validating: [" + files[i] + "]");
            }
        }
    }
}
Also used : Path(org.eclipse.core.runtime.Path) IWorkbenchContext(org.eclipse.wst.validation.internal.operations.IWorkbenchContext) IFile(org.eclipse.core.resources.IFile) IMessage(org.eclipse.wst.validation.internal.provisional.core.IMessage) Message(org.eclipse.wst.validation.internal.core.Message) IProject(org.eclipse.core.resources.IProject) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) CoreException(org.eclipse.core.runtime.CoreException)

Aggregations

Message (org.eclipse.wst.validation.internal.core.Message)19 IMessage (org.eclipse.wst.validation.internal.provisional.core.IMessage)17 CoreException (org.eclipse.core.runtime.CoreException)10 IFile (org.eclipse.core.resources.IFile)8 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)5 IResource (org.eclipse.core.resources.IResource)4 LocalizedMessage (org.eclipse.wst.validation.internal.operations.LocalizedMessage)4 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)3 Path (org.eclipse.core.runtime.Path)3 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)2 IContainer (org.eclipse.core.resources.IContainer)2 IProject (org.eclipse.core.resources.IProject)2 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)2 IPath (org.eclipse.core.runtime.IPath)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 JSPELParser (org.eclipse.jst.jsp.core.internal.java.jspel.JSPELParser)2