Search in sources :

Example 26 with Matcher

use of java.util.regex.Matcher in project elasticsearch by elastic.

the class WordDelimiterTokenFilterFactory method parseTypes.

/**
     * parses a list of MappingCharFilter style rules into a custom byte[] type table
     */
static byte[] parseTypes(Collection<String> rules) {
    SortedMap<Character, Byte> typeMap = new TreeMap<>();
    for (String rule : rules) {
        Matcher m = typePattern.matcher(rule);
        if (!m.find())
            throw new RuntimeException("Invalid Mapping Rule : [" + rule + "]");
        String lhs = parseString(m.group(1).trim());
        Byte rhs = parseType(m.group(2).trim());
        if (lhs.length() != 1)
            throw new RuntimeException("Invalid Mapping Rule : [" + rule + "]. Only a single character is allowed.");
        if (rhs == null)
            throw new RuntimeException("Invalid Mapping Rule : [" + rule + "]. Illegal type.");
        typeMap.put(lhs.charAt(0), rhs);
    }
    // ensure the table is always at least as big as DEFAULT_WORD_DELIM_TABLE for performance
    byte[] types = new byte[Math.max(typeMap.lastKey() + 1, WordDelimiterIterator.DEFAULT_WORD_DELIM_TABLE.length)];
    for (int i = 0; i < types.length; i++) types[i] = WordDelimiterIterator.getType(i);
    for (Map.Entry<Character, Byte> mapping : typeMap.entrySet()) types[mapping.getKey()] = mapping.getValue();
    return types;
}
Also used : Matcher(java.util.regex.Matcher) TreeMap(java.util.TreeMap) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 27 with Matcher

use of java.util.regex.Matcher in project elasticsearch by elastic.

the class Translog method parseIdFromFileName.

/**
     * Extracts the translog generation from a file name.
     *
     * @throws IllegalArgumentException if the path doesn't match the expected pattern.
     */
public static long parseIdFromFileName(Path translogFile) {
    final String fileName = translogFile.getFileName().toString();
    final Matcher matcher = PARSE_STRICT_ID_PATTERN.matcher(fileName);
    if (matcher.matches()) {
        try {
            return Long.parseLong(matcher.group(1));
        } catch (NumberFormatException e) {
            throw new IllegalStateException("number formatting issue in a file that passed PARSE_STRICT_ID_PATTERN: " + fileName + "]", e);
        }
    }
    throw new IllegalArgumentException("can't parse id from file: " + fileName);
}
Also used : Matcher(java.util.regex.Matcher)

Example 28 with Matcher

use of java.util.regex.Matcher in project elasticsearch by elastic.

the class AggregatorFactories method parseAggregators.

private static AggregatorFactories.Builder parseAggregators(QueryParseContext parseContext, int level) throws IOException {
    Matcher validAggMatcher = VALID_AGG_NAME.matcher("");
    AggregatorFactories.Builder factories = new AggregatorFactories.Builder();
    XContentParser.Token token = null;
    XContentParser parser = parseContext.parser();
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token != XContentParser.Token.FIELD_NAME) {
            throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [aggs]: aggregations definitions must start with the name of the aggregation.");
        }
        final String aggregationName = parser.currentName();
        if (!validAggMatcher.reset(aggregationName).matches()) {
            throw new ParsingException(parser.getTokenLocation(), "Invalid aggregation name [" + aggregationName + "]. Aggregation names must be alpha-numeric and can only contain '_' and '-'");
        }
        token = parser.nextToken();
        if (token != XContentParser.Token.START_OBJECT) {
            throw new ParsingException(parser.getTokenLocation(), "Aggregation definition for [" + aggregationName + " starts with a [" + token + "], expected a [" + XContentParser.Token.START_OBJECT + "].");
        }
        BaseAggregationBuilder aggBuilder = null;
        AggregatorFactories.Builder subFactories = null;
        Map<String, Object> metaData = null;
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token != XContentParser.Token.FIELD_NAME) {
                throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.FIELD_NAME + "] under a [" + XContentParser.Token.START_OBJECT + "], but got a [" + token + "] in [" + aggregationName + "]", parser.getTokenLocation());
            }
            final String fieldName = parser.currentName();
            token = parser.nextToken();
            if (token == XContentParser.Token.START_OBJECT) {
                switch(fieldName) {
                    case "meta":
                        metaData = parser.map();
                        break;
                    case "aggregations":
                    case "aggs":
                        if (subFactories != null) {
                            throw new ParsingException(parser.getTokenLocation(), "Found two sub aggregation definitions under [" + aggregationName + "]");
                        }
                        subFactories = parseAggregators(parseContext, level + 1);
                        break;
                    default:
                        if (aggBuilder != null) {
                            throw new ParsingException(parser.getTokenLocation(), "Found two aggregation type definitions in [" + aggregationName + "]: [" + aggBuilder.getType() + "] and [" + fieldName + "]");
                        }
                        aggBuilder = parser.namedObject(BaseAggregationBuilder.class, fieldName, new AggParseContext(aggregationName, parseContext));
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.START_OBJECT + "] under [" + fieldName + "], but got a [" + token + "] in [" + aggregationName + "]");
            }
        }
        if (aggBuilder == null) {
            throw new ParsingException(parser.getTokenLocation(), "Missing definition for aggregation [" + aggregationName + "]", parser.getTokenLocation());
        } else {
            if (metaData != null) {
                aggBuilder.setMetaData(metaData);
            }
            if (subFactories != null) {
                aggBuilder.subAggregations(subFactories);
            }
            if (aggBuilder instanceof AggregationBuilder) {
                factories.addAggregator((AggregationBuilder) aggBuilder);
            } else {
                factories.addPipelineAggregator((PipelineAggregationBuilder) aggBuilder);
            }
        }
    }
    return factories;
}
Also used : Matcher(java.util.regex.Matcher) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 29 with Matcher

use of java.util.regex.Matcher in project buck by facebook.

the class AndroidBinaryIntegrationTest method verifyTrimmedRDotJava.

private void verifyTrimmedRDotJava(ImmutableSet<String> expected) throws IOException {
    List<String> lines = filesystem.readLines(Paths.get("buck-out/gen/apps/multidex/disassemble_app_r_dot_java/all_r_fields.smali"));
    ImmutableSet.Builder<String> found = ImmutableSet.builder();
    for (String line : lines) {
        Matcher m = SMALI_STATIC_FINAL_INT_PATTERN.matcher(line);
        assertTrue("Could not match line: " + line, m.matches());
        assertThat(m.group(1), IsIn.in(expected));
        found.add(m.group(1));
    }
    assertEquals(expected, found.build());
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Matcher(java.util.regex.Matcher) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 30 with Matcher

use of java.util.regex.Matcher in project buck by facebook.

the class AndroidPlatformTargetTest method testPlatformTargetRegex.

private void testPlatformTargetRegex(String input, boolean matches, String id) {
    Matcher matcher = AndroidPlatformTarget.PLATFORM_TARGET_PATTERN.matcher(input);
    assertEquals(matches, matcher.matches());
    if (matches) {
        assertEquals(id, matcher.group(1));
    }
}
Also used : Matcher(java.util.regex.Matcher)

Aggregations

Matcher (java.util.regex.Matcher)12640 Pattern (java.util.regex.Pattern)5059 ArrayList (java.util.ArrayList)1525 IOException (java.io.IOException)913 HashMap (java.util.HashMap)575 File (java.io.File)490 Test (org.junit.Test)448 BufferedReader (java.io.BufferedReader)433 Map (java.util.Map)369 List (java.util.List)292 InputStreamReader (java.io.InputStreamReader)268 HashSet (java.util.HashSet)237 MalformedURLException (java.net.MalformedURLException)164 URL (java.net.URL)157 Date (java.util.Date)153 InputStream (java.io.InputStream)148 Field (java.lang.reflect.Field)130 ParseException (java.text.ParseException)130 PatternSyntaxException (java.util.regex.PatternSyntaxException)128 LinkedHashMap (java.util.LinkedHashMap)122