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.kuali.rice.kew.impl.document.search.DocumentSearchCriteriaBoLookupableHelperService.java

protected static String replaceCurrentUserToken(String value, Person person) {
    Matcher matcher = CURRENT_USER_PATTERN.matcher(value);
    boolean matched = false;
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        matched = true;/*from   w ww  . j  a v  a  2  s  .  c  o  m*/
        String idType = "principalName";
        if (matcher.groupCount() > 0) {
            String group = matcher.group(1);
            if (group != null) {
                idType = group.substring(1); // discard period after CURRENT_USER
            }
        }
        String idValue = UserUtils.getIdValue(idType, person);
        if (!StringUtils.isBlank(idValue)) {
            value = idValue;
        } else {
            value = matcher.group();
        }
        matcher.appendReplacement(sb, value);

    }
    matcher.appendTail(sb);
    return matched ? sb.toString() : null;
}

From source file:org.codelibs.robot.extractor.impl.AbstractXmlExtractor.java

protected String extractString(final String content) {
    String input = content.replaceAll("[\\r\\n]", " ");
    if (ignoreCommentTag) {
        input = input.replaceAll("<!--[^>]+-->", "");
    } else {/*from  w w w  . j  a v  a 2 s. c  o m*/
        input = input.replace("<!--", "").replace("-->", "");
    }
    final Matcher matcher = getTagPattern().matcher(input);
    final StringBuffer sb = new StringBuffer();
    final Pattern attrPattern = Pattern.compile("\\s[^ ]+=\"([^\"]*)\"");
    while (matcher.find()) {
        final String tagStr = matcher.group();
        final Matcher attrMatcher = attrPattern.matcher(tagStr);
        final StringBuilder buf = new StringBuilder();
        while (attrMatcher.find()) {
            buf.append(attrMatcher.group(1)).append(' ');
        }
        matcher.appendReplacement(sb, buf.toString().replace("\\", "\\\\").replace("$", "\\$"));
    }
    matcher.appendTail(sb);
    return sb.toString().replaceAll("\\s+", " ").trim();
}

From source file:org.codelibs.fess.crawler.extractor.impl.AbstractXmlExtractor.java

protected String extractString(final String content) {
    String input = content.replaceAll("[\\r\\n]", " ");
    if (ignoreCommentTag) {
        input = input.replaceAll("<!--[^>]+-->", "");
    } else {/*  w  ww.  j  a va2 s  . co m*/
        input = input.replace("<!--", "").replace("-->", "");
    }
    final Matcher matcher = getTagPattern().matcher(input);
    final StringBuffer sb = new StringBuffer();
    final Pattern attrPattern = Pattern.compile("\\s[^ ]+=\"([^\"]*)\"");
    while (matcher.find()) {
        final String tagStr = matcher.group();
        final Matcher attrMatcher = attrPattern.matcher(tagStr);
        final StringBuilder buf = new StringBuilder(100);
        while (attrMatcher.find()) {
            buf.append(attrMatcher.group(1)).append(' ');
        }
        matcher.appendReplacement(sb, buf.toString().replace("\\", "\\\\").replace("$", "\\$"));
    }
    matcher.appendTail(sb);
    return sb.toString().replaceAll("\\s+", " ").trim();
}

From source file:org.akita.proxy.ProxyInvocationHandler.java

/**
 * Replace all the {} block in url to the actual params, 
 * clear the params used in {block}, return cleared params HashMap and replaced url.
 * @param url such as http://server/{namespace}/1/do
 * @param params such as hashmap include (namespace->'mobile')
 * @return the parsed param will be removed in HashMap (params)
 *//*ww  w  . java 2  s .co  m*/
private String parseUrlbyParams(String url, HashMap<String, String> params) throws AkInvokeException {

    StringBuffer sbUrl = new StringBuffer();
    Pattern pattern = Pattern.compile("\\{(.+?)\\}");
    Matcher matcher = pattern.matcher(url);

    while (matcher.find()) {
        String paramValue = params.get(matcher.group(1));
        if (paramValue != null) {
            matcher.appendReplacement(sbUrl, paramValue);
        } else { // {name}?
            throw new AkInvokeException(AkInvokeException.CODE_PARAM_IN_URL_NOT_FOUND,
                    "Parameter {" + matcher.group(1) + "}'s value not found of url " + url + ".");
        }
        params.remove(matcher.group(1));
    }
    matcher.appendTail(sbUrl);

    return sbUrl.toString();
}

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

/**
 * Rewrites the header Set-Cookie so that path and domain is correct.
 * /*from  w ww  .j  a  v a 2s.  c o m*/
 * @param value
 *            The original header
 * @return The rewritten header
 */
private String rewriteSetCookie(String value) {
    StringBuffer header = new StringBuffer();
    Matcher matcher = pathAndDomainPattern.matcher(value);
    while (matcher.find()) {
        if (matcher.group(1).equalsIgnoreCase("path=")) {
            String path = server.getRule().revert(matcher.group(2).replace(server.getPath(), contextPath)); // server.getRule().revert(matcher.group(2));
            path = "/";
            matcher.appendReplacement(header, "$1" + path + ";");
        } else {
            matcher.appendReplacement(header, "");
        }

    }
    matcher.appendTail(header);
    log.debug("Set-Cookie header rewritten \"" + value + "\" >> " + header.toString());
    return header.toString();
}

From source file:io.scigraph.internal.CypherUtil.java

public String resolveRelationships(String cypher) {
    Matcher m = ENTAILMENT_PATTERN.matcher(cypher);
    StringBuffer buffer = new StringBuffer();
    while (m.find()) {
        String varName = m.group(1);
        String types = m.group(2);
        String modifiers = m.group(3);
        Collection<String> resolvedTypes = resolveTypes(types, modifiers.contains("!"));
        modifiers = modifiers.replaceAll("!", "");
        String typeString = resolvedTypes.isEmpty() ? "" : ":`" + on("`|`").join(resolvedTypes) + "`";
        m.appendReplacement(buffer, "[" + varName + typeString + modifiers + "]");
    }//from   w  w w  .j a v  a2  s . co  m
    m.appendTail(buffer);
    return buffer.toString();
}

From source file:net.kamhon.ieagle.dao.Jpa2Dao.java

/**
 * convert hibernate positional parameter to JPA positional parameter notation.</br> For example: convert
 * <code>select a from A a where a.id=? and a.status=?</code> to
 * <code>select a from A a where a.id=?1 and a.status=?2</code>
 * /*from  www. j  ava 2 s . com*/
 * @param query
 * @return
 */
public String convertJpaPositionParams(String query) {
    if (StringUtils.isBlank(query))
        return query;

    // bypass if the query is using JPA positional parameter notation
    if (query.indexOf("?1") >= 0) {
        return query;
    } else if (query.indexOf("?") >= 0) {
        StringBuffer sb = new StringBuffer();
        Pattern p = Pattern.compile(Pattern.quote("?"), Pattern.CASE_INSENSITIVE);
        Matcher matcher = p.matcher(query);
        boolean result = matcher.find();
        int count = 0;
        while (result) {
            String g = matcher.group();
            matcher.appendReplacement(sb, g + (++count));
            result = matcher.find();
        }

        matcher.appendTail(sb);

        log.debug("sb.toString() = " + sb.toString());

        return sb.toString();
    }

    return query;
}

From source file:com.gargoylesoftware.htmlunit.javascript.IEConditionalCompilationScriptPreProcessor.java

private String replaceCustomCompilationVariables(final String body) {
    final Matcher m = CC_VARIABLE_PATTERN.matcher(body);
    final StringBuffer sb = new StringBuffer();
    while (m.find()) {
        final String match = m.group();
        String toReplace;//from  w ww. ja va2 s .c  o  m
        if (match.startsWith("@")) {
            toReplace = replaceOneCustomCompilationVariable(match);
        } else {
            toReplace = match;
        }
        toReplace = StringUtils.sanitizeForAppendReplacement(toReplace);
        m.appendReplacement(sb, toReplace);
    }
    m.appendTail(sb);
    return sb.toString();
}

From source file:com.gargoylesoftware.htmlunit.javascript.IEConditionalCompilationScriptPreProcessor.java

String replaceCompilationVariables(final String source, final BrowserVersion browserVersion) {
    final Matcher m = C_VARIABLE_PATTERN.matcher(source);
    final StringBuffer sb = new StringBuffer();
    while (m.find()) {
        final String match = m.group();
        String toReplace;//from   w w w. ja v  a 2 s .co m
        if (match.startsWith("@")) {
            toReplace = replaceOneVariable(match, browserVersion);
        } else {
            toReplace = match;
        }
        toReplace = StringUtils.sanitizeForAppendReplacement(toReplace);
        m.appendReplacement(sb, toReplace);
    }
    m.appendTail(sb);
    return sb.toString();
}

From source file:org.dd4t.core.filters.impl.DefaultLinkResolverFilter.java

protected void resolveXhtmlField(XhtmlField xhtmlField) {

    StopWatch stopWatch = null;/*from  www . java  2s  .c  o  m*/
    if (logger.isDebugEnabled()) {
        stopWatch = new StopWatch();
        stopWatch.start();
    }

    List<Object> xhtmlValues = xhtmlField.getValues();
    List<String> newValues = new ArrayList<String>();

    if (useXslt) {
        // find all component links and try to resolve them
        for (Object xhtmlValue : xhtmlValues) {
            String result = xslTransformer.transformSourceFromFilesource(
                    "<ddtmproot>" + (String) xhtmlValue + "</ddtmproot>", "/resolveXhtmlWithLinks.xslt",
                    params);
            newValues.add(XSLTPattern.matcher(result).replaceAll(""));
        }
    } else {
        // find all component links and try to resolve them
        for (Object xhtmlValue : xhtmlValues) {

            Matcher m = RegExpPattern.matcher((String) xhtmlValue);

            StringBuffer sb = new StringBuffer();
            String resolvedLink = null;
            while (m.find()) {
                resolvedLink = getLinkResolver().resolve(m.group(1));
                // if not possible to resolve the link do nothing
                if (resolvedLink != null) {
                    m.appendReplacement(sb, "href=\"" + resolvedLink + "\"");
                }
            }
            m.appendTail(sb);
            newValues.add(sb.toString());
        }

    }

    xhtmlField.setTextValues(newValues);

    if (logger.isDebugEnabled()) {
        stopWatch.stop();
        logger.debug("Parsed rich text field '" + xhtmlField.getName() + "' in "
                + stopWatch.getTotalTimeMillis() + " ms.");
    }
}