Example usage for java.util.regex Matcher appendReplacement

List of usage examples for java.util.regex Matcher appendReplacement

Introduction

In this page you can find the example usage for java.util.regex Matcher appendReplacement.

Prototype

public Matcher appendReplacement(StringBuilder sb, String replacement) 

Source Link

Document

Implements a non-terminal append-and-replace step.

Usage

From source file:cz.cas.lib.proarc.common.process.GenericExternalProcess.java

/**
 * Interpolate parameter values. The replace pattern is {@code ${name}}.
 * @param s a string to search for placeholders
 * @return the resolved string/*from   w w w . ja va 2 s  .  c o  m*/
 * @see #addParameter(java.lang.String, java.lang.String)
 */
static String interpolateParameters(String s, Map<String, String> parameters) {
    if (s == null || s.length() < 4 || parameters.isEmpty()) { // minimal replaceable value ${x}
        return s;
    }
    // finds ${name} patterns
    Matcher m = REPLACE_PARAM_PATTERN.matcher(s);
    StringBuffer sb = null;
    while (m.find()) {
        if (m.groupCount() == 1) {
            String param = m.group(1);
            String replacement = parameters.get(param);
            if (replacement != null) {
                sb = sb != null ? sb : new StringBuffer();
                m.appendReplacement(sb, Matcher.quoteReplacement(replacement));
            }
        }
    }
    if (sb == null) {
        return s;
    }
    m.appendTail(sb);
    return sb.toString();
}

From source file:io.renren.modules.test.jmeter.report.LocalReportGenerator.java

/**
 * <p>//from   w ww  .j  av  a  2  s.co  m
 * Gets the name of property setter from the specified key.
 * </p>
 * <p>
 * E.g : with key set_granularity, returns setGranularity (camel case)
 * </p>
 *
 * @param propertyKey
 *            the property key
 * @return the name of the property setter
 */
private static String getSetterName(String propertyKey) {
    Matcher matcher = POTENTIAL_CAMEL_CASE_PATTERN.matcher(propertyKey);
    StringBuffer buffer = new StringBuffer(); // NOSONAR Unfortunately Matcher does not support StringBuilder
    while (matcher.find()) {
        matcher.appendReplacement(buffer, matcher.group(1).toUpperCase());
    }
    matcher.appendTail(buffer);
    return buffer.toString();
}

From source file:org.jbosson.plugins.amq.ArtemisMBeanDiscoveryComponent.java

protected static String formatMessage(String messageTemplate, Map<String, String> variableValues) {

    StringBuffer result = new StringBuffer();

    // collect keys and values to determine value size limit if needed
    final Map<String, String> replaceMap = new HashMap<String, String>();

    final Matcher matcher = PROPERTY_NAME_PATTERN.matcher(messageTemplate);
    int count = 0;
    while (matcher.find()) {
        final String key = matcher.group(1);
        final String value = variableValues.get(key);

        if (value != null) {
            replaceMap.put(key, value);//from  w w  w  .  j a  v  a2 s  . c o m
            matcher.appendReplacement(result, Matcher.quoteReplacement(value));
        } else {
            matcher.appendReplacement(result, Matcher.quoteReplacement(matcher.group()));
        }

        count++;
    }
    matcher.appendTail(result);

    // check if the result exceeds MAX_LENGTH for formatted properties
    if (!replaceMap.isEmpty() && result.length() > MAX_LENGTH) {
        // sort values according to size
        final SortedSet<String> values = new TreeSet<String>(new Comparator<String>() {
            public int compare(String o1, String o2) {
                return o1.length() - o2.length();
            }
        });
        values.addAll(replaceMap.values());

        // find total value characters allowed
        int available = MAX_LENGTH - PROPERTY_NAME_PATTERN.matcher(messageTemplate).replaceAll("").length();

        // fit values from small to large in the allowed size to determine the maximum average
        int averageLength = available / count;
        for (String value : values) {
            final int length = value.length();
            if (length > averageLength) {
                break;
            }
            available -= length;
            count--;
            averageLength = available / count;
        }

        // replace values
        matcher.reset();
        result.delete(0, result.length());
        while (matcher.find()) {
            String value = replaceMap.get(matcher.group(1));
            if (value != null && value.length() > averageLength) {
                value = value.substring(0, averageLength);
            }
            matcher.appendReplacement(result, value != null ? value : matcher.group());
        }
        matcher.appendTail(result);
    }

    return result.toString();
}

From source file:com.jaeksoft.searchlib.util.StringUtils.java

public static final String removeTag(String text, String[] allowedTags) {
    if (allowedTags == null)
        text = replaceConsecutiveSpaces(text, " ");
    StringBuffer sb = new StringBuffer();
    Matcher matcher;
    synchronized (removeTagPattern) {
        matcher = removeTagPattern.matcher(text);
    }//from  w w  w  .  j av  a  2s .c o m
    while (matcher.find()) {
        boolean allowed = false;
        String group = matcher.group();
        if (allowedTags != null) {
            for (String tag : allowedTags) {
                if (tag.equals(group)) {
                    allowed = true;
                    break;
                }
            }
        }
        matcher.appendReplacement(sb, allowed ? group : "");
    }
    matcher.appendTail(sb);
    return sb.toString();
}

From source file:ch.rasc.edsutil.optimizer.WebResourceProcessor.java

private static String cleanCode(String sourcecode) {
    Matcher matcher = DEV_CODE_PATTERN.matcher(sourcecode);
    StringBuffer cleanCode = new StringBuffer();
    while (matcher.find()) {
        matcher.appendReplacement(cleanCode, "");
    }//from   www. ja va  2  s . c o m
    matcher.appendTail(cleanCode);

    return cleanCode.toString().replaceAll(REQUIRES_PATTERN, "").replaceAll(USES_PATTERN, "");
}

From source file:com.trackplus.track.util.html.Html2LaTeX.java

/**
 * Get LaTeX text translated from HTML./*w  w w.jav a2  s . c o  m*/
 *
 * @param bodyHtml
 * @return the LaTeX representation of the bodyHtml
 */
public static String getLaTeX(String bodyHtml) {

    // Make sure verbatim is properly transformed, protect spaces and line
    // breaks
    Pattern patt = Pattern.compile("(?s)(.?)<div\\s*class=\\\"code\\-text\\\".+?>(.+?)</div>");
    Matcher m = patt.matcher(bodyHtml);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        String verbatim = m.group(2);
        verbatim = verbatim.replace("\n", "<br>").replace(" ", "&nbsp;");
        String text = m.group(1) + "<div class=\"code-text\">" + verbatim + "</div>"; // +
        // m.group(3);
        m.appendReplacement(sb, Matcher.quoteReplacement(text));
    }
    m.appendTail(sb);
    bodyHtml = sb.toString();

    // System.err.println(bodyHtml);

    Document doc = Jsoup.parse(bodyHtml);

    String latex = getLatexText(doc);
    sb = new StringBuffer();
    String[] lines = latex.split("\n");
    for (int i = 0; i < lines.length; ++i) {
        if ("\\\\".equals(lines[i].trim())) {
            lines[i] = "";
        }
        sb.append(lines[i] + "\n");
    }

    return sb.toString();
}

From source file:org.apache.sqoop.odps.OdpsUploadProcessor.java

public static String escapeString(String in, Map rowMap) {
    Matcher matcher = tagPattern.matcher(in);
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        String replacement = "";
        if (matcher.group(2) != null) {
            replacement = rowMap.get(matcher.group(2).toLowerCase()).toString();
            if (replacement == null) {
                replacement = "";
            }/*from   ww  w  .  j a va  2  s.  c  o m*/
        }
        replacement = replacement.replaceAll("\\\\", "\\\\\\\\");
        replacement = replacement.replaceAll("\\$", "\\\\\\$");

        matcher.appendReplacement(sb, replacement);
    }
    matcher.appendTail(sb);
    return sb.toString();
}

From source file:com.hexidec.ekit.component.HTMLUtilities.java

private static String addColgroups(String html, List<String> colgroups) {
    StringBuffer sb = new StringBuffer();

    Pattern p = Pattern.compile("<table\\b[^>]*>", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);

    Matcher m = p.matcher(html);

    if (!m.find()) {
        return html;
    }//w  ww . j a v a  2 s .c  om

    int i = 0;
    do {
        String colgroup = colgroups.get(i++);
        m.appendReplacement(sb, Matcher.quoteReplacement(m.group() + colgroup));
    } while (m.find());

    m.appendTail(sb);

    return sb.toString();
}

From source file:org.jasig.portlet.emailpreview.util.MessageUtils.java

public static String addClickableUrlsToMessageBody(String msgBody) {

    // Assertions.
    if (msgBody == null) {
        String msg = "Argument 'msgBody' cannot be null";
        throw new IllegalArgumentException(msg);
    }//from   ww w.  j a  v a 2  s .c om

    StringBuffer rslt = new StringBuffer();

    Matcher m = CLICKABLE_URLS_PATTERN.matcher(msgBody);
    while (m.find()) {
        StringBuilder bldr = new StringBuilder();
        String text = m.group(1);
        // Handle special case where URL not prefixed with required protocol
        String url = text.startsWith("www.") ? "http://" + text : text;
        if (LOG.isDebugEnabled()) {
            LOG.debug("Making embedded URL '" + text + "' clickable at the following href:  " + url);
        }
        bldr.append(CLICKABLE_URLS_PART1).append(url).append(CLICKABLE_URLS_PART2).append(text)
                .append(CLICKABLE_URLS_PART3);
        m.appendReplacement(rslt, bldr.toString());
    }
    m.appendTail(rslt);

    return rslt.toString();

}

From source file:com.google.api.tools.framework.aspects.documentation.model.DocumentationUtil.java

/**
 * Given a documentation string, replace the cross reference links with reference text.
 *///from   w w  w.j a v a 2  s  .c  o  m
public static String removeCrossReference(String text) {
    if (Strings.isNullOrEmpty(text)) {
        return "";
    }
    Pattern pattern = Pattern.compile("\\[(?<name>[^\\]]+?)\\]( |\\n)*" + "\\[(?<link>[^\\]]*?)\\]");
    Matcher matcher = pattern.matcher(text);
    StringBuffer result = new StringBuffer();
    while (matcher.find()) {
        String replacementText = matcher.group("name");
        replacementText = Matcher.quoteReplacement(replacementText);
        matcher.appendReplacement(result, replacementText);
    }
    matcher.appendTail(result);
    return result.toString();
}