Java Code Examples for org.apache.commons.lang.StringUtils#substringsBetween()

The following examples show how to use org.apache.commons.lang.StringUtils#substringsBetween() . These examples are extracted from open source projects. 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
String getFormattedString(String string, @Nullable LeaderType type) {
	String toReturn = string;
	if (string.startsWith("item:")) {
		return string;
	}
	String[] variables = StringUtils.substringsBetween(string, "{", "}");
	if (variables == null) {
		return string;
	}
	for (String var: variables) {
		String value = getVariable(var, type);
		toReturn = toReturn.replaceAll("\\{" + var + "}", value);
	}
	return toReturn;
}
 
Example 2
/**
 * Prepares the message text that will replace the property value checking for any expression placeholders
 *
 * <p>
 * The message text may contain placeholders (using brace delimiters) for expression placement. It is
 * expected when these placeholders are given the property value contains the expressions (using the
 * expression placeholders) that will be inserted into the message text
 * </p>
 *
 * @param messageText - raw text of the message
 * @param propertyValue - current value for the property
 * @return String the message text with expressions inserted (if any expressions were found)
 */
protected String getMergedMessageText(String messageText, String propertyValue) {
    String mergedText = messageText;

    String[] expressions = StringUtils.substringsBetween(propertyValue, UifConstants.EL_PLACEHOLDER_PREFIX,
            UifConstants.EL_PLACEHOLDER_SUFFIX);
    if ((expressions != null) && expressions.length > 0) {
        // add expression placeholders back on
        String[] messageParameters = new String[expressions.length];
        for (int i = 0; i < expressions.length; i++) {
            String expression = expressions[i];

            expression = UifConstants.EL_PLACEHOLDER_PREFIX + expression + UifConstants.EL_PLACEHOLDER_SUFFIX;
            messageParameters[i] = expression;
        }

        // escape single quotes for message format process
        messageText = messageText.replace("'", "''");
        try {
            mergedText = MessageFormat.format(messageText, messageParameters);
        } catch (IllegalArgumentException e) {
            throw new RiceRuntimeException(
                    "Unable to merge expressions with message text. Expression count is: " + expressions.length, e);
        }
    }

    return mergedText;
}
 
Example 3
public static void load()
 {
//PacketSamurai.getUserInterface().log("Loading Client strings... Please wait.");
   Util.drawTitle("Client Strings");
   File stringsFolder = new File("./data/client_strings");
   if (!stringsFolder.exists()) {
     stringsFolder.mkdir();
   }
   try
   {
     File[] files = stringsFolder.listFiles();
     File[] arrayOfFile1;
     int j = (arrayOfFile1 = files).length;
     for (int i = 0; i < j; i++)
     {
       File sFile = arrayOfFile1[i];
       File xml = new File(sFile.getPath());
       
       String stringFile = FileUtils.readFileToString(xml);
       String[] strings = StringUtils.substringsBetween(stringFile, "<string>", "</string>");
       if (strings != null)
       {
         String[] arrayOfString1;
         int m = (arrayOfString1 = strings).length;
         for (int k = 0; k < m; k++)
         {
           String string = arrayOfString1[k];
           int stringId = Integer.parseInt(StringUtils.substringBetween(string, "<id>", "</id>"));
           String stringBody = StringUtils.substringBetween(string, "<body>", "</body>");
           stringsById.put(Integer.valueOf(stringId), stringBody);
         }
       }
     }
     PacketSamurai.getUserInterface().log("Strings [Client] - Loaded " + stringsById.size() + " strings from "+files.length+" Files");
   }
   catch (IOException e)
   {
     PacketSamurai.getUserInterface().log("ERROR: Failed to load client_strings.xsd: " + e.toString());
     e.printStackTrace();
   }
 }
 
Example 4
Source Project: symphonyx   File: UserQueryService.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Gets user names from the specified text.
 *
 * <p>
 * A user name is between &#64; and a punctuation, a blank or a line break (\n). For example, the specified text is
 * <pre>&#64;88250 It is a nice day. &#64;Vanessa, we are on the way.</pre> There are two user names in the text,
 * 88250 and Vanessa.
 * </p>
 *
 * @param text the specified text
 * @return user names, returns an empty set if not found
 * @throws ServiceException service exception
 */
public Set<String> getUserNames(final String text) throws ServiceException {
    final Set<String> ret = new HashSet<String>();

    int idx = text.indexOf('@');

    if (-1 == idx) {
        return ret;
    }

    String copy = text.trim();
    copy = copy.replaceAll("\\n", " ");
    copy = copy.replaceAll("(?=\\pP)[^@]", " ");
    String[] uNames = StringUtils.substringsBetween(copy, "@", " ");
    String tail = StringUtils.substringAfterLast(copy, "@");

    if (tail.contains(" ")) {
        tail = null;
    }

    if (null != tail) {
        if (null == uNames) {
            uNames = new String[1];
            uNames[0] = tail;
        } else {
            uNames = Arrays.copyOf(uNames, uNames.length + 1);
            uNames[uNames.length - 1] = tail;
        }
    }

    if (null == uNames) {
        return ret;
    }

    for (int i = 0; i < uNames.length; i++) {
        final String maybeUserName = uNames[i];

        if (!UserRegisterValidation.invalidUserName(maybeUserName)) { // A string match the user name pattern
            if (null != getUserByName(maybeUserName)) { // Found a user
                ret.add(maybeUserName);

                copy = copy.replaceFirst("@" + maybeUserName, "");
                idx = copy.indexOf('@');
                if (-1 == idx) {
                    return ret;
                }
            }
        }
    }

    return ret;
}
 
Example 5
/**
 * Checks a string property value for a message placeholder and if found the message is retrieved and updated
 * in the property value
 *
 * @param propertyValue string value to process for message placeholders
 * @param nestedBeanStack stack of bean definitions that contain the property, used to determine the namespace
 * and component for the message retrieval
 * @return String new value for the property (possibly modified from an external message)
 */
protected String processMessagePlaceholders(String propertyValue, Stack<BeanDefinitionHolder> nestedBeanStack) {
    String trimmedPropertyValue = StringUtils.stripStart(propertyValue, " ");
    if (StringUtils.isBlank(trimmedPropertyValue)) {
        return propertyValue;
    }

    String newPropertyValue = propertyValue;

    // first check for a replacement message key
    if (trimmedPropertyValue.startsWith(KRADConstants.MESSAGE_KEY_PLACEHOLDER_PREFIX) && StringUtils.contains(
            trimmedPropertyValue, KRADConstants.MESSAGE_KEY_PLACEHOLDER_SUFFIX)) {
        String messageKeyStr = StringUtils.substringBetween(trimmedPropertyValue,
                KRADConstants.MESSAGE_KEY_PLACEHOLDER_PREFIX, KRADConstants.MESSAGE_KEY_PLACEHOLDER_SUFFIX);

        // get any default specified value (given after the message key)
        String messageKeyWithPlaceholder = KRADConstants.MESSAGE_KEY_PLACEHOLDER_PREFIX + messageKeyStr +
                KRADConstants.MESSAGE_KEY_PLACEHOLDER_SUFFIX;

        String defaultPropertyValue = StringUtils.substringAfter(trimmedPropertyValue, messageKeyWithPlaceholder);

        // set the new property value to the message text (if found), or the default value if a message was not found
        // note the message text could be an empty string, in which case it will override the default
        String messageText = getMessageTextForKey(messageKeyStr, nestedBeanStack);
        if (messageText != null) {
            // if default value set then we need to merge any expressions
            if (StringUtils.isNotBlank(defaultPropertyValue)) {
                newPropertyValue = getMergedMessageText(messageText, defaultPropertyValue);
            } else {
                newPropertyValue = messageText;
            }
        } else {
            newPropertyValue = defaultPropertyValue;
        }
    }
    // now check for message keys within an expression
    else if (StringUtils.contains(trimmedPropertyValue, KRADConstants.EXPRESSION_MESSAGE_PLACEHOLDER_PREFIX)) {
        String[] expressionMessageKeys = StringUtils.substringsBetween(newPropertyValue,
                KRADConstants.EXPRESSION_MESSAGE_PLACEHOLDER_PREFIX,
                KRADConstants.EXPRESSION_MESSAGE_PLACEHOLDER_SUFFIX);

        for (String expressionMessageKey : expressionMessageKeys) {
            String expressionMessageText = getMessageTextForKey(expressionMessageKey, nestedBeanStack);
            newPropertyValue = StringUtils.replace(newPropertyValue,
                    KRADConstants.EXPRESSION_MESSAGE_PLACEHOLDER_PREFIX + expressionMessageKey +
                            KRADConstants.EXPRESSION_MESSAGE_PLACEHOLDER_SUFFIX, expressionMessageText);
        }
    }

    return newPropertyValue;
}