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:org.apache.roller.weblogger.business.plugins.entry.TopicTagPlugin.java

/**
 * Apply the plugin to the given entry.  Returns the entry text with topic tags expanded.
 *
 * @param entry           WeblogEntry to which plugin should be applied.
 * @param singleEntry     Ignored.//w  w w . ja  v  a 2  s . c  o  m
 * @return Results of applying plugin to entry.
 */
public String render(WeblogEntry entry, String str) {
    String entryText = str;
    StringBuffer result = new StringBuffer(entryText.length());
    MessageFormat fmt = getLinkFormat();

    // Replace all of the instances matching the pattern with bookmark specified.
    Matcher m = tagPatternWithBookmark.matcher(entryText);
    while (m.find()) {
        String bookmark = m.group(1);
        String tag = m.group(2);
        String site = getBookmarkSite(bookmark);
        if (site == null) {
            site = getDefaultTopicSite();
        }
        if (!site.endsWith("/")) {
            site += "/";
        }
        String link = generateLink(fmt, site, tag);
        m.appendReplacement(result, link);
    }
    m.appendTail(result);

    // Now, in a second phase replace all of the instances matching the pattern without bookmark specified.
    entryText = result.toString();
    result = new StringBuffer(entryText.length());
    m = tagPatternWithoutBookmark.matcher(entryText);
    while (m.find()) {
        String tag = m.group(1);
        String site = getDefaultTopicSite();
        String link = generateLink(fmt, site, tag);
        m.appendReplacement(result, link);
    }
    m.appendTail(result);

    return result.toString();
}

From source file:com.lcw.one.common.persistence.BaseDao.java

/** 
 * hqlorderBy?? //w w  w. j a v  a2  s  . c o  m
 * @param qlString
 * @return 
 */
private String removeOrders(String qlString) {
    Pattern p = Pattern.compile("order\\s*by[\\w|\\W|\\s|\\S]*", Pattern.CASE_INSENSITIVE);
    Matcher m = p.matcher(qlString);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(sb, "");
    }
    m.appendTail(sb);
    return sb.toString();
}

From source file:org.remus.widgets.list.RichtextList.java

private String replace(String listElementWithImage, ListElement listElement) {
    Matcher m = Pattern.compile("\\[(.*?)\\]").matcher(listElementWithImage);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {

        // What to replace
        String toReplace = m.group(1);

        // New value to insert
        if (providers.containsKey(toReplace)) {

            String toInsert = providers.get(toReplace).getStringFromObj(listElement);

            // Append replaced match.
            m.appendReplacement(sb, "" + toInsert);
        }/*from   ww w  .j a v a2  s.c om*/

    }
    m.appendTail(sb);

    return sb.toString();
}

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.  j av a2 s.  c  o  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:org.opencms.util.CmsStringUtil.java

/**
 * Substitutes a pattern in a string using a {@link I_CmsRegexSubstitution}.<p>
 * /*www .j  av  a2 s.  c o  m*/
 * @param pattern the pattern to substitute 
 * @param text the text in which the pattern should be substituted 
 * @param sub the substitution handler 
 * 
 * @return the transformed string 
 */
public static String substitute(Pattern pattern, String text, I_CmsRegexSubstitution sub) {

    StringBuffer buffer = new StringBuffer();
    Matcher matcher = pattern.matcher(text);
    while (matcher.find()) {
        matcher.appendReplacement(buffer, sub.substituteMatch(text, matcher));
    }
    matcher.appendTail(buffer);
    return buffer.toString();
}

From source file:org.opencastproject.composer.gstreamer.AbstractGSEncoderEngine.java

/**
 * Substitutes template values from template with actual values from properties.
 * // w  w w  .j  a  v a 2  s  . c  o m
 * @param template
 *          String that represents template
 * @param properties
 *          Map that contains substitution for template values in template
 * @param cleanup
 *          if template values that were not matched should be removed
 * @return String built from template
 */
protected String substituteTemplateValues(String template, Map<String, String> properties, boolean cleanup) {

    StringBuffer buffer = new StringBuffer();
    Pattern pattern = Pattern.compile("#\\{\\S+?\\}");
    Matcher matcher = pattern.matcher(template);
    while (matcher.find()) {
        String match = template.substring(matcher.start() + 2, matcher.end() - 1);
        if (properties.containsKey(match)) {
            matcher.appendReplacement(buffer, properties.get(match));
        }
    }
    matcher.appendTail(buffer);

    String processedTemplate = buffer.toString();

    if (cleanup) {
        // remove all property matches
        buffer = new StringBuffer();
        Pattern ppattern = Pattern.compile("\\S+?=#\\{\\S+?\\}");
        matcher = ppattern.matcher(processedTemplate);
        while (matcher.find()) {
            matcher.appendReplacement(buffer, "");
        }
        matcher.appendTail(buffer);
        processedTemplate = buffer.toString();

        // remove all other templates
        buffer = new StringBuffer();
        matcher = pattern.matcher(processedTemplate);
        while (matcher.find()) {
            matcher.appendReplacement(buffer, "");
        }
        matcher.appendTail(buffer);
        processedTemplate = buffer.toString();
    }

    return processedTemplate;
}

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

/**
 * hql order by,?/* w  w  w . j av  a2s  . c om*/
 * 
 * @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:com.google.livingstories.servlet.DataImportServlet.java

private String doMatch(String content, Pattern pattern) {
    Matcher matcher = pattern.matcher(content);
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        String id = matcher.group(2);
        if (contentMap.containsKey(id)) {
            matcher.appendReplacement(sb, "$1" + contentMap.get(id).getId());
        } else {//  w ww  . ja  v a2 s .  c  o m
            matcher.appendReplacement(sb, "$0");
        }
    }
    matcher.appendTail(sb);
    return sb.toString();
}

From source file:net.sf.j2ep.UrlRewritingResponseWrapper.java

/**
 * Rewrites the location header. Will first locate any links in the header
 * and then rewrite them.//from   w w  w . ja  v a  2 s. co m
 * 
 * @param value
 *            The header value we are to rewrite
 * @return A rewritten header
 */
private String rewriteLocation(String value) {
    StringBuffer header = new StringBuffer();

    Matcher matcher = linkPattern.matcher(value);
    while (matcher.find()) {

        String link = matcher.group(3).replaceAll("\\$", "\\\\$");
        if (link.length() == 0) {
            link = "/";
        }
        String location = matcher.group(2) + link;

        Server matchingServer = serverChain.getServerMapped(location);
        if (matchingServer != null) {
            link = link.substring(matchingServer.getPath().length());
            link = matchingServer.getRule().revert(link);
            matcher.appendReplacement(header, "$1" + ownHostName + contextPath + link);
        }
    }
    matcher.appendTail(header);
    log.debug("Location header rewritten " + value + " >> " + header.toString());
    return header.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   w  ww .  ja v a2s.co m
    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());
}