View Javadoc

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   * An aggregate of all resource descriptors within the given plugin module or plugin.
11   *
12   * @see com.atlassian.plugin.impl.AbstractPlugin#resources
13   * @see com.atlassian.plugin.descriptors.AbstractModuleDescriptor#resources
14   */
15  public class Resources implements Resourced
16  {
17      public static final Resources EMPTY_RESOURCES = new Resources(Collections.<ResourceDescriptor>emptyList());
18      private List<ResourceDescriptor> resourceDescriptors;
19  
20      /**
21       * Parses the resource descriptors from the provided plugin XML element and creates a Resources object containing them.
22       * <p/>
23       * If the module or plugin contains no resource elements, an empty Resources object will be returned. This method will
24       * not return null.
25       *
26       * @param element the plugin or plugin module XML fragment which should not be null
27       * @return a Resources object representing the resources in the plugin or plugin module
28       * @throws PluginParseException if there are two resources with the same name and type in this element, or another parse error
29       * occurs
30       * @throws IllegalArgumentException if the provided element is null
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<Element> elements = element.elements("resource");
38  
39          List<ResourceDescriptor> templates = new ArrayList<ResourceDescriptor>(elements.size());
40  
41          for (Element e : elements)
42          {
43              final ResourceDescriptor resourceDescriptor = new ResourceDescriptor(e);
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       * Create a resource object with the given resource descriptors. The provided list must not be null.
56       *
57       * @param resourceDescriptors the descriptors which are part of this resources object
58       * @throws IllegalArgumentException if the resourceDescriptors list is null
59       */
60      public Resources(List<ResourceDescriptor> 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<ResourceDescriptor> getResourceDescriptors()
68      {
69          return resourceDescriptors;
70      }
71  
72      public List<ResourceDescriptor> getResourceDescriptors(String type)
73      {
74          List<ResourceDescriptor> typedResourceDescriptors = new LinkedList<ResourceDescriptor>();
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      * @deprecated
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 }