Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.libg.clauses; |
| 2 | |
| 3 | import java.util.*; |
| 4 | |
| 5 | import aQute.libg.log.*; |
| 6 | import aQute.libg.qtokens.*; |
| 7 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 8 | public class Clauses extends LinkedHashMap<String,Map<String,String>> { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 9 | private static final long serialVersionUID = 1L; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 10 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 11 | /** |
| 12 | * Standard OSGi header parser. This parser can handle the format clauses |
| 13 | * ::= clause ( ',' clause ) + clause ::= name ( ';' name ) (';' key '=' |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 14 | * value ) This is mapped to a Map { name => Map { attr|directive => value } |
| 15 | * } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 16 | * |
| 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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 38 | throw new IllegalArgumentException("Header contains name field after attribute or directive: " |
| 39 | + adname + " from " + value); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 40 | 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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 52 | logger.warning("Duplicate package name in header: " + packageName |
| 53 | + ". Multiple package names in one clause not supported in Bnd."); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 54 | } else |
| 55 | result.put(packageName, clause); |
| 56 | } |
| 57 | } while (del == ','); |
| 58 | return result; |
| 59 | } |
| 60 | |
| 61 | } |