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:com.qwazr.utils.HtmlUtils.java

public static final String removeTag(String text, String[] allowedTags) {
     if (allowedTags == null)
         text = StringUtils.replaceConsecutiveSpaces(text, " ");
     StringBuffer sb = new StringBuffer();
     Matcher matcher;
     synchronized (removeTagPattern) {
         matcher = removeTagPattern.matcher(text);
     }//from  w w  w . j av a  2 s  . 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:gsn.storage.SQLUtils.java

public static StringBuilder newRewrite(CharSequence query, CharSequence tableNameToRename,
        CharSequence replaceTo) {
    // Selecting strings between pair of "" : (\"[^\"]*\")
    // Selecting tableID.tableName or tableID.* : (\\w+(\\.(\w+)|\\*))
    // The combined pattern is : (\"[^\"]*\")|(\\w+\\.((\\w+)|\\*))
    Matcher matcher = pattern.matcher(query);
    StringBuffer result = new StringBuffer();
    while (matcher.find()) {
        if (matcher.group(2) == null)
            continue;
        String tableName = matcher.group(3);
        if (tableName.equals(tableNameToRename)) {
            // $4 means that the 4th group of the match should be appended to the
            // string (the forth group contains the field name).
            if (replaceTo != null)
                matcher.appendReplacement(result, new StringBuilder(replaceTo).append("$4").toString());
        }//  w  w w. jav a2  s  .  com
    }
    String toReturn = matcher.appendTail(result).toString().toLowerCase();
    int indexOfFrom = toReturn.indexOf(" from ") >= 0 ? toReturn.indexOf(" from ") + " from ".length() : 0;
    int indexOfWhere = (toReturn.lastIndexOf(" where ") > 0 ? (toReturn.lastIndexOf(" where "))
            : toReturn.length());
    String selection = toReturn.substring(indexOfFrom, indexOfWhere);
    Pattern fromClausePattern = Pattern.compile("\\s*(\\w+)\\s*", Pattern.CASE_INSENSITIVE);
    Matcher fromClauseMather = fromClausePattern.matcher(selection);
    result = new StringBuffer();
    while (fromClauseMather.find()) {
        if (fromClauseMather.group(1) == null)
            continue;
        String tableName = fromClauseMather.group(1);
        if (tableName.equals(tableNameToRename) && replaceTo != null)
            fromClauseMather.appendReplacement(result, replaceTo.toString() + " ");
    }
    String cleanFromClause = fromClauseMather.appendTail(result).toString();
    String finalResult = StringUtils.replace(toReturn, selection, cleanFromClause);
    return new StringBuilder(finalResult);
}

From source file:org.jumpmind.util.FormatUtils.java

/**
 * Replace the keys found in the target text with the values found in the
 * replacements map.// w  ww .ja  va2  s  .  co m
 * 
 * @param text
 *            The text to replace
 * @param replacements
 *            The map that contains the replacement values
 * @param matchUsingPrefixSuffix
 *            If true, look for the $(key) pattern to replace. If false,
 *            just replace the key outright.
 * @return The text with the token keys replaced
 */
public static String replaceTokens(String text, Map<?, ?> replacements, boolean matchUsingPrefixSuffix) {
    if (text != null && replacements != null && replacements.size() > 0) {
        if (matchUsingPrefixSuffix) {
            Matcher matcher = pattern.matcher(text);
            StringBuffer buffer = new StringBuffer();
            while (matcher.find()) {
                String[] match = matcher.group(1).split("\\|");
                String replacement = (String) replacements.get(match[0]);
                if (replacement != null) {
                    matcher.appendReplacement(buffer, "");
                    if (match.length == 2) {
                        replacement = formatString(match[1], replacement);
                    }
                    buffer.append(replacement);
                }
            }
            matcher.appendTail(buffer);
            text = buffer.toString();
        } else {
            for (Object key : replacements.keySet()) {
                text = text.replaceAll(key.toString(), (String) replacements.get(key));
            }
        }
    }
    return text;

}

From source file:com.epam.reportportal.jbehave.JBehaveUtils.java

@VisibleForTesting
static String expandParameters(String stepName, Map<String, String> parameters) {
    Matcher m = STEP_NAME_PATTERN.matcher(stepName);
    StringBuffer buffer = new StringBuffer();
    while (m.find()) {
        if (parameters.containsKey(m.group(1))) {
            m.appendReplacement(buffer, parameters.get(m.group(1)));
        }//w w w. ja v a  2 s.co m
    }
    m.appendTail(buffer);
    return buffer.toString();
}

From source file:com.manydesigns.elements.fields.TextField.java

public static String highlightLinks(String str) {
    if (str == null) {
        return null;
    }//from ww  w . ja  va2 s . c  o m
    // Pattern Matching will be case insensitive.
    Matcher linkMatcher = linkPattern.matcher(str);

    boolean linkTrovato = false;
    StringBuffer sb = new StringBuffer();
    while (linkMatcher.find()) {
        String text = shortenEscaped(linkMatcher.group(0), 22);
        if (linkMatcher.group(1).equalsIgnoreCase("www.")) {
            linkMatcher.appendReplacement(sb,
                    "<a href=\"http://" + linkMatcher.group(0) + "\">" + text + "</a>");
        } else {
            linkMatcher.appendReplacement(sb, "<a href=\"" + linkMatcher.group(0) + "\">" + text + "</a>");
        }
        linkTrovato = true;
    }
    if (linkTrovato) {
        linkMatcher.appendTail(sb);
        str = sb.toString();
        linkTrovato = false;
        sb = new StringBuffer();
    }

    //mail
    Matcher emailMatcher = emailPattern.matcher(str);
    while (emailMatcher.find()) {
        emailMatcher.appendReplacement(sb,
                "<a href=\"mailto:" + emailMatcher.group(0) + "\">" + emailMatcher.group(0) + "</a>");
        linkTrovato = true;
    }
    if (linkTrovato) {
        emailMatcher.appendTail(sb);
        str = sb.toString();
    }
    return str;
}

From source file:org.apache.jmeter.report.dashboard.ReportGenerator.java

/**
 * <p>//from   ww w  .  j  a va2s . c  om
 * 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(); // 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.etudes.util.HtmlHelper.java

/**
 * Remove any tags only valid in headers (title base meta link style)
 * /*w  ww .ja va 2  s .  c o m*/
 * @param data
 *        the html data.
 * @return The cleaned up data.
 */
public static String stripHeaderTags(String data) {
    if (data == null)
        return data;

    // pattern to find link/meta tags
    Pattern p = Pattern.compile("<(link|meta|title|base|style)\\s+.*?(/*>)",
            Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL);

    Matcher m = p.matcher(data);
    StringBuffer sb = new StringBuffer();

    while (m.find()) {
        m.appendReplacement(sb, "");
    }

    m.appendTail(sb);

    return sb.toString();
}

From source file:org.etudes.util.HtmlHelper.java

/**
 * Remove link and meta tags/* ww  w  . java  2  s .c o  m*/
 * 
 * @param data
 *        the html data.
 * @return The cleaned up data.
 */
public static String stripLinks(String data) {
    if (data == null)
        return data;

    // pattern to find link/meta tags
    Pattern p = Pattern.compile("<(link|meta)\\s+.*?(/*>)",
            Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL);

    Matcher m = p.matcher(data);
    StringBuffer sb = new StringBuffer();

    while (m.find()) {
        m.appendReplacement(sb, "");
    }

    m.appendTail(sb);

    return sb.toString();
}

From source file:org.etudes.util.HtmlHelper.java

/**
 * Remove image tags that have for src "file://" "webkit-fake-url://" or "x-apple-ql-id://" prefixes (transports)
 * //from ww w  .j  ava2 s  . c om
 * @param data
 *        the html data.
 * @return The cleaned up data.
 */
public static String stripBadImageTags(String data) {
    if (data == null)
        return data;

    // pattern to find link/meta tags
    // TODO: the .*? needs to stop on a >, else if there's a good image and later a bad one, it combines the two into one and removes it all!
    Pattern p = Pattern.compile("<img\\s+.*?src=\"(file:|webkit-fake-url:|x-apple-ql-id:).*?/>",
            Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL);

    Matcher m = p.matcher(data);
    StringBuffer sb = new StringBuffer();

    while (m.find()) {
        m.appendReplacement(sb, "");
    }

    m.appendTail(sb);

    return sb.toString();
}

From source file:org.etudes.util.HtmlHelper.java

/**
 * Remove any text that match the "comments from Word font definitions encoded into html by Tiny" from the data.
 * //from   w  w w.j  a v a2  s.  c  o  m
 * @param data
 *        the html data.
 * @return The cleaned up data.
 */
public static String stripEncodedFontDefinitionComments(String data) {
    if (data == null)
        return data;

    // quick check for any hint of the pattern
    if (data.indexOf("&lt;!--  /* Font Definitions */") == -1)
        return data;

    // Notes: DOTALL so the "." matches line terminators too, "*?" Reluctant quantifier so text between two different comments is not lost
    Pattern p = Pattern.compile("&lt;!--  /\\* Font Definitions \\*/.*?--&gt;",
            Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL);

    Matcher m = p.matcher(data);
    StringBuffer sb = new StringBuffer();

    while (m.find()) {
        m.appendReplacement(sb, "");
    }

    m.appendTail(sb);

    return sb.toString();
}