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:cc.aileron.workflow.util.WorkflowUriTemplate.java

/**
 * @param accessor/*w  w  w  .  ja  v a 2 s .  c  o m*/
 * @return ????
 * @throws PojoPropertiesNotFoundException
 * @throws PojoAccessorValueNotFoundException
 */
public String replace(final PojoAccessor<?> accessor)
        throws PojoAccessorValueNotFoundException, PojoPropertiesNotFoundException {
    final Matcher matcher = pattern.matcher(uriTemplate);
    final StringBuffer buffer = new StringBuffer();
    while (matcher.find()) {
        final String key = matcher.group(1);
        final String val = accessor.to(key).value(String.class);
        matcher.appendReplacement(buffer, val != null ? val : "");
    }
    matcher.appendTail(buffer);
    return buffer.toString();
}

From source file:it.tidalwave.northernwind.core.model.spi.ParameterLanguageOverrideLinkPostProcessor.java

/*******************************************************************************************************************
 *
 * {@inheritDoc}// ww w  .j a v a 2 s  . com
 *
 ******************************************************************************************************************/
@Nonnull
public String postProcess(final @Nonnull String link, final @Nonnull String parameterValue) {
    final String parameterName = parameterLanguageOverrideRequestProcessor.getParameterName();
    final String regexp = "([\\?&])(" + parameterName + "=[a-z,0-9]*)";

    final Matcher matcher = Pattern.compile(regexp).matcher(link);

    if (matcher.find()) // replace a parameter already present
    {
        final StringBuffer buffer = new StringBuffer();
        matcher.appendReplacement(buffer, matcher.group(1) + parameterName + "=" + parameterValue);
        matcher.appendTail(buffer);

        return buffer.toString();
    }

    final StringBuilder builder = new StringBuilder(link);

    if (link.contains("?")) {
        builder.append("&");
    } else {
        if (!builder.toString().endsWith("/") && !builder.toString().contains(".")) // FIXME: check . only in trailing
        {
            builder.append("/");
        }

        builder.append("?");
    }

    builder.append(parameterName).append("=").append(parameterValue);

    return builder.toString();
}

From source file:com.github.jramos.snowplow.RedshiftSink.java

private String resolveEnvVars(String input) {
    if (null == input) {
        return null;
    }/* ww  w  .  j  av  a  2s  . c om*/
    Pattern p = Pattern.compile("\\$\\{(\\w+)\\}|\\$(\\w+)"); // match ${ENV_VAR_NAME} or $ENV_VAR_NAME
    Matcher m = p.matcher(input);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        String envVarName = null == m.group(1) ? m.group(2) : m.group(1);
        String envVarValue = System.getenv(envVarName);
        m.appendReplacement(sb, null == envVarValue ? "" : envVarValue);
    }
    m.appendTail(sb);
    return sb.toString();
}

From source file:org.adidac.io.EmbededOutputStream.java

/** Flush the internal buffer */
private void flushBuffer() throws IOException {
    String s = new String(buf, 0, count); // Uses system encoding.
    Matcher m = EMBED_PATTERN.matcher(s);
    String fileName;//from  ww w .ja  v a 2s  .  c  om
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        fileName = m.group(1);
        String content = getContent(fileName);
        m.appendReplacement(sb, content);
    }
    m.appendTail(sb);
    out.write(sb.toString().getBytes());
    count = 0;
}

From source file:nl.strohalm.cyclos.utils.database.DatabaseQueryHandler.java

/**
 * Returns an HQL query without the fetch part
 *///from w w  w  . j  a  v  a  2s  .  c o m
private static String stripFetch(String hql) {
    // This is done so we don't confuse the matcher, i.e.: from X x left join fetch x.a *left* join fetch ... -> that *left* could be an alias
    hql = hql.replaceAll("left join", "^left join");

    Matcher matcher = LEFT_JOIN_FETCH.matcher(hql);
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        String path = StringUtils.trimToEmpty(matcher.group(1));
        String alias = StringUtils.trimToEmpty(matcher.group(2));
        boolean nextIsLeft = "left".equalsIgnoreCase(alias);
        boolean safeToRemove = alias.isEmpty() || nextIsLeft || "where".equalsIgnoreCase(alias)
                || "order".equalsIgnoreCase(alias) || "group".equalsIgnoreCase(alias);
        String replacement;
        if (safeToRemove) {
            // No alias - we can just remove the entire left join fetch
            replacement = nextIsLeft ? "" : " " + alias;
        } else {
            // Just remove the 'fetch'
            replacement = " left join " + path + " " + alias;
        }
        matcher.appendReplacement(sb, replacement);
    }
    matcher.appendTail(sb);
    return sb.toString().replaceAll("\\^left join", "left join");
}

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

/**
 * Removes watermarks from title attributes and replaces some strings
 *//*from   ww w .j  a v  a 2  s  .co m*/
private String removeWatermark2(final String html) {
    final Matcher matcher = WATERMARK_PAT_2.matcher(html);
    final StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        LOG.debug("Watermark = [{}]", matcher.group());
        matcher.appendReplacement(sb, "title=\"Possible watermark removed\""); //you can put any text here
    }
    matcher.appendTail(sb);
    return sb.toString();
}

From source file:org.dd4t.core.resolvers.impl.DefaultLinkResolver.java

private String replacePlaceholders(String resolvedUrl, String placeholder, String replacementText) {
    StringBuffer sb = new StringBuffer();
    if (!StringUtils.isEmpty(replacementText)) {
        if (getEncodeUrl()) {
            try {
                replacementText = URLEncoder.encode(replacementText, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                LOG.warn("Not possible to encode string: " + replacementText, e);
                return "";
            }/*from ww w. j  a va2s.  c o  m*/
        }

        Pattern p = Pattern.compile(placeholder);
        Matcher m = p.matcher(resolvedUrl);

        while (m.find()) {
            m.appendReplacement(sb, replacementText);
        }
        m.appendTail(sb);
    }
    return sb.toString();
}

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

@Override
public String replaceQueryParams(String queryString, Map<String, Object> params) {
    Matcher matcher = MACRO_PATTERN.matcher(queryString);
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        String macros = matcher.group(0);
        macros = replaceParamsInMacros(macros, params);
        matcher.appendReplacement(sb, macros);
    }/* w  w  w.  j  a va  2  s . co  m*/
    matcher.appendTail(sb);
    return sb.toString();
}

From source file:dk.dma.msinm.common.time.TimeParser.java

/**
 * Parse the time description into its XML representation
 * @param time the time description to parse
 * @return the result/*  w w  w.  j a va  2s  . com*/
 */
protected String parse(String time) throws TimeException {
    String monthMatch = "(" + Arrays.asList(months).stream().collect(Collectors.joining("|")) + ")";
    String seasonMatch = "(" + Arrays.asList(seasons).stream().collect(Collectors.joining("|")) + ")";
    String dayMatch = "(\\\\d{1,2})";
    String yearMatch = "(\\\\d{4})";
    String hourMatch = "(\\\\d{4})";
    String weekMatch = "(\\\\d{1,2})";

    BufferedReader reader = new BufferedReader(new StringReader(time));
    String line;
    StringBuilder result = new StringBuilder();
    try {
        while ((line = reader.readLine()) != null) {
            line = line.trim().toLowerCase();

            // Replace according to replace rules
            for (String key : rewriteRules.keySet()) {
                String value = rewriteRules.get(key);

                String from = key;
                from = from.replaceAll("\\$month", monthMatch);
                from = from.replaceAll("\\$season", seasonMatch);
                from = from.replaceAll("\\$date", dayMatch);
                from = from.replaceAll("\\$year", yearMatch);
                from = from.replaceAll("\\$hour", hourMatch);
                from = from.replaceAll("\\$week", weekMatch);

                Matcher m = Pattern.compile(from).matcher(line);
                StringBuffer sb = new StringBuffer();
                while (m.find()) {
                    String text = m.group();
                    m.appendReplacement(sb, value);
                }
                m.appendTail(sb);
                line = sb.toString();
            }
            result.append(line + "\n");
        }
    } catch (Exception e) {
        throw new TimeException("Failed converting time description into XML", e);
    }
    return "<time-result>" + result.toString() + "</time-result>";
}

From source file:com.gh4a.utils.HtmlUtils.java

/**
 * Rewrite relative URLs in HTML fetched e.g. from markdown files.
 *
 * @param html/*from w ww.  ja v  a2  s . c  o  m*/
 * @param repoUser
 * @param repoName
 * @param branch
 * @return
 */
public static String rewriteRelativeUrls(final String html, final String repoUser, final String repoName,
        final String branch) {
    final String baseUrl = "https://raw.github.com/" + repoUser + "/" + repoName + "/" + branch;
    final StringBuffer sb = new StringBuffer();
    final Pattern p = Pattern.compile("(href|src)=\"(\\S+)\"");
    final Matcher m = p.matcher(html);

    while (m.find()) {
        String url = m.group(2);
        if (!url.contains("://") && !url.startsWith("#")) {
            if (url.startsWith("/")) {
                url = baseUrl + url;
            } else {
                url = baseUrl + "/" + url;
            }
        }
        m.appendReplacement(sb, Matcher.quoteReplacement(m.group(1) + "=\"" + url + "\""));
    }
    m.appendTail(sb);

    return sb.toString();
}