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.nuxeo.elasticsearch.query.NxqlQueryConverter.java
private static QueryBuilder makeLikeQuery(String op, String name, String value, EsHint hint) { String fieldName = name;/*from w ww . j a va 2 s . com*/ if (op.contains("ILIKE")) { // ILIKE will work only with a correct mapping value = value.toLowerCase(); fieldName = name + ".lowercase"; } if (hint != null && hint.index != null) { fieldName = hint.index; } // convert the value to a wildcard query String wildcard = likeToWildcard(value); // use match phrase prefix when possible if (StringUtils.countMatches(wildcard, "*") == 1 && wildcard.endsWith("*") && !wildcard.contains("?") && !wildcard.contains("\\")) { MatchQueryBuilder query = QueryBuilders.matchPhrasePrefixQuery(fieldName, wildcard.replace("*", "")); if (hint != null && hint.analyzer != null) { query.analyzer(hint.analyzer); } return query; } return QueryBuilders.wildcardQuery(fieldName, wildcard); }
From source file:org.omegat.util.ProjectFileStorage.java
/** * Converts a path to the format stored on disk. If * <code>absolutePath</code> has the default location given by * <code>root/defaultName</code>, returns <code>__DEFAULT__</code>. * <p>/*w ww . j av a 2 s. c o m*/ * Otherwise it attempts to compute a relative path based at * <code>root</code>. If this isn't possible (e.g. the paths don't share a * filesystem root) or if the relative path is more than * {@link OConsts#MAX_PARENT_DIRECTORIES_ABS2REL} levels away then it gives * up and returns the original <code>absolutePath</code>. * * @param root * Root path against which to evaluate * @param absolutePath * Absolute path to a folder * @param defaultName * Default name for the folder * @since 1.6.0 * @see <a href= * "https://sourceforge.net/p/omegat/feature-requests/734/">RFE#734</a> * @see OConsts#MAX_PARENT_DIRECTORIES_ABS2REL */ private static String getPathForStoring(String root, String absolutePath, String defaultName) { if (defaultName != null && new File(absolutePath).equals(new File(root, defaultName))) { return DEFAULT_FOLDER_MARKER; } // Fall back to using the input path if all else fails. String result = absolutePath; try { // Path.normalize() will resolve any remaining "../" Path absPath = Paths.get(absolutePath).normalize(); String rel = Paths.get(root).relativize(absPath).toString(); if (StringUtils.countMatches(rel, ".." + File.separatorChar) <= OConsts.MAX_PARENT_DIRECTORIES_ABS2REL) { // Use the relativized path as it is "near" enough. result = rel; } else { // result = absPath.toString(); } } catch (IllegalArgumentException e) { } return normalizeSlashes(result); }
From source file:org.onehippo.cms7.essentials.dashboard.utils.annotations.AnnotationUtilsTest.java
@Test public void testXmlAdaptorAnnotation() throws Exception { final String javaFile = GlobalUtils .readStreamAsText(getClass().getResourceAsStream("/AnnotationTestClass.txt")); assertNotNull("Expected to find /AnnotationTestClass.txt file", javaFile); final String annotationName = "HippoHtmlAdapter"; final String importPath = "org.onehippo.cms7.essentials.components.rest.adapters"; final String hippoHtml = "HippoHtml"; String annotated = AnnotationUtils.addXmlAdaptorAnnotation(javaFile, hippoHtml, new AnnotationUtils.AdapterWrapper(importPath, annotationName)); int nrOfItems = StringUtils.countMatches(annotated, annotationName); assertEquals(1, nrOfItems);/*w ww .j a v a 2s . co m*/ annotated = AnnotationUtils.addXmlAdaptorAnnotation(javaFile, hippoHtml, new AnnotationUtils.AdapterWrapper(importPath, annotationName)); nrOfItems = StringUtils.countMatches(annotated, annotationName); assertEquals(1, nrOfItems); }
From source file:org.onehippo.cms7.essentials.dashboard.utils.annotations.AnnotationUtilsTest.java
@Test public void testXmlRootAnnotation() throws Exception { final String javaFile = GlobalUtils .readStreamAsText(getClass().getResourceAsStream("/AnnotationTestClass.txt")); assertNotNull("Expected to find /AnnotationTestClass.txt file", javaFile); String annotated = AnnotationUtils.addXmlRootAnnotation(javaFile, "testdocument"); int nrOfItems = StringUtils.countMatches(annotated, XmlRootElement.class.getName()); assertEquals(1, nrOfItems);// w w w .j a va 2 s .co m annotated = AnnotationUtils.addXmlRootAnnotation(javaFile, "testdocument"); nrOfItems = StringUtils.countMatches(annotated, XmlRootElement.class.getName()); assertEquals(1, nrOfItems); nrOfItems = StringUtils.countMatches(annotated, "XmlRootElement"); // import and annotation assertEquals(2, nrOfItems); }
From source file:org.onehippo.cms7.essentials.dashboard.utils.annotations.AnnotationUtilsTest.java
@Test public void testMethodAnnotation() throws Exception { final String javaFile = GlobalUtils .readStreamAsText(getClass().getResourceAsStream("/AnnotationTestClass.txt")); assertNotNull("Expected to find /AnnotationTestClass.txt file", javaFile); String annotated = AnnotationUtils.addXmlElementAnnotation(javaFile); int nrOfItems = StringUtils.countMatches(annotated, XmlElement.class.getName()); assertEquals(1, nrOfItems);/*www .j a v a 2s . c om*/ nrOfItems = StringUtils.countMatches(annotated, "@XmlElement"); assertEquals(8, nrOfItems); annotated = AnnotationUtils.addXmlElementAnnotation(annotated); nrOfItems = StringUtils.countMatches(annotated, XmlElement.class.getName()); assertEquals(1, nrOfItems); nrOfItems = StringUtils.countMatches(annotated, "@XmlElement"); assertEquals(8, nrOfItems); }
From source file:org.onehippo.cms7.essentials.dashboard.utils.annotations.AnnotationUtilsTest.java
@Test public void testAddClassAnnotation() throws Exception { final String javaFile = GlobalUtils .readStreamAsText(getClass().getResourceAsStream("/AnnotationTestClass.txt")); assertNotNull("Expected to find /AnnotationTestClass.txt file", javaFile); String annotated = AnnotationUtils.addXmlAccessNoneAnnotation(javaFile); annotated = AnnotationUtils.addXmlAccessNoneAnnotation(annotated); log.info("annotated {}", annotated); int nrOfItems = StringUtils.countMatches(annotated, XmlAccessType.class.getName()); assertEquals(1, nrOfItems);//from w ww . j a v a 2 s . c o m nrOfItems = StringUtils.countMatches(annotated, XmlAccessType.class.getSimpleName()); assertEquals(2, nrOfItems); }
From source file:org.onehippo.cms7.essentials.dashboard.utils.JavaSourceUtilsTest.java
@Test public void testInsertComment() throws Exception { final String myHippoBean = JavaSourceUtils.createHippoBean(docPath, "com.foo.bar.doc", "foo:namespace", "MyTesDocBean"); assertNotNull(myHippoBean);//from w ww .ja va 2s .co m final String text = "TODO test"; final String oneComment = JavaSourceUtils.addClassJavaDoc(myHippoBean, text); final String s = JavaSourceUtils.addClassJavaDoc(oneComment, text); assertTrue(s.contains(text)); assertTrue(StringUtils.countMatches(s, text) == 1); // add extends keyword: JavaSourceUtils.addExtendsClass(docPath, "HippoGalleryImageSet"); JavaSourceUtils.addImport(docPath, EssentialConst.HIPPO_IMAGE_SET_IMPORT); final String extendsClass = JavaSourceUtils.getExtendsClass(docPath); assertEquals("HippoGalleryImageSet", extendsClass); }
From source file:org.openbel.framework.tools.kamstore.KamSummarizer.java
/** * returns number unique gene reference// w w w .jav a2 s.c om * @param edges * @return */ private int getUniqueGeneReference(Collection<KamNode> nodes) { //count all protienAbundance reference Set<String> uniqueLabels = new HashSet<String>(); for (KamNode node : nodes) { if (node.getFunctionType() == FunctionEnum.PROTEIN_ABUNDANCE && StringUtils.countMatches(node.getLabel(), "(") == 1 && StringUtils.countMatches(node.getLabel(), ")") == 1) { uniqueLabels.add(node.getLabel()); } } return uniqueLabels.size(); }
From source file:org.openbravo.service.json.AdvancedQueryBuilder.java
/** * Converts the value of the sortBy member into a valid order by clause in a HQL query. The method * handles special cases as sorting by the identifier properties and descending which is * controlled with a minus sign before the property name. * /* www . j a v a 2 s.c o m*/ * @return a valid order by clause (or an empty string if no sorting) */ public String getOrderByClause() { if (orderByClause != null) { return orderByClause; } if (orderBy == null || orderBy.trim().length() == 0) { orderByClause = ""; return orderByClause; } final StringBuilder sb = new StringBuilder(); boolean firstElement = true; int columnsInDescending = StringUtils.countMatches(orderBy, "-"); int totalColumnSeperators = StringUtils.countMatches(orderBy, ","); boolean orderPrimaryKeyInDesc = false; if (columnsInDescending == totalColumnSeperators) { orderPrimaryKeyInDesc = true; } for (String localOrderBy : orderBy.split(",")) { if (orderPrimaryKeyInDesc && localOrderBy.equals("id")) { localOrderBy = "-".concat(localOrderBy); } if (!firstElement) { sb.append(","); } sb.append(getOrderByClausePart(localOrderBy.trim().replace(DalUtil.FIELDSEPARATOR, DalUtil.DOT))); firstElement = false; } // no order by elements, just use empty string if (sb.toString().trim().length() == 0) { orderByClause = ""; } else { orderByClause = " order by " + sb.toString(); } return orderByClause; }
From source file:org.opencommercesearch.AbstractSearchServer.java
@Override public Facet getFacet(Site site, Locale locale, String fieldFacet, int facetLimit, int depthLimit, String separator, FilterQuery... filterQueries) throws SearchServerException { try {// w w w .jav a 2s .c o m SolrQuery query = new SolrQuery(); query.setRows(0); query.setQuery("*:*"); query.addFacetField(fieldFacet); query.setFacetLimit(facetLimit); query.setFacetMinCount(1); query.addFilterQuery("country:" + locale.getCountry()); RepositoryItem catalog = (RepositoryItem) site.getPropertyValue("defaultCatalog"); String catalogId = catalog.getRepositoryId(); query.setFacetPrefix(CATEGORY_PATH, catalogId + "."); query.addFilterQuery(CATEGORY_PATH + ":" + catalogId); if (filterQueries != null) { for (FilterQuery filterQuery : filterQueries) { query.addFilterQuery(filterQuery.toString()); } } QueryResponse queryResponse = getCatalogSolrServer(locale).query(query); Facet facet = null; if (queryResponse != null && queryResponse.getFacetFields() != null) { FacetField facetField = queryResponse.getFacetField(fieldFacet); if (facetField != null) { List<Count> values = facetField.getValues(); if (values != null && !values.isEmpty()) { facet = new Facet(); facet.setName(StringUtils.capitalize(fieldFacet)); List<Filter> filters = new ArrayList<Facet.Filter>(); boolean filterByDepth = depthLimit > 0 && StringUtils.isNotBlank(separator); for (Count count : values) { if (filterByDepth && StringUtils.countMatches(count.getName(), separator) > depthLimit) { continue; } Filter filter = new Filter(); filter.setName(count.getName()); filter.setCount(count.getCount()); filter.setFilterQuery(count.getAsFilterQuery()); filter.setFilterQueries(count.getAsFilterQuery()); filters.add(filter); } facet.setFilters(filters); } } } return facet; } catch (SolrServerException ex) { throw create(SEARCH_EXCEPTION, ex); } catch (SolrException ex) { throw create(SEARCH_EXCEPTION, ex); } }