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.krawler.common.util.StringUtil.java

public static String serverHTMLStripper(String stripTags)
        throws IllegalStateException, IndexOutOfBoundsException {
    Pattern p = Pattern.compile("<[^>]*>");
    Matcher m = p.matcher(stripTags);
    StringBuffer sb = new StringBuffer();
    if (!isNullOrEmpty(stripTags)) {
        while (m.find()) {
            m.appendReplacement(sb, "");
        }//w w  w .jav  a 2 s . c  o  m
        m.appendTail(sb);
        stripTags = sb.toString();
        stripTags = stripTags.replaceAll("\r", "<br/>");
        stripTags = stripTags.replaceAll("\n", " ");
        stripTags = stripTags.replaceAll("\'", "&#39;");
        stripTags = stripTags.replaceAll("\"", "&quot;");
        stripTags = stripTags.replaceAll("&nbsp;", " ");
    }
    return stripTags.trim();
}

From source file:net.sf.j2ep.UrlRewritingResponseWrapper.java

/**
 * Rewrites the location header. Will first locate any links in the header
 * and then rewrite them.//  w w  w.  j a v  a2  s .  com
 * 
 * @param value
 *            The header value we are to rewrite
 * @return A rewritten header
 */
private String rewriteLocation(String value) {
    StringBuffer header = new StringBuffer();

    Matcher matcher = linkPattern.matcher(value);
    while (matcher.find()) {

        String link = matcher.group(3).replaceAll("\\$", "\\\\$");
        if (link.length() == 0) {
            link = "/";
        }
        String location = matcher.group(2) + link;

        Server matchingServer = serverChain.getServerMapped(location);
        if (matchingServer != null) {
            link = link.substring(matchingServer.getPath().length());
            link = matchingServer.getRule().revert(link);
            matcher.appendReplacement(header, "$1" + ownHostName + contextPath + link);
        }
    }
    matcher.appendTail(header);
    log.debug("Location header rewritten " + value + " >> " + header.toString());
    return header.toString();
}

From source file:org.kite9.diagram.server.AbstractKite9Controller.java

protected String sanitizeFilePath(String badFileName) {
    StringBuffer cleanFileName = new StringBuffer();
    Matcher fileMatcher = filePattern.matcher(badFileName);
    boolean match = fileMatcher.find();
    while (match) {
        fileMatcher.appendReplacement(cleanFileName, "");
        match = fileMatcher.find();//from w  w  w  .ja va  2  s.co m
    }
    fileMatcher.appendTail(cleanFileName);
    return cleanFileName.substring(0, cleanFileName.length() > 250 ? 250 : cleanFileName.length());
}

From source file:jp.go.nict.langrid.wrapper.ws_1_2.translation.AbstractTranslationService.java

/**
 * ??<br/>/*from   w  w  w.ja v a2 s  .  c  o m*/
 * ????divideByDelimiterAndTranslation ???<br/>
 * @author Hitoshi Sugihara
 * @param source ?
 * @param deli ?? Pattern 
 * @param delis ?????????
 * @return ???????
 */
private String[] divideSourceByDelimiter(String source, java.util.regex.Pattern deli,
        java.util.List<String> delis) {
    java.util.List<String> sources = new java.util.ArrayList<String>(); // ????????????
    java.util.regex.Matcher mth = deli.matcher(source); // ??? Matcher 
    StringBuffer hoge; // ????????
    while (mth.find()) {
        delis.add(mth.group());
        hoge = new StringBuffer();
        mth.appendReplacement(hoge, "");
        sources.add(hoge.toString());
    }
    hoge = new StringBuffer();
    mth.appendTail(hoge);
    if (hoge.length() > 0) {
        sources.add(hoge.toString());
    }
    return sources.toArray(new String[] {});
}

From source file:org.opencastproject.composer.gstreamer.AbstractGSEncoderEngine.java

/**
 * Substitutes template values from template with actual values from properties.
 * //from ww w  .j  a v a2s  . c  om
 * @param template
 *          String that represents template
 * @param properties
 *          Map that contains substitution for template values in template
 * @param cleanup
 *          if template values that were not matched should be removed
 * @return String built from template
 */
protected String substituteTemplateValues(String template, Map<String, String> properties, boolean cleanup) {

    StringBuffer buffer = new StringBuffer();
    Pattern pattern = Pattern.compile("#\\{\\S+?\\}");
    Matcher matcher = pattern.matcher(template);
    while (matcher.find()) {
        String match = template.substring(matcher.start() + 2, matcher.end() - 1);
        if (properties.containsKey(match)) {
            matcher.appendReplacement(buffer, properties.get(match));
        }
    }
    matcher.appendTail(buffer);

    String processedTemplate = buffer.toString();

    if (cleanup) {
        // remove all property matches
        buffer = new StringBuffer();
        Pattern ppattern = Pattern.compile("\\S+?=#\\{\\S+?\\}");
        matcher = ppattern.matcher(processedTemplate);
        while (matcher.find()) {
            matcher.appendReplacement(buffer, "");
        }
        matcher.appendTail(buffer);
        processedTemplate = buffer.toString();

        // remove all other templates
        buffer = new StringBuffer();
        matcher = pattern.matcher(processedTemplate);
        while (matcher.find()) {
            matcher.appendReplacement(buffer, "");
        }
        matcher.appendTail(buffer);
        processedTemplate = buffer.toString();
    }

    return processedTemplate;
}

From source file:org.opencms.util.CmsStringUtil.java

/**
 * Substitutes a pattern in a string using a {@link I_CmsRegexSubstitution}.<p>
 * /*from  w w  w  .j  ava  2s  . c o m*/
 * @param pattern the pattern to substitute 
 * @param text the text in which the pattern should be substituted 
 * @param sub the substitution handler 
 * 
 * @return the transformed string 
 */
public static String substitute(Pattern pattern, String text, I_CmsRegexSubstitution sub) {

    StringBuffer buffer = new StringBuffer();
    Matcher matcher = pattern.matcher(text);
    while (matcher.find()) {
        matcher.appendReplacement(buffer, sub.substituteMatch(text, matcher));
    }
    matcher.appendTail(buffer);
    return buffer.toString();
}

From source file:org.projectforge.business.scripting.GroovyEngine.java

private String replaceIncludes(final String template) {
    if (template == null) {
        return null;
    }/*from w  w  w  .j av  a 2s .  c  o  m*/
    final Pattern p = Pattern.compile("#INCLUDE\\{([0-9\\-\\.a-zA-Z/]*)\\}", Pattern.MULTILINE);
    final StringBuffer buf = new StringBuffer();
    final Matcher m = p.matcher(template);
    while (m.find()) {
        if (m.group(1) != null) {
            final String filename = m.group(1);
            final Object[] res = configurationService.getResourceContentAsString(filename);
            String content = (String) res[0];
            if (content != null) {
                content = replaceIncludes(content).replaceAll("\\\\", "#HURZ1#").replaceAll("\\$", "#HURZ2#");
                m.appendReplacement(buf, content); // Doesn't work with '$' or '\' in content
            } else {
                m.appendReplacement(buf, "*** " + filename + " not found! ***");
            }
        }
    }
    m.appendTail(buf);
    return buf.toString();
}

From source file:org.apache.roller.weblogger.business.plugins.entry.TopicTagPlugin.java

/**
 * Apply the plugin to the given entry.  Returns the entry text with topic tags expanded.
 *
 * @param entry           WeblogEntry to which plugin should be applied.
 * @param singleEntry     Ignored./*  w ww  .j a  v  a 2  s .c om*/
 * @return Results of applying plugin to entry.
 */
public String render(WeblogEntry entry, String str) {
    String entryText = str;
    StringBuffer result = new StringBuffer(entryText.length());
    MessageFormat fmt = getLinkFormat();

    // Replace all of the instances matching the pattern with bookmark specified.
    Matcher m = tagPatternWithBookmark.matcher(entryText);
    while (m.find()) {
        String bookmark = m.group(1);
        String tag = m.group(2);
        String site = getBookmarkSite(bookmark);
        if (site == null) {
            site = getDefaultTopicSite();
        }
        if (!site.endsWith("/")) {
            site += "/";
        }
        String link = generateLink(fmt, site, tag);
        m.appendReplacement(result, link);
    }
    m.appendTail(result);

    // Now, in a second phase replace all of the instances matching the pattern without bookmark specified.
    entryText = result.toString();
    result = new StringBuffer(entryText.length());
    m = tagPatternWithoutBookmark.matcher(entryText);
    while (m.find()) {
        String tag = m.group(1);
        String site = getDefaultTopicSite();
        String link = generateLink(fmt, site, tag);
        m.appendReplacement(result, link);
    }
    m.appendTail(result);

    return result.toString();
}

From source file:com.lcw.one.common.persistence.BaseDao.java

/** 
 * hqlorderBy?? /*from   ww  w .j ava2s  . co m*/
 * @param qlString
 * @return 
 */
private String removeOrders(String qlString) {
    Pattern p = Pattern.compile("order\\s*by[\\w|\\W|\\s|\\S]*", Pattern.CASE_INSENSITIVE);
    Matcher m = p.matcher(qlString);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(sb, "");
    }
    m.appendTail(sb);
    return sb.toString();
}

From source file:com.adito.boot.ReplacementEngine.java

private void replaceInto(Pattern pattern, String replacementPattern, Replacer replacer, StringBuffer input,
        StringBuffer work) {//from   ww  w. j a va2  s  . c  o  m
    work.ensureCapacity(input.length());
    work.setLength(0);
    if (log.isDebugEnabled())
        log.debug("Getting matcher for " + pattern.pattern());
    Matcher m = pattern.matcher(input);
    log.debug("Got matcher, finding first occurence.");
    while (m.find()) {
        if (log.isDebugEnabled())
            log.debug("Found occurence '" + m.group() + "'");
        String repl = replacer.getReplacement(pattern, m, replacementPattern);
        if (repl != null) {
            if (log.isDebugEnabled())
                log.debug("Found replacement, appending '" + repl + "'");
            if (encoder == null) {
                m.appendReplacement(work, Util.escapeForRegexpReplacement(repl));
            } else {
                m.appendReplacement(work, encoder.encode(Util.escapeForRegexpReplacement(repl)));
            }
        }
    }
    if (log.isDebugEnabled())
        log.debug("Processed matches, appending replacement.");
    m.appendTail(work);
}