Example usage for java.util.regex Pattern quote

List of usage examples for java.util.regex Pattern quote

Introduction

In this page you can find the example usage for java.util.regex Pattern quote.

Prototype

public static String quote(String s) 

Source Link

Document

Returns a literal pattern String for the specified String .

Usage

From source file:mondrian.util.UtilCompatibleJdk15.java

public String quotePattern(String s) {
    return Pattern.quote(s);
}

From source file:ch.unibas.fittingwizard.infrastructure.base.ResourceUtils.java

/**
 * Get the relative path from one file to another, specifying the directory separator.
 * If one of the provided resources does not exist, it is assumed to be a file unless it ends with '/' or
 * '\'./*from w  w  w .jav a 2  s . c  o  m*/
 *
 * @param targetPath targetPath is calculated to this file
 * @param basePath basePath is calculated from this file
 * @param pathSeparator directory separator. The platform default is not assumed so that we can test Unix behaviour when running on Windows (for example)
 * @return
 */
public static String getRelativePath(String targetPath, String basePath, String pathSeparator) {

    // Normalize the paths
    String normalizedTargetPath = FilenameUtils.normalizeNoEndSeparator(targetPath);
    String normalizedBasePath = FilenameUtils.normalizeNoEndSeparator(basePath);

    // Undo the changes to the separators made by normalization
    if (pathSeparator.equals("/")) {
        normalizedTargetPath = FilenameUtils.separatorsToUnix(normalizedTargetPath);
        normalizedBasePath = FilenameUtils.separatorsToUnix(normalizedBasePath);

    } else if (pathSeparator.equals("\\")) {
        normalizedTargetPath = FilenameUtils.separatorsToWindows(normalizedTargetPath);
        normalizedBasePath = FilenameUtils.separatorsToWindows(normalizedBasePath);

    } else {
        throw new IllegalArgumentException("Unrecognised dir separator '" + pathSeparator + "'");
    }

    String[] base = normalizedBasePath.split(Pattern.quote(pathSeparator));
    String[] target = normalizedTargetPath.split(Pattern.quote(pathSeparator));

    // First get all the common elements. Store them as a string,
    // and also count how many of them there are.
    StringBuffer common = new StringBuffer();

    int commonIndex = 0;
    while (commonIndex < target.length && commonIndex < base.length
            && target[commonIndex].equals(base[commonIndex])) {
        common.append(target[commonIndex] + pathSeparator);
        commonIndex++;
    }

    if (commonIndex == 0) {
        // No single common path element. This most
        // likely indicates differing drive letters, like C: and D:.
        // These paths cannot be relativized.
        throw new PathResolutionException("No common path element found for '" + normalizedTargetPath
                + "' and '" + normalizedBasePath + "'");
    }

    // The number of directories we have to backtrack depends on whether the base is a file or a dir
    // For example, the relative path from
    //
    // /foo/bar/baz/gg/ff to /foo/bar/baz
    //
    // ".." if ff is a file
    // "../.." if ff is a directory
    //
    // The following is a heuristic to figure out if the base refers to a file or dir. It's not perfect, because
    // the resource referred to by this path may not actually exist, but it's the best I can do
    boolean baseIsFile = true;

    File baseResource = new File(normalizedBasePath);

    if (baseResource.exists()) {
        baseIsFile = baseResource.isFile();

    } else if (basePath.endsWith(pathSeparator)) {
        baseIsFile = false;
    }

    StringBuffer relative = new StringBuffer();

    if (base.length != commonIndex) {
        int numDirsUp = baseIsFile ? base.length - commonIndex - 1 : base.length - commonIndex;

        for (int i = 0; i < numDirsUp; i++) {
            relative.append(".." + pathSeparator);
        }
    }
    relative.append(normalizedTargetPath.substring(common.length()));
    return relative.toString();
}

From source file:me.ryandowling.allmightybot.data.Spam.java

public boolean shouldTakeAction(MessageEvent event) {
    String message = StringEscapeUtils.escapeJava(event.getMessage());

    if (this.pattern == null) {
        this.pattern = Pattern.compile(Pattern.quote(this.search), Pattern.CASE_INSENSITIVE);
    }/*from w ww  .  j av a 2s.c o  m*/

    return this.pattern.matcher(message).find();
}

From source file:it.units.malelab.ege.util.Utils.java

public static Grammar<String> parseFromFile(File file, String charset)
        throws FileNotFoundException, IOException {
    Grammar<String> grammar = new Grammar<>();
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
    String line;/*from w w w  . j av a 2  s  . c  o m*/
    while ((line = br.readLine()) != null) {
        String[] components = line.split(Pattern.quote(Grammar.RULE_ASSIGNMENT_STRING));
        String toReplaceSymbol = components[0].trim();
        String[] optionStrings = components[1].split(Pattern.quote(Grammar.RULE_OPTION_SEPARATOR_STRING));
        if (grammar.getStartingSymbol() == null) {
            grammar.setStartingSymbol(toReplaceSymbol);
        }
        List<List<String>> options = new ArrayList<>();
        for (String optionString : optionStrings) {
            List<String> symbols = new ArrayList<>();
            for (String symbol : optionString.split("\\s+")) {
                if (!symbol.trim().isEmpty()) {
                    symbols.add(symbol.trim());
                }
            }
            if (!symbols.isEmpty()) {
                options.add(symbols);
            }
        }
        grammar.getRules().put(toReplaceSymbol, options);
    }
    br.close();
    return grammar;
}

From source file:io.wcm.dam.assetservice.impl.DamPathHandler.java

/**
 * Set DAM paths that should be handled. Only called once by {@link AssetRequestServlet}.
 * @param damPaths DAM folder paths or empty/null if all should be handled.
 *///  w w  w. ja  v  a 2 s.  c o  m
private static Pattern buildDamPathsPattern(Set<String> damPaths) {
    StringBuilder pattern = new StringBuilder();
    pattern.append("^(");
    Iterator<String> paths = damPaths.iterator();
    while (paths.hasNext()) {
        pattern.append(Pattern.quote(paths.next()));
        pattern.append("/.*");
        if (paths.hasNext()) {
            pattern.append("|");
        }
    }
    pattern.append(")$");
    return Pattern.compile(pattern.toString());
}

From source file:mediathekplugin.Database.java

private static String matchField(String name) {
    return Pattern.quote("<" + name + ">") + STRING_PATTERN + Pattern.quote("</" + name + ">");
}

From source file:io.github.jeddict.jpa.spec.extend.DataMapping.java

public boolean refractorName(String prevName, String newName) {
    if (ClassDiagramSettings.isRefractorQuery()) {
        if (StringUtils.containsIgnoreCase(this.getName(), FIND_BY + prevName)) {
            this.setName(this.getName().replaceAll("\\b(?i)" + Pattern.quote(FIND_BY + prevName) + "\\b",
                    FIND_BY + StringHelper.firstUpper(newName)));
            return true;
        } else if (StringUtils.containsIgnoreCase(this.getName(), prevName)) {
            this.setName(this.getName().replaceAll("\\b(?i)" + Pattern.quote(prevName) + "\\b", newName));
            return true;
        }//from  ww  w . j a v  a 2s  .co  m
    }
    return false;
}

From source file:bboss.org.artofsolving.jodconverter.process.LinuxProcessManager.java

public long findPid(ProcessQuery query) throws IOException {
    String regex = Pattern.quote(query.getCommand()) + ".*" + Pattern.quote(query.getArgument());
    Pattern commandPattern = Pattern.compile(regex);
    for (String line : execute(psCommand())) {
        Matcher lineMatcher = PS_OUTPUT_LINE.matcher(line);
        if (lineMatcher.matches()) {
            String command = lineMatcher.group(2);
            Matcher commandMatcher = commandPattern.matcher(command);
            if (commandMatcher.find()) {
                return Long.parseLong(lineMatcher.group(1));
            }/* ww w. j av  a 2  s .  c om*/
        }
    }
    return PID_UNKNOWN;
}

From source file:org.jtalks.common.security.acl.sids.UserSid.java

private String parseUserId(String sidId) {
    String[] splitted = sidId.split(Pattern.quote(":"));
    if (splitted.length != 2) {
        throw new WrongFormatException(sidId);
    }/*from   www. jav a 2 s . com*/
    return splitted[1];
}

From source file:com.ningzeta.avatar.YamlMapper.java

public <T> T convert(String prefix, Class<T> toValueType) {
    HashMap<String, Object> map = convert();
    HashMap<String, Object> preObj = map;

    for (String p : prefix.split(Pattern.quote("."))) {
        preObj = (HashMap<String, Object>) preObj.get(p);
    }/*w  w w  .j a v a2  s  . co  m*/

    return objectMapper.convertValue(preObj, toValueType);
}