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:org.infoscoop.util.I18NUtil.java

private static String replace(String str, Map resMap, boolean onlySystem, boolean escapeXML) {
    StringBuffer sb = new StringBuffer();
    Matcher m = onlySystem ? REPACE_PATTERN2.matcher(str) : REPACE_PATTERN.matcher(str);
    while (m.find()) {
        String g = m.group(1);//from www. ja  va 2  s  . co  m
        String replacement = (String) resMap.get(g);
        if (replacement == null)
            replacement = g;
        else
            replacement = escapeReplacement(escapeXML ? XmlUtil.escapeXmlEntities(replacement) : replacement);

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

From source file:Main.java

public static String replaceHtmlTag(String str, String beforeTag, String tagAttrib, String startTag,
        String endTag) {/* ww w  . ja v a  2s . c  o m*/
    String regxpForTag = "<\\s*" + beforeTag + "\\s+([^>]*)\\s*>";
    String regxpForTagAttrib = tagAttrib + "=\"([^\"]+)\"";
    Pattern patternForTag = Pattern.compile(regxpForTag);
    Pattern patternForAttrib = Pattern.compile(regxpForTagAttrib);
    Matcher matcherForTag = patternForTag.matcher(str);
    StringBuffer sb = new StringBuffer();
    boolean result = matcherForTag.find();
    while (result) {
        StringBuffer sbreplace = new StringBuffer();
        Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag.group(1));
        if (matcherForAttrib.find()) {
            matcherForAttrib.appendReplacement(sbreplace, startTag + matcherForAttrib.group(1) + endTag);
        }
        matcherForTag.appendReplacement(sb, sbreplace.toString());
        result = matcherForTag.find();
    }
    matcherForTag.appendTail(sb);
    return sb.toString();
}

From source file:org.cruk.genologics.api.jaxb.URIAdapter.java

/**
 * Remove any "state=" parameter from the given URI.
 *
 * @param uri The URI to modify./* www. jav a 2s.c om*/
 *
 * @return A string which is {@code uri} without the state information.
 */
public static String removeStateParameter(String uri) {
    if (uri != null) {
        StringBuffer builder = new StringBuffer();

        Matcher m = STATE_PATTERN.matcher(uri);
        while (m.find()) {
            String replacement = "";

            if (StringUtils.isNotEmpty(m.group(2))) {
                // There are more options after the state parameter.
                // Need to put the first question mark or ampersand back.
                replacement = m.group(1);
            }

            m.appendReplacement(builder, replacement);
        }
        m.appendTail(builder);

        uri = builder.toString();
    }

    return uri;
}

From source file:ddf.util.WktStandard.java

/**
 * Denormalize the given WKT to support backwards compatibility.
 *
 * @param wkt/*ww w.ja v a  2 s .co m*/
 *            wkt to denormalize
 * @return denormalized WKT
 */
public static String denormalize(String wkt) {
    if (wkt == null) {
        return wkt;
    }

    Matcher matcher = WKT_MULTIPOINT_PATTERN.matcher(wkt);
    if (matcher.find()) {
        matcher.reset();
        StringBuffer resultWkt = new StringBuffer(wkt.length());
        while (matcher.find()) {
            String currentMultiPoint = matcher.group(0);
            String currentMultiPointText = matcher.group(1);

            matcher.appendReplacement(resultWkt, currentMultiPoint.replace(currentMultiPointText,
                    currentMultiPointText.replaceAll("[\\(\\)]", "")));
        }
        matcher.appendTail(resultWkt);

        return resultWkt.toString();
    } else {
        return wkt;
    }
}

From source file:net.thucydides.core.util.Inflector.java

/**
 * Utility method to replace all occurrences given by the specific backreference with its uppercased form, and remove all
 * other backreferences.//w  w w.  j ava2 s  .  c  o  m
 *
 * The Java {@link Pattern regular expression processing} does not use the preprocessing directives <code>\l</code>,
 * <code>&#92;u</code>, <code>\L</code>, and <code>\U</code>. If so, such directives could be used in the replacement string
 * to uppercase or lowercase the backreferences. For example, <code>\L1</code> would lowercase the first backreference, and
 * <code>&#92;u3</code> would uppercase the 3rd backreference.
 *
 * @param input the input string
 * @param regex regular expression to replace
 * @param groupNumberToUppercase the regex group to convert to uppercase
 * @return the input string with the appropriate characters converted to upper-case
 */
private static String replaceAllWithUppercase(String input, String regex, int groupNumberToUppercase) {
    Pattern underscoreAndDotPattern = Pattern.compile(regex);
    Matcher matcher = underscoreAndDotPattern.matcher(input);
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        matcher.appendReplacement(sb, matcher.group(groupNumberToUppercase).toUpperCase());
    }
    matcher.appendTail(sb);
    return sb.toString();
}

From source file:Main.java

/**
 * Converts a method name to an attribute name, e.g. getFooBar() --> foo_bar, isFlag --> flag.
 * @param methodName/*from  w ww.j a va2s .  c  o m*/
 * @return
 */
public static String methodNameToAttributeName(final String methodName) {
    String name = methodName;
    if ((methodName.startsWith("get") || methodName.startsWith("set")) && methodName.length() > 3)
        name = methodName.substring(3);
    else if (methodName.startsWith("is") && methodName.length() > 2)
        name = methodName.substring(2);

    // Pattern p=Pattern.compile("[A-Z]+");
    Matcher m = METHOD_NAME_TO_ATTR_NAME_PATTERN.matcher(name);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        int start = m.start(), end = m.end();
        String str = name.substring(start, end).toLowerCase();
        if (str.length() > 1) {
            String tmp1 = str.substring(0, str.length() - 1);
            String tmp2 = str.substring(str.length() - 1);
            str = tmp1 + "_" + tmp2;
        }
        if (start == 0) {
            m.appendReplacement(sb, str);
        } else
            m.appendReplacement(sb, "_" + str);
    }
    m.appendTail(sb);
    return sb.length() > 0 ? sb.toString() : methodName;
}

From source file:com.o19s.solr.swan.nodes.SwanTermNode.java

private static String translateWildcard(String in) {
    Matcher m = WILDCARD_PATTERN.matcher(in);
    StringBuffer sb = new StringBuffer(in.length());

    while (m.find()) {
        if (m.group(0).equals("?")) {
            m.appendReplacement(sb, ".");
        } else if (m.group(0).equals("*")) {
            m.appendReplacement(sb, ".*");
        } else {/* w  w  w. j  a v  a2  s  .  c o  m*/
            if (!m.group(1).isEmpty())
                m.appendReplacement(sb, ".{0," + m.group(1) + "}");
            else
                m.appendReplacement(sb, ".*");
        }
    }
    m.appendTail(sb);

    return sb.toString();
}

From source file:Main.java

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

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

        String processContent = matcherTelContent.group(1);
        String htmlContent = matcherTelContent.group(2);
        if (htmlContent.equalsIgnoreCase("</script>") || htmlContent.equalsIgnoreCase("</a>")) {
            matcherTelContent.appendReplacement(bodyStringBuffer, processContent + htmlContent);
        } else {//  w  w w  .j a v a  2s.  c o  m
            String telContentHasProcessed = makeTelNoHerf(processContent);
            matcherTelContent.appendReplacement(bodyStringBuffer, telContentHasProcessed + htmlContent);
        }
    }
    matcherTelContent.appendTail(bodyStringBuffer);
    return bodyStringBuffer.toString();
}

From source file:com.qwazr.utils.HtmlUtils.java

public static final String removeTag(String text, String[] allowedTags) {
     if (allowedTags == null)
         text = StringUtils.replaceConsecutiveSpaces(text, " ");
     StringBuffer sb = new StringBuffer();
     Matcher matcher;
     synchronized (removeTagPattern) {
         matcher = removeTagPattern.matcher(text);
     }//from   w  w w.j av  a  2s .c o  m
     while (matcher.find()) {
         boolean allowed = false;
         String group = matcher.group();
         if (allowedTags != null) {
             for (String tag : allowedTags) {
                 if (tag.equals(group)) {
                     allowed = true;
                     break;
                 }
             }
         }
         matcher.appendReplacement(sb, allowed ? group : "");
     }
     matcher.appendTail(sb);
     return sb.toString();
 }

From source file:com.cognifide.aet.executor.xmlparser.xml.utils.EscapeUtils.java

public static String escapeUrls(String xmlString) {
    Matcher matcher = URL_TAG_PATTERN.matcher(xmlString);
    StringBuffer urlTagStringBuffer = new StringBuffer();
    while (matcher.find()) {
        String attrs = matcher.group(3);
        Matcher attrMatcher = ATTRIBUTES_PATTERN.matcher(attrs);
        StringBuffer attrsStringBuffer = new StringBuffer();
        while (attrMatcher.find()) {
            String attr = attrMatcher.group(1);
            if (attr.startsWith("description=")) {
                validateAttr(attr);/*from w ww .  j a  v a 2 s .  c  o  m*/
            }

            if (attr.startsWith("href=")) {
                attr = String.format("href=\"%s\"",
                        StringEscapeUtils.escapeXml11(attr.substring(6, attr.length() - 1)));
            }
            attrMatcher.appendReplacement(attrsStringBuffer, attr + " ");
        }
        attrMatcher.appendTail(attrsStringBuffer);
        String urlString = String.format("<url %s/>", attrsStringBuffer.toString());
        matcher.appendReplacement(urlTagStringBuffer, urlString);
    }
    matcher.appendTail(urlTagStringBuffer);
    return urlTagStringBuffer.toString();
}