View Javadoc

1   package com.atlassian.plugin;
2   
3   import com.atlassian.plugin.elements.ResourceDescriptor;
4   import com.atlassian.plugin.elements.ResourceLocation;
5   
6   import org.dom4j.Element;
7   
8   import java.util.ArrayList;
9   import java.util.Collections;
10  import java.util.LinkedList;
11  import java.util.List;
12  
13  /**
14   * An aggregate of all resource descriptors within the given plugin module or plugin.
15   *
16   * @see com.atlassian.plugin.impl.AbstractPlugin#resources
17   * @see com.atlassian.plugin.descriptors.AbstractModuleDescriptor#resources
18   */
19  public class Resources implements Resourced
20  {
21      public static final Resources EMPTY_RESOURCES = new Resources(Collections.<ResourceDescriptor> emptyList());
22  
23      private final List<ResourceDescriptor> resourceDescriptors;
24  
25      /**
26       * Parses the resource descriptors from the provided plugin XML element and creates a Resources object containing them.
27       * <p/>
28       * If the module or plugin contains no resource elements, an empty Resources object will be returned. This method will
29       * not return null.
30       *
31       * @param element the plugin or plugin module XML fragment which should not be null
32       * @return a Resources object representing the resources in the plugin or plugin module
33       * @throws PluginParseException if there are two resources with the same name and type in this element, or another parse error
34       * occurs
35       * @throws IllegalArgumentException if the provided element is null
36       */
37      public static Resources fromXml(final Element element) throws PluginParseException, IllegalArgumentException
38      {
39          if (element == null)
40          {
41              throw new IllegalArgumentException("Cannot parse resources from null XML element");
42          }
43  
44          @SuppressWarnings("unchecked")
45          final List<Element> elements = element.elements("resource");
46  
47          final List<ResourceDescriptor> templates = new ArrayList<ResourceDescriptor>(elements.size());
48  
49          for (final Element e : elements)
50          {
51              final ResourceDescriptor resourceDescriptor = new ResourceDescriptor(e);
52  
53              if (templates.contains(resourceDescriptor))
54              {
55                  throw new PluginParseException(
56                      "Duplicate resource with type '" + resourceDescriptor.getType() + "' and name '" + resourceDescriptor.getName() + "' found");
57              }
58  
59              templates.add(resourceDescriptor);
60          }
61          return new Resources(templates);
62      }
63  
64      /**
65       * Create a resource object with the given resource descriptors. The provided list must not be null.
66       *
67       * @param resourceDescriptors the descriptors which are part of this resources object
68       * @throws IllegalArgumentException if the resourceDescriptors list is null
69       */
70      public Resources(final List<ResourceDescriptor> resourceDescriptors) throws IllegalArgumentException
71      {
72          if (resourceDescriptors == null)
73          {
74              throw new IllegalArgumentException("Resources cannot be created with a null resources list. Pass empty list instead");
75          }
76          this.resourceDescriptors = Collections.unmodifiableList(new ArrayList<ResourceDescriptor>(resourceDescriptors));
77      }
78  
79      public List<ResourceDescriptor> getResourceDescriptors()
80      {
81          return resourceDescriptors;
82      }
83  
84      public List<ResourceDescriptor> getResourceDescriptors(final String type)
85      {
86          final List<ResourceDescriptor> typedResourceDescriptors = new LinkedList<ResourceDescriptor>();
87          for (final ResourceDescriptor resourceDescriptor : resourceDescriptors)
88          {
89              if (resourceDescriptor.getType().equalsIgnoreCase(type))
90              {
91                  typedResourceDescriptors.add(resourceDescriptor);
92              }
93          }
94          return Collections.unmodifiableList(typedResourceDescriptors);
95      }
96  
97      public ResourceLocation getResourceLocation(final String type, final String name)
98      {
99          for (final ResourceDescriptor resourceDescriptor : resourceDescriptors)
100         {
101             if (resourceDescriptor.doesTypeAndNameMatch(type, name))
102             {
103                 return resourceDescriptor.getResourceLocationForName(name);
104             }
105         }
106         return null;
107     }
108 
109     public ResourceDescriptor getResourceDescriptor(final String type, final String name)
110     {
111         for (final ResourceDescriptor resourceDescriptor : resourceDescriptors)
112         {
113             if (resourceDescriptor.getType().equalsIgnoreCase(type) && resourceDescriptor.getName().equalsIgnoreCase(name))
114             {
115                 return resourceDescriptor;
116             }
117         }
118         return null;
119     }
120 }