List of usage examples for org.apache.commons.lang StringUtils countMatches
public static int countMatches(String str, String sub)
Counts how many times the substring appears in the larger String.
From source file:org.socraticgrid.workbench.ontomodel.service.SyntacticalOntoModelServiceImpl.java
/** * Removes super-categories from the list. * If the list contains:/* w w w.j a v a 2s. co m*/ * - /A * - /A/B * - /A/B/C * - /D * - /E * - /E/F * * After the invocation of this method, the list will contain: * - /A/B/C * - /D * - /E/F * * @param categories */ private void removeSuperCategories(List<String> categories) { //order the list according to the depth of each element Collections.sort(categories, new Comparator<String>() { public int compare(String o1, String o2) { return new Integer(StringUtils.countMatches(o1, "/")).compareTo(StringUtils.countMatches(o2, "/")); } }); String[] categoriesArray = categories.toArray(new String[categories.size()]); for (int i = 0; i < categoriesArray.length; i++) { boolean subcategoryFound = false; for (int j = i + 1; j < categoriesArray.length; j++) { if (categoriesArray[j].startsWith(categoriesArray[i])) { //then categoriesArray[j] is a subcategory of categoriesArray[i] //we don't need categoriesArray[i] in the result subcategoryFound = true; break; } } if (subcategoryFound) { categories.remove(categoriesArray[i]); } } }
From source file:org.sonar.batch.issue.tracking.IssueTrackingDecoratorTest.java
private Resource mockHashes(String originalSource, String newSource) throws IOException { DefaultInputFile inputFile = mock(DefaultInputFile.class); java.io.File f = temp.newFile(); when(inputFile.path()).thenReturn(f.toPath()); when(inputFile.file()).thenReturn(f); when(inputFile.charset()).thenReturn(StandardCharsets.UTF_8); when(inputFile.lines()).thenReturn(StringUtils.countMatches(newSource, "\n") + 1); FileUtils.write(f, newSource, StandardCharsets.UTF_8); when(inputFile.key()).thenReturn("foo:Action.java"); when(inputPathCache.getFile("foo", "Action.java")).thenReturn(inputFile); when(lastSnapshots.getLineHashes("foo:Action.java")).thenReturn(computeHexHashes(originalSource)); Resource file = File.create("Action.java"); return file;/*from w w w . j av a 2s . c o m*/ }
From source file:org.sonar.batch.issue.tracking.IssueTrackingTest.java
private void initLastHashes(String reference, String newSource) throws IOException { DefaultInputFile inputFile = mock(DefaultInputFile.class); File f = temp.newFile();//w w w . j av a 2 s . c o m when(inputFile.path()).thenReturn(f.toPath()); when(inputFile.file()).thenReturn(f); when(inputFile.charset()).thenReturn(StandardCharsets.UTF_8); String data = load(newSource); when(inputFile.lines()).thenReturn(StringUtils.countMatches(data, "\n") + 1); FileUtils.write(f, data, StandardCharsets.UTF_8); when(inputFile.key()).thenReturn("foo:Action.java"); when(lastSnapshots.getLineHashes("foo:Action.java")).thenReturn(computeHexHashes(load(reference))); sourceHashHolder = new SourceHashHolder(inputFile, lastSnapshots); }
From source file:org.sonar.core.persistence.profiling.ProfilingPreparedStatementHandler.java
ProfilingPreparedStatementHandler(PreparedStatement statement, String sql) { this.statement = statement; this.sql = sql; this.arguments = Lists.newArrayList(); for (int argCount = 0; argCount < StringUtils.countMatches(sql, "?"); argCount++) { arguments.add("!"); }/*from w w w . j ava2 s .c om*/ }
From source file:org.sonar.cxx.checks.TooLongLineCheck.java
@Override public void visitFile(AstNode astNode) { List<String> lines; try {/*from w w w . ja v a2 s.c o m*/ lines = Files.readLines(getContext().getFile(), charset); } catch (IOException e) { throw new IllegalStateException(e); } for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); int length = line.length() + StringUtils.countMatches(line, "\t") * (tabWidth - 1); if (length > maximumLineLength) { getContext().createLineViolation(this, "Split this {0} characters long line (which is greater than {1} authorized).", i + 1, length, maximumLineLength); } } }
From source file:org.sonar.db.version.v451.DeleteUnescapedActivities.java
static boolean isUnescaped(@Nullable String csv) { if (csv != null) { String[] splits = StringUtils.split(csv, ';'); for (String split : splits) { if (StringUtils.countMatches(split, "=") != 1) { return true; }//w ww . j av a 2 s . com } } return false; }
From source file:org.sonar.java.checks.AbstractPrintfChecker.java
private static boolean isMessageFormatPattern(String formatString, int start) { return start == 0 || formatString.charAt(start - 1) != '\'' || StringUtils.countMatches(formatString.substring(0, start), "\'") % 2 == 0; }
From source file:org.sonar.java.checks.HardcodedIpCheck.java
private static boolean isValidIPV6PartCount(String ip) { int partCount = ip.split("::?").length; int compressionSeparatorCount = StringUtils.countMatches(ip, "::"); boolean validUncompressed = compressionSeparatorCount == 0 && partCount == 8; boolean validCompressed = compressionSeparatorCount == 1 && partCount <= 7; return validUncompressed || validCompressed; }
From source file:org.sonar.java.checks.PreparedStatementAndResultSetCheck.java
@CheckForNull private static Integer countQuery(ExpressionTree expression) { return expression.is(Tree.Kind.STRING_LITERAL) ? StringUtils.countMatches(((LiteralTree) expression).value(), "?") : null;/*from w w w. jav a 2 s . c om*/ }
From source file:org.sonar.plugins.pmd.PmdExecutorTest.java
@Test public void executeOnManySourceDirs() throws URISyntaxException, IOException, PMDException { Project project = new Project("two-source-dirs"); ProjectFileSystem fs = mock(ProjectFileSystem.class); File root = new File( getClass().getResource("/org/sonar/plugins/pmd/PmdExecutorTest/executeOnManySourceDirs/").toURI()); when(fs.getSourceFiles(Java.INSTANCE)).thenReturn( Arrays.asList(new File(root, "src1/FirstClass.java"), new File(root, "src2/SecondClass.java"))); when(fs.getSourceCharset()).thenReturn(Charset.forName("UTF-8")); when(fs.getSonarWorkingDirectory()).thenReturn(new File("target")); project.setFileSystem(fs);//from w w w.ja v a 2 s . c om PmdConfiguration conf = mock(PmdConfiguration.class); File file = FileUtils.toFile( getClass().getResource("/org/sonar/plugins/pmd/PmdExecutorTest/executeOnManySourceDirs/pmd.xml") .toURI().toURL()); when(conf.getRulesets()).thenReturn(Arrays.asList(file.getAbsolutePath())); PmdExecutor executor = new PmdExecutor(project, conf); File xmlReport = executor.execute(); assertThat(xmlReport.exists(), is(true)); String xml = FileUtils.readFileToString(xmlReport); // errors on the two source files assertThat(StringUtils.countMatches(xml, "<file"), is(2)); assertThat(StringUtils.countMatches(xml, "<violation"), greaterThan(2)); }