org.antlr.v4.runtime.TokenStreamRewriter Java Examples

The following examples show how to use org.antlr.v4.runtime.TokenStreamRewriter. 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: QueryTest.java    From mobi with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void replaceDatasetClause() throws Exception {
    InputStream query = getClass().getResourceAsStream("/example2.rq");
    Sparql11Parser parser = Query.getParser(streamToString(query));
    parser.addErrorListener(new BaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
            throw new IllegalStateException("failed to parse at line " + line + " due to " + msg, e);
        }
    });

    Sparql11Parser.QueryContext queryContext = parser.query();

    // Rewrite the dataset clause
    TokenStreamRewriter rewriter = new TokenStreamRewriter(parser.getTokenStream());
    ParseTreeWalker walker = new ParseTreeWalker();
    DatasetListener listener = new DatasetListener(rewriter);
    walker.walk(listener, queryContext);

    // Test result
    String newText = rewriter.getText();
    parser = Query.getParser(newText);
    String datasetText = parser.query().selectQuery().datasetClause().get(0).getText();
    assertEquals(DATASET_REPLACEMENT, datasetText);
}
 
Example #2
Source File: SimpleDatasetRepositoryConnection.java    From mobi with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Rewrites a SPARQL query replacing the dataset clauses with appropriate dataset clauses for this dataset.
 *
 * @param query The query to rewrite.
 * @return A String representing the query rewritten with dataset clauses appropriate for this dataset.
 */
private String rewriteQuery(String query, Resource... contexts) {
    Sparql11Parser parser = Query.getParser(query);
    Sparql11Parser.QueryContext queryContext = parser.query();
    TokenStreamRewriter rewriter = new TokenStreamRewriter(parser.getTokenStream());
    ParseTreeWalker walker = new ParseTreeWalker();

    DatasetListener listener;
    if (varargsPresent(contexts)) {
        listener = new DatasetListener(rewriter, Arrays.asList(contexts));
    } else {
        listener = new DatasetListener(rewriter);
    }

    walker.walk(listener, queryContext);

    String processedQuery = rewriter.getText();
    log.debug("Dataset Processed Query: \n" + processedQuery);

    return processedQuery;
}
 
Example #3
Source File: ContentUriChecker.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * Replace internal from uri.
 *
 * @param input the input
 * @param replace the replace
 * @param rewriterListener the rewriter listener
 * @return the string
 */
private String replaceInternalFromUri(String input, final List<Triple<Token, Token, String>> replace, UriBaseListener rewriterListener) {
	Pair<ParserRuleContext, CommonTokenStream> parser = prepareUri(input);
	pathSegmentIndex = -1;
	walker.walk(rewriterListener, parser.value0);

	TokenStreamRewriter rewriter = new TokenStreamRewriter(parser.value1);

	for (Triple<Token, Token, String> item : replace) {
		rewriter.replace(item.value0, item.value1, item.value2);
	}

	return rewriter.getText();
}
 
Example #4
Source File: JQLChecker.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * Replace internal.
 *
 * @param jqlContext
 *            the jql context
 * @param jql
 *            the jql
 * @param replace
 *            the replace
 * @param rewriterListener
 *            the rewriter listener
 * @return the string
 */
private String replaceInternal(final JQLContext jqlContext, String jql,
		final List<Triple<Token, Token, String>> replace, JqlBaseListener rewriterListener) {
	Pair<ParserRuleContext, CommonTokenStream> parser = prepareParser(jqlContext, jql);
	walker.walk(rewriterListener, parser.value0);

	TokenStreamRewriter rewriter = new TokenStreamRewriter(parser.value1);

	for (Triple<Token, Token, String> item : replace) {
		rewriter.replace(item.value0, item.value1, item.value2);
	}

	return rewriter.getText();
}
 
Example #5
Source File: JQLChecker.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * Replace from variable statement internal.
 *
 * @param context
 *            the context
 * @param jql
 *            the jql
 * @param replace
 *            the replace
 * @param rewriterListener
 *            the rewriter listener
 * @return the string
 */
private String replaceFromVariableStatementInternal(JQLContext context, String jql,
		final List<Triple<Token, Token, String>> replace, JqlBaseListener rewriterListener) {
	Pair<ParserRuleContext, CommonTokenStream> parser = prepareVariableStatement(context, jql);
	walker.walk(rewriterListener, parser.value0);

	TokenStreamRewriter rewriter = new TokenStreamRewriter(parser.value1);

	for (Triple<Token, Token, String> item : replace) {
		rewriter.replace(item.value0, item.value1, item.value2);
	}

	return rewriter.getText();
}
 
Example #6
Source File: MysqlSchemaDatabase.java    From SpinalTap with Apache License 2.0 4 votes vote down vote up
MySQLDBNamePrefixAdder(TokenStream tokens) {
  rewriter = new TokenStreamRewriter(tokens);
}
 
Example #7
Source File: QueryTest.java    From mobi with GNU Affero General Public License v3.0 4 votes vote down vote up
DatasetListener(TokenStreamRewriter rewriter) {
    this.rewriter = rewriter;
}
 
Example #8
Source File: SimpleDatasetRepositoryConnection.java    From mobi with GNU Affero General Public License v3.0 4 votes vote down vote up
DatasetListener(TokenStreamRewriter rewriter) {
    this.rewriter = rewriter;
}
 
Example #9
Source File: SimpleDatasetRepositoryConnection.java    From mobi with GNU Affero General Public License v3.0 4 votes vote down vote up
DatasetListener(TokenStreamRewriter rewriter, Collection<Resource> contexts) {
    this.rewriter = rewriter;
    this.contexts = new HashSet<>(contexts);
}