/**
   * This method is used to help in converting a complex object's data into a JSON string
   *
   * @param data The submitted data as a string
   * @return The JSON equivalent of the submitted string
   */
  private String applyRegEx(String data) {
    // regular expresion for getting property names
    String elementPart = "([\\w]*:)";

    // regular expression for getting the values
    String elValuePart = "(:)([\\w]*)";

    // apply regular expressions to patterns
    Pattern elPattern = Pattern.compile(elementPart);
    Pattern valPattern = Pattern.compile(elValuePart);

    // get matchers to use patterns to match the data/String
    Matcher elMatcher = elPattern.matcher(data);
    Matcher vMatcher = valPattern.matcher(data);
    while (elMatcher.find()) // handles value part
    {
      String element = elMatcher.group();
      data = StringUtils.replaceOnce(data, element, "\"" + element.replace(":", "\":"));
    }
    while (vMatcher.find()) // handles the value part
    {
      String value = vMatcher.group();
      value = StringUtils.remove(value, ":");
      if (!StringUtils.isNumeric(value)
          && // not a number
          !StringUtils.equals(value, "true")
          && // not a boolean value
          !StringUtils.equals(value, "false")) // not a boolean value
      {
        if (StringUtils.isEmpty(value)) data = data.replace(":,", ":null,");
        else data = StringUtils.replaceOnce(data, value, "\"" + value + "\"");
      }
    }
    return data;
  }
Example #2
0
 /**
  * Checks whether the given String is a parsable number.
  *
  * <p>Parsable numbers include those Strings understood by {@link Integer#parseInt(String)},
  * {@link Long#parseLong(String)}, {@link Float#parseFloat(String)} or {@link
  * Double#parseDouble(String)}. This method can be used instead of catching {@link
  * java.text.ParseException} when calling one of those methods.
  *
  * <p>Hexadecimal and scientific notations are <strong>not</strong> considered parsable. See
  * {@link #isNumber(String)} on those cases.
  *
  * <p>{@code Null} and empty String will return <code>false</code>.
  *
  * @param str the String to check.
  * @return {@code true} if the string is a parsable number.
  * @since 3.4
  */
 public static boolean isParsable(final String str) {
   if (StringUtils.endsWith(str, ".")) {
     return false;
   }
   if (StringUtils.startsWith(str, "-")) {
     return isDigits(StringUtils.replaceOnce(str.substring(1), ".", StringUtils.EMPTY));
   } else {
     return isDigits(StringUtils.replaceOnce(str, ".", StringUtils.EMPTY));
   }
 }
  public static Optional<ItemStack> convertToDustIfPossible(final ItemStack stack) {

    String oreName = OreDictionaryHelper.getOreName(stack);

    if (oreName == null) return Optional.of(stack);

    if (oreName.startsWith("ingot")) oreName = StringUtils.replaceOnce(oreName, "ingot", "dust");
    else if (oreName.startsWith("plank"))
      oreName = StringUtils.replaceOnce(oreName, "plank", "dust");
    else return Optional.of(stack);

    return getItemStack(oreName);
  }
Example #4
0
  private void flushSection() throws SAXException {
    String section = samplerBuffer.toString();

    if (classNames != null && !classNames.isEmpty()) {
      String search = "testname=\"" + testName + "\"";
      String newName = "testname=\"" + counter + " /" + StringUtils.join(classNames, ",") + "\"";
      section = StringUtils.replaceOnce(section, search, newName);
    }

    testName = null;
    samplerBuffer = null;
    classNames = null;
    emit(section);
  }
  /**
   * Formats an elapsed time into a plurialization correct string.
   *
   * <p>This method formats durations using the days and lower fields of the format pattern. Months
   * and larger are not used.
   *
   * @param durationMillis the elapsed time to report in milliseconds
   * @param suppressLeadingZeroElements suppresses leading 0 elements
   * @param suppressTrailingZeroElements suppresses trailing 0 elements
   * @return the formatted text in days/hours/minutes/seconds, not null
   * @throws java.lang.IllegalArgumentException if durationMillis is negative
   */
  public static String formatDurationWords(
      final long durationMillis,
      final boolean suppressLeadingZeroElements,
      final boolean suppressTrailingZeroElements) {

    // This method is generally replacable by the format method, but
    // there are a series of tweaks and special cases that require
    // trickery to replicate.
    String duration = formatDuration(durationMillis, "d' days 'H' hours 'm' minutes 's' seconds'");
    if (suppressLeadingZeroElements) {
      // this is a temporary marker on the front. Like ^ in regexp.
      duration = " " + duration;
      String tmp = StringUtils.replaceOnce(duration, " 0 days", "");
      if (tmp.length() != duration.length()) {
        duration = tmp;
        tmp = StringUtils.replaceOnce(duration, " 0 hours", "");
        if (tmp.length() != duration.length()) {
          duration = tmp;
          tmp = StringUtils.replaceOnce(duration, " 0 minutes", "");
          duration = tmp;
          if (tmp.length() != duration.length()) {
            duration = StringUtils.replaceOnce(tmp, " 0 seconds", "");
          }
        }
      }
      if (duration.length() != 0) {
        // strip the space off again
        duration = duration.substring(1);
      }
    }
    if (suppressTrailingZeroElements) {
      String tmp = StringUtils.replaceOnce(duration, " 0 seconds", "");
      if (tmp.length() != duration.length()) {
        duration = tmp;
        tmp = StringUtils.replaceOnce(duration, " 0 minutes", "");
        if (tmp.length() != duration.length()) {
          duration = tmp;
          tmp = StringUtils.replaceOnce(duration, " 0 hours", "");
          if (tmp.length() != duration.length()) {
            duration = StringUtils.replaceOnce(tmp, " 0 days", "");
          }
        }
      }
    }
    // handle plurals
    duration = " " + duration;
    duration = StringUtils.replaceOnce(duration, " 1 seconds", " 1 second");
    duration = StringUtils.replaceOnce(duration, " 1 minutes", " 1 minute");
    duration = StringUtils.replaceOnce(duration, " 1 hours", " 1 hour");
    duration = StringUtils.replaceOnce(duration, " 1 days", " 1 day");
    return duration.trim();
  }