Stuart McCulloch | 26e7a5a | 2011-10-17 10:31:43 +0000 | [diff] [blame] | 1 | package aQute.lib.osgi; |
| 2 | |
| 3 | import java.util.*; |
| 4 | import java.util.regex.*; |
| 5 | |
| 6 | import aQute.libg.generics.*; |
| 7 | |
| 8 | public class Instruction { |
| 9 | Pattern pattern; |
| 10 | String instruction; |
| 11 | boolean negated; |
| 12 | boolean optional; |
| 13 | |
| 14 | public Instruction(String instruction, boolean negated) { |
| 15 | this.instruction = instruction; |
| 16 | this.negated = negated; |
| 17 | } |
| 18 | |
| 19 | public boolean matches(String value) { |
| 20 | return getMatcher(value).matches(); |
| 21 | } |
| 22 | |
| 23 | public boolean isNegated() { |
| 24 | return negated; |
| 25 | } |
| 26 | |
| 27 | public String getPattern() { |
| 28 | return instruction; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Convert a string based pattern to a regular expression based pattern. |
| 33 | * This is called an instruction, this object makes it easier to handle the |
| 34 | * different cases |
| 35 | * |
| 36 | * @param string |
| 37 | * @return |
| 38 | */ |
| 39 | public static Instruction getPattern(String string) { |
| 40 | boolean negated = false; |
| 41 | if (string.startsWith("!")) { |
| 42 | negated = true; |
| 43 | string = string.substring(1); |
| 44 | } |
| 45 | StringBuffer sb = new StringBuffer(); |
| 46 | for (int c = 0; c < string.length(); c++) { |
| 47 | switch (string.charAt(c)) { |
| 48 | case '.': |
| 49 | sb.append("\\."); |
| 50 | break; |
| 51 | case '*': |
| 52 | sb.append(".*"); |
| 53 | break; |
| 54 | case '?': |
| 55 | sb.append(".?"); |
| 56 | break; |
| 57 | default: |
| 58 | sb.append(string.charAt(c)); |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | string = sb.toString(); |
| 63 | if (string.endsWith("\\..*")) { |
| 64 | sb.append("|"); |
| 65 | sb.append(string.substring(0, string.length() - 4)); |
| 66 | } |
| 67 | return new Instruction(sb.toString(), negated); |
| 68 | } |
| 69 | |
| 70 | public String toString() { |
| 71 | return getPattern(); |
| 72 | } |
| 73 | |
| 74 | public Matcher getMatcher(String value) { |
| 75 | if (pattern == null) { |
| 76 | pattern = Pattern.compile(instruction); |
| 77 | } |
| 78 | return pattern.matcher(value); |
| 79 | } |
| 80 | |
| 81 | public int hashCode() { |
| 82 | return instruction.hashCode(); |
| 83 | } |
| 84 | |
| 85 | public boolean equals(Object other) { |
| 86 | return other != null && (other instanceof Instruction) |
| 87 | && instruction.equals(((Instruction) other).instruction); |
| 88 | } |
| 89 | |
| 90 | public void setOptional() { |
| 91 | optional = true; |
| 92 | } |
| 93 | |
| 94 | public boolean isOptional() { |
| 95 | return optional; |
| 96 | } |
| 97 | |
| 98 | public static Map<Instruction, Map<String, String>> replaceWithInstruction( |
| 99 | Map<String, Map<String, String>> header) { |
| 100 | Map<Instruction, Map<String, String>> map = Processor.newMap(); |
| 101 | for (Iterator<Map.Entry<String, Map<String, String>>> e = header |
| 102 | .entrySet().iterator(); e.hasNext();) { |
| 103 | Map.Entry<String, Map<String, String>> entry = e.next(); |
| 104 | String pattern = entry.getKey(); |
| 105 | Instruction instr = getPattern(pattern); |
| 106 | String presence = entry.getValue() |
| 107 | .get(Constants.PRESENCE_DIRECTIVE); |
| 108 | if ("optional".equals(presence)) |
| 109 | instr.setOptional(); |
| 110 | map.put(instr, entry.getValue()); |
| 111 | } |
| 112 | return map; |
| 113 | } |
| 114 | |
| 115 | public static <T> Collection<T> select(Collection<Instruction> matchers, |
| 116 | Collection<T> targets) { |
| 117 | Collection<T> result = Create.list(); |
| 118 | outer: for (T t : targets) { |
| 119 | String s = t.toString(); |
| 120 | for (Instruction i : matchers) { |
| 121 | if (i.matches(s)) { |
| 122 | if (!i.isNegated()) |
| 123 | result.add(t); |
| 124 | continue outer; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | return result; |
| 129 | } |
| 130 | } |