blob: 5701babebc92d2dd9ff8f72e673443f2ea92e3d8 [file] [log] [blame]
Stuart McCullochf3173222012-06-07 21:57:32 +00001package aQute.libg.clauses;
2
3import java.util.*;
4
5import aQute.libg.log.*;
6import aQute.libg.qtokens.*;
7
Stuart McCulloch4482c702012-06-15 13:27:53 +00008public class Clauses extends LinkedHashMap<String,Map<String,String>> {
Stuart McCullochf3173222012-06-07 21:57:32 +00009 private static final long serialVersionUID = 1L;
Stuart McCulloch4482c702012-06-15 13:27:53 +000010
Stuart McCullochf3173222012-06-07 21:57:32 +000011 /**
12 * Standard OSGi header parser. This parser can handle the format clauses
13 * ::= clause ( ',' clause ) + clause ::= name ( ';' name ) (';' key '='
Stuart McCulloch4482c702012-06-15 13:27:53 +000014 * value ) This is mapped to a Map { name => Map { attr|directive => value }
15 * }
Stuart McCullochf3173222012-06-07 21:57:32 +000016 *
17 * @param value
18 * @return
19 * @throws MojoExecutionException
20 */
21 static public Clauses parse(String value, Logger logger) {
22 if (value == null || value.trim().length() == 0)
23 return new Clauses();
24
25 Clauses result = new Clauses();
26 QuotedTokenizer qt = new QuotedTokenizer(value, ";=,");
27 char del;
28 do {
29 boolean hadAttribute = false;
30 Clause clause = new Clause();
31 List<String> aliases = new ArrayList<String>();
32 aliases.add(qt.nextToken());
33 del = qt.getSeparator();
34 while (del == ';') {
35 String adname = qt.nextToken();
36 if ((del = qt.getSeparator()) != '=') {
37 if (hadAttribute)
Stuart McCulloch4482c702012-06-15 13:27:53 +000038 throw new IllegalArgumentException("Header contains name field after attribute or directive: "
39 + adname + " from " + value);
Stuart McCullochf3173222012-06-07 21:57:32 +000040 aliases.add(adname);
41 } else {
42 String advalue = qt.nextToken();
43 clause.put(adname, advalue);
44 del = qt.getSeparator();
45 hadAttribute = true;
46 }
47 }
48 for (Iterator<String> i = aliases.iterator(); i.hasNext();) {
49 String packageName = i.next();
50 if (result.containsKey(packageName)) {
51 if (logger != null)
Stuart McCulloch4482c702012-06-15 13:27:53 +000052 logger.warning("Duplicate package name in header: " + packageName
53 + ". Multiple package names in one clause not supported in Bnd.");
Stuart McCullochf3173222012-06-07 21:57:32 +000054 } else
55 result.put(packageName, clause);
56 }
57 } while (del == ',');
58 return result;
59 }
60
61}