@Test public void test() throws Exception { SensorContextTester ctxTester = SensorContextTester.create(folder.getRoot()); ctxTester.fileSystem().setWorkDir(folder.getRoot().toPath()); File baseFile = folder.newFile("test.sql"); FileUtils.copyURLToFile(getClass().getResource("/tsql/sample1.sql"), baseFile); String contents = FileUtils.readFileToString(baseFile); DefaultInputFile ti = new TestInputFileBuilder("test", folder.getRoot(), baseFile).initMetadata(contents) .setLanguage(Constants.languageKey).setContents(contents).setProjectBaseDir(folder.getRoot().toPath()) .build(); ctxTester.fileSystem().add(ti); AntlrContext antlrContext = Dialects.TSQL.parse(FileUtils.readFileToString(baseFile)); CpdTokensFiller filler = new CpdTokensFiller(); filler.fill(ti, ctxTester, antlrContext); List<TokensLine> lines = ctxTester.cpdTokens("test:test.sql"); Assert.assertEquals(2, lines.size()); }
@Override public void fill(InputFile file, SensorContext context, AntlrContext antlrContext) { try { if (!(file instanceof DefaultInputFile)) { return; } NewCpdTokens newCpdTokens = context.newCpdTokens().onFile(file); List<? extends Token> tokens = antlrContext.getAllTokens(); DefaultInputFile defaultInputFile = (DefaultInputFile) file; for (Token token : tokens) { try { final String text = token.getText(); if (token.getType() == -1 || token.getStartIndex() >= token.getStopIndex() || StringUtils.isEmpty(text)) { continue; } final TextRange range = defaultInputFile.newRange(token.getStartIndex(), token.getStopIndex() + 1); defaultInputFile.validate(range); newCpdTokens.addToken(range, text); } catch (Throwable e) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Cannot add range: {} on file {} for token {}", e, file, token); } } } synchronized (context) { newCpdTokens.save(); } } catch (Throwable e) { LOGGER.warn("Error adding cpd tokens on " + file, e); } }
CpdTokensFiller implements Filler { @Override public void fill(InputFile file, SensorContext context, AntlrContext antlrContext) { try { if (!(file instanceof DefaultInputFile)) { return; } NewCpdTokens newCpdTokens = context.newCpdTokens().onFile(file); List<? extends Token> tokens = antlrContext.getAllTokens(); DefaultInputFile defaultInputFile = (DefaultInputFile) file; for (Token token : tokens) { try { final String text = token.getText(); if (token.getType() == -1 || token.getStartIndex() >= token.getStopIndex() || StringUtils.isEmpty(text)) { continue; } final TextRange range = defaultInputFile.newRange(token.getStartIndex(), token.getStopIndex() + 1); defaultInputFile.validate(range); newCpdTokens.addToken(range, text); } catch (Throwable e) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Cannot add range: {} on file {} for token {}", e, file, token); } } } synchronized (context) { newCpdTokens.save(); } } catch (Throwable e) { LOGGER.warn("Error adding cpd tokens on " + file, e); } } }
CpdTokensFiller implements Filler { @Override public void fill(InputFile file, SensorContext context, AntlrContext antlrContext) { try { if (!(file instanceof DefaultInputFile)) { return; } NewCpdTokens newCpdTokens = context.newCpdTokens().onFile(file); List<? extends Token> tokens = antlrContext.getAllTokens(); DefaultInputFile defaultInputFile = (DefaultInputFile) file; for (Token token : tokens) { try { final String text = token.getText(); if (token.getType() == -1 || token.getStartIndex() >= token.getStopIndex() || StringUtils.isEmpty(text)) { continue; } final TextRange range = defaultInputFile.newRange(token.getStartIndex(), token.getStopIndex() + 1); defaultInputFile.validate(range); newCpdTokens.addToken(range, text); } catch (Throwable e) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Cannot add range: {} on file {} for token {}", e, file, token); } } } synchronized (context) { newCpdTokens.save(); } } catch (Throwable e) { LOGGER.warn("Error adding cpd tokens on " + file, e); } }  }
CpdTokensFiller implements Filler { @Override public void fill(InputFile file, SensorContext context, AntlrContext antlrContext) { try { if (!(file instanceof DefaultInputFile)) { return; } NewCpdTokens newCpdTokens = context.newCpdTokens().onFile(file); List<? extends Token> tokens = antlrContext.getAllTokens(); DefaultInputFile defaultInputFile = (DefaultInputFile) file; for (Token token : tokens) { try { final String text = token.getText(); if (token.getType() == -1 || token.getStartIndex() >= token.getStopIndex() || StringUtils.isEmpty(text)) { continue; } final TextRange range = defaultInputFile.newRange(token.getStartIndex(), token.getStopIndex() + 1); defaultInputFile.validate(range); newCpdTokens.addToken(range, text); } catch (Throwable e) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Cannot add range: {} on file {} for token {}", e, file, token); } } } synchronized (context) { newCpdTokens.save(); } } catch (Throwable e) { LOGGER.warn("Error adding cpd tokens on " + file, e); } }  @Override void fill(InputFile file, SensorContext context, AntlrContext antlrContext); }
CpdTokensFiller implements Filler { @Override public void fill(InputFile file, SensorContext context, AntlrContext antlrContext) { try { if (!(file instanceof DefaultInputFile)) { return; } NewCpdTokens newCpdTokens = context.newCpdTokens().onFile(file); List<? extends Token> tokens = antlrContext.getAllTokens(); DefaultInputFile defaultInputFile = (DefaultInputFile) file; for (Token token : tokens) { try { final String text = token.getText(); if (token.getType() == -1 || token.getStartIndex() >= token.getStopIndex() || StringUtils.isEmpty(text)) { continue; } final TextRange range = defaultInputFile.newRange(token.getStartIndex(), token.getStopIndex() + 1); defaultInputFile.validate(range); newCpdTokens.addToken(range, text); } catch (Throwable e) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Cannot add range: {} on file {} for token {}", e, file, token); } } } synchronized (context) { newCpdTokens.save(); } } catch (Throwable e) { LOGGER.warn("Error adding cpd tokens on " + file, e); } }  @Override void fill(InputFile file, SensorContext context, AntlrContext antlrContext);  }