Java Code Examples for org.eclipse.emf.common.util.BasicDiagnostic#add()

The following examples show how to use org.eclipse.emf.common.util.BasicDiagnostic#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: M2DocParser.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Parses while matching an AQL expression.
 * 
 * @param expression
 *            the expression to parse
 * @return the corresponding {@link AstResult}
 */
private AstResult parseWhileAqlExpression(String expression) {
    final IQueryBuilderEngine.AstResult result;

    if (expression != null && expression.length() > 0) {
        AstBuilderListener astBuilder = AQL56Compatibility.createAstBuilderListener(queryEnvironment);
        CharStream input = new UnbufferedCharStream(new StringReader(expression), expression.length());
        QueryLexer lexer = new QueryLexer(input);
        lexer.setTokenFactory(new CommonTokenFactory(true));
        lexer.removeErrorListeners();
        lexer.addErrorListener(astBuilder.getErrorListener());
        TokenStream tokens = new UnbufferedTokenStream<CommonToken>(lexer);
        QueryParser parser = new QueryParser(tokens);
        parser.addParseListener(astBuilder);
        parser.removeErrorListeners();
        parser.addErrorListener(astBuilder.getErrorListener());
        // parser.setTrace(true);
        parser.expression();
        result = astBuilder.getAstResult();
    } else {
        ErrorExpression errorExpression = (ErrorExpression) EcoreUtil
                .create(AstPackage.eINSTANCE.getErrorExpression());
        List<org.eclipse.acceleo.query.ast.Error> errors = new ArrayList<>(1);
        errors.add(errorExpression);
        final Map<Object, Integer> positions = new HashMap<>();
        if (expression != null) {
            positions.put(errorExpression, Integer.valueOf(0));
        }
        final BasicDiagnostic diagnostic = new BasicDiagnostic();
        diagnostic.add(new BasicDiagnostic(Diagnostic.ERROR, AstBuilderListener.PLUGIN_ID, 0,
                "null or empty string.", new Object[] {errorExpression }));
        result = new AstResult(errorExpression, positions, positions, errors, diagnostic);
    }

    return result;
}
 
Example 2
Source File: M2DocParser.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Parses while matching an AQL type literal.
 * 
 * @param expression
 *            the expression to parse
 * @return the corresponding {@link AstResult}
 */
protected AstResult parseWhileAqlTypeLiteral(String expression) {
    final IQueryBuilderEngine.AstResult result;

    if (expression != null && expression.length() > 0) {
        AstBuilderListener astBuilder = AQL56Compatibility.createAstBuilderListener(queryEnvironment);
        CharStream input = new UnbufferedCharStream(new StringReader(expression), expression.length());
        QueryLexer lexer = new QueryLexer(input);
        lexer.setTokenFactory(new CommonTokenFactory(true));
        lexer.removeErrorListeners();
        lexer.addErrorListener(astBuilder.getErrorListener());
        TokenStream tokens = new UnbufferedTokenStream<CommonToken>(lexer);
        QueryParser parser = new QueryParser(tokens);
        parser.addParseListener(astBuilder);
        parser.removeErrorListeners();
        parser.addErrorListener(astBuilder.getErrorListener());
        // parser.setTrace(true);
        parser.typeLiteral();
        result = astBuilder.getAstResult();
    } else {
        ErrorTypeLiteral errorTypeLiteral = (ErrorTypeLiteral) EcoreUtil
                .create(AstPackage.eINSTANCE.getErrorTypeLiteral());
        List<org.eclipse.acceleo.query.ast.Error> errs = new ArrayList<>(1);
        errs.add(errorTypeLiteral);
        final Map<Object, Integer> positions = new HashMap<>();
        if (expression != null) {
            positions.put(errorTypeLiteral, Integer.valueOf(0));
        }
        final BasicDiagnostic diagnostic = new BasicDiagnostic();
        diagnostic.add(new BasicDiagnostic(Diagnostic.ERROR, AstBuilderListener.PLUGIN_ID, 0,
                "missing type literal", new Object[] {errorTypeLiteral }));
        result = new AstResult(errorTypeLiteral, positions, positions, errs, diagnostic);
    }

    return result;
}
 
Example 3
Source File: TemplateCustomProperties.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Parses while matching an AQL expression.
 * 
 * @param queryEnvironment
 *            the {@link IReadOnlyQueryEnvironment}
 * @param type
 *            the type to parse
 * @return the corresponding {@link AstResult}
 */
private AstResult parseWhileAqlTypeLiteral(IReadOnlyQueryEnvironment queryEnvironment, String type) {
    final IQueryBuilderEngine.AstResult result;

    if (type != null && type.length() > 0) {
        AstBuilderListener astBuilder = AQL56Compatibility
                .createAstBuilderListener((IQueryEnvironment) queryEnvironment);
        CharStream input = new UnbufferedCharStream(new StringReader(type), type.length());
        QueryLexer lexer = new QueryLexer(input);
        lexer.setTokenFactory(new CommonTokenFactory(true));
        lexer.removeErrorListeners();
        lexer.addErrorListener(astBuilder.getErrorListener());
        TokenStream tokens = new UnbufferedTokenStream<CommonToken>(lexer);
        QueryParser parser = new QueryParser(tokens);
        parser.addParseListener(astBuilder);
        parser.removeErrorListeners();
        parser.addErrorListener(astBuilder.getErrorListener());
        // parser.setTrace(true);
        parser.typeLiteral();
        result = astBuilder.getAstResult();
    } else {
        ErrorTypeLiteral errorTypeLiteral = (ErrorTypeLiteral) EcoreUtil
                .create(AstPackage.eINSTANCE.getErrorTypeLiteral());
        List<org.eclipse.acceleo.query.ast.Error> errors = new ArrayList<>(1);
        errors.add(errorTypeLiteral);
        final Map<Object, Integer> positions = new HashMap<>();
        if (type != null) {
            positions.put(errorTypeLiteral, Integer.valueOf(0));
        }
        final BasicDiagnostic diagnostic = new BasicDiagnostic();
        diagnostic.add(new BasicDiagnostic(Diagnostic.ERROR, AstBuilderListener.PLUGIN_ID, 0, "null or empty type.",
                new Object[] {errorTypeLiteral }));
        result = new AstResult(errorTypeLiteral, positions, positions, errors, diagnostic);
    }

    return result;
}
 
Example 4
Source File: DiagnosticTreeIterableTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testIteratorIsNotConsumed() {
	BasicDiagnostic root = new BasicDiagnostic();
	root.add(new BasicDiagnostic());
	AssertableDiagnostics diagnostics = new AssertableDiagnostics(root);
	Iterable<Diagnostic> allDiagnostics = diagnostics.getAllDiagnostics();
	Iterator<Diagnostic> first = allDiagnostics.iterator();
	while (first.hasNext()) {
		first.next();
	}

	Assert.assertTrue(allDiagnostics.iterator().hasNext());
}
 
Example 5
Source File: DiagnosticTreeIterableTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testIteratorIsNotConsumed() {
	BasicDiagnostic root = new BasicDiagnostic();
	root.add(new BasicDiagnostic());
	AssertableDiagnostics diagnostics = new AssertableDiagnostics(root);
	Iterable<Diagnostic> allDiagnostics = diagnostics.getAllDiagnostics();
	Iterator<Diagnostic> first = allDiagnostics.iterator();
	while (first.hasNext()) {
		first.next();
	}

	Assert.assertTrue(allDiagnostics.iterator().hasNext());
}
 
Example 6
Source File: AbstractValidationTest.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Check if the given issue code is found among issue codes for the object, located at the given position.
 *
 * @param root
 *          root object of the document
 * @param pos
 *          position to locate the target object
 */
@Override
public void apply(final EObject root, final Integer pos) {
  final Diagnostic diagnostics = validate(root);
  final BasicDiagnostic diagnosticsOnTargetPosition = new BasicDiagnostic();
  boolean issueFound = false;
  int actualSeverity = SEVERITY_UNDEFINED;
  boolean expectedSeverityMatches = false;
  boolean expectedMessageMatches = false;
  String actualMessage = "";

  for (AbstractValidationDiagnostic avd : Iterables.filter(diagnostics.getChildren(), AbstractValidationDiagnostic.class)) {
    if (diagnosticPositionEquals(pos, avd)) {
      // Add issue to the list of issues at the given position
      diagnosticsOnTargetPosition.add(avd);
      if (avd.getIssueCode().equals(issueCode)) {
        issueFound = true;
        actualSeverity = avd.getSeverity();
        // True if the expected severity is not set, or if matches with the actual one
        expectedSeverityMatches = expectedSeverity == SEVERITY_UNDEFINED || expectedSeverity == actualSeverity;
        actualMessage = avd.getMessage();
        // True if message matches with actual message or message is null
        expectedMessageMatches = message == null || actualMessage.equals(message);
        if (issueMustBeFound) {
          // Remove the diagnostic from the list of non-expected diagnostics
          getUnexpectedDiagnostics().remove(avd);
          // Don't need to display error messages
          if (expectedSeverityMatches && expectedMessageMatches) {
            return;
          }
        }
      }
    }
  }

  // Create error message
  createErrorMessage(pos, diagnosticsOnTargetPosition, issueFound, expectedSeverityMatches, actualSeverity, expectedMessageMatches, actualMessage);
}
 
Example 7
Source File: ErrorToDiagnoticTranslator.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
public Diagnostic translate(final ValidationContext errors, final GamlResource r, final CheckMode mode) {
	final BasicDiagnostic chain = new BasicDiagnostic();
	for (final GamlCompilationError e : errors) {
		final Diagnostic d = translate(e, r, mode);
		if (d != null) {
			chain.add(d);
		}
	}
	return chain;
}