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

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

Introduction

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

Prototype

public static boolean startsWith(final CharSequence str, final CharSequence prefix) 

Source Link

Document

Check if a CharSequence starts with a specified prefix.

null s are handled without exceptions.

Usage

From source file:com.kingfong.webcrawler.util.DOMContentUtils.java

/**
 * This method finds all anchors below the supplied DOM
 * <code>node</code>, and creates appropriate {@link Outlink}
 * records for each (relative to the supplied <code>base</code>
 * URL), and adds them to the <code>outlinks</code> {@link
 * ArrayList}./*ww  w.ja v a 2  s .  co m*/
 *
 * <p>
 *
 * Links without inner structure (tags, text, etc) are discarded, as
 * are links which contain only single nested links and empty text
 * nodes (this is a common DOM-fixup artifact, at least with
 * nekohtml).
 */
public void getOutlinks(String html, URL url, HashSet<String> outlinks) {

    Document document = Jsoup.parse(html);
    Elements elements = document.getAllElements();
    for (Element currentNode : elements) {
        String nodeName = currentNode.tagName();
        // short nodeType = currentNode.;
        Elements children = currentNode.children();
        nodeName = nodeName.toLowerCase();
        LinkParams params = linkParams.get(nodeName);
        if (params != null) {
            // if (!shouldThrowAwayLink(currentNode, children, childLen,
            // params)) {

            // StringBuilder linkText = new StringBuilder();
            // getText(linkText, currentNode, true);

            Attributes attrs = currentNode.attributes();
            String target = null;
            boolean noFollow = false;
            boolean post = false;
            Iterator<Attribute> iterator = attrs.iterator();
            while (iterator.hasNext()) {
                Attribute attr = iterator.next();
                String attrName = attr.getKey();
                if (params.attrName.equalsIgnoreCase(attrName)) {
                    target = attr.getValue();
                } else if ("rel".equalsIgnoreCase(attrName) && "nofollow".equalsIgnoreCase(attr.getValue())) {
                    noFollow = true;
                } else if ("method".equalsIgnoreCase(attrName) && "post".equalsIgnoreCase(attr.getValue())) {
                    post = true;
                }
            }
            if (StringUtils.startsWith(target, "/")) {
                target = url.getProtocol() + "://" + url.getHost() + target;
            }
            if (target != null && URLFilter.filt(target)) {
                outlinks.add(target);
            }
            // }
            // this should not have any children, skip them
            if (params.childLen == 0)
                continue;
        }
    }
}

From source file:bjerne.gallery.service.impl.GalleryServiceImpl.java

private boolean isVideo(File file) throws IOException {
    return StringUtils.startsWith(getContentType(file), "video");
}

From source file:bjerne.gallery.controller.GalleryController.java

/**
 * Extracts the request range header if present.
 * /* w  w w .  ja  v  a2 s  .co  m*/
 * @param rangeHeader
 *            Range header.
 * @return a long[] which will always be the size 2. The first element is
 *         the start index, and the second the end index. If the end index
 *         is not set (which means till the end of the resource), 0 is
 *         returned in that field.
 */
private long[] getRangesFromHeader(String rangeHeader) {
    LOG.debug("Range header: {}", rangeHeader);
    long[] result = new long[2];
    final String headerPrefix = "bytes=";
    if (StringUtils.startsWith(rangeHeader, headerPrefix)) {
        String[] splitRange = rangeHeader.substring(headerPrefix.length()).split("-");
        try {
            result[0] = Long.parseLong(splitRange[0]);
            if (splitRange.length > 1) {
                result[1] = Long.parseLong(splitRange[1]);
            }
            if (result[0] < 0 || (result[1] != 0 && result[0] > result[1])) {
                throw new RangeException();
            }
        } catch (NumberFormatException nfe) {
            throw new RangeException();
        }
    }
    return result;
}

From source file:com.mirth.connect.client.ui.components.MirthTree.java

private static PathNode.NodeType getXmlNodeType(MirthTreeNode node) {
    PathNode.NodeType type = null;/*from   www  . ja v a  2s .  com*/
    String nodeValue = node.getValue().replaceAll(" \\(.*\\)", "");

    if (nodeContainImplicitNamespace(node)) {
        type = PathNode.NodeType.XML_XMLNS_NODE;
    } else if (StringUtils.startsWith(nodeValue, "@xmlns")) {
        if (StringUtils.contains(nodeValue, ":")) {
            type = PathNode.NodeType.XML_PREFIX_DEFINITION;
        } else {
            type = PathNode.NodeType.XMLNS_DEFINITION;
        }
    } else if (StringUtils.contains(nodeValue, "@")) {
        if (StringUtils.contains(nodeValue, ":")) {
            type = PathNode.NodeType.XML_PREFIXED_ATTRIBUTE;
        } else {
            type = PathNode.NodeType.XML_ATTRIBUTE;
        }
    } else if (StringUtils.contains(nodeValue, ":")) {
        type = PathNode.NodeType.XML_PREFIXED_NODE;
    }
    return type;
}

From source file:com.nridge.ds.solr.SolrConfigJSON.java

private void populateCfgRequestHandler(JsonReader aReader, Document aCfgDocument, String aDocType)
        throws IOException {
    DataBag rhBag, snBag;//from  w  ww . j av  a  2  s  .  c  om
    String jsonName, jsonValue;
    Document snDocument, defDocument, coDocument, lcDocument, inDocument, upDocument;

    Document rhDocument = new Document(aDocType);
    rhBag = rhDocument.getBag();
    aReader.beginObject();
    while (aReader.hasNext()) {
        jsonName = aReader.nextName();
        if (StringUtils.startsWith(jsonName, "/")) {
            snDocument = new Document(Solr.RESPONSE_CONFIG_RH_SN);
            snDocument.setName(jsonName);
            snBag = snDocument.getBag();
            DataTextField operationField = new DataTextField(Solr.CONFIG_OPERATION_FIELD_NAME,
                    Field.nameToTitle(Solr.CONFIG_OPERATION_FIELD_NAME));
            operationField.addFeature(Field.FEATURE_IS_STORED, StrUtl.STRING_FALSE);
            snBag.add(operationField);
            aReader.beginObject();
            while (aReader.hasNext()) {
                jsonName = aReader.nextName();
                if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_SN_DEFAULTS)) {
                    defDocument = new Document(Solr.RESPONSE_CONFIG_RH_SN_DEFAULTS);
                    defDocument.setName(jsonName);
                    aReader.beginObject();
                    while (aReader.hasNext())
                        addFieldToDocument(aReader, defDocument);
                    aReader.endObject();
                    snDocument.addRelationship(Solr.RESPONSE_CONFIG_RH_SN_DEFAULTS, defDocument);
                } else if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_SN_CO)) {
                    coDocument = new Document(Solr.RESPONSE_CONFIG_RH_SN_COMPONENTS);
                    coDocument.setName(jsonName);
                    addFieldToDocument(aReader, coDocument, jsonName);
                    snDocument.addRelationship(Solr.RESPONSE_CONFIG_RH_SN_COMPONENTS, coDocument);
                } else if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_SN_LC)) {
                    lcDocument = new Document(Solr.RESPONSE_CONFIG_RH_SN_LAST_COMPONENTS);
                    lcDocument.setName(jsonName);
                    addFieldToDocument(aReader, lcDocument, jsonName);
                    snDocument.addRelationship(Solr.RESPONSE_CONFIG_RH_SN_LAST_COMPONENTS, lcDocument);
                } else if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_SN_IN)) {
                    inDocument = new Document(Solr.RESPONSE_CONFIG_RH_SN_INVARIANTS);
                    inDocument.setName(jsonName);
                    aReader.beginObject();
                    while (aReader.hasNext())
                        addFieldToDocument(aReader, inDocument);
                    aReader.endObject();
                    snDocument.addRelationship(Solr.RESPONSE_CONFIG_RH_SN_INVARIANTS, inDocument);
                } else {
                    jsonValue = nextValueAsString(aReader);
                    addBagTextField(snBag, jsonName, jsonValue);
                    snBag.add(new DataTextField(jsonName, Field.nameToTitle(jsonName), jsonValue));
                }
            }
            aReader.endObject();
            rhDocument.addRelationship(Solr.RESPONSE_CONFIG_RH_SN, snDocument);
        } else if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_SN_UPDATE)) {
            upDocument = new Document(Solr.RESPONSE_CONFIG_RH_SN_UPDATE);
            upDocument.setName(jsonName);
            aReader.beginObject();
            while (aReader.hasNext())
                addFieldToDocument(aReader, upDocument);
            aReader.endObject();
            rhDocument.addRelationship(Solr.RESPONSE_CONFIG_RH_SN_UPDATE, upDocument);
        } else {
            jsonValue = nextValueAsString(aReader);
            addBagTextField(rhBag, jsonName, jsonValue);
        }
    }
    aReader.endObject();
    aCfgDocument.addRelationship(Solr.RESPONSE_CONFIG_REQUEST_HANDLER, rhDocument);
}

From source file:ching.icecreaming.action.ResourceDescriptors.java

private boolean searchFilter(String searchField, String searchOper, String searchString, Object object1) {
    boolean result1 = true;
    String string1 = null;//from  ww w.  j  a  v a2s.  co m
    Integer integer1 = null;
    java.sql.Timestamp timestamp1 = null;
    org.joda.time.DateTime dateTime1 = null, dateTime2 = null;
    DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm");
    java.util.Date date1 = null;

    if (object1 instanceof String) {
        string1 = (String) object1;
        switch (searchOper) {
        case "eq":
            result1 = StringUtils.equals(string1, searchString);
            break;
        case "ne":
            result1 = !StringUtils.equals(string1, searchString);
            break;
        case "bw":
            result1 = StringUtils.startsWith(string1, searchString);
            break;
        case "bn":
            result1 = !StringUtils.startsWith(string1, searchString);
            break;
        case "ew":
            result1 = StringUtils.endsWith(string1, searchString);
            break;
        case "en":
            result1 = !StringUtils.endsWith(string1, searchString);
            break;
        case "cn":
            result1 = StringUtils.contains(string1, searchString);
            break;
        case "nc":
            result1 = !StringUtils.contains(string1, searchString);
            break;
        case "nu":
            result1 = StringUtils.isBlank(string1);
            break;
        case "nn":
            result1 = StringUtils.isNotBlank(string1);
            break;
        case "in":
        case "ni":
        case "lt":
        case "le":
        case "gt":
        case "ge":
        default:
            break;
        }
    } else if (object1 instanceof Integer) {
        if (NumberUtils.isNumber(searchString)) {
            integer1 = (Integer) object1;
            switch (searchOper) {
            case "eq":
                result1 = (NumberUtils.toInt(searchString, 0) == integer1.intValue());
                break;
            case "ne":
                result1 = (NumberUtils.toInt(searchString, 0) != integer1.intValue());
                break;
            case "lt":
                result1 = (NumberUtils.toInt(searchString, 0) > integer1.intValue());
                break;
            case "le":
                result1 = (NumberUtils.toInt(searchString, 0) >= integer1.intValue());
                break;
            case "gt":
                result1 = (NumberUtils.toInt(searchString, 0) < integer1.intValue());
                break;
            case "ge":
                result1 = (NumberUtils.toInt(searchString, 0) <= integer1.intValue());
                break;
            case "bw":
            case "bn":
            case "ew":
            case "en":
            case "cn":
            case "nc":
            case "in":
            case "ni":
            case "nu":
            case "nn":
            default:
                break;
            }
        }
    } else if (object1 instanceof java.sql.Timestamp || object1 instanceof java.util.Date) {
        if (object1 instanceof java.sql.Timestamp) {
            timestamp1 = (java.sql.Timestamp) object1;
            dateTime1 = new org.joda.time.DateTime(timestamp1.getTime());
        } else if (object1 instanceof java.util.Date) {
            date1 = (java.util.Date) object1;
            if (date1 != null)
                dateTime1 = new org.joda.time.DateTime(date1);
        }
        try {
            dateTime2 = dateTimeFormatter.parseDateTime(searchString);
        } catch (java.lang.IllegalArgumentException exception1) {
            dateTime2 = null;
        }
        if (dateTime2 != null && dateTime1 != null) {
            switch (searchOper) {
            case "eq":
                result1 = dateTime1.equals(dateTime2);
                break;
            case "ne":
                result1 = !dateTime1.equals(dateTime2);
                break;
            case "lt":
                result1 = dateTime1.isBefore(dateTime2);
                break;
            case "le":
                result1 = (dateTime1.isBefore(dateTime2) || dateTime1.equals(dateTime2));
                break;
            case "gt":
                result1 = dateTime1.isAfter(dateTime2);
                break;
            case "ge":
                result1 = (dateTime1.isAfter(dateTime2) || dateTime1.equals(dateTime2));
                break;
            case "bw":
            case "bn":
            case "ew":
            case "en":
            case "cn":
            case "nc":
            case "in":
            case "ni":
                break;
            case "nu":
                result1 = (timestamp1 == null);
                break;
            case "nn":
                result1 = (timestamp1 != null);
                break;
            default:
                break;
            }
        }
    }
    return !result1;
}

From source file:io.swagger.v3.parser.converter.SwaggerConverter.java

private List<Server> convert(List<Scheme> schemes, String host, String basePath) {
    List<Server> servers = new ArrayList<>();
    String baseUrl;/*from w w w  . j  a va  2  s .  c  o  m*/

    if (StringUtils.isNotEmpty(basePath)) {
        baseUrl = basePath;
    } else {
        baseUrl = "/";
    }

    if (StringUtils.isNotEmpty(host)) {
        baseUrl = host + baseUrl;
    }

    if (!StringUtils.startsWith(baseUrl, "/") && schemes != null && !schemes.isEmpty()) {
        for (Scheme scheme : schemes) {
            Server server = new Server();
            server.setUrl(scheme.toValue() + "://" + baseUrl);

            servers.add(server);
        }
    } else {
        if (!"/".equals(baseUrl)) {
            baseUrl = "//" + baseUrl;
        }
        Server server = new Server();
        server.setUrl(baseUrl);
        servers.add(server);
    }

    return servers;
}

From source file:io.apiman.gateway.platforms.vertx3.common.config.VertxEngineConfig.java

private List<Entry<String, String>> getKeys(String prefix) {
    return System.getProperties().entrySet().stream()
            .map(pair -> new AbstractMap.SimpleEntry<>(String.valueOf(pair.getKey()),
                    String.valueOf(pair.getValue())))
            .filter(pair -> StringUtils.startsWith(pair.getKey(), prefix)).collect(Collectors.toList());
}

From source file:com.pidoco.juri.JURI.java

/**
 * <pre>//  ww  w.j a v a2  s . c  o m
 *     "".addRawPath("") -> ""
 *     "/".addRawPath("") -> "/"
 *     "".addRawPath("/") -> "/"
 *     "a".addRawPath("") -> "a/"
 *     "a".addRawPath("b") -> "a/b"
 *     "/".addRawPath("/") -> "/"
 * </pre>
 */
public static String concatRawPaths(CharSequence left, CharSequence right) {
    boolean needsSeparator = false;
    boolean rightStartsWithSlash = StringUtils.startsWith(right, "/");
    int rightStart = 0;
    if (left.length() > 0) {
        if (StringUtils.endsWith(left, "/")) {
            if (rightStartsWithSlash) {
                rightStart = 1;
            }
        } else {
            if (!rightStartsWithSlash) {
                needsSeparator = true;
            }
        }
    }

    return left + (needsSeparator ? "/" : "") + right.subSequence(rightStart, right.length());
}

From source file:com.mirth.connect.server.Mirth.java

private String getWebServerUrl(String prefix, String host, int port, String contextPath) {
    if (StringUtils.equals(host, "0.0.0.0") || StringUtils.equals(host, "::")) {
        try {/*  w ww .  ja v a2  s  . c om*/
            host = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            host = "localhost";
        }
    } else if (StringUtils.isEmpty(host)) {
        host = "localhost";
    }

    if (!StringUtils.startsWith(contextPath, "/")) {
        contextPath = "/" + contextPath;
    }

    if (!StringUtils.endsWith(contextPath, "/")) {
        contextPath = contextPath + "/";
    }

    return prefix + host + ":" + port + contextPath;
}