1 package com.atlassian.plugin;
2
3 import com.atlassian.plugin.elements.ResourceDescriptor;
4 import com.atlassian.plugin.elements.ResourceLocation;
5 import com.google.common.base.Function;
6 import com.google.common.base.Predicate;
7 import com.google.common.collect.ImmutableList;
8 import com.google.common.collect.Iterables;
9 import com.google.common.collect.Lists;
10 import org.dom4j.Element;
11
12 import javax.annotation.Nullable;
13 import java.util.Collections;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Set;
17
18 import static com.atlassian.plugin.util.Assertions.notNull;
19 import static com.google.common.collect.Iterables.filter;
20
21
22
23
24
25
26
27
28 public class Resources implements Resourced {
29 public static final Resources EMPTY_RESOURCES = new Resources((Element) null);
30
31 private final List<ResourceDescriptor> resourceDescriptors;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public static Resources fromXml(final Element element) throws PluginParseException, IllegalArgumentException {
49 if (element == null) {
50 throw new IllegalArgumentException("Cannot parse resources from null XML element");
51 }
52
53 @SuppressWarnings("unchecked")
54 final List<Element> elements = element.elements("resource");
55
56 final Set<ResourceDescriptor> templates = new HashSet<>();
57
58 for (final Element e : elements) {
59 final ResourceDescriptor resourceDescriptor = new ResourceDescriptor(e);
60
61 if (templates.contains(resourceDescriptor)) {
62 throw new PluginParseException("Duplicate resource with type '" + resourceDescriptor.getType() + "' and name '" + resourceDescriptor.getName() + "' found");
63 }
64
65 templates.add(resourceDescriptor);
66 }
67
68 return new Resources(element);
69 }
70
71
72
73
74
75
76 private Resources(final Element element) {
77 if (element != null) {
78 this.resourceDescriptors = Lists.newArrayList(Iterables.transform(element.elements("resource"), new Function<Element, ResourceDescriptor>() {
79
80 @Override
81 public ResourceDescriptor apply(@Nullable final Element e) {
82 return new ResourceDescriptor(e);
83 }
84 }));
85 } else {
86 this.resourceDescriptors = Collections.emptyList();
87 }
88 }
89
90 public List<ResourceDescriptor> getResourceDescriptors() {
91 return resourceDescriptors;
92 }
93
94 public ResourceLocation getResourceLocation(final String type, final String name) {
95 for (final ResourceDescriptor resourceDescriptor : getResourceDescriptors()) {
96 if (resourceDescriptor.doesTypeAndNameMatch(type, name)) {
97 return resourceDescriptor.getResourceLocationForName(name);
98 }
99 }
100 return null;
101 }
102
103 public ResourceDescriptor getResourceDescriptor(final String type, final String name) {
104 for (final ResourceDescriptor resourceDescriptor : getResourceDescriptors()) {
105 if (resourceDescriptor.getType().equalsIgnoreCase(type) && resourceDescriptor.getName().equalsIgnoreCase(name)) {
106 return resourceDescriptor;
107 }
108 }
109 return null;
110 }
111
112 public static class TypeFilter implements Predicate<ResourceDescriptor> {
113 private final String type;
114
115 public TypeFilter(final String type) {
116 this.type = notNull("type", type);
117 }
118
119 public boolean apply(final ResourceDescriptor input) {
120
121 return type.equals(input.getType());
122 }
123 }
124 }