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: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 va  2 s .  c om
            String emailContentHasProcessed = makeEmailHerf(processContent);
            matcherEmailContent.appendReplacement(bodyStringBuffer, emailContentHasProcessed + htmlContent);
        }

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

From source file:com.networknt.utility.Util.java

public static String substituteVariables(String template, Map<String, String> variables) {
    Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}");
    Matcher matcher = pattern.matcher(template);
    // StringBuilder cannot be used here because Matcher expects StringBuffer
    StringBuffer buffer = new StringBuffer();
    while (matcher.find()) {
        if (variables.containsKey(matcher.group(1))) {
            String replacement = variables.get(matcher.group(1));
            // quote to work properly with $ and {,} signs
            matcher.appendReplacement(buffer,
                    replacement != null ? Matcher.quoteReplacement(replacement) : "null");
        }//from   ww  w  . jav a 2 s. co  m
    }
    matcher.appendTail(buffer);
    return buffer.toString();
}

From source file:org.etudes.util.HtmlHelper.java

/**
 * Remove any tags only valid in headers (title base meta link style)
 * /*  www .ja  v  a  2 s  .co  m*/
 * @param data
 *        the html data.
 * @return The cleaned up data.
 */
public static String stripHeaderTags(String data) {
    if (data == null)
        return data;

    // pattern to find link/meta tags
    Pattern p = Pattern.compile("<(link|meta|title|base|style)\\s+.*?(/*>)",
            Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL);

    Matcher m = p.matcher(data);
    StringBuffer sb = new StringBuffer();

    while (m.find()) {
        m.appendReplacement(sb, "");
    }

    m.appendTail(sb);

    return sb.toString();
}

From source file:org.etudes.util.HtmlHelper.java

/**
 * Remove link and meta tags/*from   w  w  w .j av a  2  s  .  c  om*/
 * 
 * @param data
 *        the html data.
 * @return The cleaned up data.
 */
public static String stripLinks(String data) {
    if (data == null)
        return data;

    // pattern to find link/meta tags
    Pattern p = Pattern.compile("<(link|meta)\\s+.*?(/*>)",
            Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL);

    Matcher m = p.matcher(data);
    StringBuffer sb = new StringBuffer();

    while (m.find()) {
        m.appendReplacement(sb, "");
    }

    m.appendTail(sb);

    return sb.toString();
}

From source file:org.etudes.util.HtmlHelper.java

/**
 * Remove image tags that have for src "file://" "webkit-fake-url://" or "x-apple-ql-id://" prefixes (transports)
 * /*from w w w . ja va 2 s  . com*/
 * @param data
 *        the html data.
 * @return The cleaned up data.
 */
public static String stripBadImageTags(String data) {
    if (data == null)
        return data;

    // pattern to find link/meta tags
    // TODO: the .*? needs to stop on a >, else if there's a good image and later a bad one, it combines the two into one and removes it all!
    Pattern p = Pattern.compile("<img\\s+.*?src=\"(file:|webkit-fake-url:|x-apple-ql-id:).*?/>",
            Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL);

    Matcher m = p.matcher(data);
    StringBuffer sb = new StringBuffer();

    while (m.find()) {
        m.appendReplacement(sb, "");
    }

    m.appendTail(sb);

    return sb.toString();
}

From source file:org.etudes.util.HtmlHelper.java

/**
 * Remove any text that match the "comments from Word font definitions encoded into html by Tiny" from the data.
 * /*from  w ww  . j  a v a2s.com*/
 * @param data
 *        the html data.
 * @return The cleaned up data.
 */
public static String stripEncodedFontDefinitionComments(String data) {
    if (data == null)
        return data;

    // quick check for any hint of the pattern
    if (data.indexOf("&lt;!--  /* Font Definitions */") == -1)
        return data;

    // Notes: DOTALL so the "." matches line terminators too, "*?" Reluctant quantifier so text between two different comments is not lost
    Pattern p = Pattern.compile("&lt;!--  /\\* Font Definitions \\*/.*?--&gt;",
            Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL);

    Matcher m = p.matcher(data);
    StringBuffer sb = new StringBuffer();

    while (m.find()) {
        m.appendReplacement(sb, "");
    }

    m.appendTail(sb);

    return sb.toString();
}

From source file:org.etudes.util.HtmlHelper.java

/**
 * Remove any text that match the "comments from Word style definitions encoded into html by Tiny" from the data.
 * /*from   w ww.j av  a 2  s . c om*/
 * @param data
 *        the html data.
 * @return The cleaned up data.
 */
public static String stripEncodedStyleDefinitionComments(String data) {
    if (data == null)
        return data;

    // quick check for any hint of the pattern
    if (data.indexOf("&lt;!-- /* Style Definitions */") == -1)
        return data;

    // Notes: DOTALL so the "." matches line terminators too, "*?" Reluctant quantifier so text between two different comments is not lost
    Pattern p = Pattern.compile("&lt;!-- /\\* Style Definitions \\*/.*?--&gt;",
            Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL);

    Matcher m = p.matcher(data);
    StringBuffer sb = new StringBuffer();

    while (m.find()) {
        m.appendReplacement(sb, "");
    }

    m.appendTail(sb);

    return sb.toString();
}

From source file:org.etudes.util.HtmlHelper.java

/**
 * Remove any text that match the "comments damaged from IE and Tiny" from the data.
 * // ww w.j ava2 s  .c  om
 * @param data
 *        the html data.
 * @return The cleaned up data.
 */
public static String stripDamagedComments(String data) {
    if (data == null)
        return data;

    // quick check for any hint of the pattern
    if (data.indexOf("<! [endif] >") == -1)
        return data;

    // Notes: DOTALL so the "." matches line terminators too, "*?" Reluctant quantifier so text between two different comments is not lost
    Pattern p = Pattern.compile("<!--\\[if.*?<! \\[endif\\] >",
            Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL);

    Matcher m = p.matcher(data);
    StringBuffer sb = new StringBuffer();

    while (m.find()) {
        m.appendReplacement(sb, "");
    }

    m.appendTail(sb);

    // now remove the bad comment end
    String rv = sb.toString().replace("<-->", "");
    return rv;
}

From source file:org.etudes.util.HtmlHelper.java

/**
 * Remove any HTML comments from the data.
 * /* w w w  . ja v  a2s  . co  m*/
 * @param data
 *        the html data.
 * @return The cleaned up data.
 */
public static String stripComments(String data) {
    if (data == null)
        return data;

    // quick check for any comments
    if (data.indexOf("<!--") == -1)
        return data;

    // pattern to find html comments
    // Notes: DOTALL so the "." matches line terminators too, "*?" Reluctant quantifier so text between two different comments is not lost
    Pattern p = Pattern.compile("<!--.*?-->", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL);

    Matcher m = p.matcher(data);
    StringBuffer sb = new StringBuffer();

    while (m.find()) {
        m.appendReplacement(sb, "");
    }

    m.appendTail(sb);

    data = sb.toString();

    // if any open tags are left, likely because of missing a matching close tag, we will remove them.
    // if we leave them in, a missing close comment tag will be inserted by HtmlCleaner at the very END of the document, making the rest a big comment.
    // this fix exposes some comment text into the content, but preserves actual content.
    data = data.replaceAll("<!--", "");

    return data;
}

From source file:Main.java

/**
 * Identify variables in the given string and replace with their values as
 * defined in the variableDefs map.  Variables without definitions are left
 * alone.//from  w  w w.  ja  va2 s  .  co m
 *
 * @param string String to do variable replacement in.
 * @param variableDefs Map from variable names to values.
 * @return modified string
 */
public static String replaceVariablesInString(String string, Map<String, String> variableDefs) {

    StringBuffer replacementStringBuffer = new StringBuffer();

    Matcher variableMatcher = variablePattern.matcher(string);
    while (variableMatcher.find()) {
        String varString = variableMatcher.group(1).trim();
        int eqIdx = varString.indexOf("=");

        String varName;
        if (eqIdx > -1)
            varName = varString.substring(0, eqIdx).trim();
        else
            varName = varString;

        String replacementString = variableDefs.containsKey(varName) ? variableDefs.get(varName)
                : variableMatcher.group(0);

        variableMatcher.appendReplacement(replacementStringBuffer, Matcher.quoteReplacement(replacementString));
    }
    variableMatcher.appendTail(replacementStringBuffer);

    return replacementStringBuffer.toString();
}