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:com.adobe.cq.wcm.core.components.internal.models.v2.ImageImpl.java

protected void buildAreas() {
    areas = new ArrayList<>();
    String mapProperty = properties.get(Image.PN_MAP, String.class);
    if (StringUtils.isNotEmpty(mapProperty)) {
        // Parse the image map areas as defined at {@code Image.PN_MAP}
        String[] mapAreas = StringUtils.split(mapProperty, "][");
        for (String area : mapAreas) {
            int coordinatesEndIndex = area.indexOf(")");
            if (coordinatesEndIndex < 0) {
                break;
            }/* w  w w .j ava 2s  .c  o  m*/
            String shapeAndCoords = StringUtils.substring(area, 0, coordinatesEndIndex + 1);
            String shape = StringUtils.substringBefore(shapeAndCoords, "(");
            String coordinates = StringUtils.substringBetween(shapeAndCoords, "(", ")");
            String remaining = StringUtils.substring(area, coordinatesEndIndex + 1);
            String[] remainingTokens = StringUtils.split(remaining, "|");
            if (StringUtils.isBlank(shape) || StringUtils.isBlank(coordinates)) {
                break;
            }
            if (remainingTokens.length > 0) {
                String href = StringUtils.removeAll(remainingTokens[0], "\"");
                if (StringUtils.isBlank(href)) {
                    break;
                }
                String target = remainingTokens.length > 1 ? StringUtils.removeAll(remainingTokens[1], "\"")
                        : "";
                String alt = remainingTokens.length > 2 ? StringUtils.removeAll(remainingTokens[2], "\"") : "";
                String relativeCoordinates = remainingTokens.length > 3 ? remainingTokens[3] : "";
                relativeCoordinates = StringUtils.substringBetween(relativeCoordinates, "(", ")");
                if (href.startsWith("/")) {
                    href = Utils.getURL(request, pageManager, href);
                }
                areas.add(new ImageAreaImpl(shape, coordinates, relativeCoordinates, href, target, alt));
            }
        }
    }
}

From source file:ch.elexis.core.ui.dialogs.ZusatzAdresseEingabeDialog.java

private void setFieldValues() {
    zusatzAdresseDTO.setStreet1(str1.getText());
    zusatzAdresseDTO.setStreet2(str2.getText());
    zusatzAdresseDTO.setZip(StringUtils.substring(plz.getText(), 0, 6));
    zusatzAdresseDTO.setPlace(ort.getText());
    zusatzAdresseDTO.setCountry(StringUtils.substring(land.getText(), 0, 3));

    StructuredSelection selection = (StructuredSelection) comboAddressType.getSelection();
    if (selection != null && !selection.isEmpty()) {
        AddressType addressType = (AddressType) selection.getFirstElement();
        zusatzAdresseDTO.setAddressType(addressType);
    }/*from w w  w  . jav  a 2s. com*/
    zusatzAdresseDTO.setPostalAddress(postanschrift.getText().replaceAll("\\r\\n", StringTool.lf));
}

From source file:com.joyent.manta.client.MantaClientRangeIT.java

@Test
public final void canGetAllRanges() throws IOException {
    final String name = UUID.randomUUID().toString();
    final String path = testPathPrefix + name;

    String testData = TEST_DATA;/*from w  ww. j a v  a2  s .  c o  m*/
    for (int i = 0; i < 501; i++) {
        testData += TEST_DATA;
    }
    mantaClient.put(path, testData);
    int fifth = testData.length() / 5;

    for (int start = 0; start <= testData.length(); start += fifth) {
        for (int end = testData.length(); end >= start; end -= fifth) {
            String expected = StringUtils.substring(testData, start, end + 1);

            final MantaHttpHeaders headers = new MantaHttpHeaders();
            // Range is inclusive, inclusive
            String rangeHeader = "bytes=" + start + "-" + end;
            headers.setRange(rangeHeader);

            try (final InputStream min = mantaClient.getAsInputStream(path, headers)) {
                String actual = IOUtils.toString(min, Charset.defaultCharset());
                System.out.println("Range: " + rangeHeader);
                Assert.assertEquals(actual, expected,
                        "Didn't receive correct range value for range: " + rangeHeader);
            }
        }
    }
}

From source file:com.github.dactiv.fear.commons.AuthCoder.java

/**
 * //  ww w. j  av  a2s  . c  om
 *
 * @param value           ?
 * @param key             
 * @param expiry          
 * @param randomKeyLength ? ? 0-32????
 *                        ???iv??
 *                        ? = 16  randomKeyLength  0 ??
 * @param encoding        ?
 * @return ?
 * @throws UnsupportedEncodingException
 */
public static String encode(String value, String key, int expiry, int randomKeyLength, String encoding)
        throws UnsupportedEncodingException {

    key = DigestUtils.md5Hex(key.getBytes());
    String keya = DigestUtils.md5Hex(StringUtils.substring(key, 0, 16).getBytes());
    String keyb = DigestUtils.md5Hex(StringUtils.substring(key, 16, 16 + 16).getBytes());
    String keyc = randomKeyLength > 0 ? StringUtils.substring(
            DigestUtils.md5Hex(String.valueOf(System.currentTimeMillis() / 1000).getBytes()), -randomKeyLength)
            : "";

    String cryptKey = keya + DigestUtils.md5Hex((keya + keyc).getBytes());
    int cryptKeyLen = cryptKey.length();

    value = String.format("%010d", expiry > 0 ? expiry + System.currentTimeMillis() / 1000 : 0)
            + StringUtils.substring(DigestUtils.md5Hex((value + keyb).getBytes()), 0, 16) + value;
    int stringLen = value.length();

    List<Integer> rndKey = new ArrayList<>();
    StringBuilder result = new StringBuilder();
    Integer[] box = new Integer[256];
    for (int i = 0; i < box.length; i++) {
        box[i] = i;
    }

    for (int i = 0; i <= 255; i++) {
        rndKey.add((int) cryptKey.charAt(i % cryptKeyLen));
    }

    for (int j = 0, i = 0; i < 256; i++) {
        j = (j + box[i] + rndKey.get(i)) % 256;
        int tmp = box[i];
        box[i] = box[j];
        box[j] = tmp;
    }

    for (int k = 0, j = 0, i = 0; i < stringLen; i++) {
        k = (k + 1) % 256;
        j = (j + box[k]) % 256;
        int tmp = box[k];
        box[k] = box[j];
        box[j] = tmp;
        int a = (int) value.charAt(i);
        int b = box[(box[k] + box[j]) % 256];
        char r = (char) (a ^ b);
        result.append(r);
    }

    return keyc + Base64.encodeBase64String(result.toString().getBytes(encoding)).replaceAll("=", "");
}

From source file:com.cognifide.aet.vs.mongodb.MongoDBClient.java

/**
 * @param companyName company name/*from  ww  w. j  a va2  s.  co m*/
 * @param projectName project name
 * @return dataBase name with applied db naming rules, based on project and company names
 */
public static String getDbName(String companyName, String projectName) {
    String result = companyName + DB_NAME_SEPARATOR + projectName;
    return StringUtils.substring(result, 0, MAX_DB_NAME_LENGTH);
}

From source file:com.labs64.mojo.swid.GenerateMojo.java

private ArtifactVersion getArtifactVersion() {
    if (ArtifactUtils.isSnapshot(product_version)) {
        product_version = StringUtils.substring(product_version, 0,
                product_version.length() - Artifact.SNAPSHOT_VERSION.length() - 1);
    }//from w  ww. j  a v a 2s.c  o  m
    return new DefaultArtifactVersion(product_version);
}

From source file:com.brianscottrussell.gameoflife.GameGrid.java

/**
 * given the grid input String, parses the row count if this is a valid input String
 *
 * Sample of expected format of input String where rowCount = 4:
 *
4 8//from  ww  w  .  ja  va2s .  c  o  m
......../n
....*.../n
...**.../n
........
 *
 * @param input String
 * @return int as the row count
 * @throws InvalidGameGridInputException
 */
private int parseRowCountFromInputString(String input) throws InvalidGameGridInputException {
    try {
        // get the character(s), before the 1st space as the rowCount
        return Integer.valueOf(StringUtils.substring(input, 0, StringUtils.indexOf(input, ' ')));
    } catch (NumberFormatException e) {
        throw new InvalidGameGridInputException(e.getClass().getSimpleName() + ": " + e.getMessage());
    }
}

From source file:cc.recommenders.names.VmMethodName.java

@Override
public ITypeName getReturnType() {
    String returnType = StringUtils.substringAfterLast(identifier, ")");
    // strip off throws type from method return
    returnType = StringUtils.substringBefore(returnType, "|");
    if (!returnType.endsWith(";")) {

        // be sure that if it does not end with a ';' is MUST be a primitive
        // or an array of primitives:
        final ITypeName res = VmTypeName.get(returnType);
        ensureIsTrue(res.isPrimitiveType() || res.isArrayType() && res.getArrayBaseType().isPrimitiveType());
        return res;
    } else {/*w  w  w  .ja va2 s .  c o  m*/
        returnType = StringUtils.substring(returnType, 0, -1);
        return VmTypeName.get(returnType);
    }
}

From source file:cc.recommenders.names.CoReMethodName.java

@Override
public ICoReTypeName getReturnType() {
    String returnType = StringUtils.substringAfterLast(identifier, ")");
    // strip off throws type from method return
    returnType = StringUtils.substringBefore(returnType, "|");
    if (!returnType.endsWith(";")) {

        // be sure that if it does not end with a ';' is MUST be a primitive
        // or an array of primitives:
        final ICoReTypeName res = CoReTypeName.get(returnType);
        ensureIsTrue(res.isPrimitiveType() || res.isArrayType() && res.getArrayBaseType().isPrimitiveType());
        return res;
    } else {/*  w  w  w.j av  a  2s . c  o  m*/
        returnType = StringUtils.substring(returnType, 0, -1);
        return CoReTypeName.get(returnType);
    }
}

From source file:com.brianscottrussell.gameoflife.GameGrid.java

/**
 * given the grid input String, parses the column count if this is a valid input String
 *
 * Sample of expected format of input String where columnCount = 8:
 *
4 8//from   w w  w  . j  a v a 2s . c  o  m
......../n
....*.../n
...**.../n
........
 *
 * @param input String
 * @return int as the column count
 * @throws InvalidGameGridInputException
 */
private int parseColumnCountFromInputString(String input) throws InvalidGameGridInputException {
    try {
        // get the character(s), before the 1st carriage return as the rowCount
        return Integer.valueOf(StringUtils.substring(input, StringUtils.indexOf(input, ' ') + 1,
                StringUtils.indexOf(input, GameOfLife.LF)));
    } catch (NumberFormatException e) {
        throw new InvalidGameGridInputException(e.getClass().getSimpleName() + ": " + e.getMessage());
    }
}