Example usage for java.util.regex Matcher quoteReplacement

List of usage examples for java.util.regex Matcher quoteReplacement

Introduction

In this page you can find the example usage for java.util.regex Matcher quoteReplacement.

Prototype

public static String quoteReplacement(String s) 

Source Link

Document

Returns a literal replacement String for the specified String .

Usage

From source file:Main.java

private static String lowerCaseAttributes(String formatted) {
    Matcher m = ID_PATTERN.matcher(formatted);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        String text = m.group();//from w w  w  . j  ava 2s .c o m
        m.appendReplacement(sb, Matcher.quoteReplacement(text.toLowerCase()));
    }
    m.appendTail(sb);
    return sb.toString();
}

From source file:Main.java

private static String capitalizeTagNames(String xpath) {
    Matcher m = TAG_PATTERN.matcher(xpath);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        String text = m.group();//from w ww.  jav a 2s. c om
        m.appendReplacement(sb, Matcher.quoteReplacement(text.toUpperCase()));
    }
    m.appendTail(sb);
    return sb.toString();
}

From source file:Main.java

/**
 * Creates a new thread name with the given pattern
 * <p/>/*from   ww w . ja  va 2 s.  c  om*/
 * See {@link org.apache.camel.spi.ExecutorServiceManager#setThreadNamePattern(String)} for supported patterns.
 *
 * @param pattern the pattern
 * @param name    the name
 * @return the thread name, which is unique
 */
public static String resolveThreadName(String pattern, String name) {
    if (pattern == null) {
        pattern = DEFAULT_PATTERN;
    }

    // we support #longName# and #name# as name placeholders
    String longName = name;
    String shortName = name.contains("?") ? "" : name;
    // must quote the names to have it work as literal replacement
    shortName = Matcher.quoteReplacement(shortName);
    longName = Matcher.quoteReplacement(longName);

    // replace tokens
    String answer = pattern.replaceFirst("#counter#", "" + nextThreadCounter());
    answer = answer.replaceFirst("#longName#", longName);
    answer = answer.replaceFirst("#name#", shortName);

    // are there any #word# combos left, if so they should be considered invalid tokens
    if (INVALID_PATTERN.matcher(answer).matches()) {
        throw new IllegalArgumentException("Pattern is invalid: " + pattern);
    }

    return answer;
}

From source file:Main.java

public static boolean renameDict(final File dictFile, final File newDictFile) {
    if (dictFile.isFile()) {
        return dictFile.renameTo(newDictFile);
    } else if (dictFile.isDirectory()) {
        final String dictName = dictFile.getName();
        final String newDictName = newDictFile.getName();
        if (newDictFile.exists()) {
            return false;
        }//from   w w w  . j  a v  a  2 s . co m
        for (final File file : dictFile.listFiles()) {
            if (!file.isFile()) {
                continue;
            }
            final String fileName = file.getName();
            final String newFileName = fileName.replaceFirst(Pattern.quote(dictName),
                    Matcher.quoteReplacement(newDictName));
            if (!file.renameTo(new File(dictFile, newFileName))) {
                return false;
            }
        }
        return dictFile.renameTo(newDictFile);
    }
    return false;
}

From source file:ch.oakmountain.tpa.web.TpaWebPersistor.java

public static void createGraph(String name, String outputDir, GraphCSV csv, String htmlData)
        throws IOException {
    String content = IOUtils.toString(TpaWebPersistor.class.getResourceAsStream("/graph-curved.html"));
    content = content.replaceAll(Pattern.quote("$CSVNAME$"), Matcher.quoteReplacement(name))
            .replaceAll(Pattern.quote("$HTML$"), Matcher.quoteReplacement(htmlData));

    FileUtils.writeStringToFile(Paths.get(outputDir + File.separator + name + ".html").toFile(), content);
    FileUtils.writeStringToFile(new File(outputDir + File.separator + name + ".csv"), csv.toString());
    TpaPersistorUtils.copyFromResourceToDir("d3.v3.js", outputDir);
}

From source file:Main.java

/**
 * Creates a new thread name with the given pattern
 * <p/>/*from w  w  w . j  a va 2 s .c o m*/
 * @param pattern the pattern
 * @param name    the name
 * @return the thread name, which is unique
 */
public static String resolveThreadName(String pattern, String name) {
    if (pattern == null) {
        pattern = DEFAULT_PATTERN;
    }

    // we support #longName# and #name# as name placeholders
    String longName = name;
    String shortName = name;
    // must quote the names to have it work as literal replacement
    shortName = Matcher.quoteReplacement(shortName);
    longName = Matcher.quoteReplacement(longName);

    // replace tokens
    String answer = pattern.replaceFirst("#counter#", "" + nextThreadCounter());
    answer = answer.replaceFirst("#longName#", longName);
    answer = answer.replaceFirst("#name#", shortName);

    // are there any #word# combos left, if so they should be considered invalid tokens
    if (INVALID_PATTERN.matcher(answer).matches()) {
        throw new IllegalArgumentException("Pattern is invalid: " + pattern);
    }

    return answer;
}

From source file:Main.java

private static String convertXMLEscapeChar(String str) {
    /* /* w  w w.  ja  va2 s. com*/
     * direction
     *    0 = convert XML escape to original character
     *  1 = convert regular characters to XML escape
     * Modify any XML escaped values: 
       "   &quot;
       '   &apos;
       <   &lt;
       >   &gt;
       &   &amp;
     */
    if (str.contains("&quot;"))
        str = str.replaceAll(Matcher.quoteReplacement("&quot;"), Matcher.quoteReplacement("\""));
    if (str.contains("&apos;"))
        str = str.replaceAll(Matcher.quoteReplacement("&apos;"), Matcher.quoteReplacement("'"));
    if (str.contains("&lt;"))
        str = str.replaceAll(Matcher.quoteReplacement("&lt;"), Matcher.quoteReplacement("<"));
    if (str.contains("&gt;"))
        str = str.replaceAll(Matcher.quoteReplacement("&gt;"), Matcher.quoteReplacement(">"));
    if (str.contains("&amp;"))
        str = str.replaceAll(Matcher.quoteReplacement("&amp;"), Matcher.quoteReplacement("&"));
    return str;
}

From source file:mergedoc.core.FastStringUtils.java

/**
 * ?? target ??????????/*from  ww w .  j av a  2  s  .co m*/
 *
 * <p>????? JDK ? String#replace(CharSequence,CharSequence)
 * ?????????? Pattern 
 * ?????? target ???2 ????
 *
 * <pre>
 *     input.replace(target, replacement);
 * </pre>
 *
 * ?<br>
 * target ???????? Pattern ?
 * ????????????????
 *
 * @param input ??
 * @param target ??
 * @param replacement ?
 * @return ??
 */
public static String replace(String input, String target, String replacement) {
    Pattern pattern = PatternCache.getLiteralPattern(target);
    return pattern.matcher(input).replaceAll(Matcher.quoteReplacement(replacement));
}

From source file:dk.deck.resolver.AbstractArtifactResolver.java

protected String getArtifactItemPath(Artifact artifact) {
    String artifactPath = artifact.getGroupId().replaceAll(Pattern.quote("."), Matcher.quoteReplacement("/"))
            + "/" + artifact.getArtifactId();
    String artifactVersionPath = artifactPath + "/" + artifact.getVersion();
    String artifactFilename = artifact.getFileName();
    String artifactItemPath = artifactVersionPath + "/" + artifactFilename;
    return artifactItemPath;
}

From source file:org.eclipse.smarthome.io.transport.mqtt.internal.TopicSubscribers.java

public TopicSubscribers(String topic) {
    this.regexMatchTopic = StringUtils
            .replace(StringUtils.replace(Matcher.quoteReplacement(topic), "+", "[^/]*"), "#", ".*");
}