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.eu.evaluation.server.dao.AbstractDAO.java

/**
 * jpqlorder By??//from  w w  w.  j a  va 2 s  .  c om
 *
 * @param jpql
 * @return
 */
private static String removeOrders(String jpql) {
    Pattern p = Pattern.compile("order\\s*by[\\w|\\W|\\s|\\S]*", Pattern.CASE_INSENSITIVE);
    Matcher m = p.matcher(jpql);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(sb, "");
    }
    m.appendTail(sb);
    return sb.toString();
}

From source file:org.codehaus.mojo.hibernate3.processor.ProcessorUtil.java

public static String insertImportIntoClassDefinition(String importCode, String body) {
    StringBuffer buffer = new StringBuffer();
    Matcher matcher = pkgImportPattern.matcher(body);

    String importStmt = "import " + importCode + ";";
    if (body.contains(importStmt)) {
        return body;
    }//from www . j av a2 s.co m

    while (matcher.find()) {
        String replacement = matcher.group(0) + " " + importStmt + "\n";
        matcher.appendReplacement(buffer, replacement);
    }
    matcher.appendTail(buffer);
    return buffer.toString();
}

From source file:de.egore911.versioning.deployer.performer.PerformExtraction.java

/**
 * Returns the matching part of the entry's name.
 * //www .  j av a  2  s.co  m
 * FIXME assumes that the '*' is only used for matching files, not
 * directories!
 */
private static String getSourcePatternMatch(String entryName, String sourcePattern) {

    // Create a regular expression pattern for the given sourcePattern
    Pattern pattern = patternCache.get(sourcePattern);
    if (pattern == null) {
        // Pattern compilation is expensive, so cache it
        pattern = Pattern.compile(transformSourcePatternToRegularExpression(sourcePattern));
        patternCache.put(sourcePattern, pattern);
    }

    // Perform the actual replacement
    Matcher matcher = pattern.matcher(entryName);
    StringBuffer buffer = new StringBuffer();
    if (matcher.matches()) {
        if (matcher.groupCount() == 1) {
            matcher.appendReplacement(buffer, matcher.group(1));
        }
    }
    matcher.appendTail(buffer);
    return buffer.toString();
}

From source file:koubachi.internal.json.JSONFactory.java

private static String cleanDateFormat(String json) { // takes in a string of JSON
    Pattern regex = Pattern.compile("\\d\\d:\\d\\d:\\d\\d[-\\+]\\d\\d:\\d\\d");
    Matcher regexMatcher = regex.matcher(json);
    StringBuffer buff = new StringBuffer();
    while (regexMatcher.find()) {
        regexMatcher.appendReplacement(buff, getSubOfMatch(regexMatcher));
    }/*from   w  w  w.  j  a  v  a2 s  .c  o  m*/
    regexMatcher.appendTail(buff);
    return buff.toString();
}

From source file:org.kitodo.production.plugin.opac.pica.ConfigOpacCatalogue.java

/**
 * The function fillIn() replaces marks in a given string by values derived
 * from match results. There are two different mechanisms available for
 * replacement./* w w w . j  a v a2  s  .c o  m*/
 *
 * <p>
 * If the marked string contains the replacement mark <code>{\\@}</code>,
 * the matchers find() operation will be invoked over and over again and
 * all match results are concatenated and inserted in place of the
 * replacement marks.
 * </p>
 *
 * <p>
 * Otherwise, all replacement marks <code>{1}</code>, <code>{2}</code>,
 * <code>{3}</code>,  will be replaced by the capturing groups matched by
 * the matcher.
 * </p>
 *
 * @param markedString
 *            a string with replacement markers
 * @param matcher
 *            a matcher whos values shall be inserted
 * @return the string with the replacements filled in
 * @throws IndexOutOfBoundsException
 *             If there is no capturing group in the pattern with the given
 *             index
 */
private static String fillIn(String markedString, Matcher matcher) {
    if (matcher == null) {
        return markedString;
    }
    if (markedString.contains("{@}")) {
        StringBuilder composer = new StringBuilder();
        composer.append(matcher.group());
        while (matcher.find()) {
            composer.append(matcher.group());
        }
        return markedString.replaceAll("\\{@\\}", composer.toString());
    } else {
        StringBuffer replaced = new StringBuffer();
        Matcher replacer = Pattern.compile("\\{(\\d+)\\}").matcher(markedString);
        while (replacer.find()) {
            replacer.appendReplacement(replaced, matcher.group(Integer.parseInt(replacer.group(1))));
        }
        replacer.appendTail(replaced);
        return replaced.toString();
    }
}

From source file:org.infoscoop.request.filter.ProxyHtmlUtil.java

private static Collection getReplacedHeaders(String url, Map<String, List<String>> responseHeaders) {
    int contextRoot = url.indexOf("://", 1);
    contextRoot = url.indexOf("/", contextRoot + 3);

    String contextUrl = (contextRoot > 0) ? url.substring(0, contextRoot) : url + "/";
    String[] replacement = { "$1" + contextUrl + "$2" };

    Collection headers = new ArrayList();
    for (Map.Entry<String, List<String>> header : responseHeaders.entrySet()) {

        String name = header.getKey();

        int idx = contains(name, REPLACE_HEADER);
        if (idx < 0)
            continue;
        for (String value : header.getValue()) {
            StringBuffer newValue = null;
            Matcher m = REPLACE_HEADER_PATTERN[idx].matcher(value);
            while (m.find()) {
                if (newValue == null)
                    newValue = new StringBuffer();

                m.appendReplacement(newValue, replacement[idx]);
            }/*from w  ww .  j a v  a  2 s . c  om*/

            if (newValue != null) {
                m.appendTail(newValue);
                log.info("Replace response header [" + name + " = " + newValue + "]");

                headers.add(new Header(name, newValue.toString()));
            }
        }
    }

    return headers;
}

From source file:Main.java

/**
 * Substitutes links in the given text with valid HTML mark-up. For instance,
 * http://dhis2.org is replaced with <a href="http://dhis2.org">http://dhis2.org</a>,
 * and www.dhis2.org is replaced with <a href="http://dhis2.org">www.dhis2.org</a>.
 *
 * @param text the text to substitute links for.
 * @return the substituted text./* w  w  w  .  j a  v a 2 s. c  o  m*/
 */
public static String htmlLinks(String text) {
    if (text == null || text.trim().isEmpty()) {
        return null;
    }

    Matcher matcher = LINK_PATTERN.matcher(text);

    StringBuffer buffer = new StringBuffer();

    while (matcher.find()) {
        String url = matcher.group(1);
        String suffix = matcher.group(3);

        String ref = url.startsWith("www.") ? "http://" + url : url;

        url = "<a href=\"" + ref + "\">" + url + "</a>" + suffix;

        matcher.appendReplacement(buffer, url);
    }

    return matcher.appendTail(buffer).toString();
}

From source file:ddf.util.WktStandard.java

/**
 * Denormalize the given WKT to support backwards compatibility.
 *
 * @param wkt/*ww  w  .j  av  a 2  s  .c  om*/
 *            wkt to denormalize
 * @return denormalized WKT
 */
public static String denormalize(String wkt) {
    if (wkt == null) {
        return wkt;
    }

    Matcher matcher = WKT_MULTIPOINT_PATTERN.matcher(wkt);
    if (matcher.find()) {
        matcher.reset();
        StringBuffer resultWkt = new StringBuffer(wkt.length());
        while (matcher.find()) {
            String currentMultiPoint = matcher.group(0);
            String currentMultiPointText = matcher.group(1);

            matcher.appendReplacement(resultWkt, currentMultiPoint.replace(currentMultiPointText,
                    currentMultiPointText.replaceAll("[\\(\\)]", "")));
        }
        matcher.appendTail(resultWkt);

        return resultWkt.toString();
    } else {
        return wkt;
    }
}

From source file:eu.nerdz.api.impl.fastreverse.messages.FastReverseConversationHandler.java

/**
 * Replaces a single tag.//from   w  w  w .  j  av a  2 s.c  om
 * @param regex A regex
 * @param message A message to be parsed
 * @return A string in which all occurrences of regex have been substituted with the contents matched
 */
private static String replaceSingle(String regex, String message) {

    Matcher matcher = Pattern.compile(regex, Pattern.DOTALL | Pattern.CASE_INSENSITIVE).matcher(message);
    StringBuffer result = new StringBuffer();

    while (matcher.find()) {
        matcher.appendReplacement(result, matcher.group(1).replace("$", "\\$"));
    }

    matcher.appendTail(result);

    return result.toString();
}

From source file:eu.nerdz.api.impl.fastreverse.messages.FastReverseConversationHandler.java

/**
 * Replaces a composite tag.//from   w w  w  .  j  a  v a 2  s . c  o  m
 * @param regex A regex
 * @param message A message to be parsed
 * @param format A format for pretty formatting. Only 2 string fields.
 * @return A string in which all occurrences of regex have been substituted with the contents matched
 */
private static String replaceDouble(String regex, String message, String format) {

    Matcher matcher = Pattern.compile(regex, Pattern.DOTALL | Pattern.CASE_INSENSITIVE).matcher(message);
    StringBuffer result = new StringBuffer();

    while (matcher.find()) {
        matcher.appendReplacement(result,
                String.format(format, matcher.group(1), matcher.group(2)).replace("$", "\\$"));
    }

    matcher.appendTail(result);

    return result.toString();

}