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.HashSet;
9 import java.util.List;
10 import java.util.Set;
11
12 import com.atlassian.util.concurrent.ResettableLazyReference;
13 import com.google.common.base.Function;
14 import com.google.common.collect.Iterables;
15 import com.google.common.collect.Lists;
16 import org.dom4j.Element;
17
18 import com.atlassian.plugin.elements.ResourceDescriptor;
19 import com.atlassian.plugin.elements.ResourceLocation;
20 import com.atlassian.util.concurrent.Assertions;
21 import com.google.common.base.Predicate;
22 import com.google.common.collect.ImmutableList;
23
24 import javax.annotation.Nullable;
25
26
27
28
29
30
31
32
33 public class Resources implements Resourced
34 {
35 public static final Resources EMPTY_RESOURCES = new Resources((Element) null);
36
37 private final List<ResourceDescriptor> resourceDescriptors;
38 private final Element element;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public static Resources fromXml(final Element element) throws PluginParseException, IllegalArgumentException
56 {
57 if (element == null)
58 {
59 throw new IllegalArgumentException("Cannot parse resources from null XML element");
60 }
61
62 @SuppressWarnings("unchecked")
63 final List<Element> elements = element.elements("resource");
64
65 final Set<ResourceDescriptor> templates = new HashSet<ResourceDescriptor>();
66
67 for (final Element e : elements)
68 {
69 final ResourceDescriptor resourceDescriptor = new ResourceDescriptor(e);
70
71 if (templates.contains(resourceDescriptor))
72 {
73 throw new PluginParseException("Duplicate resource with type '" + resourceDescriptor.getType() + "' and name '" + resourceDescriptor.getName() + "' found");
74 }
75
76 templates.add(resourceDescriptor);
77 }
78
79 return new Resources(element);
80 }
81
82
83
84
85
86
87 private Resources(final Element element)
88 {
89 this.resourceDescriptors = Collections.emptyList();
90 this.element = element;
91 }
92
93
94
95
96
97
98
99
100
101
102
103 @Deprecated
104 public Resources(final Iterable<ResourceDescriptor> resourceDescriptors) throws IllegalArgumentException
105 {
106 Assertions.notNull("Resources cannot be created with a null resources list. Pass empty list instead", resourceDescriptors);
107 this.resourceDescriptors = ImmutableList.<ResourceDescriptor> builder().addAll(resourceDescriptors).build();
108 this.element = null;
109 }
110
111 public List<ResourceDescriptor> getResourceDescriptors()
112 {
113 if (element != null) {
114 return Lists.transform(element.elements("resource"), new Function<Element, ResourceDescriptor>() {
115
116 @Override
117 public ResourceDescriptor apply(@Nullable final Element e)
118 {
119 return new ResourceDescriptor(e);
120 }
121 });
122 } else {
123 return resourceDescriptors;
124 }
125 }
126
127
128
129
130
131 @Deprecated
132 public List<ResourceDescriptor> getResourceDescriptors(final String type)
133 {
134 return ImmutableList.<ResourceDescriptor> builder().addAll(filter(getResourceDescriptors(), new TypeFilter(type))).build();
135 }
136
137 public ResourceLocation getResourceLocation(final String type, final String name)
138 {
139 for (final ResourceDescriptor resourceDescriptor : getResourceDescriptors())
140 {
141 if (resourceDescriptor.doesTypeAndNameMatch(type, name))
142 {
143 return resourceDescriptor.getResourceLocationForName(name);
144 }
145 }
146 return null;
147 }
148
149 public ResourceDescriptor getResourceDescriptor(final String type, final String name)
150 {
151 for (final ResourceDescriptor resourceDescriptor : getResourceDescriptors())
152 {
153 if (resourceDescriptor.getType().equalsIgnoreCase(type) && resourceDescriptor.getName().equalsIgnoreCase(name))
154 {
155 return resourceDescriptor;
156 }
157 }
158 return null;
159 }
160
161 public static class TypeFilter implements Predicate<ResourceDescriptor>
162 {
163 private final String type;
164
165 public TypeFilter(final String type)
166 {
167 this.type = notNull("type", type);
168 }
169
170 public boolean apply(final ResourceDescriptor input)
171 {
172
173 return type.equals(input.getType());
174 }
175 }
176 }