aboutsummaryrefslogtreecommitdiff
path: root/java/pablotron/luigi/actions/FilterAction.java
blob: b69923b31d844e4e8bea64389866a52e0a4dd383 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package pablotron.luigi.actions;

import java.util.Map;
import pablotron.luigi.actions.Action;
import pablotron.luigi.FilterReference;
import pablotron.luigi.Filter;
import pablotron.luigi.LuigiError;

public final class FilterAction implements Action {
  private final String key;
  private final FilterReference filters[];

  public FilterAction(final String key, final FilterReference filters[]) {
    this.key = key;
    this.filters = filters;
  }

  public String run(
    Map<String, Filter.Handler> filters,
    Map<String, String> args
  ) throws LuigiError {
    // check for key
    if (!args.containsKey(key))
      throw new LuigiError("unknown key: " + key);

    // reduce value to result
    String r = args.get(key);
    for (int i = 0, l = this.filters.length; i < l; i++) {
      // get/check filter
      Filter.Handler f = filters.get(this.filters[i].name);
      if (f == null)
        throw new LuigiError("unknown filter: " + this.filters[i].name);

      // run filter 
      r = f.filter(r, this.filters[i].args, args);
    }

    // return result
    return r;
  }
};