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.norconex.commons.lang.url.URLNormalizer.java

/**
 * Converts letters in URL-encoded escape sequences to upper case.<p>
 * <code>http://www.example.com/a%c2%b1b &rarr; 
 *       http://www.example.com/a%C2%B1b</code>
 * @return this instance//ww w  .  j  a  va2s. c o  m
 */
public URLNormalizer upperCaseEscapeSequence() {
    if (url.contains("%")) {
        StringBuffer sb = new StringBuffer();
        Matcher m = PATTERN_PERCENT_ENCODED_CHAR.matcher(url);
        while (m.find()) {
            m.appendReplacement(sb, m.group(1).toUpperCase());
        }
        url = m.appendTail(sb).toString();
    }
    return this;
}

From source file:org.freeplane.features.export.mindmapmode.ExportWithXSLT.java

String getProperty(final String key) {
    final String property = getProperty(key, null);
    if (property == null)
        return property;
    Matcher r = propertyReferenceEpression.matcher(property);
    r.reset();//from w w  w.java  2s .c  o  m
    boolean result = r.find();
    if (result) {
        StringBuffer sb = new StringBuffer();
        do {
            String propertyReference = r.group();
            String propertyName = propertyReference.substring(2, propertyReference.length() - 1);
            r.appendReplacement(sb, System.getProperty(propertyName, propertyReference));
            result = r.find();
        } while (result);
        r.appendTail(sb);
        return sb.toString();
    }
    return property;
}

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 ww w .  ja v a2 s .com
        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 {/*from   w  w  w .  ja va 2 s .  com*/
        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.encuestame.core.util.HTMLInputFilter.java

protected String decodeEntities(String string) {
    StringBuffer buf = new StringBuffer();

    Pattern pattern = Pattern.compile("&#(\\d+);?");
    Matcher matcher = pattern.matcher(string);
    while (matcher.find()) {
        String match = matcher.group(1);
        int decimal = Integer.decode(match).intValue();
        matcher.appendReplacement(buf, chr(decimal));
    }/*w  ww  .  j a va  2s  . c o  m*/
    matcher.appendTail(buf);
    string = buf.toString();

    buf = new StringBuffer();
    pattern = Pattern.compile("&#x([0-9a-f]+);?");
    matcher = pattern.matcher(string);
    while (matcher.find()) {
        String match = matcher.group(1);
        int decimal = Integer.decode(match).intValue();
        matcher.appendReplacement(buf, chr(decimal));
    }
    matcher.appendTail(buf);
    string = buf.toString();

    buf = new StringBuffer();
    pattern = Pattern.compile("%([0-9a-f]{2});?");
    matcher = pattern.matcher(string);
    while (matcher.find()) {
        String match = matcher.group(1);
        int decimal = Integer.decode(match).intValue();
        matcher.appendReplacement(buf, chr(decimal));
    }
    matcher.appendTail(buf);
    string = buf.toString();

    string = validateEntities(string);
    return string;
}

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 w ww.ja va  2 s.  c  o m*/
 * @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.gs.obevo.db.apps.reveng.AquaRevengMain.java

private MutableList<File> preprocessSchemaTokens(MutableList<File> files, String dbSchema,
        final File interimFolder, DbPlatform dbPlatform) {
    String schemaSeparatorRegex = dbPlatform.getSchemaSeparator().replace(".", "\\.");
    if (schemaSeparatorRegex.equals("\\.\\.")) {
        schemaSeparatorRegex = "\\.(?:dbo)?\\."; // adding DBO to help w/ Sybase ASE; we should make this code more polymorphic
    }// ww w  .  jav  a  2s. c  o m
    final Pattern dbSchemaPattern = Pattern
            .compile(String.format("(?i)%1$s%2$s(\\w+)", dbSchema, schemaSeparatorRegex));
    return files.collect(new Function<File, File>() {
        @Override
        public File valueOf(File file) {
            String fileContent = FileUtilsCobra.readFileToString(file);
            final Matcher matcher = dbSchemaPattern.matcher(fileContent);
            StringBuffer sb = new StringBuffer(fileContent.length());

            while (matcher.find()) {
                matcher.appendReplacement(sb, matcher.group(1));
            }
            matcher.appendTail(sb);

            File tempFile = new File(interimFolder, file.getName());
            FileUtilsCobra.writeStringToFile(tempFile, sb.toString());
            return tempFile;
        }
    });
}

From source file:com.google.apigee.edgecallouts.HashcashCallout.java

private String resolvePropertyValue(String spec, MessageContext msgCtxt) {
    Matcher matcher = variableReferencePattern.matcher(spec);
    StringBuffer sb = new StringBuffer();
    // System.out.printf("resolvePropertyValue spec(%s)\n", spec);
    while (matcher.find()) {

        // System.out.printf("resolvePropertyValue matcher: (%s) (%s) (%s)\n",
        //                   matcher.group(1),
        //                   matcher.group(2),
        //                   matcher.group(3));

        matcher.appendReplacement(sb, "");
        sb.append(matcher.group(1));/*from  w w w. j  a va2 s .c  o  m*/
        sb.append((String) msgCtxt.getVariable(matcher.group(2)));
        sb.append(matcher.group(3));
    }
    matcher.appendTail(sb);

    //System.out.printf("resolvePropertyValue result: (%s)\n", sb.toString());

    return sb.toString();
}

From source file:com.gewara.util.XSSFilter.java

protected String decodeEntities(String s) {
    StringBuffer buf = new StringBuffer();

    Pattern p = Pattern.compile("&#(\\d+);?");
    Matcher m = p.matcher(s);
    while (m.find()) {
        String match = m.group(1);
        int decimal = Integer.decode(match).intValue();
        m.appendReplacement(buf, chr(decimal));
    }//from   w  w w .j av  a2  s  . c  om
    m.appendTail(buf);
    s = buf.toString();

    buf = new StringBuffer();
    p = Pattern.compile("&#x([0-9a-f]+);?");
    m = p.matcher(s);
    while (m.find()) {
        String match = m.group(1);
        int decimal = Integer.decode(match).intValue();
        m.appendReplacement(buf, chr(decimal));
    }
    m.appendTail(buf);
    s = buf.toString();

    buf = new StringBuffer();
    p = Pattern.compile("%([0-9a-f]{2});?");
    m = p.matcher(s);
    while (m.find()) {
        String match = m.group(1);
        int decimal = Integer.decode(match).intValue();
        m.appendReplacement(buf, chr(decimal));
    }
    m.appendTail(buf);
    s = buf.toString();

    s = validateEntities(s);
    return s;
}

From source file:org.forgerock.openidm.repo.orientdb.impl.query.TokenHandler.java

/**
 * Replaces a query string with tokens of format ${token-name} with the 
 * token format in OrientDB, which is of the form :token-name.
 * /*from www. ja v a  2  s.co m*/
 * OrientDB tokens has some limitations, e.g. they can currently only be used
 * in the where clause, and hence the returned string is not guaranteed to be
 * valid for use in a prepared statement. If the parsing fails the system may
 * have to fall back onto non-prepared statements and manual token replacement.
 * 
 * @param queryString the query with OpenIDM format tokens ${token}
 * @return the query with all tokens replaced with the OrientDB style tokens :token
 * @throws PrepareNotSupported if this method knows a given statement can not be converted into a prepared statement.
 * That a statement was not rejected here though does not mean it could not fail during the parsing phase later.
 */
String replaceTokensWithOrientToken(String queryString) throws PrepareNotSupported {
    Matcher matcher = tokenPattern.matcher(queryString);
    StringBuffer buf = new StringBuffer();
    while (matcher.find()) {
        String origToken = matcher.group(1);

        String tokenKey = origToken;
        String tokenPrefix = null;
        String[] tokenKeyParts = tokenKey.split(":", 2);
        // if prefix found
        if (tokenKeyParts.length == 2) {
            tokenPrefix = tokenKeyParts[0];
            tokenKey = tokenKeyParts[1];
        }
        matcher.appendReplacement(buf, "");
        if (tokenPrefix != null && tokenPrefix.equals(PREFIX_DOTNOTATION)) {
            buf.append(JSON_POINTER_TO_DOT_NOTATION.apply(tokenKey));
        } else if (tokenKey != null && tokenKey.length() > 0) {
            // OrientDB token is of format :token-name
            String newToken = ":" + tokenKey;
            buf.append(newToken);
        }
    }
    matcher.appendTail(buf);
    return buf.toString();
}