org.cqframework.cql.elm.tracking.TrackBack Java Examples

The following examples show how to use org.cqframework.cql.elm.tracking.TrackBack. 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: TranslationTests.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@Test
public void testIdentifierLocation() throws IOException {
    CqlTranslator translator = TestUtils.createTranslator("TranslatorTests/UnknownIdentifier.cql");
    assertEquals(1, translator.getErrors().size());

    CqlTranslatorException e = translator.getErrors().get(0);
    TrackBack tb = e.getLocator();

    assertEquals(6, tb.getStartLine());
    assertEquals(6, tb.getEndLine());

    assertEquals(5, tb.getStartChar());
    assertEquals(10, tb.getEndChar());
}
 
Example #2
Source File: CqlTestSuite.java    From cql_engine with Apache License 2.0 5 votes vote down vote up
private Library translate(String file)  throws UcumException, JAXBException, IOException {
    ModelManager modelManager = new ModelManager();
    LibraryManager libraryManager = new LibraryManager(modelManager);
    UcumService ucumService = new UcumEssenceService(UcumEssenceService.class.getResourceAsStream("/ucum-essence.xml"));

    File cqlFile = new File(URLDecoder.decode(this.getClass().getResource(file).getFile(), "UTF-8"));

    CqlTranslator translator = CqlTranslator.fromFile(cqlFile, modelManager, libraryManager, ucumService);

    if (translator.getErrors().size() > 0) {
        System.err.println("Translation failed due to errors:");
        ArrayList<String> errors = new ArrayList<>();
        for (CqlTranslatorException error : translator.getErrors()) {
            TrackBack tb = error.getLocator();
            String lines = tb == null ? "[n/a]" : String.format("[%d:%d, %d:%d]",
                    tb.getStartLine(), tb.getStartChar(), tb.getEndLine(), tb.getEndChar());
            System.err.printf("%s %s%n", lines, error.getMessage());
            errors.add(lines + error.getMessage());
        }
        throw new IllegalArgumentException(errors.toString());
    }

    assertThat(translator.getErrors().size(), is(0));

    String xml = translator.toXml();

    return CqlLibraryReader.read(new StringReader(xml));
}
 
Example #3
Source File: TranslatorHelper.java    From cqf-ruler with Apache License 2.0 5 votes vote down vote up
public static String errorsToString(Iterable<CqlTranslatorException> exceptions) {
    ArrayList<String> errors = new ArrayList<>();
    for (CqlTranslatorException error : exceptions) {
        TrackBack tb = error.getLocator();
        String lines = tb == null ? "[n/a]" : String.format("%s[%d:%d, %d:%d]",
                (tb.getLibrary() != null ? tb.getLibrary().getId() + (tb.getLibrary().getVersion() != null
                        ? ("-" + tb.getLibrary().getVersion()) : "") : ""),
                tb.getStartLine(), tb.getStartChar(), tb.getEndLine(), tb.getEndChar());
        errors.add(lines + error.getMessage());
    }

    return errors.toString();
}
 
Example #4
Source File: CqlTranslator.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
private static void outputExceptions(Iterable<CqlTranslatorException> exceptions) {
    for (CqlTranslatorException error : exceptions) {
        TrackBack tb = error.getLocator();
        String lines = tb == null ? "[n/a]" : String.format("[%d:%d, %d:%d]",
                tb.getStartLine(), tb.getStartChar(), tb.getEndLine(), tb.getEndChar());
        System.err.printf("%s:%s %s%n", error.getSeverity(), lines, error.getMessage());
    }
}
 
Example #5
Source File: CqlTranslator.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@Override
public void syntaxError(@NotNull Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, @NotNull String msg, RecognitionException e) {
    VersionedIdentifier libraryIdentifier = builder.getLibraryIdentifier();
    if (libraryIdentifier == null) {
        // Attempt to extract a libraryIdentifier from the currently parsed content
        if (recognizer instanceof cqlParser) {
            libraryIdentifier = extractLibraryIdentifier((cqlParser)recognizer);
        }
        if (libraryIdentifier == null) {
            libraryIdentifier = sourceInfo;
        }
    }
    TrackBack trackback = new TrackBack(libraryIdentifier, line, charPositionInLine, line, charPositionInLine);

    if (detailedErrors) {
        builder.recordParsingException(new CqlSyntaxException(msg, trackback, e));
        builder.recordParsingException(new CqlTranslatorException(msg, trackback, e));
    }
    else {
        if (offendingSymbol instanceof CommonToken) {
            CommonToken token = (CommonToken) offendingSymbol;
            builder.recordParsingException(new CqlSyntaxException(String.format("Syntax error at %s", token.getText()), trackback, e));
        } else {
            builder.recordParsingException(new CqlSyntaxException("Syntax error", trackback, e));
        }
    }
}
 
Example #6
Source File: CqlTranslatorException.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
public CqlTranslatorException(String message, TrackBack locator, Throwable cause) {
    super(message, cause);
    this.severity = ErrorSeverity.Error;
    this.locator = locator;
}
 
Example #7
Source File: CqlSemanticException.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
public CqlSemanticException(String message, ErrorSeverity severity, TrackBack locator, Throwable cause) {
    super(message, severity, locator, cause);
}
 
Example #8
Source File: CqlSemanticException.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
public CqlSemanticException(String message, TrackBack locator, Throwable cause) {
    super(message, locator, cause);
}
 
Example #9
Source File: CqlSemanticException.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
public CqlSemanticException(String message, ErrorSeverity severity, TrackBack locator) {
    super(message, severity, locator);
}
 
Example #10
Source File: CqlSemanticException.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
public CqlSemanticException(String message, TrackBack locator) {
    super(message, locator);
}
 
Example #11
Source File: LibraryBuilder.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
private void reportWarning(String message, Expression expression) {
    TrackBack trackback = expression.getTrackbacks() != null && expression.getTrackbacks().size() > 0 ? expression.getTrackbacks().get(0) : null;
    CqlSemanticException warning = new CqlSemanticException(message, CqlTranslatorException.ErrorSeverity.Warning, trackback);
    recordParsingException(warning);
}
 
Example #12
Source File: CqlTranslatorException.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
public void setLocator(TrackBack locator) {
    this.locator = locator;
}
 
Example #13
Source File: CqlTranslatorException.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
public TrackBack getLocator() {
    return locator;
}
 
Example #14
Source File: CqlTranslatorException.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
public CqlTranslatorException(String message, ErrorSeverity severity, TrackBack locator, Throwable cause) {
    super(message, cause);
    this.severity = severity;
    this.locator = locator;
}
 
Example #15
Source File: FhirExecutionTestBase.java    From cql_engine with Apache License 2.0 4 votes vote down vote up
@BeforeMethod
public void beforeEachTestMethod() throws JAXBException, IOException, UcumException {
    String fileName = this.getClass().getSimpleName();
    library = libraries.get(fileName);
    if (library == null) {
        ModelManager modelManager = new ModelManager();
        LibraryManager libraryManager = new LibraryManager(modelManager);
        UcumService ucumService = new UcumEssenceService(UcumEssenceService.class.getResourceAsStream("/ucum-essence.xml"));
        try {
            File cqlFile = new File(URLDecoder.decode(this.getClass().getResource(fileName + ".cql").getFile(), "UTF-8"));

            ArrayList<CqlTranslator.Options> options = new ArrayList<>();
            options.add(CqlTranslator.Options.EnableDateRangeOptimization);
            CqlTranslator translator = CqlTranslator.fromFile(cqlFile, modelManager, libraryManager, ucumService, options.toArray(new CqlTranslator.Options[options.size()]));

            if (translator.getErrors().size() > 0) {
                System.err.println("Translation failed due to errors:");
                ArrayList<String> errors = new ArrayList<>();
                for (CqlTranslatorException error : translator.getErrors()) {
                    TrackBack tb = error.getLocator();
                    String lines = tb == null ? "[n/a]" : String.format("[%d:%d, %d:%d]",
                            tb.getStartLine(), tb.getStartChar(), tb.getEndLine(), tb.getEndChar());
                    System.err.printf("%s %s%n", lines, error.getMessage());
                    errors.add(lines + error.getMessage());
                }
                throw new IllegalArgumentException(errors.toString());
            }

            assertThat(translator.getErrors().size(), is(0));

            xmlFile = new File(cqlFile.getParent(), fileName + ".xml");
            xmlFile.createNewFile();

            PrintWriter pw = new PrintWriter(xmlFile, "UTF-8");
            pw.println(translator.toXml());
            pw.println();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        library = CqlLibraryReader.read(xmlFile);
        libraries.put(fileName, library);
    }
}
 
Example #16
Source File: CqlTranslatorException.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
public CqlTranslatorException(String message, ErrorSeverity severity, TrackBack locator) {
    super(message);
    this.severity = severity;
    this.locator = locator;
}
 
Example #17
Source File: CqlTranslatorException.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
public CqlTranslatorException(String message, TrackBack locator) {
    super(message);
    this.severity = ErrorSeverity.Error;
    this.locator = locator;
}
 
Example #18
Source File: CqlSyntaxException.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
public CqlSyntaxException(String message, ErrorSeverity severity, TrackBack locator, Throwable cause) {
    super(message, severity, locator, cause);
}
 
Example #19
Source File: CqlSyntaxException.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
public CqlSyntaxException(String message, TrackBack locator, Throwable cause) {
    super(message, locator, cause);
}
 
Example #20
Source File: CqlSyntaxException.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
public CqlSyntaxException(String message, ErrorSeverity severity, TrackBack locator) {
    super(message, severity, locator);
}
 
Example #21
Source File: CqlSyntaxException.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
public CqlSyntaxException(String message, TrackBack locator) {
    super(message, locator);
}
 
Example #22
Source File: CqlInternalException.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
public CqlInternalException(String message, TrackBack locator, Throwable cause) {
    super(message, ErrorSeverity.Error, locator, cause);
}
 
Example #23
Source File: CqlInternalException.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
public CqlInternalException(String message, TrackBack locator) {
    super(message, ErrorSeverity.Error, locator);
}
 
Example #24
Source File: CqlExecutionTestBase.java    From cql_engine with Apache License 2.0 4 votes vote down vote up
@BeforeMethod
public void beforeEachTestMethod() throws JAXBException, IOException, UcumException {
    String fileName = this.getClass().getSimpleName();
    library = libraries.get(fileName);
    if (library == null) {
        UcumService ucumService = new UcumEssenceService(UcumEssenceService.class.getResourceAsStream("/ucum-essence.xml"));
        try {
            File cqlFile = new File(URLDecoder.decode(this.getClass().getResource(fileName + ".cql").getFile(), "UTF-8"));

            ArrayList<CqlTranslator.Options> options = new ArrayList<>();
            options.add(CqlTranslator.Options.EnableDateRangeOptimization);
            options.add(CqlTranslator.Options.EnableAnnotations);
            options.add(CqlTranslator.Options.EnableLocators);
            CqlTranslator translator = CqlTranslator.fromFile(cqlFile, getModelManager(), getLibraryManager(), ucumService, options.toArray(new CqlTranslator.Options[options.size()]));

            if (translator.getErrors().size() > 0) {
                System.err.println("Translation failed due to errors:");
                ArrayList<String> errors = new ArrayList<>();
                for (CqlTranslatorException error : translator.getErrors()) {
                    TrackBack tb = error.getLocator();
                    String lines = tb == null ? "[n/a]" : String.format("[%d:%d, %d:%d]",
                            tb.getStartLine(), tb.getStartChar(), tb.getEndLine(), tb.getEndChar());
                    System.err.printf("%s %s%n", lines, error.getMessage());
                    errors.add(lines + error.getMessage());
                }
                throw new IllegalArgumentException(errors.toString());
            }

            assertThat(translator.getErrors().size(), is(0));

            xmlFile = new File(cqlFile.getParent(), fileName + ".xml");
            xmlFile.createNewFile();

            String xml = translator.toXml();

            PrintWriter pw = new PrintWriter(xmlFile, "UTF-8");
            pw.println(xml);
            pw.println();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        library = CqlLibraryReader.read(xmlFile);
        libraries.put(fileName, library);
    }
}