View Javadoc

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   * An aggregate of all resource descriptors within the given plugin module or
20   * plugin.
21   * 
22   * @see com.atlassian.plugin.impl.AbstractPlugin#resources
23   * @see com.atlassian.plugin.descriptors.AbstractModuleDescriptor#resources
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       * Parses the resource descriptors from the provided plugin XML element and
33       * creates a Resources object containing them.
34       * <p/>
35       * If the module or plugin contains no resource elements, an empty Resources
36       * object will be returned. This method will not return null.
37       * 
38       * @param element the plugin or plugin module XML fragment which should not
39       *            be null
40       * @return a Resources object representing the resources in the plugin or
41       *         plugin module
42       * @throws PluginParseException if there are two resources with the same
43       *             name and type in this element, or another parse error occurs
44       * @throws IllegalArgumentException if the provided element is null
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       * Create a resource object with the given resource descriptors. The
74       * provided list must not be null.
75       * 
76       * @param resourceDescriptors the descriptors which are part of this
77       *            resources object
78       * @throws IllegalArgumentException if the resourceDescriptors list is null
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       * * @deprecated since 2.5.0 use {@link #getResourceDescriptors()} and
93       * filter as required
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             // TODO Auto-generated method stub
137             return type.equals(input.getType());
138         }
139     }
140 }