Example usage for java.util.regex Matcher appendTail

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

Introduction

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

Prototype

public StringBuilder appendTail(StringBuilder sb) 

Source Link

Document

Implements a terminal append-and-replace step.

Usage

From source file:com.dinochiesa.edgecallouts.Base64.java

private String resolvePropertyValue(String spec, MessageContext msgCtxt) {
    Matcher matcher = variableReferencePattern.matcher(spec);
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        matcher.appendReplacement(sb, "");
        sb.append(matcher.group(1));//from   w  w  w.j ava  2s .com
        sb.append((String) msgCtxt.getVariable(matcher.group(2)));
        sb.append(matcher.group(3));
    }
    matcher.appendTail(sb);
    return sb.toString();
}

From source file:com.haulmont.cuba.core.sys.querymacro.TimeBetweenQueryMacroHandler.java

protected String replaceParamsInMacros(String macros, Map<String, Object> params) {
    Matcher matcher = QUERY_PARAM_PATTERN.matcher(macros);
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        String paramName = matcher.group(1);
        if (params.containsKey(paramName)) {
            matcher.appendReplacement(sb, params.get(paramName).toString());
            params.remove(paramName);//from  www .j  a  v  a 2 s  .  c  o m
        }
    }
    matcher.appendTail(sb);
    return sb.toString();
}

From source file:com.yukthitech.persistence.NativeQueryFactory.java

/**
 * Fetches the query template with specified "name" and builds the query using specified "context".
 * If the query uses "param" directive, corresponding values will be collected into "paramValues".
 * //from www  . j a  va 2s.  c o  m
 * @param name Name of the query to be fetched
 * @param outputParamValues Collected param values that needs to be passed to query as prepared statement params
 * @param context Context to be used to parse template into query
 * @return Built query
 */
public String buildQuery(String name, List<Object> outputParamValues, Object context) {
    String rawQuery = queryMap.get(name);

    //if no query found with specified name
    if (rawQuery == null) {
        throw new InvalidArgumentException("No query found with specified name - {}", name);
    }

    try {
        //build the final query (replacing the param expressions)
        String query = freeMarkerEngine.processTemplate(name, rawQuery, context);
        StringBuffer finalQuery = new StringBuffer();
        Matcher matcher = QUERY_PARAM_PATTERN.matcher(query);
        String property = null;

        while (matcher.find()) {
            property = matcher.group(1);
            matcher.appendReplacement(finalQuery, "?");

            outputParamValues.add(PropertyUtils.getProperty(context, property));
        }

        matcher.appendTail(finalQuery);

        return finalQuery.toString();
    } catch (Exception ex) {
        throw new InvalidStateException(ex, "An exception occurred while building query: " + name);
    }
}

From source file:net.opentsdb.tools.ConfigArgP.java

/**
 * Evaluates JS expressions defines as configuration values
 * @param text The value of a configuration item to evaluate for JS expressions
 * @return The passed value with any embedded JS expressions evaluated and replaced
 *//*w w w  .ja v  a2s .  c o  m*/
public static String evaluate(CharSequence text) {
    if (text == null)
        return null;
    Matcher m = JS_PATTERN.matcher(text);
    StringBuffer ret = new StringBuffer();
    final boolean isNas = scriptEngine.getFactory().getEngineName().toLowerCase().contains("nashorn");
    while (m.find()) {
        String source = (isNas ? "load(\"nashorn:mozilla_compat.js\");\nimportPackage(java.lang); "
                : "\nimportPackage(java.lang); ") + m.group(1);
        try {

            Object obj = scriptEngine.eval(source);
            if (obj != null) {
                //log("Evaled [%s] --> [%s]", source, obj);
                m.appendReplacement(ret, obj.toString());
            } else {
                m.appendReplacement(ret, "");
            }
        } catch (Exception ex) {
            ex.printStackTrace(System.err);
            throw new IllegalArgumentException("Failed to evaluate expression [" + text + "]");
        }
    }
    m.appendTail(ret);
    return ret.toString();
}

From source file:com.blocks.framework.utils.date.StringUtil.java

/**
  * ????/*  w  w w.  ja  v  a2s  . co  m*/
  * 
  * @param src
  *            String
  * @param pattern
  *            String
  * @param to
  *            String
  * @return String
  */
 public static String replaceAll(String src, String pattern, String to) {
     if (src == null) {
         return null;
     }
     if (pattern == null) {
         return src;
     }

     StringBuffer sb = new StringBuffer();
     Pattern p = Pattern.compile(pattern);
     Matcher m = p.matcher(src);

     int i = 1;
     while (m.find()) {
         // System.out.println("" + i + "?:" + m.group() +
         // " ?:" + m.start() + "-" + (m.end() - 1));
         m.appendReplacement(sb, to);
         i++;
     }
     m.appendTail(sb);
     // System.out.println("??:" + sb);
     return sb.toString();
 }

From source file:edu.sdsc.scigraph.internal.CypherUtil.java

String entailRelationships(String cypher) {
    Matcher m = pattern.matcher(cypher);
    StringBuffer buffer = new StringBuffer();
    while (m.find()) {
        String group = m.group().substring(1, m.group().length() - 1);
        Set<String> parentTypes = newHashSet(Splitter.on('|').split(group));
        String entailedTypes = on('|').join(getEntailedRelationshipTypes(parentTypes));
        m.appendReplacement(buffer, ":" + entailedTypes);
    }/*  w w w. j a  v  a2  s  .  c om*/
    m.appendTail(buffer);
    return buffer.toString();
}

From source file:nl.ivonet.epub.strategy.epub.KepubStrategy.java

@Override
public void execute(final Epub epub) {
    LOG.debug("Applying {} on [{}]", getClass().getSimpleName(), epub.getOrigionalFilename());
    if (epub.hasDropout(Dropout.CORRUPT_HTML)) {
        LOG.debug("Html corruption already detected so skipping kepub processing");
        return;/*from w w w  . j av a 2  s. co m*/
    }
    final List<Resource> htmlResources = epub.getContents().stream()
            .filter(HtmlCorruptDetectionStrategy::isHtml).collect(toList());

    int idx = 1;
    for (final Resource content : htmlResources) {
        try {
            final String html = IOUtils.toString(content.getReader());
            final Matcher matcher = PAT.matcher(html);
            final StringBuffer sb = new StringBuffer();
            while (matcher.find()) {
                final String group = matcher.group();
                if (doNotProcess(group)) {
                    continue;
                }
                matcher.appendReplacement(sb, group.replace(">", String.format(KOBO, idx)));
                idx++;
            }
            matcher.appendTail(sb);
            content.setData(sb.toString().getBytes());

        } catch (IOException e) {
            epub.addDropout(Dropout.READ_ERROR);
        } catch (IllegalArgumentException e) {
            epub.addDropout(Dropout.KEPBUB);
        }
    }
}

From source file:org.echocat.jomon.resources.optimizing.CombineCssResourcesOptimizer.java

@Nonnull
protected String handleImports(@Nonnull String oldCssBody, @Nonnull OptimizationContext context)
        throws Exception {
    final Matcher matcher = IMPORT_PATTERN.matcher(oldCssBody);
    final StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        final String uri = matcher.group(3) != null ? matcher.group(3) : matcher.group(2);
        final String importedBody = getBodyOf(uri, context);
        final String newReplacement = quoteReplacement(importedBody);
        matcher.appendReplacement(sb, newReplacement);
    }/*  w w  w  .  j  ava  2  s  .c o m*/
    matcher.appendTail(sb);
    return sb.toString();
}

From source file:com.yukthi.persistence.NativeQueryFactory.java

/**
 * Fetches the query template with specified "name" and builds the query using specified "context".
 * If the query uses "param" directive, corresponding values will be collected into "paramValues".
 * /*from   w  w w  .jav  a  2 s. c om*/
 * @param name Name of the query to be fetched
 * @param paramValues Collected param values that needs to be passed to query as prepared statement params
 * @param context Context to be used to parse template into query
 * @return Built query
 */
public String buildQuery(String name, List<Object> paramValues, Object context) {
    Template template = templateMap.get(name);

    //if template is not found build it from raw query
    if (template == null) {
        String rawQuery = queryMap.get(name);

        //if no query found with specified name
        if (rawQuery == null) {
            throw new InvalidArgumentException("No query found with specified name - {}", name);
        }

        //build the free marker template from raw query
        try {
            template = new Template(name, rawQuery, configuration);
            templateMap.put(name, template);
        } catch (Exception ex) {
            throw new InvalidStateException(ex, "An error occurred while loading query template - {}", name);
        }
    }

    try {
        //process the template into query
        StringWriter writer = new StringWriter();
        template.process(context, writer);

        writer.flush();

        //build the final query (replacing the param expressions)
        String query = writer.toString();
        StringBuffer finalQuery = new StringBuffer();
        Matcher matcher = QUERY_PARAM_PATTERN.matcher(query);
        String property = null;

        while (matcher.find()) {
            property = matcher.group(1);
            matcher.appendReplacement(finalQuery, "?");

            paramValues.add(PropertyUtils.getProperty(context, property));
        }

        matcher.appendTail(finalQuery);

        return finalQuery.toString();
    } catch (Exception ex) {
        throw new InvalidStateException(ex, "An exception occurred while building query: " + name);
    }
}

From source file:org.carewebframework.maven.plugin.theme.ZKThemeProcessor.java

/**
 * Adjust any color references using the active hue filter.
 * //from   ww w  .  j a va 2 s .  com
 * @param line The string to modify
 * @return the modified string
 */
public String replaceColor(String line) {
    StringBuffer sb = new StringBuffer();
    Matcher matcher = COLOR_PATTERN.matcher(line);

    while (matcher.find()) {
        String hexColor = matcher.group(1);
        int rgb = hueFilter.filterRGB(0, 0, Integer.parseInt(hexColor, 16));
        String transfHexColor = String.format("%06x", rgb);
        matcher.appendReplacement(sb, "#" + transfHexColor);
    }

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