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

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

Introduction

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

Prototype

public static String substring(final String str, int start, int end) 

Source Link

Document

Gets a substring from the specified String avoiding exceptions.

A negative start position can be used to start/end n characters from the end of the String.

The returned substring starts with the character in the start position and ends before the end position.

Usage

From source file:org.neo4art.importer.wikipedia.parser.util.WikipediaInfoboxParserUtils.java

/**
 * @param input//from  w  ww.  ja  va2s . co  m
 * @return
 */
public static String getTextFromLink(String input) {

    String result = null;

    if (StringUtils.isNotBlank(input)) {

        result = input;
        result = StringUtils.remove(result, "[[");
        result = StringUtils.remove(result, "]]");

        if (StringUtils.indexOf(result, "|") != -1) {

            result = StringUtils.substring(result, 0, StringUtils.indexOf(result, "|"));
        }

        result = StringUtils.trim(result);
    }

    return result;
}

From source file:org.neo4art.importer.wikipedia.parser.util.WikipediaInfoboxParserUtils.java

/**
 * //www . j av a 2s  .  c  om
 * @param input
 * @return
 * @throws MalformedURLException
 */
public static URL parseAsURL(String input) throws MalformedURLException {

    URL result = null;

    if (!StringUtils.isBlank(input)) {

        String temp = input;

        temp = StringUtils.remove(temp, "{");
        temp = StringUtils.remove(temp, "}");

        if (StringUtils.indexOf(temp, "[") != -1) {

            temp = StringUtils.remove(temp, "[");
            temp = StringUtils.remove(temp, "]");

            if (StringUtils.indexOf(temp, " ") != -1) {
                temp = StringUtils.substring(temp, 0, StringUtils.indexOf(temp, " "));
            }
        }

        temp = StringUtils.remove(temp, "URL|");
        temp = StringUtils.replace(temp, " ", "_");

        if (!StringUtils.startsWith(temp, "http")) {

            if (StringUtils.startsWith(temp, "www")) {

                temp = "http://" + temp;
            } else {

                temp = "https://en.wikipedia.org/wiki/File:" + temp;
            }
        }

        temp = StringUtils.removeEnd(temp, "|");

        result = new URL(temp);
    }

    return result;
}

From source file:org.oimutils.PasswordUtils.java

/**
 * gets a random character from the input set
 * /*ww  w.j ava  2 s.  co m*/
 * @param string
 * @return
 */
private static char getRandomChar(String string) {
    int pos = (int) (Math.random() * StringUtils.length(string));
    return CharUtils.toChar(StringUtils.substring(string, pos, pos + 1));
}

From source file:org.openestate.is24.restapi.utils.XmlUtils.java

/**
 * Write a {@link String} value into XML output
 * with a valid phone number subscriber part.
 *
 * @param value//from   w  w w  .  j a v a2 s.  c  o  m
 * value to write
 *
 * @return
 * XML string
 *
 * @throws IllegalArgumentException
 * if a validation error occured
 */
public static String printPhoneNumberSubscriber(String value) {
    String val = StringUtils.trimToNull(value);

    if (val != null) {
        // sicherstellen, dass am Anfang und Ende ein numerischer Wert steht
        String firstDigit = StringUtils.substring(val, 0, 1);
        String lastDigit = StringUtils.substring(val, val.length() - 1);
        if (!StringUtils.isNumeric(firstDigit) || !StringUtils.isNumeric(lastDigit)) {
            throw new IllegalArgumentException("The provided phone subscriber  '" + value + "' is invalid!");
        }

        // alle nicht numerischen Zeichen durch "-" ersetzen
        val = val.replaceAll("\\D", "-");
        while (val.contains("--")) {
            val = StringUtils.replace(val, "--", "-");
        }
    }

    if (val == null || !val.matches("[\\d][\\d \\-]{0,24}[\\d]")) {
        throw new IllegalArgumentException("The provided phone subscriber  '" + value + "' is invalid!");
    }

    return val;
}

From source file:org.openiot.ide.core.MenuFactory.java

/**
 * checks if the base Url ends with a '/' character and appends accordingly
 * // ww  w.ja va2s  . c o  m
 * @param baseUrl
 * @return
 */
private String createMonitorURL(String baseUrl) {
    // if (!StringUtils.endsWith(baseUrl, "/")) {
    // baseUrl += "/";
    // }

    // int indexDomainUrl = StringUtils.indexOf(baseUrl, );
    String suffix = StringUtils.substring(baseUrl, Faces.getRequestDomainURL().length(), baseUrl.length() - 1);
    String moduleName = StringUtils.split(suffix, "/")[0];

    return Faces.getRequestDomainURL() + "/" + moduleName + "/" + FIELD_MONITORING;
}

From source file:org.opennms.tools.jmxconfiggenerator.graphs.SnmpGraphConfigGenerator.java

public static void generateGraphs(String serviceName, String inputFile, String outFile) throws IOException {
    JmxDatacollectionConfig inputConfig = JAXB.unmarshal(new File(inputFile), JmxDatacollectionConfig.class);

    for (JmxCollection jmxCollection : inputConfig.getJmxCollection()) {
        logger.debug("jmxCollection: '{}'", jmxCollection.getName());
        Mbeans mbeans = jmxCollection.getMbeans();
        int i = 0;
        for (Mbean mBean : mbeans.getMbean()) {
            logger.debug("\t" + "mBean: '{}'", mBean.getObjectname());
            String reportName = "jsr160" + "." + serviceName + "." + "combo" + i;
            graphList = graphList + reportName + ", \\" + "\n";
            i++;// ww w  .  j  a v  a  2s  .  co m
            graphBodies = graphBodies + "\n" + generateGraphCombo(jmxCollection, mBean, reportName);
            for (Attrib attrib : mBean.getAttrib()) {
                logger.debug("\t\t" + "attrib: '{}'", attrib.getAlias());
                graphList = graphList + "jsr160" + "." + serviceName + "."
                        + StringUtils.substring(attrib.getAlias(), 0, 19) + ", \\" + "\n";
                graphBodies = graphBodies + "\n" + generateGraph(jmxCollection, mBean, attrib);
            }
        }
    }
    graphList = StringUtils.substringBeforeLast(graphList, ", \\\n") + "\n";
    output = graphList + "\n" + graphBodies;
    output = StringUtils.substringBeforeLast(output, " \\" + "\n") + "\n";
    FileUtils.writeStringToFile(new File(outFile), output, "UTF-8");
}

From source file:org.opennms.tools.jmxconfiggenerator.graphs.SnmpGraphConfigGenerator.java

private static String generateGraphCombo(JmxCollection jmxCollection, Mbean mBean, String reportName) {
    String report = "";

    //      report.jvm.mempool.oldgen.name=JVM Memory Pool: OldGen Space
    report = report.concat("report." + reportName + ".name=" + reportName + "\n");

    List<String> attribNameList = new ArrayList<String>();
    String columnsNames = "";
    for (Attrib attrib : mBean.getAttrib()) {
        String attribAliasShort = StringUtils.substring(attrib.getAlias(), 0, 19);
        attribNameList.add(attribAliasShort);
        columnsNames = columnsNames.concat(attribAliasShort + ", ");
    }/*  w  w w.ja v a  2  s.  c  o  m*/
    columnsNames = StringUtils.substringBeforeLast(columnsNames, ", ");

    //      report.jvm.mempool.oldgen.columns=OGenUsage.used, OGenUsage.max
    report = report.concat("report." + reportName + ".columns=" + columnsNames + "\n");

    //      report.jvm.mempool.oldgen.type=interfaceSnmp
    report = report.concat("report." + reportName + ".type=interfaceSnmp" + "\n");

    //      report.jvm.mempool.oldgen.command=--title="JVM Memory Pool: Old Gen Space" \
    report = report
            .concat("report." + reportName + ".command=--title=\"" + mBean.getName() + "\"" + " \\" + "\n");

    //       --vertical-label="COUNT Hits" \
    report = report.concat(" --vertical-label=\"COUNT\" " + "\\" + "\n");

    int i = 0;
    for (Attrib attrib : mBean.getAttrib()) {
        i++;
        String attribAliasShort = StringUtils.substring(attrib.getAlias(), 0, 19);
        //          DEF:used={rrd1}:OGenUsage.used:AVERAGE \
        //          DEF:max={rrd2}:OGenUsage.max:AVERAGE \
        report = report.concat(
                " DEF:" + attribAliasShort + "={rrd" + i + "}:" + attribAliasShort + ":AVERAGE" + " \\" + "\n");
    }

    i = 0;
    for (Attrib attrib : mBean.getAttrib()) {
        i++;
        String attribAliasShort = StringUtils.substring(attrib.getAlias(), 0, 19);
        //          LINE2:used#73d216:"Bytes Used" \
        report = report.concat(" LINE2:" + attribAliasShort + "#" + getNextColor() + ":\"" + attribAliasShort
                + "\"" + " \\" + "\n");

        //          GPRINT:used:AVERAGE:" Avg \\: %5.2lf %s " \
        report = report
                .concat(" GPRINT:" + attribAliasShort + ":AVERAGE:\" Avg \\\\: %8.2lf %s\"" + " \\" + "\n");

        //          GPRINT:used:MIN:" Min \\: %5.2lf %s " \
        report = report.concat(" GPRINT:" + attribAliasShort + ":MIN:\" Min \\\\: %8.2lf %s\"" + " \\" + "\n");

        //          GPRINT:used:MAX:" Max \\: %5.2lf %s " \         
        report = report.concat(" GPRINT:" + attribAliasShort + ":MAX:\" Max \\\\: %8.2lf %s\"" + " \\" + "\n");

    }

    colorIndex = 0;
    report = StringUtils.substringBeforeLast(report, "\" \\\n") + "\\\\n\" \\\n";

    //       LINE2:max#ff0000:"Bytes Allocated" \
    //       GPRINT:max:AVERAGE:"Avg \\: %5.2lf %s " \
    //       GPRINT:max:MIN:"Min \\: %5.2lf %s " \
    //       GPRINT:max:MAX:"Max \\: %5.2lf %s\\n"

    return report;
}

From source file:org.opennms.tools.jmxconfiggenerator.graphs.SnmpGraphConfigGenerator.java

private static String generateGraph(JmxCollection jmxCollection, Mbean mBean, Attrib attrib) {
    String report = "";
    String attibutAliasShort = StringUtils.substring(attrib.getAlias(), 0, 19);

    //String reportDomainLikeName = "report." + jmxCollection.getName() + "." + attibutAliasShort;
    String reportDomainLikeName = "report." + "jsr160." + serviceName + "." + attibutAliasShort;

    //      report.jsr160.cassandra.${attributAlias}.name=${reportDisplayName}

    //Alias passt nicht wirklich
    //      report.${rrdReportTitle}.name=${reportDisplayName}
    report = report.concat(reportDomainLikeName + ".name=" + attibutAliasShort + "\n");

    //      report.${jmx-collectionName}.columns=${attributAlias}
    report = report.concat(reportDomainLikeName + ".columns=" + attibutAliasShort + "\n");

    //      report.${jmx-collectionName}.type=interfaceSnmp
    report = report.concat(reportDomainLikeName + ".type=interfaceSnmp" + "\n");

    //      report.${jmx-collectionName}.command=--title=${"graphTitle"} \
    report = report.concat(reportDomainLikeName + ".command=--title=\"" + mBean.getName() + " "
            + attrib.getName() + "\" \\" + "\n");

    //      --vertical-label=${"graphUnitName"} \
    report = report.concat(" --vertical-label=" + "\"" + "COUNT " + attrib.getName() + "\"" + " \\" + "\n");

    //      DEF:${attributAlias}={rrd1}:${attributAlias}:AVERAGE \
    report = report//  www.j a  v  a2 s .co m
            .concat(" DEF:" + attibutAliasShort + "={rrd1}:" + attibutAliasShort + ":AVERAGE" + " \\" + "\n");

    //      LINE2:${attributAlias}#00ff00:"In " \
    report = report.concat(
            " LINE2:" + attibutAliasShort + "#73d216" + ":\"" + attibutAliasShort + "\"" + " \\" + "\n");

    //      GPRINT:${attributAlias}:AVERAGE:"Avg  \\: %8.2lf %s" \
    report = report.concat(" GPRINT:" + attibutAliasShort + ":AVERAGE:\"Avg \\\\: %8.2lf %s\"" + " \\" + "\n");

    //      GPRINT:${attributAlias}:MIN:"Min  \\: %8.2lf %s" \
    report = report.concat(" GPRINT:" + attibutAliasShort + ":MIN:\"Min \\\\: %8.2lf %s\"" + " \\" + "\n");

    //      GPRINT:${attributAlias}:MAX:"Max  \\: %8.2lf %s\\n" \
    report = report.concat(" GPRINT:" + attibutAliasShort + ":MAX:\"Max \\\\: %8.2lf %s\\\\n\"" + " \\" + "\n");

    return report;
}

From source file:org.opens.tanaguru.rules.domelement.DomElement.java

public String getDisplayableBgColor() {
    if (StringUtils.startsWith(bgColor, BACKGROUND_IMAGE_KEY)) {
        return StringUtils.substring(bgColor, bgColor.indexOf(":") + 1, bgColor.length());
    }//from w  w w  .ja  v  a2 s . com
    return bgColor;
}

From source file:org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.DlmsLogItemRequestMessage.java

public DlmsLogItemRequestMessage(final String deviceIdentification, final String organisationIdentification,
        final boolean incoming, final byte[] encodedMessage, final String decodedMessage) {
    this.deviceIdentification = deviceIdentification;
    this.organisationIdentification = organisationIdentification;
    this.incoming = incoming;
    this.valid = true;
    if (encodedMessage == null) {
        this.payloadMessageSerializedSize = 0;
        this.encodedMessage = null;
    } else {/*w  w  w  .j av a2s  .  c o  m*/
        this.payloadMessageSerializedSize = encodedMessage.length;
        this.encodedMessage = StringUtils.substring(bytesToCArray(encodedMessage), 0, MAX_MESSAGE_LENGTH);
    }
    if (decodedMessage == null) {
        this.decodedMessage = null;
    } else {
        this.decodedMessage = StringUtils.substring(decodedMessage, 0, MAX_MESSAGE_LENGTH);
    }
}