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

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

Introduction

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

Prototype

public static int indexOf(final CharSequence seq, final CharSequence searchSeq) 

Source Link

Document

Finds the first index within a CharSequence, handling null .

Usage

From source file:net.pms.network.UPNPHelper.java

/**
 * Starts up two threads: one to broadcast UPnP ALIVE messages and another
 * to listen for responses. /*from  w ww. j  av a2 s .  co m*/
 *
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void listen() throws IOException {
    Runnable rAlive = new Runnable() {
        @Override
        public void run() {
            int delay = 10000;

            while (true) {
                sleep(delay);
                sendAlive();

                // The first delay for sending an ALIVE message is 10 seconds,
                // the second delay is for 20 seconds. From then on, all other
                // delays are for 180 seconds.
                switch (delay) {
                case 10000:
                    delay = 20000;
                    break;
                case 20000:
                    delay = 180000;
                    break;
                }
            }
        }
    };

    aliveThread = new Thread(rAlive, "UPNP-AliveMessageSender");
    aliveThread.start();

    Runnable r = new Runnable() {
        @Override
        public void run() {
            boolean bindErrorReported = false;

            while (true) {
                MulticastSocket multicastSocket = null;

                try {
                    // Use configurable source port as per http://code.google.com/p/ps3mediaserver/issues/detail?id=1166
                    multicastSocket = new MulticastSocket(configuration.getUpnpPort());

                    if (bindErrorReported) {
                        logger.warn(
                                "Finally, acquiring port " + configuration.getUpnpPort() + " was successful!");
                    }

                    NetworkInterface ni = NetworkConfiguration.getInstance().getNetworkInterfaceByServerName();

                    try {
                        // Setting the network interface will throw a SocketException on Mac OSX
                        // with Java 1.6.0_45 or higher, but if we don't do it some Windows
                        // configurations will not listen at all.
                        if (ni != null) {
                            multicastSocket.setNetworkInterface(ni);
                        } else if (PMS.get().getServer().getNetworkInterface() != null) {
                            multicastSocket.setNetworkInterface(PMS.get().getServer().getNetworkInterface());
                            logger.trace("Setting multicast network interface: "
                                    + PMS.get().getServer().getNetworkInterface());
                        }
                    } catch (SocketException e) {
                        // Not setting the network interface will work just fine on Mac OSX.
                    }

                    multicastSocket.setTimeToLive(4);
                    multicastSocket.setReuseAddress(true);
                    InetAddress upnpAddress = getUPNPAddress();
                    multicastSocket.joinGroup(upnpAddress);

                    while (true) {
                        byte[] buf = new byte[1024];
                        DatagramPacket receivePacket = new DatagramPacket(buf, buf.length);
                        multicastSocket.receive(receivePacket);

                        String s = new String(receivePacket.getData());

                        InetAddress address = receivePacket.getAddress();

                        if (s.startsWith("M-SEARCH")) {
                            String remoteAddr = address.getHostAddress();
                            int remotePort = receivePacket.getPort();

                            if (configuration.getIpFiltering().allowed(address)) {
                                logger.trace(
                                        "Receiving a M-SEARCH from [" + remoteAddr + ":" + remotePort + "]");

                                if (StringUtils.indexOf(s,
                                        "urn:schemas-upnp-org:service:ContentDirectory:1") > 0) {
                                    sendDiscover(remoteAddr, remotePort,
                                            "urn:schemas-upnp-org:service:ContentDirectory:1");
                                }

                                if (StringUtils.indexOf(s, "upnp:rootdevice") > 0) {
                                    sendDiscover(remoteAddr, remotePort, "upnp:rootdevice");
                                }

                                if (StringUtils.indexOf(s, "urn:schemas-upnp-org:device:MediaServer:1") > 0) {
                                    sendDiscover(remoteAddr, remotePort,
                                            "urn:schemas-upnp-org:device:MediaServer:1");
                                }

                                if (StringUtils.indexOf(s, "ssdp:all") > 0) {
                                    sendDiscover(remoteAddr, remotePort,
                                            "urn:schemas-upnp-org:device:MediaServer:1");
                                }

                                if (StringUtils.indexOf(s, PMS.get().usn()) > 0) {
                                    sendDiscover(remoteAddr, remotePort, PMS.get().usn());
                                }
                            }
                        } else if (s.startsWith("NOTIFY")) {
                            String remoteAddr = address.getHostAddress();
                            int remotePort = receivePacket.getPort();

                            logger.trace("Receiving a NOTIFY from [" + remoteAddr + ":" + remotePort + "]");
                        }
                    }
                } catch (BindException e) {
                    if (!bindErrorReported) {
                        logger.error("Unable to bind to " + configuration.getUpnpPort()
                                + ", which means that PMS will not automatically appear on your renderer! "
                                + "This usually means that another program occupies the port. Please "
                                + "stop the other program and free up the port. "
                                + "PMS will keep trying to bind to it...[" + e.getMessage() + "]");
                    }

                    bindErrorReported = true;
                    sleep(5000);
                } catch (IOException e) {
                    logger.error("UPNP network exception", e);
                    sleep(1000);
                } finally {
                    if (multicastSocket != null) {
                        // Clean up the multicast socket nicely
                        try {
                            InetAddress upnpAddress = getUPNPAddress();
                            multicastSocket.leaveGroup(upnpAddress);
                        } catch (IOException e) {
                        }

                        multicastSocket.disconnect();
                        multicastSocket.close();
                    }
                }
            }
        }
    };

    listenerThread = new Thread(r, "UPNPHelper");
    listenerThread.start();
}

From source file:Heuristics.TermLevelHeuristics.java

public boolean isImmediatelyPrecededByANegation(String status, String termOrig) {
    termHeuristic = termHeuristic.toLowerCase();
    int indexTerm = StringUtils.indexOf(status, termHeuristic);
    String[] temp = StringUtils.left(status, indexTerm).split(" ");

    //if the array is empty it means that the term is the first of the status;
    if (temp.length == 0) {
        return false;
        //in this case the term is the second in the status. If the previous one is a negative word, return true (as in "like" being preceded by "don't") 
    } else if (temp.length == 1) {
        boolean res = HLoader.getSetNegations().contains(temp[0]) ? true : false;
        return res;
        //in this case the term is preceded by many other terms. We just check the three previous ones.    
    } else if (temp.length == 2) {
        if (HLoader.getSetNegations().contains(temp[temp.length - 1])) {
            return true;
        }/*from   w w w  .  j  a v  a  2 s.co  m*/
        //in the case of "don't really like", return true
        if (HLoader.getMapH3().containsKey(temp[temp.length - 1])
                & HLoader.getSetNegations().contains(temp[temp.length - 2])) {
            return true;
        }
        //in the case of "not the hottest", return true
        String concat = temp[0] + " " + temp[1];
        boolean res = (HLoader.getSetNegations().contains(concat)) ? true : false;
        return res;
    } else if (temp.length > 2) {
        if (HLoader.getSetNegations().contains(temp[temp.length - 1])) {
            return true;
        }
        if (HLoader.getMapH3().containsKey(temp[temp.length - 1])
                & HLoader.getSetNegations().contains(temp[temp.length - 2])) {
            return true;
        }
        //in the case of "not the hottest", return true
        String concat = temp[temp.length - 2] + " " + temp[temp.length - 1];
        boolean res = (HLoader.getSetNegations().contains(concat)) ? true : false;
        return res;
    }
    return false;
}

From source file:io.wcm.config.core.persistence.impl.ToolsConfigPagePersistenceProvider.java

private String getConfigPagePathFromConfigResourcePath(String configResourcePath) {
    int index = StringUtils.indexOf(configResourcePath, "/jcr:content/");
    if (index <= 0) {
        return null;
    }/*w w w . j a  v  a  2s. com*/
    return StringUtils.substring(configResourcePath, 0, index);
}

From source file:cgeo.geocaching.CacheDetailActivity.java

/**
 * Hide the short description, if it is contained somewhere at the start of the long description.
 *///from   ww  w  .j  a  v  a  2 s.com
public void potentiallyHideShortDescription() {
    final View shortView = ButterKnife.findById(this, R.id.description);
    if (shortView == null) {
        return;
    }
    if (shortView.getVisibility() == View.GONE) {
        return;
    }
    final String shortDescription = cache.getShortDescription();
    if (StringUtils.isNotBlank(shortDescription)) {
        final int index = StringUtils.indexOf(cache.getDescription(), shortDescription);
        // allow up to 200 characters of HTML formatting
        if (index >= 0 && index < 200) {
            shortView.setVisibility(View.GONE);
        }
    }
}

From source file:nl.sidn.pcap.util.GeoLookupUtil.java

/**
 * parse maxmind asn result/*  www .ja  va 2  s.  c o m*/
 * @return ASXXXX formatted string
 */
private String parseASN(String asn) {

    if (asn != null) {
        int pos = StringUtils.indexOf(asn, ' ');
        if (pos != -1) {
            return StringUtils.substring(asn, 0, pos);
        }
        //not a valid asn string
        return "UNKN";
    }
    //not found
    return null;
}

From source file:org.apache.lens.cube.parse.TestQuery.java

private String getJoinString(String joinQueryStr) {
    int nextJoinIndex = Integer.MAX_VALUE;
    for (JoinType joinType : JoinType.values()) {
        int joinIndex = StringUtils.indexOf(joinQueryStr, joinType.name());
        if (joinIndex < nextJoinIndex && joinIndex > 0) {
            nextJoinIndex = joinIndex;/*from   w  w w.ja v a 2  s  .  c  om*/
        }
    }
    if (nextJoinIndex == Integer.MAX_VALUE) {
        int minClauseIndex = getMinIndexOfClause(joinQueryStr);
        // return join query completely if there is no Clause in the query
        return minClauseIndex == -1 ? joinQueryStr : joinQueryStr.substring(0, minClauseIndex);
    }
    return joinQueryStr.substring(0, nextJoinIndex);
}

From source file:org.apache.lens.cube.parse.TestQuery.java

private int getMinIndexOfClause(String query) {
    int minClauseIndex = Integer.MAX_VALUE;
    for (Clause clause : Clause.values()) {
        int clauseIndex = StringUtils.indexOf(query, clause.name());
        if (clauseIndex == -1) {
            continue;
        }/*from  www.  j a  va  2s  .  c  o  m*/
        minClauseIndex = clauseIndex < minClauseIndex ? clauseIndex : minClauseIndex;
    }
    return (minClauseIndex == Integer.MAX_VALUE) ? query.length() : minClauseIndex;
}

From source file:org.apache.lens.cube.parse.TestQuery.java

private int getMinIndexOfJoinType() {
    int minJoinTypeIndex = Integer.MAX_VALUE;
    for (JoinType joinType : JoinType.values()) {
        int joinIndex = StringUtils.indexOf(trimmedQuery, joinType.name());
        if (joinIndex == -1) {
            continue;
        }/*from   ww w  .  j  a  v a 2 s .  c o m*/
        minJoinTypeIndex = joinIndex < minJoinTypeIndex ? joinIndex : minJoinTypeIndex;
    }
    return minJoinTypeIndex == Integer.MAX_VALUE ? -1 : minJoinTypeIndex;
}

From source file:org.apache.sling.contextaware.config.impl.metadata.AnnotationClassParser.java

private static Map<String, String> propsArrayToMap(String[] properties) {
    Map<String, String> props = new HashMap<>();
    for (String property : properties) {
        int index = StringUtils.indexOf(property, "=");
        if (index >= 0) {
            String key = property.substring(0, index);
            String value = property.substring(index + 1);
            props.put(key, value);/*  www . j  av  a 2s . com*/
        }
    }
    return props;
}

From source file:org.apache.struts2.components.template.TemplateEngineManager.java

/**
 * <p>/* w w  w  . j  ava  2  s.c om*/
 * Gets the TemplateEngine for the template name. If the template name has an extension (for instance foo.jsp), then
 * this extension will be used to look up the appropriate TemplateEngine. If it does not have an extension, it will
 * look for a Configuration setting "struts.ui.templateSuffix" for the extension, and if that is not set, it
 * will fall back to "ftl" as the default.
 * </p>
 * 
 * @param template               Template used to determine which TemplateEngine to return
 * @param templateTypeOverride Overrides the default template type
 * @return the engine.
 */
public TemplateEngine getTemplateEngine(Template template, String templateTypeOverride) {
    String templateType = DEFAULT_TEMPLATE_TYPE;
    String templateName = template.toString();
    if (StringUtils.contains(templateName, ".")) {
        templateType = StringUtils.substring(templateName, StringUtils.indexOf(templateName, ".") + 1);
    } else if (StringUtils.isNotBlank(templateTypeOverride)) {
        templateType = templateTypeOverride;
    } else {
        String type = defaultTemplateType;
        if (type != null) {
            templateType = type;
        }
    }
    return templateEngines.get(templateType).create();
}