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:com.clustercontrol.accesscontrol.dialog.LoginDialog.java

private void updateCounter(String managerName) {
    String pattern = "^" + Pattern.quote(Messages.getString("facility.manager"));
    if (managerName.matches(pattern + "\\d+$")) {
        try {/*from   ww w .ja  v  a  2  s .c om*/
            int found = Integer.parseInt(managerName.replaceFirst(pattern, ""));
            if (found >= counter) {
                counter = found + 1;
            }
        } catch (NumberFormatException e) {
        }
    }
}

From source file:com.android.ddmuilib.log.event.EventDisplay.java

private void loadPidFilters(String storageString) {
    if (storageString.length() > 0) {
        String[] values = storageString.split(Pattern.quote(PID_STORAGE_SEPARATOR));

        for (String value : values) {
            if (mPidFilterList == null) {
                mPidFilterList = new ArrayList<Integer>();
            }// w  w w . j  a  v a  2s. c  om
            mPidFilterList.add(Integer.parseInt(value));
        }
    }
}

From source file:com.jkoolcloud.tnt4j.streams.configure.sax.ConfigParserHandler.java

/**
 * Processes a {@code <field>} element.
 *
 * @param attrs/*w w w  .j a v  a 2 s.  c o  m*/
 *            List of element attributes
 *
 * @throws SAXException
 *             if error occurs parsing element
 */
private void processField(Attributes attrs) throws SAXException {
    if (currField != null) {
        throw new SAXParseException(StreamsResources.getStringFormatted(StreamsResources.RESOURCE_BUNDLE_NAME,
                "ConfigParserHandler.malformed.configuration", FIELD_ELMT), currParseLocation);
    }
    if (currParser == null) {
        throw new SAXParseException(
                StreamsResources.getStringFormatted(StreamsResources.RESOURCE_BUNDLE_NAME,
                        "ConfigParserHandler.malformed.configuration2", FIELD_ELMT, PARSER_ELMT),
                currParseLocation);
    }
    currFieldHasLocValAttr = false;
    // currFieldHasLocElmt = false;
    // currFieldHasMapElmt = false;
    String field = null;
    ActivityFieldDataType dataType = null;
    String locatorType = null;
    String locator = null;
    String valueType = null;
    String separator = null;
    String pattern = null;
    String units = null;
    String format = null;
    String locale = null;
    String timeZone = null;
    String value = null;
    int radix = 10;
    String reqVal = "";
    boolean transparent = false;
    boolean split = false;
    String id = null;
    for (int i = 0; i < attrs.getLength(); i++) {
        String attName = attrs.getQName(i);
        String attValue = attrs.getValue(i);
        if (NAME_ATTR.equals(attName)) {
            field = attValue;
        } else if (DATA_TYPE_ATTR.equals(attName)) {
            dataType = ActivityFieldDataType.valueOf(attValue);
        } else if (LOC_TYPE_ATTR.equals(attName)) {
            locatorType = attValue;
        } else if (LOCATOR_ATTR.equals(attName)) {
            locator = attValue;
        } else if (VALUE_TYPE_ATTR.equals(attName)) {
            valueType = attValue;
        } else if (SEPARATOR_ATTR.equals(attName)) {
            separator = attValue;
        } else if (FORMATTING_PATTERN_ATTR.equals(attName))
            pattern = attValue;
        else if (RADIX_ATTR.equals(attName)) {
            radix = Integer.parseInt(attValue);
        } else if (UNITS_ATTR.equals(attName)) {
            units = attValue;
        } else if (FORMAT_ATTR.equals(attName)) {
            format = attValue;
        } else if (LOCALE_ATTR.equals(attName)) {
            locale = attValue;
        } else if (TIMEZONE_ATTR.equals(attName)) {
            timeZone = attValue;
        } else if (VALUE_ATTR.equals(attName)) {
            value = attValue;
        } else if (REQUIRED_ATTR.equals(attName)) {
            reqVal = attValue;
        } else if (TRANSPARENT_ATTR.equals(attName)) {
            transparent = Boolean.parseBoolean(attValue);
        } else if (SPLIT_ATTR.equals(attName)) {
            split = Boolean.parseBoolean(attValue);
        } else if (ID_ATTR.equals(attName)) {
            id = attValue;
        }
    }
    // make sure required fields are present
    notEmpty(field, FIELD_ELMT, NAME_ATTR);

    if (separator != null && pattern != null) {
        throw new SAXParseException(StreamsResources.getStringFormatted(StreamsResources.RESOURCE_BUNDLE_NAME,
                "ConfigParserHandler.cannot.contain", FIELD_ELMT, SEPARATOR_ATTR, FORMATTING_PATTERN_ATTR),
                currParseLocation);
    }

    ActivityFieldLocator afl;
    ActivityField af = new ActivityField(field);

    if (value != null) {
        currFieldHasLocValAttr = true;
        afl = new ActivityFieldLocator(value);
        afl.setRadix(radix);
        afl.setRequired(reqVal);
        if (dataType != null) {
            afl.setDataType(dataType);
        }
        if (StringUtils.isNotEmpty(units)) {
            afl.setUnits(units);
        }
        if (StringUtils.isNotEmpty(format)) {
            afl.setFormat(format, locale);
        }
        if (StringUtils.isNotEmpty(timeZone)) {
            afl.setTimeZone(timeZone);
        }
        if (StringUtils.isNotEmpty(id)) {
            afl.setId(id);
        }
        af.addLocator(afl);
    } else if (StringUtils.isNotEmpty(locator)) {
        currFieldHasLocValAttr = true;
        String[] locators = currParser.canHaveDelimitedLocators() ? locator.split(Pattern.quote(LOC_DELIM))
                : new String[] { locator };
        for (String loc : locators) {
            if (StringUtils.isNotEmpty(loc)) {
                afl = new ActivityFieldLocator(locatorType, loc);
                afl.setRadix(radix);
                afl.setRequired(reqVal);
                if (dataType != null) {
                    afl.setDataType(dataType);
                }
                if (StringUtils.isNotEmpty(units)) {
                    afl.setUnits(units);
                }
                if (StringUtils.isNotEmpty(format)) {
                    afl.setFormat(format, locale);
                }
                if (StringUtils.isNotEmpty(timeZone)) {
                    afl.setTimeZone(timeZone);
                }
                if (StringUtils.isNotEmpty(id)) {
                    afl.setId(id);
                }
                af.addLocator(afl);
            }
        }
    } else if (StringUtils.isEmpty(locator)) {
        af.setGroupLocator(radix, reqVal, dataType, units, format, locale, timeZone);
    }

    if (separator != null) {
        af.setSeparator(separator);
    }
    if (StringUtils.isNotEmpty(pattern)) {
        af.setFormattingPattern(pattern);
    }
    if (StringUtils.isNotEmpty(valueType)) {
        af.setValueType(valueType);
    }
    af.setRequired(reqVal);
    af.setTransparent(transparent);
    af.setSplitCollection(split);
    currField = af;
}

From source file:com.android.ddmuilib.log.event.EventDisplay.java

private void loadOccurrenceDescriptors(String storageString) {
    if (storageString.length() == 0) {
        return;//from  w ww .  j a  v a2 s .  co  m
    }

    String[] values = storageString.split(Pattern.quote(DESCRIPTOR_STORAGE_SEPARATOR));

    for (String value : values) {
        OccurrenceDisplayDescriptor desc = new OccurrenceDisplayDescriptor();
        desc.loadFrom(value);
        mOccurrenceDescriptors.add(desc);
    }
}

From source file:com.ichi2.libanki.Finder.java

public static int findReplace(Collection col, List<Long> nids, String src, String dst, boolean isRegex,
        String field, boolean fold) {
    Map<Long, Integer> mmap = new HashMap<Long, Integer>();
    if (field != null) {
        try {//from   w w  w  .  j  ava 2 s.  c  o  m
            for (JSONObject m : col.getModels().all()) {
                JSONArray flds = m.getJSONArray("flds");
                for (int fi = 0; fi < flds.length(); ++fi) {
                    JSONObject f = flds.getJSONObject(fi);
                    if (f.getString("name").equals(field)) {
                        mmap.put(m.getLong("id"), f.getInt("ord"));
                    }
                }
            }
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        if (mmap.isEmpty()) {
            return 0;
        }
    }
    // find and gather replacements
    if (!isRegex) {
        src = Pattern.quote(src);
    }
    if (fold) {
        src = "(?i)" + src;
    }
    Pattern regex = Pattern.compile(src);

    ArrayList<Object[]> d = new ArrayList<Object[]>();
    String snids = Utils.ids2str(nids);
    nids = new ArrayList<Long>();
    Cursor cur = null;
    try {
        cur = col.getDb().getDatabase().rawQuery("select id, mid, flds from notes where id in " + snids, null);
        while (cur.moveToNext()) {
            String flds = cur.getString(2);
            String origFlds = flds;
            // does it match?
            String[] sflds = Utils.splitFields(flds);
            if (field != null) {
                long mid = cur.getLong(1);
                if (!mmap.containsKey(mid)) {
                    // note doesn't have that field
                    continue;
                }
                int ord = mmap.get(mid);
                sflds[ord] = regex.matcher(sflds[ord]).replaceAll(dst);
            } else {
                for (int i = 0; i < sflds.length; ++i) {
                    sflds[i] = regex.matcher(sflds[i]).replaceAll(dst);
                }
            }
            flds = Utils.joinFields(sflds);
            if (!flds.equals(origFlds)) {
                long nid = cur.getLong(0);
                nids.add(nid);
                d.add(new Object[] { flds, Utils.intNow(), col.usn(), nid }); // order based on query below
            }
        }
    } finally {
        if (cur != null) {
            cur.close();
        }
    }
    if (d.isEmpty()) {
        return 0;
    }
    // replace
    col.getDb().executeMany("update notes set flds=?,mod=?,usn=? where id=?", d);
    long[] pnids = Utils.toPrimitive(nids);
    col.updateFieldCache(pnids);
    col.genCards(pnids);
    return d.size();
}

From source file:com.screenslicer.core.util.BrowserUtil.java

private static WebElement toElement(Browser browser, HtmlNode htmlNode, Element body, boolean recurse)
        throws ActionFailed {
    if (body == null) {
        body = BrowserUtil.openElement(browser, true, null, null, null, null);
    }//ww w  .j  a  va2s . com
    if (!CommonUtil.isEmpty(htmlNode.id)) {
        Elements elements = body.getElementsByAttributeValue("id", htmlNode.id);
        if (elements.size() == 1) {
            WebElement element = toElement(browser, elements.get(0), htmlNode, recurse);
            if (element != null) {
                return element;
            }
        }
    }
    List<Elements> selected = new ArrayList<Elements>();
    if (!CommonUtil.isEmpty(htmlNode.tagName)) {
        selected.add(body.getElementsByTag(htmlNode.tagName));
    } else if (!CommonUtil.isEmpty(htmlNode.href)) {
        selected.add(body.getElementsByTag("a"));
    }
    if (!CommonUtil.isEmpty(htmlNode.id)) {
        selected.add(body.getElementsByAttributeValue("id", htmlNode.id));
    }
    if (!CommonUtil.isEmpty(htmlNode.name)) {
        selected.add(body.getElementsByAttributeValue("name", htmlNode.name));
    }
    if (!CommonUtil.isEmpty(htmlNode.type)) {
        selected.add(body.getElementsByAttributeValue("type", htmlNode.type));
    }
    if (!CommonUtil.isEmpty(htmlNode.value)) {
        selected.add(body.getElementsByAttributeValue("value", htmlNode.value));
    }
    if (!CommonUtil.isEmpty(htmlNode.title)) {
        selected.add(body.getElementsByAttributeValue("title", htmlNode.title));
    }
    if (!CommonUtil.isEmpty(htmlNode.role)) {
        selected.add(body.getElementsByAttributeValue("role", htmlNode.role));
    }
    if (!CommonUtil.isEmpty(htmlNode.alt)) {
        selected.add(body.getElementsByAttributeValue("alt", htmlNode.alt));
    }
    if (htmlNode.classes != null && htmlNode.classes.length > 0) {
        Map<Element, Integer> found = new HashMap<Element, Integer>();
        for (int i = 0; i < htmlNode.classes.length; i++) {
            Elements elements = body.getElementsByClass(htmlNode.classes[i]);
            for (Element element : elements) {
                if (!found.containsKey(element)) {
                    found.put(element, 0);
                }
                found.put(element, found.get(element) + 1);
            }
        }
        Elements elements = new Elements();
        for (int i = htmlNode.classes.length; i > 0; i--) {
            for (Map.Entry<Element, Integer> entry : found.entrySet()) {
                if (entry.getValue() == i) {
                    elements.add(entry.getKey());
                }
            }
            if (!elements.isEmpty()) {
                break;
            }
        }
        selected.add(elements);
    }
    if (!CommonUtil.isEmpty(htmlNode.href)) {
        Elements hrefs = body.getElementsByAttribute("href");
        Elements toAdd = new Elements();
        String currentUrl = browser.getCurrentUrl();
        String hrefGiven = htmlNode.href;
        for (Element href : hrefs) {
            String hrefFound = href.attr("href");
            if (hrefGiven.equalsIgnoreCase(hrefFound)) {
                toAdd.add(href);
                toAdd.add(href);
                toAdd.add(href);
            } else if (htmlNode.fuzzy && hrefFound != null && hrefFound.endsWith(hrefGiven)) {
                toAdd.add(href);
                toAdd.add(href);
            } else if (htmlNode.fuzzy && hrefFound != null && hrefFound.contains(hrefGiven)) {
                toAdd.add(href);
            } else {
                String uriGiven = UrlUtil.toCanonicalUri(currentUrl, hrefGiven);
                String uriFound = UrlUtil.toCanonicalUri(currentUrl, hrefFound);
                if (uriGiven.equalsIgnoreCase(uriFound)) {
                    toAdd.add(href);
                }
            }
        }
        selected.add(toAdd);
    }
    if (!CommonUtil.isEmpty(htmlNode.innerText)) {
        selected.add(body.getElementsMatchingText(Pattern.quote(htmlNode.innerText)));
        selected.add(body.getElementsMatchingText("^\\s*" + Pattern.quote(htmlNode.innerText) + "\\s*$"));
    }
    if (htmlNode.multiple != null) {
        selected.add(body.getElementsByAttribute("multiple"));
    }
    Map<Element, Integer> votes = new HashMap<Element, Integer>();
    for (Elements elements : selected) {
        for (Element element : elements) {
            if (!votes.containsKey(element)) {
                votes.put(element, 0);
            }
            votes.put(element, votes.get(element) + 2);
            if (!NodeUtil.isHidden(element)) {
                votes.put(element, votes.get(element) + 1);
            }
        }
    }
    int maxVote = 0;
    Element maxElement = null;
    for (Map.Entry<Element, Integer> entry : votes.entrySet()) {
        if (entry.getValue() > maxVote) {
            maxVote = entry.getValue();
            maxElement = entry.getKey();
        }
    }
    return toElement(browser, maxElement, htmlNode, recurse);
}

From source file:hudson.plugins.clearcase.AbstractClearCaseScm.java

public static String getViewPathsRegexp(String[] loadRules, boolean isUnix) {
    // Note - the logic here to do ORing to match against *any* of the load rules is, quite frankly,
    // hackishly ugly. I'm embarassed by it. But it's what I've got for right now.
    String filterRegexp = "";
    if (loadRules != null) {
        String tempFilterRules = "";
        for (String loadRule : loadRules) {
            if (StringUtils.isNotEmpty(loadRule)) {
                if (loadRule.endsWith("/") || loadRule.endsWith("\\")) {
                    loadRule = loadRule.substring(0, loadRule.length() - 1);
                }//from  ww w.  ja  v a2  s.co  m
                loadRule = PathUtil.convertPathForOS(loadRule, isUnix);
                tempFilterRules += "|" + Pattern.quote(loadRule + PathUtil.fileSepForOS(isUnix));
                tempFilterRules += "|" + Pattern.quote(loadRule) + "$";
            }
        }

        // Adding tweak for ignoring leading slashes or Windows drives in case of strange situations using setview.
        if (StringUtils.isNotEmpty(tempFilterRules)) {
            filterRegexp = tempFilterRules.substring(1);
        }
    }
    return filterRegexp;
}

From source file:com.android.ddmuilib.log.event.EventDisplay.java

private void loadValueDescriptors(String storageString) {
    if (storageString.length() == 0) {
        return;//  w w  w . jav a2  s  . co  m
    }

    String[] values = storageString.split(Pattern.quote(DESCRIPTOR_STORAGE_SEPARATOR));

    for (String value : values) {
        ValueDisplayDescriptor desc = new ValueDisplayDescriptor();
        desc.loadFrom(value);
        mValueDescriptors.add(desc);
    }
}

From source file:com.microsoft.tfs.client.common.ui.commands.search.WorkItemSearchCommand.java

private String evaluateInvariantMacro(final FieldDefinition field, final String invariantMacro) {
    // is it a macro, replace with actual macro value
    if (field.getFieldType() == FieldType.DATETIME && LocaleInvariantStringHelpers
            .caseInsensitiveStartsWith(invariantMacro.trim(), WIQLOperators.MACRO_TODAY)) {
        // if it is only @today with no @today - n, then just return the
        // date// ww w  . j a va  2 s  .  co  m
        if (invariantMacro.trim().equalsIgnoreCase(WIQLOperators.MACRO_TODAY)) {
            return wiqlDateFormatter.format(Calendar.getInstance().getTime());
        }

        // if the macro contains a '-' sign that isn't at the end of
        // the string
        boolean subtract = true;
        String[] macroTokens = invariantMacro.split(Pattern.quote("-")); //$NON-NLS-1$

        if (macroTokens.length != 2) {
            // try for the '+' sign
            macroTokens = invariantMacro.split(Pattern.quote("+")); //$NON-NLS-1$
            subtract = false;
        }

        // if there aren't two tokens, one on either side of the minus or
        // plus sign, the macro is ill-formed and we will not parse it.
        if (macroTokens.length != 2) {
            return invariantMacro;
        }

        int n;
        try {
            n = Integer.parseInt(macroTokens[1].trim());
        } catch (final NumberFormatException e) {
            return invariantMacro;
        }

        if (subtract) {
            n *= -1;
        }

        // return the date corresponding to today + (-n)
        final Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, n);
        throwIfDateOutOfRange(calendar.getTime());
        return wiqlDateFormatter.format(calendar.getTime());
    } else if (invariantMacro.equalsIgnoreCase(WIQLOperators.MACRO_ME)) {
        return workItemClient.getUserDisplayName();
    } else if (invariantMacro.equalsIgnoreCase(WIQLOperators.MACRO_PROJECT)) {
        return project.getName();
    }

    return invariantMacro;
}