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.adobe.acs.commons.rewriter.impl.StaticReferenceRewriteTransformerFactory.java

private String handleMatchingPatternAttribute(Pattern pattern, String attrValue) {
    String unescapedValue = StringEscapeUtils.unescapeHtml(attrValue);
    Matcher m = pattern.matcher(unescapedValue);
    StringBuffer sb = new StringBuffer(unescapedValue.length());

    while (m.find()) {
        String url = m.group(1);//www  .j a va  2s  . c o m
        for (String prefix : prefixes) {
            if (url.startsWith(prefix)) {
                // prepend host
                url = prependHostName(url);
                m.appendReplacement(sb, Matcher.quoteReplacement(url));
                // First prefix match wins
                break;
            }
        }

    }
    m.appendTail(sb);

    return sb.toString();
}

From source file:org.ballerinalang.composer.server.launcher.log.LogManagerUtils.java

/**
 * Substituting environment variables./*from  ww w  . j av  a  2s.  c  o m*/
 * @param value The value to be replaced
 * @return The updated value.
 */
private String substituteVariables(String value) {
    Matcher matcher = ENV_VAR_PATTERN.matcher(value);
    boolean found = matcher.find();
    if (!found) {
        return value;
    }
    StringBuffer buffer = new StringBuffer();
    do {
        String sysPropertyKey = matcher.group(1);
        String sysPropertyValue = getSystemVariableValue(sysPropertyKey);
        if (sysPropertyValue != null && !sysPropertyValue.isEmpty()) {
            sysPropertyValue = sysPropertyValue.replace("\\", "\\\\");
            matcher.appendReplacement(buffer, sysPropertyValue);
        }
    } while (matcher.find());
    matcher.appendTail(buffer);
    return buffer.toString();
}

From source file:com.haulmont.cuba.core.sys.AbstractScripting.java

protected Script createScript(String text) {
    StringBuilder sb = new StringBuilder();
    for (String importItem : imports) {
        sb.append("import ").append(importItem).append("\n");
    }/*w w  w.  j a  va  2  s.  co m*/

    Matcher matcher = IMPORT_PATTERN.matcher(text);
    String result;
    if (matcher.find()) {
        StringBuffer s = new StringBuffer();
        matcher.appendReplacement(s, sb + "$0");
        result = matcher.appendTail(s).toString();
    } else {
        Matcher packageMatcher = PACKAGE_PATTERN.matcher(text);
        if (packageMatcher.find()) {
            StringBuffer s = new StringBuffer();
            packageMatcher.appendReplacement(s, "$0\n" + sb);
            result = packageMatcher.appendTail(s).toString();
        } else {
            result = sb.append(text).toString();
        }
    }

    CompilerConfiguration cc = new CompilerConfiguration();
    cc.setClasspath(groovyClassPath);
    cc.setRecompileGroovySource(true);
    GroovyShell shell = new GroovyShell(javaClassLoader, new Binding(), cc);
    //noinspection UnnecessaryLocalVariable
    Script script = shell.parse(result);
    return script;
}

From source file:org.fao.unredd.portal.ApplicationController.java

private String setLayerTimes() {
    String jsonLayers = config.getLayers();
    Pattern patt = Pattern.compile("\\$\\{time\\.([\\w.]*)\\}");
    Matcher m = patt.matcher(jsonLayers);
    StringBuffer sb = new StringBuffer(jsonLayers.length());
    while (m.find()) { // Found time-dependant layer in json file
        String layerName = m.group(1);
        try {//from w w w .j  a  v a  2 s . c  om
            m.appendReplacement(sb, getLayerTimesFromGeostore(layerName));
        } catch (Exception e) {
            m.appendReplacement(sb, "");
            logger.error("Error getting layer times from GeoStore.");
        }
    }
    m.appendTail(sb);
    return sb.toString();
}

From source file:org.encuestame.core.util.HTMLInputFilter.java

protected String escapeComments(String string) {
    final Pattern pattern = Pattern.compile("<!--(.*?)-->", Pattern.DOTALL);
    final Matcher matcher = pattern.matcher(string);
    StringBuffer buf = new StringBuffer();
    if (matcher.find()) {
        String match = matcher.group(1); // (.*?)
        matcher.appendReplacement(buf, "<!--" + htmlSpecialChars(match) + "-->");
    }/* w ww. j  a v a2s  . c  om*/
    matcher.appendTail(buf);

    return buf.toString();
}

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

protected String escapeComments(String s) {
    Pattern p = Pattern.compile("<!--(.*?)-->", Pattern.DOTALL);
    Matcher m = p.matcher(s);
    StringBuffer buf = new StringBuffer();
    if (m.find()) {
        String match = m.group(1); // (.*?)
        m.appendReplacement(buf, "<!--" + htmlSpecialChars(match) + "-->");
    }/*w ww . j a va 2s.c  o m*/
    m.appendTail(buf);

    return buf.toString();
}

From source file:org.opencms.i18n.CmsEncoder.java

/**
 * Decodes HTML entity references like <code>&amp;#8364;</code> that are contained in the 
 * String to a regular character, but only if that character is contained in the given 
 * encodings charset.<p> /*from   ww  w . jav a2  s .c  o m*/
 * 
 * @param input the input to decode the HTML entities in
 * @param encoding the charset to decode the input for
 * @return the input with the decoded HTML entities
 * 
 * @see #encodeHtmlEntities(String, String)
 */
public static String decodeHtmlEntities(String input, String encoding) {

    Matcher matcher = ENTITIY_PATTERN.matcher(input);
    StringBuffer result = new StringBuffer(input.length());
    Charset charset = Charset.forName(encoding);
    CharsetEncoder encoder = charset.newEncoder();

    while (matcher.find()) {
        String entity = matcher.group();
        String value = entity.substring(2, entity.length() - 1);
        int c = Integer.valueOf(value).intValue();
        if (c < 128) {
            // first 128 chars are contained in almost every charset
            entity = new String(new char[] { (char) c });
            // this is intended as performance improvement since 
            // the canEncode() operation appears quite CPU heavy
        } else if (encoder.canEncode((char) c)) {
            // encoder can encode this char
            entity = new String(new char[] { (char) c });
        }
        matcher.appendReplacement(result, entity);
    }
    matcher.appendTail(result);
    return result.toString();
}

From source file:org.mule.transport.jdbc.JdbcConnector.java

/**
 * Parse the given statement filling the parameter list and return the ready to use statement.
 * /*w  ww  . j  a  va2s  .  c o  m*/
 * @param stmt
 * @param params
 */
public String parseStatement(String stmt, List params) {
    if (stmt == null) {
        return stmt;
    }
    Matcher m = STATEMENT_ARGS.matcher(stmt);
    StringBuffer sb = new StringBuffer(200);
    while (m.find()) {
        String key = m.group();
        m.appendReplacement(sb, "?");
        // Special legacy handling for #[payload]
        if (key.equals("#[payload]")) {
            // MULE-3597
            logger.error(
                    "invalid expression template #[payload]. It should be replaced with #[payload:] to conform with the correct expression syntax. Mule has replaced this for you, but may not in future versions.");
            key = "#[payload:]";
        }
        params.add(key);
    }
    m.appendTail(sb);
    return sb.toString();
}

From source file:org.olat.modules.fo.archiver.formatters.ForumRTFFormatter.java

/**
 * @param originalText/*w ww. j a v  a  2  s.c o m*/
 * @return
 */
private String convertHTMLMarkupToRTF(final String originalText) {
    String htmlText = originalText;

    final Matcher mb = PATTERN_HTML_BOLD.matcher(htmlText);
    final StringBuffer bolds = new StringBuffer();
    while (mb.find()) {
        mb.appendReplacement(bolds, "{\\\\b $1} ");
    }
    mb.appendTail(bolds);
    htmlText = bolds.toString();

    final Matcher mi = PATTERN_HTML_ITALIC.matcher(htmlText);
    final StringBuffer italics = new StringBuffer();
    while (mi.find()) {
        mi.appendReplacement(italics, "{\\\\i $1} ");
    }
    mi.appendTail(italics);
    htmlText = italics.toString();

    final Matcher mbr = PATTERN_HTML_BREAK.matcher(htmlText);
    final StringBuffer breaks = new StringBuffer();
    while (mbr.find()) {
        mbr.appendReplacement(breaks, "\\\\line ");
    }
    mbr.appendTail(breaks);
    htmlText = breaks.toString();

    final Matcher mofo = PATTERN_CSS_O_FOQUOTE.matcher(htmlText);
    final StringBuffer foquotes = new StringBuffer();
    while (mofo.find()) {
        mofo.appendReplacement(foquotes, "\\\\line {\\\\i $1} {\\\\pard $2\\\\par}");
    }
    mofo.appendTail(foquotes);
    htmlText = foquotes.toString();

    final Matcher mp = PATTERN_HTML_PARAGRAPH.matcher(htmlText);
    final StringBuffer paragraphs = new StringBuffer();
    while (mp.find()) {
        mp.appendReplacement(paragraphs, "\\\\line $1 \\\\line");
    }
    mp.appendTail(paragraphs);
    htmlText = paragraphs.toString();

    final Matcher mahref = PATTERN_HTML_AHREF.matcher(htmlText);
    final StringBuffer ahrefs = new StringBuffer();
    while (mahref.find()) {
        mahref.appendReplacement(ahrefs,
                "{\\\\field{\\\\*\\\\fldinst{HYPERLINK\"$1\"}}{\\\\fldrslt{\\\\ul $2}}}");
    }
    mahref.appendTail(ahrefs);
    htmlText = ahrefs.toString();

    final Matcher mli = PATTERN_HTML_LIST.matcher(htmlText);
    final StringBuffer lists = new StringBuffer();
    while (mli.find()) {
        mli.appendReplacement(lists, "$1\\\\line ");
    }
    mli.appendTail(lists);
    htmlText = lists.toString();

    final Matcher mtp = PATTERN_THREEPOINTS.matcher(htmlText);
    final StringBuffer tps = new StringBuffer();
    while (mtp.find()) {
        mtp.appendReplacement(tps, THREEPOINTS);
    }
    mtp.appendTail(tps);
    htmlText = tps.toString();

    // strip all other html-fragments, because not convertable that easy
    htmlText = FilterFactory.getHtmlTagsFilter().filter(htmlText);
    // Remove all &nbsp;
    final Matcher tmp = HTML_SPACE_PATTERN.matcher(htmlText);
    htmlText = tmp.replaceAll(" ");
    htmlText = StringEscapeUtils.unescapeHtml(htmlText);

    return htmlText;
}

From source file:io.github.swagger2markup.markup.builder.internal.confluenceMarkup.ConfluenceMarkupBuilder.java

private String escapeCellPipes(String cell) {
    Matcher m = ESCAPE_CELL_PIPE_PATTERN.matcher(cell);

    StringBuffer res = new StringBuffer();
    while (m.find()) {
        String repl = m.group(1);
        if (repl.equals(ConfluenceMarkup.TABLE_COLUMN_DELIMITER.toString()))
            repl = "\\" + ConfluenceMarkup.TABLE_COLUMN_DELIMITER.toString();
        m.appendReplacement(res, Matcher.quoteReplacement(repl));
    }/*from  w w  w  .j  av  a 2 s  . co  m*/
    m.appendTail(res);

    return res.toString();
}