Java Code Examples for org.apache.commons.lang3.StringUtils#ordinalIndexOf()

The following examples show how to use org.apache.commons.lang3.StringUtils#ordinalIndexOf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UrlUtils.java    From cetty with Apache License 2.0 5 votes vote down vote up
public static String getHost(String url) {
    String host = url;
    int i = StringUtils.ordinalIndexOf(url, "/", 3);
    if (i > 0) {
        host = StringUtils.substring(url, 0, i);
    }
    return host;
}
 
Example 2
Source File: AllureReporter.java    From colibri-ui with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeStory(Story story, boolean givenStory) {
    uid = generateSuiteUid(story);
    String path = story.getPath();
    int secondIndex = StringUtils.ordinalIndexOf(path, "/", 2);
    String subPath = path.substring(secondIndex + 1);
    TestSuiteStartedEvent event = new TestSuiteStartedEvent(uid, story.getName());
    event.withLabels(AllureModelUtils.createTestFrameworkLabel("JBehave"));
    event.withTitle(subPath);
    allure.fire(event);
}
 
Example 3
Source File: UrlUtils.java    From zongtui-webcrawler with GNU General Public License v2.0 5 votes vote down vote up
public static String getHost(String url) {
    String host = url;
    int i = StringUtils.ordinalIndexOf(url, "/", 3);
    if (i > 0) {
        host = StringUtils.substring(url, 0, i);
    }
    return host;
}
 
Example 4
Source File: DataSourceInfo.java    From yawp with MIT License 5 votes vote down vote up
public String getDatabaseName() {
    int startIndex = StringUtils.ordinalIndexOf(url, "/", 3) + 1;
    String databaseName = url.substring(startIndex);

    int endIndex = databaseName.indexOf("?");
    if(endIndex == -1) {
        return databaseName;
    }
    return databaseName.substring(0, endIndex);
}
 
Example 5
Source File: TextPositionUtils.java    From saros with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Calculates the offset of the given text position in the given text using the given line
 * separator.
 *
 * @param text the text with which to calculate the offset
 * @param position the position for which to calculate the offset
 * @param lineSeparator the line separator used in the text
 * @return the offset of the given text position in the given text
 * @throws NullPointerException if the given text position, text, or line separator is <code>null
 *     </code>
 * @throws IllegalArgumentException if the given text position is invalid or if an empty string is
 *     passed as the line separator in combination with a text position that is not located in
 *     line 0
 * @throws IllegalStateException if the given text contains fewer lines than specified by the text
 *     position
 */
public static int calculateOffset(String text, TextPosition position, String lineSeparator) {

  Objects.requireNonNull(text, "The given document content must not be null");
  Objects.requireNonNull(position, "The given text position must not be null");
  Objects.requireNonNull(lineSeparator, "The given line separator must not be null");

  if (!position.isValid()) {
    throw new IllegalArgumentException("The given position must not be invalid");
  }

  int lineNumber = position.getLineNumber();

  if (lineNumber == 0) {
    return position.getInLineOffset();

  } else if (lineSeparator.isEmpty()) {
    throw new IllegalArgumentException(
        "No line separator was passed for a position that expects a text having multiple lines: "
            + position);
  }

  int previousLineEndOffset = StringUtils.ordinalIndexOf(text, lineSeparator, lineNumber);

  if (previousLineEndOffset == -1) {
    throw new IllegalStateException(
        "The given text contains fewer lines than specified by the text position");
  }

  int lineStartOffset = previousLineEndOffset + lineSeparator.length();

  return lineStartOffset + position.getInLineOffset();
}
 
Example 6
Source File: HttpResponseMock.java    From iaf with Apache License 2.0 5 votes vote down vote up
private void appendHeaders(HttpRequestBase request, StringBuilder response) {
	Header[] headers = request.getAllHeaders();
	for (Header header : headers) {
		String headerName = header.getName();
		String headerValue = header.getValue();
		if(headerName.equals("X-Akamai-ACS-Auth-Data")) { //Ignore timestamps in request header
			int start = StringUtils.ordinalIndexOf(headerValue, ",", 3);
			int end = headerValue.lastIndexOf(",");
			headerValue = headerValue.substring(0, start) + ", timestamp, timestamp" + headerValue.substring(end);
		}
		response.append(headerName + ": " + headerValue + lineSeparator);
	}
}
 
Example 7
Source File: UrlUtils.java    From webmagic with Apache License 2.0 5 votes vote down vote up
public static String getHost(String url) {
    String host = url;
    int i = StringUtils.ordinalIndexOf(url, "/", 3);
    if (i > 0) {
        host = StringUtils.substring(url, 0, i);
    }
    return host;
}
 
Example 8
Source File: Strings.java    From startup-os with Apache License 2.0 4 votes vote down vote up
public static int ordinalIndexOf(String string, String searchString, int n) {
  return StringUtils.ordinalIndexOf(string, searchString, n);
}
 
Example 9
Source File: DataSourceInfo.java    From yawp with MIT License 4 votes vote down vote up
public String getInitDatabaseUrl() {
    int endIndex = StringUtils.ordinalIndexOf(url, "/", 3) + 1;
    return url.substring(0, endIndex) + "template1";
}
 
Example 10
Source File: GOTOInclusionTransformation.java    From saros with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Drops the leading overlap of delete operation A with delete operation B.
 *
 * <p>The entire text section between the start of operation A and the end of operation B is
 * dropped, even if operation B starts after the start of operation A.
 *
 * <p><b>NOTE:</b> This method does not check whether such an overlap actually exists. This must
 * be determined by the caller beforehand.
 *
 * <p><b>NOTE:</b> This method does not check its input. It is expected that such checks are done
 * by the caller.
 *
 * @param delA the operation whose leading overlap to drop
 * @param delB the operation overlapping with the start of the first operation
 * @return a new deletion operation containing the content of delete operation A after its leading
 *     overlap with delete operation B was dropped
 */
private DeleteOperation dropLeadingOverlap(DeleteOperation delA, DeleteOperation delB) {
  TextPosition startA = delA.getStartPosition();

  TextPosition startB = delB.getStartPosition();
  TextPosition endB = delB.getEndPosition();

  int inLineOffsetEndB = endB.getInLineOffset();

  String text = delA.getText();

  int lineDeltaA = delA.getLineDelta();
  int offsetDeltaA = delA.getOffsetDelta();

  int droppedLines = endB.getLineNumber() - startA.getLineNumber();

  String adjustedText;
  int adjustedLineDelta;
  int adjustedOffsetDelta;

  if (droppedLines == 0) {
    int droppedCharacters = inLineOffsetEndB - startA.getInLineOffset();

    adjustedText = text.substring(droppedCharacters);

    if (lineDeltaA == 0) {
      adjustedOffsetDelta = offsetDeltaA - droppedCharacters;
    } else {
      adjustedOffsetDelta = offsetDeltaA;
    }

    adjustedLineDelta = lineDeltaA;

  } else {
    int splitLineOffset =
        StringUtils.ordinalIndexOf(text, NORMALIZED_LINE_SEPARATOR, droppedLines);

    if (splitLineOffset == -1) {
      throw new IllegalStateException(
          "Could not find line separator " + droppedLines + " in text");
    }

    int newStartOffset = splitLineOffset + NORMALIZED_LINE_SEPARATOR.length() + inLineOffsetEndB;

    adjustedText = text.substring(newStartOffset);

    adjustedLineDelta = lineDeltaA - droppedLines;

    if (adjustedLineDelta == 0) {
      adjustedOffsetDelta = offsetDeltaA - inLineOffsetEndB;
    } else {
      adjustedOffsetDelta = offsetDeltaA;
    }
  }

  return new DeleteOperation(startB, adjustedLineDelta, adjustedOffsetDelta, adjustedText);
}
 
Example 11
Source File: StringFunctions.java    From incubator-pinot with Apache License 2.0 2 votes vote down vote up
/**
 * @see StringUtils#ordinalIndexOf(CharSequence, CharSequence, int)
 * Return the Nth occurence of a substring within the String
 * @param input
 * @param find substring to find
 * @param instance Integer denoting the instance no.
 * @return start index of the Nth instance of subtring in main string
 */
@ScalarFunction
static Integer strpos(String input, String find, Integer instance) {
  return StringUtils.ordinalIndexOf(input, find, instance);
}
 
Example 12
Source File: GOTOInclusionTransformation.java    From saros with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Drops the trailing overlap of delete operation A with delete operation B.
 *
 * <p>The entire text section between the start of operation B and the end of operation A is
 * dropped, even if operation B ends before the end of operation A.
 *
 * <p><b>NOTE:</b> This method does not check whether such an overlap actually exists. This must
 * be determined by the caller beforehand.
 *
 * <p><b>NOTE:</b> This method does not check its input. It is expected that such checks are done
 * by the caller.
 *
 * @param delA the operation whose trailing overlap to drop
 * @param delB the operation overlapping with the end of the first operation
 * @return a new deletion operation containing the content of delete operation A after its
 *     trailing overlap with delete operation B was dropped
 */
private DeleteOperation dropTrailingOverlap(DeleteOperation delA, DeleteOperation delB) {
  TextPosition startA = delA.getStartPosition();
  TextPosition endA = delA.getEndPosition();

  TextPosition startB = delB.getStartPosition();

  int inLineOffsetStartB = startB.getInLineOffset();

  String text = delA.getText();

  int lineDeltaA = delA.getLineDelta();

  int droppedLines = endA.getLineNumber() - startB.getLineNumber();

  String adjustedText;
  int adjustedLineDelta;
  int adjustedOffsetDelta;

  if (droppedLines == 0) {
    int droppedCharacters = endA.getInLineOffset() - inLineOffsetStartB;

    adjustedText = text.substring(0, text.length() - droppedCharacters);

    adjustedOffsetDelta = delA.getOffsetDelta() - droppedCharacters;

    adjustedLineDelta = lineDeltaA;

  } else {
    adjustedLineDelta = lineDeltaA - droppedLines;

    if (adjustedLineDelta == 0) {

      adjustedOffsetDelta = inLineOffsetStartB - startA.getInLineOffset();

      adjustedText = text.substring(0, adjustedOffsetDelta);

    } else {
      int splitLineOffset =
          StringUtils.ordinalIndexOf(text, NORMALIZED_LINE_SEPARATOR, adjustedLineDelta);

      if (splitLineOffset == -1) {
        throw new IllegalStateException(
            "Could not find line separator " + adjustedLineDelta + " in text");
      }

      int newEndOffset =
          splitLineOffset + NORMALIZED_LINE_SEPARATOR.length() + inLineOffsetStartB;

      adjustedText = text.substring(0, newEndOffset);

      adjustedOffsetDelta = inLineOffsetStartB;
    }
  }

  return new DeleteOperation(startA, adjustedLineDelta, adjustedOffsetDelta, adjustedText);
}