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:framework.retrieval.engine.common.RetrievalUtil.java

/**
 * HTML?/*from  www .j  av a  2s  .co  m*/
 * @param htmlContent
 * @param charsetName
 * @return
 */
public static String parseHTML(String htmlContent, String charsetName) {
    if (null == htmlContent || "".equals(htmlContent.trim())) {
        return htmlContent;
    }

    StringBuffer txt = new StringBuffer();
    Pattern pattern = Pattern.compile("<[^<|^>]*>", Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(htmlContent);
    while (matcher.find()) {
        String group = matcher.group();
        if (group.matches("<[\\s]*>")) {
            matcher.appendReplacement(txt, group);
        } else {
            matcher.appendReplacement(txt, "");
        }
    }

    matcher.appendTail(txt);
    String str = txt.toString();

    return str;
}

From source file:org.neo4j.browser.CannedCypherExecutionTest.java

private static String replaceAngularExpressions(String statement) {
    Pattern angularExpressionPattern = Pattern.compile("\\{\\{(.*?)}}");
    Matcher matcher = angularExpressionPattern.matcher(statement);

    StringBuffer buffer = new StringBuffer();
    while (matcher.find()) {
        String expression = matcher.group(1);
        matcher.appendReplacement(buffer, chooseSuitableExpressionValue(expression));
    }/*from  www.  j  a  va  2 s  .  c o m*/
    matcher.appendTail(buffer);
    return buffer.toString();
}

From source file:org.bibsonomy.util.tex.TexDecode.java

/**
 * Decodes a String which contains TeX macros into it's Unicode
 * representation.//from www  .  ja v a  2  s . c  o m
 * 
 * @param s
 * @return Unicode representation of the String
 */
public static String decode(String s) {
    if (s != null) {
        final Matcher texRegexpMatcher = texRegexpPattern.matcher(s.trim().replaceAll(CURLY_BRACKETS, ""));
        final StringBuffer sb = new StringBuffer();
        int i = 0;
        while (texRegexpMatcher.find()) {
            i++;
            texRegexpMatcher.appendReplacement(sb, texMap.get(texRegexpMatcher.group()));
        }
        texRegexpMatcher.appendTail(sb);
        return sb.toString().trim().replaceAll(BRACKETS, "");
    }
    return "";
}

From source file:org.nuxeo.ecm.rating.operations.MostLiked.java

protected static String replaceURLsByLinks(String message) {
    String escapedMessage = StringEscapeUtils.escapeHtml(message);
    Matcher m = HTTP_URL_PATTERN.matcher(escapedMessage);
    StringBuffer sb = new StringBuffer(escapedMessage.length());
    while (m.find()) {
        String url = m.group(1);/*from   w  w w. j a v a 2s. c  om*/
        m.appendReplacement(sb, computeLinkFor(url));
    }
    m.appendTail(sb);
    return sb.toString();
}

From source file:Main.java

private static String renderEmail(String bodyHtml) {
    StringBuffer bodyStringBuffer = new StringBuffer();

    Matcher matcherEmailContent = patternTagTitle.matcher(bodyHtml);
    while (matcherEmailContent.find()) {

        String processContent = matcherEmailContent.group(1);
        String htmlContent = matcherEmailContent.group(2);

        if (htmlContent.equalsIgnoreCase("</script>") || htmlContent.equalsIgnoreCase("</a>")) {
            matcherEmailContent.appendReplacement(bodyStringBuffer, processContent + htmlContent);
        } else {//from  w  w w  .  j  a  v a2 s  . c om
            String emailContentHasProcessed = makeEmailHerf(processContent);
            matcherEmailContent.appendReplacement(bodyStringBuffer, emailContentHasProcessed + htmlContent);
        }

    }
    matcherEmailContent.appendTail(bodyStringBuffer);
    return bodyStringBuffer.toString();
}

From source file:org.openhab.binding.ACDBCommon.db.DBManager.java

/**
 * execute insert //from  w  w w.j a  v a2s  .co  m
 *
 * @param insertSql
 * @param sqlParam
 * @throws Exception
 */
private static void insert(ServerInfo server, String insertSql, HashMap<String, String> sqlParam)
        throws Exception {
    Pattern sqlPattern = Pattern.compile("(\\?)");
    Matcher matcher = sqlPattern.matcher(insertSql);
    StringBuffer newSql = new StringBuffer();

    if (matcher.find()) {
        matcher.appendReplacement(newSql, sqlParam.get("value"));
    }

    if (matcher.find()) {
        matcher.appendReplacement(newSql, "'" + sqlParam.get("time") + "'");
    }
    matcher.appendTail(newSql);
    logger.debug("### sql:{}", newSql);

    try (PreparedStatement stmt = server.getConnection().prepareStatement(newSql.toString())) {
        stmt.executeUpdate();
    }
}

From source file:com.hexidec.ekit.component.HTMLTableUtilities.java

private static String converteTHemTD(String str) {

    Matcher m = pTH.matcher(str);
    if (!m.find()) {
        return str;
    }//w ww .jav a 2s  .  c  o  m

    StringBuffer sb = new StringBuffer();
    String antes, depois;
    do {
        antes = m.group(1);
        depois = m.group(2);

        m.appendReplacement(sb, Matcher.quoteReplacement(antes + "td" + depois));

    } while (m.find());

    m.appendTail(sb);

    return sb.toString();
}

From source file:io.wcm.handler.url.impl.Externalizer.java

/**
 * Mangle the namespaces in the given path for usage in sling-based URLs.
 * <p>//from   w  w  w  .  j a v  a  2  s.  c o  m
 * Example: /path/jcr:content to /path/_jcr_content
 * </p>
 * @param path Path to mangle
 * @return Mangled path
 */
public static String mangleNamespaces(String path) {
    if (!StringUtils.contains(path, NAMESPACE_SEPARATOR)) {
        return path;
    }
    Matcher matcher = NAMESPACE_PATTERN.matcher(path);
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        String replacement = MANGLED_NAMESPACE_PREFIX + matcher.group(1) + MANGLED_NAMESPACE_SUFFIX;
        matcher.appendReplacement(sb, replacement);
    }
    matcher.appendTail(sb);
    return sb.toString();
}

From source file:org.onehippo.cms7.essentials.components.utils.SiteUtils.java

/**
 * Replaces <strong>{@code ${variableName}}</strong> variable in a template with the replacement value provided.
 *
 * @param variableName     variable name
 * @param replacementValue replacement value
 * @param template         string which contains variable e.g
 *                         <p /><strong>{@code My name is ${username} and my login is ${login}}</strong>
 * @return string (template with string replacements)
 *///from www  .j av  a  2 s  .c o  m
public static String replacePlaceHolders(final String variableName, final String replacementValue,
        final CharSequence template) {
    Pattern pattern = Pattern.compile("(\\$\\{" + variableName + "*\\})");
    Matcher matcher = pattern.matcher(template);
    StringBuffer buffer = new StringBuffer();
    while (matcher.find()) {
        matcher.appendReplacement(buffer, replacementValue);
    }
    matcher.appendTail(buffer);
    return buffer.toString();
}

From source file:com.aurel.track.exchange.docx.exporter.DocxExportBL.java

/**
 * Serializes the docx content into the response's output stream
 * @param response/*from w w w .j  av a2  s.  co m*/
 * @param wordMLPackage
 * @return
 */
static String prepareReportResponse(HttpServletResponse response, String baseFileName,
        WordprocessingMLPackage wordMLPackage) {
    if (baseFileName == null) {
        baseFileName = "TrackReport";
    } else {
        baseFileName = StringEscapeUtils.unescapeHtml4(baseFileName);
        StringBuffer sb = new StringBuffer(baseFileName.replaceAll("[^a-zA-Z0-9_]", "_"));
        Pattern regex = Pattern.compile("(_[a-z])");
        Matcher regexMatcher = regex.matcher(sb);
        StringBuffer sb2 = new StringBuffer();

        try {
            while (regexMatcher.find()) {
                regexMatcher.appendReplacement(sb2, regexMatcher.group(1).toUpperCase());
            }
            regexMatcher.appendTail(sb2);

        } catch (Exception e) {
            LOGGER.error(e.getMessage());
        }
        baseFileName = sb2.toString().replaceAll("_", "");
    }
    response.reset();
    response.setHeader("Content-Type",
            "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + baseFileName + ".docx\"");
    DownloadUtil.prepareCacheControlHeader(ServletActionContext.getRequest(), response);
    OutputStream outputStream = null;
    try {
        outputStream = response.getOutputStream();
    } catch (IOException e) {
        LOGGER.error("Getting the output stream failed with " + e.getMessage());
        LOGGER.error(ExceptionUtils.getStackTrace(e));
    }
    try {
        Docx4J.save(wordMLPackage, outputStream, Docx4J.FLAG_NONE);
        /*SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
        saver.save(outputStream);*/
    } catch (Exception e) {
        LOGGER.error("Exporting the docx failed with throwable " + e.getMessage());
        LOGGER.debug(ExceptionUtils.getStackTrace(e));
    }
    return null;
}