Stuart McCulloch | 26e7a5a | 2011-10-17 10:31:43 +0000 | [diff] [blame^] | 1 | package aQute.lib.deployer.obr; |
| 2 | |
| 3 | import java.util.Collection; |
| 4 | import java.util.Collections; |
| 5 | import java.util.HashMap; |
| 6 | import java.util.HashSet; |
| 7 | import java.util.LinkedList; |
| 8 | import java.util.List; |
| 9 | import java.util.Map; |
| 10 | import java.util.Set; |
| 11 | |
| 12 | import org.xml.sax.Attributes; |
| 13 | |
| 14 | import aQute.libg.sax.filters.ElementSelectionFilter; |
| 15 | |
| 16 | public class UniqueResourceFilter extends ElementSelectionFilter { |
| 17 | |
| 18 | final Set<String> uris = new HashSet<String>(); |
| 19 | final Map<String, List<String>> filteredResources = new HashMap<String, List<String>>(); |
| 20 | |
| 21 | @Override |
| 22 | protected boolean select(int depth, String uri, String localName, String qName, Attributes attribs) { |
| 23 | if ("resource".equals(qName)) { |
| 24 | String resourceUri = attribs.getValue("uri"); |
| 25 | if (uris.contains(resourceUri)) { |
| 26 | addFilteredBundle(attribs.getValue("symbolicname"), attribs.getValue("version")); |
| 27 | return false; |
| 28 | } |
| 29 | uris.add(resourceUri); |
| 30 | } |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | private void addFilteredBundle(String bsn, String version) { |
| 35 | List<String> versions = filteredResources.get(bsn); |
| 36 | if (versions == null) { |
| 37 | versions = new LinkedList<String>(); |
| 38 | filteredResources.put(bsn, versions); |
| 39 | } |
| 40 | versions.add(version); |
| 41 | } |
| 42 | |
| 43 | public Collection<String> getFilteredBSNs() { |
| 44 | return filteredResources.keySet(); |
| 45 | } |
| 46 | |
| 47 | public Collection<String> getFilteredVersions(String bsn) { |
| 48 | List<String> list = filteredResources.get(bsn); |
| 49 | if (list == null) |
| 50 | list = Collections.emptyList(); |
| 51 | return list; |
| 52 | } |
| 53 | |
| 54 | } |