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:ch.rasc.edsutil.optimizer.WebResourceProcessor.java

private static String cleanCode(String sourcecode) {
    Matcher matcher = DEV_CODE_PATTERN.matcher(sourcecode);
    StringBuffer cleanCode = new StringBuffer();
    while (matcher.find()) {
        matcher.appendReplacement(cleanCode, "");
    }//from   w  w  w  .  java 2  s.  c  o m
    matcher.appendTail(cleanCode);

    return cleanCode.toString().replaceAll(REQUIRES_PATTERN, "").replaceAll(USES_PATTERN, "");
}

From source file:org.apache.sqoop.odps.OdpsUploadProcessor.java

public static String escapeString(String in, Map rowMap) {
    Matcher matcher = tagPattern.matcher(in);
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        String replacement = "";
        if (matcher.group(2) != null) {
            replacement = rowMap.get(matcher.group(2).toLowerCase()).toString();
            if (replacement == null) {
                replacement = "";
            }/*from  ww w . jav a 2 s .c  o m*/
        }
        replacement = replacement.replaceAll("\\\\", "\\\\\\\\");
        replacement = replacement.replaceAll("\\$", "\\\\\\$");

        matcher.appendReplacement(sb, replacement);
    }
    matcher.appendTail(sb);
    return sb.toString();
}

From source file:net.triptech.metahive.CalculationParser.java

/**
 * Marks up the calculation.//from  w  w  w .  j  a  va  2 s  . co  m
 *
 * @param calculation the calculation
 * @return the string
 */
public static String maredUpCalculation(String calculation) {

    String markedUpCalculation = "";

    logger.debug("Calculation: " + calculation);

    if (StringUtils.isNotBlank(calculation)) {
        try {
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(calculation);
            StringBuffer sb = new StringBuffer();
            logger.debug("Regular expression: " + regEx);
            while (m.find()) {
                logger.info("Variable instance found: " + m.group());
                try {
                    String text = "<span class=\"variable\">" + m.group().toUpperCase() + "</span>";

                    m.appendReplacement(sb, Matcher.quoteReplacement(text));

                } catch (NumberFormatException nfe) {
                    logger.error("Error parsing variable id");
                }
            }
            m.appendTail(sb);

            markedUpCalculation = sb.toString();
            logger.info("Marked up calculation: " + markedUpCalculation);

        } catch (PatternSyntaxException pe) {
            logger.error("Regex syntax error ('" + pe.getPattern() + "') " + pe.getMessage());
        }
    }
    return markedUpCalculation;
}

From source file:org.jasig.portlet.emailpreview.util.MessageUtils.java

public static String addClickableUrlsToMessageBody(String msgBody) {

    // Assertions.
    if (msgBody == null) {
        String msg = "Argument 'msgBody' cannot be null";
        throw new IllegalArgumentException(msg);
    }//from www .  j a va 2  s .  c o  m

    StringBuffer rslt = new StringBuffer();

    Matcher m = CLICKABLE_URLS_PATTERN.matcher(msgBody);
    while (m.find()) {
        StringBuilder bldr = new StringBuilder();
        String text = m.group(1);
        // Handle special case where URL not prefixed with required protocol
        String url = text.startsWith("www.") ? "http://" + text : text;
        if (LOG.isDebugEnabled()) {
            LOG.debug("Making embedded URL '" + text + "' clickable at the following href:  " + url);
        }
        bldr.append(CLICKABLE_URLS_PART1).append(url).append(CLICKABLE_URLS_PART2).append(text)
                .append(CLICKABLE_URLS_PART3);
        m.appendReplacement(rslt, bldr.toString());
    }
    m.appendTail(rslt);

    return rslt.toString();

}

From source file:com.trackplus.track.util.html.Html2LaTeX.java

/**
 * Get LaTeX text translated from HTML.//ww  w.  j a  v  a  2s .c  om
 *
 * @param bodyHtml
 * @return the LaTeX representation of the bodyHtml
 */
public static String getLaTeX(String bodyHtml) {

    // Make sure verbatim is properly transformed, protect spaces and line
    // breaks
    Pattern patt = Pattern.compile("(?s)(.?)<div\\s*class=\\\"code\\-text\\\".+?>(.+?)</div>");
    Matcher m = patt.matcher(bodyHtml);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        String verbatim = m.group(2);
        verbatim = verbatim.replace("\n", "<br>").replace(" ", "&nbsp;");
        String text = m.group(1) + "<div class=\"code-text\">" + verbatim + "</div>"; // +
        // m.group(3);
        m.appendReplacement(sb, Matcher.quoteReplacement(text));
    }
    m.appendTail(sb);
    bodyHtml = sb.toString();

    // System.err.println(bodyHtml);

    Document doc = Jsoup.parse(bodyHtml);

    String latex = getLatexText(doc);
    sb = new StringBuffer();
    String[] lines = latex.split("\n");
    for (int i = 0; i < lines.length; ++i) {
        if ("\\\\".equals(lines[i].trim())) {
            lines[i] = "";
        }
        sb.append(lines[i] + "\n");
    }

    return sb.toString();
}

From source file:info.magnolia.cms.util.LinkUtil.java

/**
 * Transforms all the links to the magnolia format. Used during storing.
 * @param str html/*from www  .  j a va2  s  .  c om*/
 * @return html with changed hrefs
 */
public static String convertAbsoluteLinksToUUIDs(String str) {
    // get all link tags
    Matcher matcher = linkPattern.matcher(str);
    StringBuffer res = new StringBuffer();
    while (matcher.find()) {
        String path = matcher.group(2);
        String uuid = makeUUIDFromAbsolutePath(path);
        matcher.appendReplacement(res, "$1\\${link:{" //$NON-NLS-1$
                + "uuid:{" //$NON-NLS-1$
                + uuid + "}," //$NON-NLS-1$
                + "repository:{website}," //$NON-NLS-1$
                + "workspace:{default}," //$NON-NLS-1$
                + "path:{" //$NON-NLS-1$
                + path + "}}}$3"); //$NON-NLS-1$
    }
    matcher.appendTail(res);
    return res.toString();
}

From source file:com.google.api.tools.framework.aspects.documentation.model.DocumentationUtil.java

/**
 * Given a documentation string, replace the cross reference links with reference text.
 *//*from   w  w  w.j  a  va  2  s  .  c  o  m*/
public static String removeCrossReference(String text) {
    if (Strings.isNullOrEmpty(text)) {
        return "";
    }
    Pattern pattern = Pattern.compile("\\[(?<name>[^\\]]+?)\\]( |\\n)*" + "\\[(?<link>[^\\]]*?)\\]");
    Matcher matcher = pattern.matcher(text);
    StringBuffer result = new StringBuffer();
    while (matcher.find()) {
        String replacementText = matcher.group("name");
        replacementText = Matcher.quoteReplacement(replacementText);
        matcher.appendReplacement(result, replacementText);
    }
    matcher.appendTail(result);
    return result.toString();
}

From source file:br.com.diegosilva.jsfcomponents.util.Utils.java

public static String escapeHtml(String string, boolean convertNewlines, boolean convertSpaces) {
    if (string == null)
        return null;
    StringBuilder sb = new StringBuilder();
    String htmlEntity;//from   w  w w  . ja  v a 2s.  c om
    char c;
    for (int i = 0; i < string.length(); ++i) {
        htmlEntity = null;
        c = string.charAt(i);
        switch (c) {
        case '<':
            htmlEntity = "&lt;";
            break;
        case '>':
            htmlEntity = "&gt;";
            break;
        case '&':
            htmlEntity = "&amp;";
            break;
        case '"':
            htmlEntity = "&quot;";
            break;
        }
        if (htmlEntity != null) {
            sb.append(htmlEntity);
        } else {
            sb.append(c);
        }
    }
    String result = sb.toString();
    if (convertSpaces) {
        // Converts the _beginning_ of line whitespaces into non-breaking
        // spaces
        Matcher matcher = Pattern.compile("(\\n+)(\\s*)(.*)").matcher(result);
        StringBuffer temp = new StringBuffer();
        while (matcher.find()) {
            String group = matcher.group(2);
            StringBuilder spaces = new StringBuilder();
            for (int i = 0; i < group.length(); i++) {
                spaces.append("&#160;");
            }
            matcher.appendReplacement(temp, "$1" + spaces.toString() + "$3");
        }
        matcher.appendTail(temp);
        result = temp.toString();
    }
    if (convertNewlines) {
        result = result.replaceAll("\n", "<br/>");
    }
    return result;
}

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

private static String addColgroups(String html, List<String> colgroups) {
    StringBuffer sb = new StringBuffer();

    Pattern p = Pattern.compile("<table\\b[^>]*>", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);

    Matcher m = p.matcher(html);

    if (!m.find()) {
        return html;
    }/*w w  w.ja  v  a2s .c  om*/

    int i = 0;
    do {
        String colgroup = colgroups.get(i++);
        m.appendReplacement(sb, Matcher.quoteReplacement(m.group() + colgroup));
    } while (m.find());

    m.appendTail(sb);

    return sb.toString();
}

From source file:com.addthis.hydra.job.alert.JobAlertUtil.java

/**
 * Split a path up and replace any {{now-1}}-style elements with the YYMMDD equivalent.
 * {{now-1h}} subtracts one hour from the current time and returns a YYMMDDHH formatted string.
 *
 * @param path The input path to process
 * @return The path with the relevant tokens replaced
 *///  w  w  w .  j av a  2 s.c o  m
@VisibleForTesting
static String expandDateMacro(String path) {
    StringBuffer sb = new StringBuffer();
    Matcher matcher = DATE_MACRO_PATTERN.matcher(path);
    while (matcher.find()) {
        if (matcher.group(3) != null) {
            String match = "{{now" + matcher.group(2) + Strings.nullToEmpty(matcher.group(4)) + "}}";
            matcher.appendReplacement(sb,
                    DateUtil.getDateTime(ymdhFormatter, match, true).toString(ymdhFormatter));
        } else {
            String match = matcher.group();
            matcher.appendReplacement(sb,
                    DateUtil.getDateTime(ymdFormatter, match, false).toString(ymdFormatter));
        }
    }
    matcher.appendTail(sb);
    return sb.toString();
}