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

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

Introduction

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

Prototype

public static String replaceEachRepeatedly(final String text, final String[] searchList,
        final String[] replacementList) 

Source Link

Document

Replaces all occurrences of Strings within another String.

Usage

From source file:com.andrada.sitracker.reader.SamlibAuthorPageReader.java

private String sanitizeHTML(String value) {
    String[] tokensToReplace = new String[] { "<br />", "<BR />", "&bull;", "&lsaquo;", "&rsaquo;", "&trade;",
            "&frasl;", "&lt;", "&LT;", "&gt;", "&GT;", "&copy;", "&COPY;", "&nbsp;", "&NBSP;", "&quot;", "\r",
            "\n", "\f" };
    String[] replacements = new String[] { "<br>", "<br>", " * ", "<", ">", "(tm)", "/", "<", "<", ">", ">",
            "(c)", "(c)", " ", " ", "\"", "", "", "" };

    value = StringUtils.replaceEachRepeatedly(value, tokensToReplace, replacements);
    return value;
}

From source file:net.lmxm.ute.configuration.ConfigurationInterpolator.java

/**
 * Interpolate properties./*from  w  w  w . j  av  a  2  s .  c o m*/
 * 
 * @param string the string
 * @param propertyNames the property names
 * @param propertyValues the property values
 * @return the string
 */
private static String interpolateProperties(final String string, final String[] propertyNames,
        final String[] propertyValues) {
    final String newString = StringUtils.replaceEachRepeatedly(string, propertyNames, propertyValues);

    validateDoesNotContainProperties(newString);

    return newString;
}

From source file:org.fabrician.enabler.util.SpecialDirective.java

/**
 * Resolve a string value including the special directive runtime context variables substitution tokens.
 * /*from  ww w. ja va2s.  c  om*/
 * @param enabler
 *            the enabler use
 * @param str
 *            the string value
 * @return a fully resolved string value
 * @throws Exception
 */
public static String resolveStringValue(AbstractContainer enabler, String str) throws Exception {
    if (StringUtils.trimToEmpty(str).isEmpty()) {
        return str;
    }
    String resolvedStr = enabler.resolveVariables(str);
    List<String> substitution_tokens = extractSubstitutionTokens(resolvedStr);
    if (substitution_tokens.isEmpty()) {
        return resolvedStr;
    }
    Map<String, String> resolutionMap = resolveSubstitutionTokens(enabler, substitution_tokens);
    resolvedStr = StringUtils.replaceEachRepeatedly(resolvedStr, substitution_tokens.toArray(new String[] {}),
            getReplacementValues(substitution_tokens, resolutionMap));
    return resolvedStr;
}

From source file:org.fabrician.enabler.util.SpecialDirective.java

private static Map<String, String> resolveSubstitutionTokens(AbstractContainer enabler,
        List<String> substitutionTokens) {
    Map<String, String> resolvedMap = new HashMap<String, String>();
    for (String subToken : substitutionTokens) {
        try {/*from  w  w  w  . j  av  a2s.co  m*/
            String contextVar = subToken.substring(2, subToken.length() - 1); // assume ${XYZ} where XYZ == the contextVar
            String contextVarValue = enabler.getStringVariableValue(contextVar);
            String resolvedVarValue = enabler.resolveVariables(contextVarValue);
            List<String> specialSubstitutions2 = extractSubstitutionTokens(resolvedVarValue);
            if (specialSubstitutions2.isEmpty()) {
                resolvedMap.put(subToken, resolvedVarValue);
            } else {
                Map<String, String> resolvedMap2 = resolveSubstitutionTokens(enabler, specialSubstitutions2);
                String resolvedVarValue2 = StringUtils.replaceEachRepeatedly(resolvedVarValue,
                        specialSubstitutions2.toArray(new String[] {}),
                        getReplacementValues(specialSubstitutions2, resolvedMap2));
                resolvedMap.put(subToken, resolvedVarValue2);
            }
        } catch (Exception ex) {

        }
    }
    return Collections.unmodifiableMap(resolvedMap);
}

From source file:org.hobbit.storage.queries.SparqlQueries.java

/**
 * Replaces the given placeholders in the given query with the given
 * replacements. If a replacement is <code>null</code>, it is replaced by a
 * variable./*  w w w  . java2  s.co  m*/
 *
 * @param query
 *            the query containing placeholders
 * @param placeholders
 *            the placeholders that should be replaced
 * @param replacements
 *            the replacements that should be used to replace the placeholders.
 * @return the newly created query or <code>null</code> if the given query was
 *         <code>null</code>.
 */
private static final String replacePlaceholders(String query, String[] placeholders, String[] replacements) {
    if (query == null) {
        return null;
    }
    if (placeholders.length != replacements.length) {
        throw new IllegalArgumentException("The length of the placeholders != length of replacements.");
    }
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < replacements.length; ++i) {
        if (replacements[i] == null) {
            // create a variable name
            builder.append("?v");
            builder.append(i);
        } else if (replacements[i].charAt(0) == '"') {
            // create literal
            builder.append(replacements[i]);
        } else {
            // create <URI>
            builder.append('<');
            builder.append(replacements[i]);
            builder.append('>');
        }
        replacements[i] = builder.toString();
        builder.delete(0, builder.length());
    }
    return StringUtils.replaceEachRepeatedly(query, placeholders, replacements);
}

From source file:org.pircbotx.Benchmark.java

public static void main(String[] args) throws Exception {
    //Change logging to no-op
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(context);//from   www .  j ava2  s.c  o m
    context.reset();
    configurator.doConfigure(Benchmark.class.getResource("/logback-nop.xml"));
    StatusPrinter.printInCaseOfErrorsOrWarnings(context);

    if (args.length != 1) {
        System.err.println("Must specify thread count:");
        System.err.println(" -1: GenericListenerManager");
        System.err.println(" 0: ThreadedListenerManager (default threads)");
        System.err.println(" 1+: ThreadedListenerManager (specified threads)");
        return;
    }
    int threadCount = Integer.parseInt(args[0]);

    //Init
    String[][] responseTemplateGroups = new String[13][];
    responseTemplateGroups[0] = new String[] {
            ":${thisNick}!~jmeter@bots.jmeter PRIVMSG ${channel} ?jmeter ${thisNick}" };
    responseTemplateGroups[1] = new String[] {
            ":${thisNick}!~jmeter@bots.jmeter PRIVMSG ${targetNick} ?jmeter ${thisNick}" };
    responseTemplateGroups[2] = new String[] {
            ":${thisNick}!~jmeter@bots.jmeter PRIVMSG ${channel} :${thisNick}" };
    responseTemplateGroups[3] = new String[] {
            ":${thisNick}!~jmeter@bots.jmeter PRIVMSG ${channel} :\u0001ACTION ${thisNick}\u0001" };
    responseTemplateGroups[4] = new String[] {
            ":${thisNick}!~jmeter@bots.jmeter NOTICE ${channel} :${thisNick}" };
    responseTemplateGroups[5] = new String[] {
            ":${thisNick}!~jmeter@bots.jmeter PRIVMSG ${targetNick} :${thisNick}" };
    responseTemplateGroups[6] = new String[] {
            ":${thisNick}!~jmeter@bots.jmeter PRIVMSG ${targetNick} :\u0001ACTION ${thisNick}\u0001" };
    responseTemplateGroups[7] = new String[] {
            ":${thisNick}!~jmeter@bots.jmeter MODE ${channel} +o ${thisNick}",
            ":${thisNick}!~jmeter@bots.jmeter MODE ${channel} -o ${thisNick}" };
    responseTemplateGroups[8] = new String[] {
            ":${thisNick}!~jmeter@bots.jmeter MODE ${channel} +v ${thisNick}",
            ":${thisNick}!~jmeter@bots.jmeter MODE ${channel} -v ${thisNick}" };
    responseTemplateGroups[9] = new String[] {
            ":${thisNick}!~jmeter@bots.jmeter KICK ${channel} ${targetNick}: ${thisNick}",
            ":${thisNick}!~jmeter@bots.jmeter JOIN :${channel}" };
    responseTemplateGroups[10] = new String[] {
            ":${thisNick}!~jmeter@bots.jmeter MODE ${channel} +b ${thisNick}!*@*",
            ":${thisNick}!~jmeter@bots.jmeter MODE ${channel} -b ${thisNick}!*@*" };
    responseTemplateGroups[11] = new String[] { ":${thisNick}!~jmeter@bots.jmeter PART ${channel}",
            ":${thisNick}!~jmeter@bots.jmeter JOIN :${channel}" };
    responseTemplateGroups[12] = new String[] { ":${thisNick}!~jmeter@bots.jmeter QUIT :${thisNick}",
            ":${thisNick}!~jmeter@bots.jmeter JOIN :${channel}" };

    Runtime runtime = Runtime.getRuntime();
    System.out.println("Memory usage: " + (runtime.totalMemory() / 1024));

    System.out.println("Building responses");
    responseGroups = new String[MAX_USERS * MAX_CHANNELS * MAX_ITERATIONS * responseTemplateGroups.length][];
    int responseGroupsCounter = 0;
    SecureRandom sortRandom = new SecureRandom();
    String[] searchList = new String[] { "${thisNick}", "${channel}" };
    for (int userNum = 0; userNum < MAX_USERS; userNum++) {
        shuffleArray(responseTemplateGroups, sortRandom);
        for (int channelNum = 0; channelNum < MAX_CHANNELS; channelNum++) {
            String[] replaceList = new String[] { "umark" + userNum, "#cbench" + channelNum };
            for (int iterationNum = 0; iterationNum < MAX_ITERATIONS; iterationNum++)
                //Parse template
                for (int templateNum = 0, templateSize = responseTemplateGroups.length; templateNum < templateSize; templateNum++) {
                    String[] templateGroup = responseTemplateGroups[templateNum];
                    String[] responseGroup = new String[templateGroup.length];
                    int responseCounter = 0;
                    for (int templateLineNum = 0, templateLineSize = templateGroup.length; templateLineNum < templateLineSize; templateLineNum++)
                        responseGroup[responseCounter++] = StringUtils
                                .replaceEachRepeatedly(templateGroup[templateLineNum], searchList, replaceList);
                    responseGroups[responseGroupsCounter++] = responseGroup;
                }
        }
    }

    System.out.println("Sorting");
    shuffleArray(responseGroups, sortRandom);

    //Init other objects
    StopWatch stopWatch = new StopWatch();
    ListenerManager listenerManager;
    if (threadCount == -1)
        listenerManager = new GenericListenerManager();
    else if (threadCount == 0)
        listenerManager = new ThreadedListenerManager(Executors.newCachedThreadPool());
    else
        listenerManager = new ThreadedListenerManager(Executors.newFixedThreadPool(threadCount));
    PircBotX bot = new PircBotX(TestUtils.generateConfigurationBuilder().setListenerManager(listenerManager)
            .addListener(new PircBotXJMeter()).buildConfiguration()) {
        @Override
        public boolean isConnected() {
            return true;
        }

        @Override
        protected void sendRawLineToServer(String line) {
            //Do nothing
        }
    };
    inputParser = bot.getInputParser();

    System.out.println("Waiting 5 seconds");
    Thread.sleep(5000);

    System.out.println("Executing with " + responseGroups.length + " response groups");
    int counter = run(stopWatch);

    System.out.println("Parsed " + counter + " enteries in " + stopWatch.toString());
    System.out.println(
            "Average parse speed: " + ((float) counter / (stopWatch.getTime() / 1000)) + " per second");

    System.out.println("Memory usage: " + (runtime.totalMemory() / 1024));

    //Kill the listener manager so the JVM can shutdown
    if (listenerManager instanceof ThreadedListenerManager)
        ((ThreadedListenerManager) listenerManager).shutdown();
}

From source file:org.structr.common.MailHelper.java

/**
 * Parse the template and replace any of the keys in the replacement map by
 * the given values/* w  ww  . j  av  a  2 s.c  o m*/
 *
 * @param template
 * @param replacementMap
 * @return template string with included replacements
 */
public static String replacePlaceHoldersInTemplate(final String template,
        final Map<String, String> replacementMap) {

    List<String> toReplace = new ArrayList<>();
    List<String> replaceBy = new ArrayList<>();

    for (Entry<String, String> property : replacementMap.entrySet()) {

        toReplace.add(property.getKey());
        replaceBy.add(property.getValue());

    }

    return StringUtils.replaceEachRepeatedly(template, toReplace.toArray(new String[toReplace.size()]),
            replaceBy.toArray(new String[replaceBy.size()]));

}