Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.bnd.make; |
| 2 | |
| 3 | import java.util.*; |
| 4 | import java.util.Map.Entry; |
| 5 | import java.util.regex.*; |
| 6 | |
| 7 | import aQute.bnd.service.*; |
| 8 | import aQute.lib.osgi.*; |
| 9 | import aQute.libg.header.*; |
| 10 | |
| 11 | public class Make { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 12 | Builder builder; |
| 13 | Map<Instruction,Map<String,String>> make; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 14 | |
| 15 | public Make(Builder builder) { |
| 16 | this.builder = builder; |
| 17 | } |
| 18 | |
| 19 | public Resource process(String source) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 20 | Map<Instruction,Map<String,String>> make = getMakeHeader(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 21 | builder.trace("make " + source); |
| 22 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 23 | for (Map.Entry<Instruction,Map<String,String>> entry : make.entrySet()) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 24 | Instruction instr = entry.getKey(); |
| 25 | Matcher m = instr.getMatcher(source); |
| 26 | if (m.matches() || instr.isNegated()) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 27 | Map<String,String> arguments = replace(m, entry.getValue()); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 28 | List<MakePlugin> plugins = builder.getPlugins(MakePlugin.class); |
| 29 | for (MakePlugin plugin : plugins) { |
| 30 | try { |
| 31 | Resource resource = plugin.make(builder, source, arguments); |
| 32 | if (resource != null) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 33 | builder.trace("Made " + source + " from args " + arguments + " with " + plugin); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 34 | return resource; |
| 35 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 36 | } |
| 37 | catch (Exception e) { |
| 38 | builder.error("Plugin " + plugin + " generates error when use in making " + source |
| 39 | + " with args " + arguments, e); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | return null; |
| 45 | } |
| 46 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 47 | private Map<String,String> replace(Matcher m, Map<String,String> value) { |
| 48 | Map<String,String> newArgs = Processor.newMap(); |
| 49 | for (Map.Entry<String,String> entry : value.entrySet()) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 50 | String s = entry.getValue(); |
| 51 | s = replace(m, s); |
| 52 | newArgs.put(entry.getKey(), s); |
| 53 | } |
| 54 | return newArgs; |
| 55 | } |
| 56 | |
| 57 | String replace(Matcher m, CharSequence s) { |
| 58 | StringBuilder sb = new StringBuilder(); |
| 59 | int max = '0' + m.groupCount() + 1; |
| 60 | for (int i = 0; i < s.length(); i++) { |
| 61 | char c = s.charAt(i); |
| 62 | if (c == '$' && i < s.length() - 1) { |
| 63 | c = s.charAt(++i); |
| 64 | if (c >= '0' && c <= max) { |
| 65 | int index = c - '0'; |
| 66 | String replacement = m.group(index); |
| 67 | if (replacement != null) |
| 68 | sb.append(replacement); |
| 69 | } else { |
| 70 | if (c == '$') |
| 71 | i++; |
| 72 | sb.append(c); |
| 73 | } |
| 74 | } else |
| 75 | sb.append(c); |
| 76 | } |
| 77 | return sb.toString(); |
| 78 | } |
| 79 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 80 | Map<Instruction,Map<String,String>> getMakeHeader() { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 81 | if (make != null) |
| 82 | return make; |
| 83 | make = Processor.newMap(); |
| 84 | |
| 85 | String s = builder.getProperty(Builder.MAKE); |
| 86 | Parameters make = builder.parseHeader(s); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 87 | |
| 88 | for (Entry<String,Attrs> entry : make.entrySet()) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 89 | String pattern = Processor.removeDuplicateMarker(entry.getKey()); |
| 90 | |
| 91 | Instruction instr = new Instruction(pattern); |
| 92 | this.make.put(instr, entry.getValue()); |
| 93 | } |
| 94 | |
| 95 | return this.make; |
| 96 | } |
| 97 | } |