1 package com.atlassian.plugin;
2
3 import com.atlassian.plugin.elements.ResourceDescriptor;
4 import com.atlassian.plugin.elements.ResourceLocation;
5 import org.dom4j.Element;
6
7 import java.util.*;
8
9
10
11
12
13
14
15 public class Resources implements Resourced
16 {
17 public static final Resources EMPTY_RESOURCES = new Resources(Collections.EMPTY_LIST);
18 private List resourceDescriptors;
19
20
21
22
23
24
25
26
27
28
29
30
31
32 public static Resources fromXml(Element element) throws PluginParseException, IllegalArgumentException
33 {
34 if (element == null)
35 throw new IllegalArgumentException("Cannot parse resources from null XML element");
36
37 List elements = element.elements("resource");
38
39 List templates = new ArrayList(elements.size());
40
41 for (Iterator iterator = elements.iterator(); iterator.hasNext();)
42 {
43 final ResourceDescriptor resourceDescriptor = new ResourceDescriptor((Element) iterator.next());
44
45 if (templates.contains(resourceDescriptor))
46 throw new PluginParseException("Duplicate resource with type '" + resourceDescriptor.getType() + "' and name '" + resourceDescriptor.getName() + "' found");
47
48 templates.add(resourceDescriptor);
49 }
50
51 return new Resources(templates);
52 }
53
54
55
56
57
58
59
60 public Resources(List resourceDescriptors) throws IllegalArgumentException
61 {
62 if (resourceDescriptors == null)
63 throw new IllegalArgumentException("Resources cannot be created with a null resources list. Pass empty list instead");
64 this.resourceDescriptors = resourceDescriptors;
65 }
66
67 public List getResourceDescriptors()
68 {
69 return resourceDescriptors;
70 }
71
72 public List getResourceDescriptors(String type)
73 {
74 List typedResourceDescriptors = new LinkedList();
75
76 for (Iterator iterator = resourceDescriptors.iterator(); iterator.hasNext();)
77 {
78 ResourceDescriptor resourceDescriptor = (ResourceDescriptor) iterator.next();
79 if (resourceDescriptor.getType().equalsIgnoreCase(type))
80 {
81 typedResourceDescriptors.add(resourceDescriptor);
82 }
83 }
84
85 return typedResourceDescriptors;
86 }
87
88 public ResourceLocation getResourceLocation(String type, String name)
89 {
90 for (Iterator iterator = resourceDescriptors.iterator(); iterator.hasNext();)
91 {
92 ResourceDescriptor resourceDescriptor = (ResourceDescriptor) iterator.next();
93 if (resourceDescriptor.doesTypeAndNameMatch(type, name))
94 {
95 return resourceDescriptor.getResourceLocationForName(name);
96 }
97 }
98
99 return null;
100 }
101
102
103
104
105 public ResourceDescriptor getResourceDescriptor(String type, String name)
106 {
107 for (Iterator iterator = resourceDescriptors.iterator(); iterator.hasNext();)
108 {
109 ResourceDescriptor resourceDescriptor = (ResourceDescriptor) iterator.next();
110 if (resourceDescriptor.getType().equalsIgnoreCase(type) && resourceDescriptor.getName().equalsIgnoreCase(name))
111 {
112 return resourceDescriptor;
113 }
114 }
115
116 return null;
117 }
118 }