1   package org.daisy.pipeline.css;
2   
3   import java.net.URL;
4   import java.util.ArrayList;
5   import java.util.Collection;
6   
7   import static com.google.common.collect.Iterables.any;
8   
9   import org.osgi.service.component.annotations.Component;
10  import org.osgi.service.component.annotations.Reference;
11  import org.osgi.service.component.annotations.ReferenceCardinality;
12  import org.osgi.service.component.annotations.ReferencePolicy;
13  
14  @Component(
15  	name = "UserAgentStylesheetRegistry",
16  	service = { UserAgentStylesheetRegistry.class }
17  )
18  public class UserAgentStylesheetRegistry {
19  
20  	/**
21  	 * Return the user agent style sheet(s) for the given stylesheet types, content type
22  	 * and medium. If there are multiple style sheets, their order is undetermined.
23  	 */
24  	public Collection<URL> get(Collection<String> types, Collection<String> contentTypes, Collection<Medium> media) {
25  		Collection<URL> filtered = new ArrayList<>();
26  		for (UserAgentStylesheet s : stylesheets)
27  			if (types.contains(s.getType()))
28  				if (any(contentTypes, s::matchesContentType))
29  					if (any(media, s::matchesMedium))
30  						filtered.add(s.getURL());
31  		return filtered;
32  	}
33  
34  	private Collection<UserAgentStylesheet> stylesheets = new ArrayList<>();
35  
36  	@Reference(
37  		name = "UserAgentStylesheet",
38  		unbind = "-",
39  		service = UserAgentStylesheet.class,
40  		cardinality = ReferenceCardinality.MULTIPLE,
41  		policy = ReferencePolicy.STATIC
42  	)
43  	protected void addStylesheet(UserAgentStylesheet stylesheet) {
44  		stylesheets.add(stylesheet);
45  	}
46  }