Example usage for org.apache.commons.lang3 StringUtils substringBefore

List of usage examples for org.apache.commons.lang3 StringUtils substringBefore

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils substringBefore.

Prototype

public static String substringBefore(final String str, final String separator) 

Source Link

Document

Gets the substring before the first occurrence of a separator.

Usage

From source file:de.jfachwert.post.Adressat.java

/**
 * Der Name ist der Teil vor dem Komma (bei Personen). Bei Firmen ist
 * es der komplette Name./*from  www.  ja v  a  2s.  co m*/
 * 
 * @return z.B. "Mustermann"
 */
public String getName() {
    return StringUtils.substringBefore(this.getCode(), ",").trim();
}

From source file:com.thinkbiganalytics.servicemonitor.support.ServiceMonitorCheckUtil.java

/**
 * get a map of the Service and any components that should be checked within that service.
 */// w  w  w . ja  v a  2  s. c  om
public static Map<String, List<String>> getMapOfServiceAndComponents(String services) {
    Map<String, List<String>> map = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
    if (StringUtils.isNotBlank(services)) {
        String finalServiceString = services;
        if (services.contains("/")) {
            int i = 1;
            String serviceName = null;
            for (String servicePart : StringUtils.split(services, "/")) {
                //service name is the first string before the /

                if (serviceName == null) {
                    if (servicePart.contains(",")) {
                        serviceName = StringUtils.substringAfterLast(servicePart, ",");
                    } else {
                        serviceName = servicePart;
                    }
                } else {
                    String components = "";
                    String origComponents = "";
                    if (servicePart.contains("]")) {
                        components = StringUtils.substringBeforeLast(servicePart, "]");
                        components = StringUtils.substringAfter(components, "[");
                        origComponents = "[" + components + "]";
                    } else {
                        components = StringUtils.substringBefore(servicePart, ",");
                        origComponents = components;
                    }
                    String[] componentsArr = StringUtils.split(components, ",");
                    map.put(serviceName, Arrays.asList(componentsArr));

                    //now remove these from the finalServiceString
                    finalServiceString = StringUtils.replace(finalServiceString,
                            serviceName + "/" + origComponents, "");

                    //reset serviceName
                    serviceName = StringUtils.substringAfterLast(servicePart, ",");

                }
                i++;
            }
        }

        for (String service : StringUtils.split(finalServiceString, ",")) {
            String serviceName = service;
            map.put(serviceName, Arrays.asList(new String[] { ALL_COMPONENTS }));
        }
    }
    return map;
}

From source file:de.blizzy.documentr.search.GetVisibleBranchDocIdsTask.java

@Override
public BitSet call() throws IOException {
    List<String> branches = Lists
            .newArrayList(permissionEvaluator.getBranchesForPermission(authentication, Permission.VIEW));
    if (!branches.isEmpty()) {
        Collections.sort(branches);
        BooleanQuery allBranchesQuery = new BooleanQuery();
        for (String projectAndBranch : branches) {
            String projectName = StringUtils.substringBefore(projectAndBranch, "/"); //$NON-NLS-1$
            String branchName = StringUtils.substringAfter(projectAndBranch, "/"); //$NON-NLS-1$
            TermQuery projectQuery = new TermQuery(new Term(PageIndex.PROJECT, projectName));
            TermQuery branchQuery = new TermQuery(new Term(PageIndex.BRANCH, branchName));
            BooleanQuery projectAndBranchQuery = new BooleanQuery();
            projectAndBranchQuery.add(projectQuery, BooleanClause.Occur.MUST);
            projectAndBranchQuery.add(branchQuery, BooleanClause.Occur.MUST);
            allBranchesQuery.add(projectAndBranchQuery, BooleanClause.Occur.SHOULD);
        }/*from w  w  w. j a va 2s .c o m*/
        AbstractDocIdsCollector collector = new AllDocIdsCollector();
        searcher.search(allBranchesQuery, collector);
        return collector.getDocIds();
    } else {
        return new BitSet(1);
    }
}

From source file:lolth.autohome.buy.AutohomeBuyInfoListTaskFetch.java

@Override
protected void parsePage(Document doc, FetchTask task) throws Exception {
    Elements lis = doc.select("li.price-item");

    for (Element li : lis) {
        AutohomeBuyInfoBean bean = new AutohomeBuyInfoBean();
        bean.setUrl(task.getUrl());//from   w ww. j a  v  a2  s. c om
        bean.setForumId(task.getExtra());

        // post id
        Elements id = li.select("div.price-share a.share");
        if (!id.isEmpty()) {
            String idStr = id.first().attr("data-target");
            idStr = StringUtils.substringAfterLast(idStr, "_");
            if (StringUtils.isBlank(idStr)) {
                continue;
            }

            bean.setId(idStr);
        }

        // 
        Elements user = li.select("div.user-name a");
        if (!user.isEmpty()) {
            String userUrl = user.first().absUrl("href");
            String userId = StringUtils.substringAfterLast(userUrl, "/");
            String userName = user.first().text();

            bean.setUserId(userId);
            bean.setUserUrl(userUrl);
            bean.setUserName(userName);
        }

        // ?
        Elements postTime = li.select("div.user-name span");
        if (!postTime.isEmpty()) {
            bean.setPostTime(StringUtils.trim(StringUtils.substringBefore(postTime.first().text(), "?")));
        }

        Elements dataLis = li.select("div.price-item-bd li");
        for (Element dataLi : dataLis) {
            String data = dataLi.text();

            if (StringUtils.startsWith(data, "")) {
                bean.setCar(StringUtils.trim(StringUtils.substringAfter(data, "")));
            }

            if (StringUtils.startsWith(data, "")) {
                bean.setPrice(StringUtils.trim(StringUtils.substringAfter(data, "")));
            }

            if (StringUtils.startsWith(data, "")) {
                bean.setGuidePrice(StringUtils.trim(StringUtils.substringAfter(data, "")));
            }

            if (StringUtils.startsWith(data, "?")) {
                bean.setTotalPrice(StringUtils.trim(StringUtils.substringAfter(data, "")));
            }

            if (StringUtils.startsWith(data, "")) {
                bean.setPurchaseTax(StringUtils.trim(StringUtils.substringAfter(data, "")));
            }

            if (StringUtils.startsWith(data, "?")) {
                bean.setCommercialInsurance(StringUtils.trim(StringUtils.substringAfter(data, "")));
            }

            if (StringUtils.startsWith(data, "")) {
                bean.setVehicleUseTax(StringUtils.trim(StringUtils.substringAfter(data, "")));
            }
            if (StringUtils.startsWith(data, "")) {
                bean.setCompulsoryInsurance(StringUtils.trim(StringUtils.substringAfter(data, "")));
            }
            if (StringUtils.startsWith(data, "")) {
                bean.setLicenseFee(StringUtils.trim(StringUtils.substringAfter(data, "")));
            }
            if (StringUtils.startsWith(data, "?")) {
                bean.setPromotion(StringUtils.trim(StringUtils.substringAfter(data, "")));
            }
            if (StringUtils.startsWith(data, "")) {
                bean.setBuyTime(StringUtils.trim(StringUtils.substringAfter(data, "")));
            }
            if (StringUtils.startsWith(data, "")) {
                String area = StringUtils.trim(StringUtils.substringAfter(data, ""));
                String[] pAndC = StringUtils.splitByWholeSeparator(area, ",", 2);

                if (pAndC.length == 1) {
                    bean.setBuyProvince(pAndC[0]);
                    bean.setBuyCity(pAndC[0]);
                }

                if (pAndC.length == 2) {
                    bean.setBuyProvince(pAndC[0]);
                    bean.setBuyCity(pAndC[1]);
                }

            }
            if (StringUtils.startsWith(data, "")) {
                Elements level = dataLi.select("span.level");
                // 
                if (!level.isEmpty()) {
                    bean.setSellerComment(level.first().text());
                }

                // ?
                Elements seller = dataLi.select("a.title");
                if (!seller.isEmpty()) {
                    String sellerUrl = seller.first().absUrl("href");
                    String sellerName = seller.first().text();
                    String sellerId = StringUtils.substringAfterLast(sellerUrl, "/");

                    bean.setSellerId(sellerId);
                    bean.setSellerName(sellerName);
                    bean.setSellerUrl(sellerUrl);
                }

                // ?
                Elements sellerPhone = dataLi.select("em.phone-num");
                if (!sellerPhone.isEmpty()) {
                    bean.setSellerPhone(sellerPhone.first().text());
                }

                // ?
                // Elements sellerAddress = dataLi.select("em.phone-num");

            }
            if (StringUtils.startsWith(data, "?")) {
                bean.setBuyFeeling(StringUtils.trim(StringUtils.substringAfter(data, "")));
            }
        }

        log.debug("Bean : {}", bean);

        bean.persistOnNotExist();
    }
}

From source file:com.norconex.commons.lang.time.YearMonthDayInterval.java

/**
 * Constructs a YearMonthDayInterval out of a string.  The recommended
 * string format is <code>yyyy-MM-dd - yyyy-MM-dd</code>, but any 
 * characters in between the start and end are accepted as long as there 
 * is a space after the start YearMontDay and before the end YearMonthDay.
 * @param interval the interval to parse
 *//*from  ww w  .  j a  v a 2s. c om*/
public YearMonthDayInterval(String interval) {
    String trimmed = StringUtils.trim(interval);
    String startStr = StringUtils.substringBefore(trimmed, " ");
    String endStr = StringUtils.substringAfterLast(trimmed, " ");
    if (StringUtils.isBlank(startStr) || StringUtils.isBlank(endStr)) {
        throw new IllegalArgumentException("String YearMonthDay interval cannot be null or empty.");
    }
    this.start = new YearMonthDay(startStr);
    this.end = new YearMonthDay(endStr);
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlMeta.java

/**
 * Handles the cookies specified in meta tags,
 * like <tt>&lt;meta http-equiv='set-cookie' content='webm=none; path=/;'&gt;</tt>.
 *//*  w w  w .  j  a  v  a2 s  .  c  o m*/
protected void performSetCookie() {
    final String[] parts = COOKIES_SPLIT_PATTERN.split(getContentAttribute(), 0);
    final String name = StringUtils.substringBefore(parts[0], "=");
    final String value = StringUtils.substringAfter(parts[0], "=");
    final URL url = getPage().getUrl();
    final String host = url.getHost();
    final boolean secure = "https".equals(url.getProtocol());
    String path = null;
    Date expires = null;
    for (int i = 1; i < parts.length; i++) {
        final String partName = StringUtils.substringBefore(parts[i], "=").trim().toLowerCase();
        final String partValue = StringUtils.substringAfter(parts[i], "=").trim();
        if ("path".equals(partName)) {
            path = partValue;
        } else if ("expires".equals(partName)) {
            expires = com.gargoylesoftware.htmlunit.util.StringUtils.parseHttpDate(partValue);
        } else {
            notifyIncorrectness("set-cookie http-equiv meta tag: unknown attribute '" + partName + "'.");
        }
    }
    final Cookie cookie = new Cookie(host, name, value, path, expires, secure);
    getPage().getWebClient().getCookieManager().addCookie(cookie);
}

From source file:cc.recommenders.utils.Zips.java

public static ICoReMethodName method(ZipEntry e, String suffix) {
    String name = "L" + StringUtils.substringBefore(e.getName(), suffix);
    int start = name.lastIndexOf('/');
    char[] chars = name.toCharArray();
    chars[start] = '.';
    for (int i = start + 1; i < chars.length; i++) {
        if (chars[i] == '.')
            chars[i] = '/';
    }/*w w w  .ja  va 2s.c o m*/
    return CoReMethodName.get(new String(chars));
}

From source file:de.blizzy.documentr.search.GetVisibleBranchDocIdsTaskTest.java

@Test
public void call() throws IOException {
    List<String> branches = Lists.newArrayList("project1/branch1", "project1/branch2", "project2/branch"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    when(permissionEvaluator.getBranchesForPermission(authentication, Permission.VIEW))
            .thenReturn(Sets.newHashSet(branches));

    BooleanQuery query = new BooleanQuery();
    for (String projectAndBranch : branches) {
        BooleanQuery q = new BooleanQuery();
        String projectName = StringUtils.substringBefore(projectAndBranch, "/"); //$NON-NLS-1$
        String branchName = StringUtils.substringAfter(projectAndBranch, "/"); //$NON-NLS-1$
        q.add(new TermQuery(new Term(PageIndex.PROJECT, projectName)), BooleanClause.Occur.MUST);
        q.add(new TermQuery(new Term(PageIndex.BRANCH, branchName)), BooleanClause.Occur.MUST);
        query.add(q, BooleanClause.Occur.SHOULD);
    }//from w  ww  . j a  v a 2 s.c  om

    task.call();

    verify(searcher).search(eq(query), Matchers.any(AllDocIdsCollector.class));
}

From source file:io.wcm.devops.conga.plugins.aem.util.JsonContentLoaderTest.java

@SuppressWarnings("unchecked")
private static Map<String, Object> getDeep(Map<String, Object> map, String path) {
    String name = StringUtils.substringBefore(path, "/");
    Object object = map.get(name);
    if (object == null || !(object instanceof Map)) {
        return null;
    }/*from  ww  w  .j  a  v  a 2s. co m*/
    String remainingPath = StringUtils.substringAfter(path, "/");
    Map<String, Object> childMap = (Map<String, Object>) object;
    if (StringUtils.isEmpty(remainingPath)) {
        return childMap;
    } else {
        return getDeep(childMap, remainingPath);
    }
}

From source file:com.threewks.thundr.configuration.PropertiesLoader.java

private Map<String, String> readProperties(String resourceAsString) {
    Map<String, String> properties = new LinkedHashMap<String, String>();
    Scanner scanner = new Scanner(resourceAsString);
    try {//w  w  w  .ja  v a 2  s. com
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            line = StringUtils.substringBefore(line, "#");
            line = StringUtils.trimToNull(line);
            String key = StringUtils.substringBefore(line, "=");
            String value = StringUtils.substringAfter(line, "=");
            if (key != null) {
                properties.put(key, value);
            }
        }
    } finally {
        scanner.close();
    }
    return properties;
}