public Pattern getGranularityPattern()

in main/src/main/java/org/apache/james/jdkim/tagvalue/PublicKeyRecordImpl.java [126:154]


    public Pattern getGranularityPattern() {
        String g = getValue("g").toString();
        int pStar = g.indexOf('*');
        if (VALIDATION) {
            if (!granularityPattern.matcher(g).matches())
                throw new IllegalStateException("Syntax error in granularity: "
                        + g);
        }
        if (g.isEmpty()) {
            // TODO this works but smells too much as an hack.
            // in case of "g=" with nothing specified then we return a pattern
            // that won't match
            // SPEC: An empty "g=" value never matches any addresses.
            return Pattern.compile("@");
        } else if (pStar != -1) {
            if (g.indexOf('*', pStar + 1) != -1)
                throw new IllegalStateException(
                        "Invalid granularity using more than one wildcard: "
                                + g);
            String pattern = "^\\Q" + g.subSequence(0, pStar).toString()
                    + "\\E.*\\Q"
                    + g.subSequence(pStar + 1, g.length()).toString() + "\\E$";
            return Pattern.compile(pattern);
        } else {
            // TODO we need some escaping. On Java 5 we have Pattern.quote that
            // is better
            return Pattern.compile("^\\Q" + g + "\\E$");
        }
    }