Example usage for org.apache.lucene.util.automaton Automata makeAnyString

List of usage examples for org.apache.lucene.util.automaton Automata makeAnyString

Introduction

In this page you can find the example usage for org.apache.lucene.util.automaton Automata makeAnyString.

Prototype

public static Automaton makeAnyString() 

Source Link

Document

Returns a new (deterministic) automaton that accepts all strings.

Usage

From source file:org.codelibs.elasticsearch.common.regex.Regex.java

License:Apache License

/** Return an {Automaton} that matches the given pattern. */
public static Automaton simpleMatchToAutomaton(String pattern) {
    List<Automaton> automata = new ArrayList<>();
    int previous = 0;
    for (int i = pattern.indexOf('*'); i != -1; i = pattern.indexOf('*', i + 1)) {
        automata.add(Automata.makeString(pattern.substring(previous, i)));
        automata.add(Automata.makeAnyString());
        previous = i + 1;/*from   w  ww .j  a  v a2s  .  com*/
    }
    automata.add(Automata.makeString(pattern.substring(previous)));
    return Operations.concatenate(automata);
}

From source file:org.codelibs.elasticsearch.common.xcontent.support.XContentMapValues.java

License:Apache License

/**
 * Returns a function that filters a document map based on the given include and exclude rules.
 * @see #filter(Map, String[], String[]) for details
 *//*from   w w w  .  j  a v  a 2s  .co m*/
public static Function<Map<String, ?>, Map<String, Object>> filter(String[] includes, String[] excludes) {
    CharacterRunAutomaton matchAllAutomaton = new CharacterRunAutomaton(Automata.makeAnyString());

    CharacterRunAutomaton include;
    if (includes == null || includes.length == 0) {
        include = matchAllAutomaton;
    } else {
        Automaton includeA = Regex.simpleMatchToAutomaton(includes);
        includeA = makeMatchDotsInFieldNames(includeA);
        include = new CharacterRunAutomaton(includeA);
    }

    Automaton excludeA;
    if (excludes == null || excludes.length == 0) {
        excludeA = Automata.makeEmpty();
    } else {
        excludeA = Regex.simpleMatchToAutomaton(excludes);
        excludeA = makeMatchDotsInFieldNames(excludeA);
    }
    CharacterRunAutomaton exclude = new CharacterRunAutomaton(excludeA);

    // NOTE: We cannot use Operations.minus because of the special case that
    // we want all sub properties to match as soon as an object matches

    return (map) -> filter(map, include, 0, exclude, 0, matchAllAutomaton);
}

From source file:org.codelibs.elasticsearch.common.xcontent.support.XContentMapValues.java

License:Apache License

/** Make matches on objects also match dots in field names.
 *  For instance, if the original simple regex is `foo`, this will translate
 *  it into `foo` OR `foo.*`. *///from  w  ww.  j ava 2s .co m
private static Automaton makeMatchDotsInFieldNames(Automaton automaton) {
    return Operations.union(automaton,
            Operations.concatenate(Arrays.asList(automaton, Automata.makeChar('.'), Automata.makeAnyString())));
}

From source file:org.codelibs.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude.java

License:Apache License

private Automaton toAutomaton() {
    Automaton a = null;//from   w w  w.  j a  v  a  2 s.  c  o  m
    if (include != null) {
        a = include.toAutomaton();
    } else if (includeValues != null) {
        a = Automata.makeStringUnion(includeValues);
    } else {
        a = Automata.makeAnyString();
    }
    if (exclude != null) {
        a = Operations.minus(a, exclude.toAutomaton(), Operations.DEFAULT_MAX_DETERMINIZED_STATES);
    } else if (excludeValues != null) {
        a = Operations.minus(a, Automata.makeStringUnion(excludeValues),
                Operations.DEFAULT_MAX_DETERMINIZED_STATES);
    }
    return a;
}

From source file:org.elasticsearch.xpack.core.security.authz.accesscontrol.FieldSubsetReaderTests.java

License:Open Source License

public void testSourceFiltering() {
    // include on top-level value
    Map<String, Object> map = new HashMap<>();
    map.put("foo", 3);
    map.put("bar", "baz");

    CharacterRunAutomaton include = new CharacterRunAutomaton(Automata.makeString("foo"));
    Map<String, Object> filtered = FieldSubsetReader.filter(map, include, 0);
    Map<String, Object> expected = new HashMap<>();
    expected.put("foo", 3);

    assertEquals(expected, filtered);/*from   ww  w  .j  av  a 2 s  .c o  m*/

    // include on inner wildcard
    map = new HashMap<>();
    Map<String, Object> subMap = new HashMap<>();
    subMap.put("bar", 42);
    subMap.put("baz", 6);
    map.put("foo", subMap);
    map.put("bar", "baz");

    include = new CharacterRunAutomaton(Automatons.patterns("foo.*"));
    filtered = FieldSubsetReader.filter(map, include, 0);
    expected = new HashMap<>();
    expected.put("foo", subMap);

    assertEquals(expected, filtered);

    // include on leading wildcard
    include = new CharacterRunAutomaton(Automatons.patterns("*.bar"));
    filtered = FieldSubsetReader.filter(map, include, 0);
    expected = new HashMap<>();
    subMap = new HashMap<>();
    subMap.put("bar", 42);
    expected.put("foo", subMap);

    assertEquals(expected, filtered);

    // include on inner value
    include = new CharacterRunAutomaton(Automatons.patterns("foo.bar"));
    filtered = FieldSubsetReader.filter(map, include, 0);

    assertEquals(expected, filtered);

    // exclude on exact value
    include = new CharacterRunAutomaton(Operations.minus(Automata.makeAnyString(),
            Automatons.patterns("foo.bar"), Operations.DEFAULT_MAX_DETERMINIZED_STATES));
    filtered = FieldSubsetReader.filter(map, include, 0);
    expected = new HashMap<>();
    expected.put("bar", "baz");
    expected.put("foo", Collections.singletonMap("baz", 6));

    assertEquals(expected, filtered);

    // exclude on wildcard
    include = new CharacterRunAutomaton(Operations.minus(Automata.makeAnyString(), Automatons.patterns("foo.*"),
            Operations.DEFAULT_MAX_DETERMINIZED_STATES));
    filtered = FieldSubsetReader.filter(map, include, 0);
    expected = Collections.singletonMap("bar", "baz");

    assertEquals(expected, filtered);

    // include on inner array
    map = new HashMap<>();
    List<Object> subArray = new ArrayList<>();
    subMap = new HashMap<>();
    subMap.put("bar", 42);
    subMap.put("baz", "foo");
    subArray.add(subMap);
    subArray.add(12);
    map.put("foo", subArray);

    include = new CharacterRunAutomaton(Automatons.patterns("foo.bar"));
    filtered = FieldSubsetReader.filter(map, include, 0);
    expected = new HashMap<>();
    subArray = new ArrayList<>();
    subMap = new HashMap<>();
    subMap.put("bar", 42);
    subArray.add(subMap);
    expected.put("foo", subArray);

    assertEquals(expected, filtered);

    // include on inner array 2
    include = new CharacterRunAutomaton(Automatons.patterns("foo"));
    filtered = FieldSubsetReader.filter(map, include, 0);
    expected = new HashMap<>();
    subArray = new ArrayList<>();
    subArray.add(12);
    expected.put("foo", subArray);

    assertEquals(expected, filtered);

    // exclude on inner array
    include = new CharacterRunAutomaton(Operations.minus(Automata.makeAnyString(),
            Automatons.patterns("foo.baz"), Operations.DEFAULT_MAX_DETERMINIZED_STATES));
    filtered = FieldSubsetReader.filter(map, include, 0);
    expected = new HashMap<>();
    subArray = new ArrayList<>();
    subMap = new HashMap<>();
    subMap.put("bar", 42);
    subArray.add(subMap);
    subArray.add(12);
    expected.put("foo", subArray);

    assertEquals(expected, filtered);

    // exclude on inner array 2
    include = new CharacterRunAutomaton(Operations.minus(Automata.makeAnyString(), Automatons.patterns("foo"),
            Operations.DEFAULT_MAX_DETERMINIZED_STATES));
    filtered = FieldSubsetReader.filter(map, include, 0);
    expected = new HashMap<>();
    subArray = new ArrayList<>();
    subMap = new HashMap<>();
    subMap.put("bar", 42);
    subMap.put("baz", "foo");
    subArray.add(subMap);
    expected.put("foo", subArray);

    assertEquals(expected, filtered);

    // json array objects that have no matching fields should be left empty instead of being removed:
    // (otherwise nested inner hit source filtering fails with AOOB)
    map = new HashMap<>();
    map.put("foo", "value");
    List<Map<?, ?>> values = new ArrayList<>();
    values.add(Collections.singletonMap("foo", "1"));
    values.add(Collections.singletonMap("baz", "2"));
    map.put("bar", values);

    include = new CharacterRunAutomaton(Automatons.patterns("bar.baz"));
    filtered = FieldSubsetReader.filter(map, include, 0);

    expected = new HashMap<>();
    expected.put("bar", Arrays.asList(new HashMap<>(), Collections.singletonMap("baz", "2")));
    assertEquals(expected, filtered);
}

From source file:org.elasticsearch.xpack.core.security.authz.permission.FieldPermissions.java

License:Open Source License

private static Automaton initializePermittedFieldsAutomaton(final String[] grantedFields,
        final String[] deniedFields) {
    Automaton grantedFieldsAutomaton;/*from w  w w.  j  a  va 2s  . co m*/
    if (grantedFields == null || Arrays.stream(grantedFields).anyMatch(Regex::isMatchAllPattern)) {
        grantedFieldsAutomaton = Automatons.MATCH_ALL;
    } else {
        // an automaton that includes metadata fields, including join fields created by the _parent field such
        // as _parent#type
        Automaton metaFieldsAutomaton = Operations.concatenate(Automata.makeChar('_'),
                Automata.makeAnyString());
        grantedFieldsAutomaton = Operations.union(Automatons.patterns(grantedFields), metaFieldsAutomaton);
    }

    Automaton deniedFieldsAutomaton;
    if (deniedFields == null || deniedFields.length == 0) {
        deniedFieldsAutomaton = Automatons.EMPTY;
    } else {
        deniedFieldsAutomaton = Automatons.patterns(deniedFields);
    }

    grantedFieldsAutomaton = MinimizationOperations.minimize(grantedFieldsAutomaton,
            Operations.DEFAULT_MAX_DETERMINIZED_STATES);
    deniedFieldsAutomaton = MinimizationOperations.minimize(deniedFieldsAutomaton,
            Operations.DEFAULT_MAX_DETERMINIZED_STATES);

    if (subsetOf(deniedFieldsAutomaton, grantedFieldsAutomaton) == false) {
        throw new ElasticsearchSecurityException("Exceptions for field permissions must be a subset of the "
                + "granted fields but " + Strings.arrayToCommaDelimitedString(deniedFields)
                + " is not a subset of " + Strings.arrayToCommaDelimitedString(grantedFields));
    }

    grantedFieldsAutomaton = Automatons.minusAndMinimize(grantedFieldsAutomaton, deniedFieldsAutomaton);
    return grantedFieldsAutomaton;
}

From source file:org.elasticsearch.xpack.core.security.support.Automatons.java

License:Open Source License

/**
 * Builds and returns an automaton that represents the given pattern.
 *///ww  w  .ja  va2  s  . com
@SuppressWarnings("fallthrough") // explicit fallthrough at end of switch
static Automaton wildcard(String text) {
    List<Automaton> automata = new ArrayList<>();
    for (int i = 0; i < text.length();) {
        final char c = text.charAt(i);
        int length = 1;
        switch (c) {
        case WILDCARD_STRING:
            automata.add(Automata.makeAnyString());
            break;
        case WILDCARD_CHAR:
            automata.add(Automata.makeAnyChar());
            break;
        case WILDCARD_ESCAPE:
            // add the next codepoint instead, if it exists
            if (i + length < text.length()) {
                final char nextChar = text.charAt(i + length);
                length += 1;
                automata.add(Automata.makeChar(nextChar));
                break;
            } // else fallthru, lenient parsing with a trailing \
        default:
            automata.add(Automata.makeChar(c));
        }
        i += length;
    }
    return concatenate(automata);
}