blob: a42b5e0af01679467b070d1feab80774416420eb [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +00001package aQute.bnd.make;
2
3import java.util.*;
4import java.util.Map.Entry;
5import java.util.regex.*;
6
7import aQute.bnd.service.*;
8import aQute.lib.osgi.*;
9import aQute.libg.header.*;
10
11public class Make {
Stuart McCulloch2286f232012-06-15 13:27:53 +000012 Builder builder;
13 Map<Instruction,Map<String,String>> make;
Stuart McCullochbb014372012-06-07 21:57:32 +000014
15 public Make(Builder builder) {
16 this.builder = builder;
17 }
18
19 public Resource process(String source) {
Stuart McCulloch2286f232012-06-15 13:27:53 +000020 Map<Instruction,Map<String,String>> make = getMakeHeader();
Stuart McCullochbb014372012-06-07 21:57:32 +000021 builder.trace("make " + source);
22
Stuart McCulloch2286f232012-06-15 13:27:53 +000023 for (Map.Entry<Instruction,Map<String,String>> entry : make.entrySet()) {
Stuart McCullochbb014372012-06-07 21:57:32 +000024 Instruction instr = entry.getKey();
25 Matcher m = instr.getMatcher(source);
26 if (m.matches() || instr.isNegated()) {
Stuart McCulloch2286f232012-06-15 13:27:53 +000027 Map<String,String> arguments = replace(m, entry.getValue());
Stuart McCullochbb014372012-06-07 21:57:32 +000028 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 McCulloch2286f232012-06-15 13:27:53 +000033 builder.trace("Made " + source + " from args " + arguments + " with " + plugin);
Stuart McCullochbb014372012-06-07 21:57:32 +000034 return resource;
35 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000036 }
37 catch (Exception e) {
38 builder.error("Plugin " + plugin + " generates error when use in making " + source
39 + " with args " + arguments, e);
Stuart McCullochbb014372012-06-07 21:57:32 +000040 }
41 }
42 }
43 }
44 return null;
45 }
46
Stuart McCulloch2286f232012-06-15 13:27:53 +000047 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 McCullochbb014372012-06-07 21:57:32 +000050 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 McCulloch2286f232012-06-15 13:27:53 +000080 Map<Instruction,Map<String,String>> getMakeHeader() {
Stuart McCullochbb014372012-06-07 21:57:32 +000081 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 McCulloch2286f232012-06-15 13:27:53 +000087
88 for (Entry<String,Attrs> entry : make.entrySet()) {
Stuart McCullochbb014372012-06-07 21:57:32 +000089 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}