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.thoughtworks.go.domain.label.PipelineLabel.java

private String interpolateLabel(Map<CaseInsensitiveString, String> materialRevisions, int pipelineCounter) {
    final Matcher matcher = PATTERN.matcher(this.label);
    final StringBuffer buffer = new StringBuffer();

    while (matcher.find()) {
        String token = matcher.group("name");
        String value;/*from w  w  w.jav a 2s .co  m*/

        if (COUNT.equalsIgnoreCase(token)) {
            value = Integer.toString(pipelineCounter);
        } else if (token.toLowerCase().startsWith(ENV_VAR_PREFIX)) {
            value = resolveEnvironmentVariable(token);
        } else {
            final String truncate = matcher.group("truncation");
            value = resolveMaterialRevision(materialRevisions, token, truncate);
        }

        if (null == value) {
            value = "\\" + matcher.group(0);
        }

        matcher.appendReplacement(buffer, value);
    }

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

From source file:com.jsmartframework.web.manager.ExpressionHandler.java

public Object getExpressionValue(Object expr) {
    if (expr != null) {
        String evalExpr = expr.toString();

        Matcher matcher = EL_PATTERN.matcher(evalExpr);
        if (!matcher.find()) {
            return expr;
        }//from   w  w  w .  j a  va2 s. co m

        boolean hasMoreGroup = false;
        StringBuffer exprBuffer = new StringBuffer();

        Object result = evaluateExpression(evalExpr.substring(matcher.start() + 2, matcher.end() - 1));
        matcher.appendReplacement(exprBuffer,
                result != null ? Matcher.quoteReplacement(result.toString()) : "null");

        while (matcher.find()) {
            hasMoreGroup = true;
            Object object = evaluateExpression(evalExpr.substring(matcher.start() + 2, matcher.end() - 1));
            matcher.appendReplacement(exprBuffer,
                    object != null ? Matcher.quoteReplacement(object.toString()) : "null");
        }

        if (hasMoreGroup || result instanceof String) {
            return matcher.appendTail(exprBuffer).toString();
        } else {
            return result;
        }
    }
    return null;
}

From source file:org.mule.transport.ldap.LdapConnector.java

public String parseQuery(final String query, final Object[] values) {

    logger.debug(query);/*w  w w  . j av  a 2 s.  c  o  m*/
    logger.debug(Arrays.asList(values).toString());

    if (query == null) {
        return query;
    }
    final Matcher m = STATEMENT_ARGS.matcher(query);
    final StringBuffer sb = new StringBuffer(200);
    int i = 0;
    while (m.find()) {

        m.appendReplacement(sb, values[i] == null ? "null" : values[i].toString());
        i++;
    }
    m.appendTail(sb);
    return sb.toString();
}

From source file:com.mobile.system.db.abatis.AbatisService.java

/**
  */*from  w w  w.  java 2s . c o  m*/
  */
private String chgDataName(String targetStr) {
    Pattern p = Pattern.compile("_([a-z])");
    Matcher m = p.matcher(targetStr.toLowerCase());

    StringBuffer sb = new StringBuffer(targetStr.length());
    while (m.find()) {
        m.appendReplacement(sb, m.group(1).toUpperCase());
    }
    m.appendTail(sb);
    return sb.toString();
}

From source file:org.endiansummer.tools.soaprunner.VariableResolver.java

public String resolve(String expression) {
    StringBuffer resolvedReplacement = new StringBuffer();
    Matcher matcher = pattern.matcher(expression);
    while (matcher.find()) {
        String variable = matcher.group(1);
        String unresolvedVar = variable;

        if ("random_uuid".equals(variable)) {
            variable = UUID.randomUUID().toString();
        } else if ("random_int".equals(variable)) {
            variable = "" + RandomUtils.nextInt();
        } else if ("current_time".equals(variable)) {
            variable = new Date().toString();
        } else {//from  w ww .  j  a  v a 2 s  .  c  o m
            variable = VariableStore.getInstance().getVariable(variable);
            if (variable == null) {
                variable = "";
            } else if (verbose) {
                Log.line("Resolved variable " + unresolvedVar + " to " + variable);
            }
        }

        matcher.appendReplacement(resolvedReplacement, variable);
    }

    return resolvedReplacement.toString();
}

From source file:ru.xxlabaza.popa.pack.PackingService.java

private String correctURLs(Path path, String content) {
    Matcher matcher = CSS_URL_PATTERN.matcher(content);
    StringBuffer buffer = new StringBuffer(content.length());
    while (matcher.find()) {
        String url = matcher.group(1);

        String[] tokens = url.split("[\\?#]", 2);

        Path pathToUrlFile = path.getParent().resolve(tokens[0]);
        Path urlPath = build.relativize(pathToUrlFile);
        if (Files.exists(pathToUrlFile)) {
            FileSystemUtils.copy(pathToUrlFile, compressed.resolve(urlPath));
        }/*from  w  w  w.  j  a  v  a 2s . c  o  m*/

        StringBuilder sb = new StringBuilder('/');
        sb.append(urlPath);
        if (tokens.length > 1) {
            sb.append(url.contains("?") ? '?' : '#').append(tokens[1]);
        }
        matcher.appendReplacement(buffer, sb.toString());
    }
    matcher.appendTail(buffer);
    return buffer.toString();
}

From source file:org.pentaho.js.require.RequireJsGenerator.java

private void requirejsFromJs(String moduleName, String moduleVersion, String jsScript)
        throws IOException, ScriptException, NoSuchMethodException, ParseException {
    moduleInfo = new ModuleInfo(moduleName, moduleVersion);

    Pattern pat = Pattern.compile("webjars!(.*).js");
    Matcher m = pat.matcher(jsScript);

    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(sb, m.group(1));
    }//from  ww  w  . j  av  a 2s.  c om
    m.appendTail(sb);

    jsScript = sb.toString();

    pat = Pattern.compile("webjars\\.path\\(['\"]{1}(.*)['\"]{1}, (['\"]{0,1}[^\\)]+['\"]{0,1})\\)");
    m = pat.matcher(jsScript);
    while (m.find()) {
        m.appendReplacement(sb, m.group(2));
    }
    m.appendTail(sb);

    jsScript = sb.toString();

    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("JavaScript");
    String script = IOUtils
            .toString(getClass().getResourceAsStream("/org/pentaho/js/require/require-js-aggregator.js"));
    script = script.replace("{{EXTERNAL_CONFIG}}", jsScript);

    engine.eval(script);

    requireConfig = (Map<String, Object>) parser
            .parse(((Invocable) engine).invokeFunction("processConfig", "").toString());
}

From source file:com.github.dactiv.orm.core.hibernate.support.BasicHibernateDao.java

/**
 * hql order by,?//from   w ww  .jav  a2s .co  m
 * 
 * @param hql
 * 
 * @return String
 */
private String removeOrders(String hql) {
    Pattern p = Pattern.compile("order\\s*by[\\w|\\W|\\s|\\S]*", Pattern.CASE_INSENSITIVE);
    Matcher m = p.matcher(hql);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(sb, "");
    }
    m.appendTail(sb);
    return sb.toString();
}

From source file:org.mule.transport.ldap.LdapConnector.java

public String parseStatement(final String stmt, final List params) {
    if (stmt == null) {
        return stmt;
    }//from  w  w  w .j ava2 s  .  co  m
    final Matcher m = STATEMENT_ARGS.matcher(stmt);
    final StringBuffer sb = new StringBuffer(200);
    while (m.find()) {
        String key = m.group();
        m.appendReplacement(sb, "?");
        // Special legacy handling for #[payload]
        if (key.equals("#[payload]")) {
            // MULE-3597
            logger.warn(
                    "invalid expression template #[payload]. It should be replaced with #[payload:] to conform with the correct expression syntax. Mule has replaced this for you, but may not in future versions.");
            key = "#[payload:]";
        }
        params.add(key);
    }
    m.appendTail(sb);
    return sb.toString();
}

From source file:au.edu.anu.portal.portlets.tweetal.servlet.TweetalServlet.java

private String replaceLinks(String statusText) {
    Pattern p = Pattern.compile("http[s]{0,1}://[A-Za-z0-9\\._\\-~#/]+");
    Matcher m = p.matcher(statusText);

    // if we have a match anywhere in the string
    StringBuffer buf = new StringBuffer();
    while (m.find()) {
        //replace that portion that matched, with the link
        String matched = m.group();
        m.appendReplacement(buf, String.format("<a target=\"_blank\" href=\"%s\">%s</a>", matched, matched));
    }/*from   w ww  . j a v  a2s . com*/
    m.appendTail(buf);
    return buf.toString();
}