1 package com.atlassian.plugin;
2
3 import static com.atlassian.plugin.util.Assertions.notNull;
4 import static com.google.common.collect.Iterables.filter;
5
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.List;
9
10 import org.dom4j.Element;
11
12 import com.atlassian.plugin.elements.ResourceDescriptor;
13 import com.atlassian.plugin.elements.ResourceLocation;
14 import com.atlassian.util.concurrent.Assertions;
15 import com.google.common.base.Predicate;
16 import com.google.common.collect.ImmutableList;
17
18
19
20
21
22
23
24
25 public class Resources implements Resourced
26 {
27 public static final Resources EMPTY_RESOURCES = new Resources(Collections.<ResourceDescriptor> emptyList());
28
29 private final List<ResourceDescriptor> resourceDescriptors;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public static Resources fromXml(final Element element) throws PluginParseException, IllegalArgumentException
47 {
48 if (element == null)
49 {
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 List<ResourceDescriptor> templates = new ArrayList<ResourceDescriptor>(elements.size());
57
58 for (final Element e : elements)
59 {
60 final ResourceDescriptor resourceDescriptor = new ResourceDescriptor(e);
61
62 if (templates.contains(resourceDescriptor))
63 {
64 throw new PluginParseException("Duplicate resource with type '" + resourceDescriptor.getType() + "' and name '" + resourceDescriptor.getName() + "' found");
65 }
66
67 templates.add(resourceDescriptor);
68 }
69 return new Resources(templates);
70 }
71
72
73
74
75
76
77
78
79
80 public Resources(final Iterable<ResourceDescriptor> resourceDescriptors) throws IllegalArgumentException
81 {
82 Assertions.notNull("Resources cannot be created with a null resources list. Pass empty list instead", resourceDescriptors);
83 this.resourceDescriptors = ImmutableList.<ResourceDescriptor> builder().addAll(resourceDescriptors).build();
84 }
85
86 public List<ResourceDescriptor> getResourceDescriptors()
87 {
88 return resourceDescriptors;
89 }
90
91
92
93
94
95 @Deprecated
96 public List<ResourceDescriptor> getResourceDescriptors(final String type)
97 {
98 return ImmutableList.<ResourceDescriptor> builder().addAll(filter(resourceDescriptors, new TypeFilter(type))).build();
99 }
100
101 public ResourceLocation getResourceLocation(final String type, final String name)
102 {
103 for (final ResourceDescriptor resourceDescriptor : resourceDescriptors)
104 {
105 if (resourceDescriptor.doesTypeAndNameMatch(type, name))
106 {
107 return resourceDescriptor.getResourceLocationForName(name);
108 }
109 }
110 return null;
111 }
112
113 public ResourceDescriptor getResourceDescriptor(final String type, final String name)
114 {
115 for (final ResourceDescriptor resourceDescriptor : resourceDescriptors)
116 {
117 if (resourceDescriptor.getType().equalsIgnoreCase(type) && resourceDescriptor.getName().equalsIgnoreCase(name))
118 {
119 return resourceDescriptor;
120 }
121 }
122 return null;
123 }
124
125 public static class TypeFilter implements Predicate<ResourceDescriptor>
126 {
127 private final String type;
128
129 public TypeFilter(final String type)
130 {
131 this.type = notNull("type", type);
132 }
133
134 public boolean apply(final ResourceDescriptor input)
135 {
136
137 return type.equals(input.getType());
138 }
139 }
140 }