1 package fr.in2p3.jsaga.helpers;
2
3 import java.util.*;
4 import java.util.regex.Matcher;
5 import java.util.regex.Pattern;
6
7
8
9
10
11
12
13
14
15
16
17
18
19 public class SAGAPatternFinder {
20 private static final Pattern PATTERN = Pattern.compile("([^=]*)(=(.*))?");
21 private Map<String,String[]> m_attributes;
22
23 public SAGAPatternFinder(Map<String,String[]> attributes) {
24 m_attributes = attributes;
25 }
26
27 public String[] findKey(String... valuePatterns) {
28 List<String> foundKeys = new ArrayList<String>();
29 for (Map.Entry<String,String[]> attribute : m_attributes.entrySet()) {
30 if (matches(attribute, valuePatterns)) {
31 foundKeys.add(attribute.getKey());
32 }
33 }
34 return foundKeys.toArray(new String[foundKeys.size()]);
35 }
36
37 private static boolean matches(Map.Entry<String,String[]> attribute, String... patterns) {
38 for (String pattern : patterns) {
39 Matcher matcher = PATTERN.matcher(pattern);
40 if (matcher.matches()) {
41 String keyPattern = matcher.group(1);
42 Pattern keyRegexp = SAGAPattern.toRegexp(keyPattern);
43 if (keyRegexp == null) {
44 return true;
45 } else {
46 if (keyRegexp.matcher(attribute.getKey()).matches()) {
47 String valuePattern = matcher.group(3);
48 Pattern valueRegexp = SAGAPattern.toRegexp(valuePattern);
49 if (valueRegexp == null) {
50 return true;
51 } else {
52 String[] values = attribute.getValue();
53 for (int i=0; i<values.length; i++) {
54 if (valueRegexp.matcher(values[i]).matches()) {
55 return true;
56 }
57 }
58 }
59 }
60 }
61 }
62 }
63 return false;
64 }
65 }