View Javadoc
1   package com.atlassian.plugin;
2   
3   import com.atlassian.plugin.elements.ResourceDescriptor;
4   import com.atlassian.plugin.elements.ResourceLocation;
5   import com.google.common.base.Function;
6   import com.google.common.base.Predicate;
7   import com.google.common.collect.ImmutableList;
8   import com.google.common.collect.Iterables;
9   import com.google.common.collect.Lists;
10  import org.dom4j.Element;
11  
12  import javax.annotation.Nullable;
13  import java.util.Collections;
14  import java.util.HashSet;
15  import java.util.List;
16  import java.util.Set;
17  
18  import static com.atlassian.plugin.util.Assertions.notNull;
19  import static com.google.common.collect.Iterables.filter;
20  
21  /**
22   * An aggregate of all resource descriptors within the given plugin module or
23   * plugin.
24   *
25   * @see com.atlassian.plugin.impl.AbstractPlugin#resources
26   * @see com.atlassian.plugin.descriptors.AbstractModuleDescriptor#resources
27   */
28  public class Resources implements Resourced {
29      public static final Resources EMPTY_RESOURCES = new Resources((Element) null);
30  
31      private final List<ResourceDescriptor> resourceDescriptors;
32  
33      /**
34       * Parses the resource descriptors from the provided plugin XML element and
35       * creates a Resources object containing them.
36       * <p>
37       * If the module or plugin contains no resource elements, an empty Resources
38       * object will be returned. This method will not return null.
39       *
40       * @param element the plugin or plugin module XML fragment which should not
41       *                be null
42       * @return a Resources object representing the resources in the plugin or
43       * plugin module
44       * @throws PluginParseException     if there are two resources with the same
45       *                                  name and type in this element, or another parse error occurs
46       * @throws IllegalArgumentException if the provided element is null
47       */
48      public static Resources fromXml(final Element element) throws PluginParseException, IllegalArgumentException {
49          if (element == null) {
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 Set<ResourceDescriptor> templates = new HashSet<>();
57  
58          for (final Element e : elements) {
59              final ResourceDescriptor resourceDescriptor = new ResourceDescriptor(e);
60  
61              if (templates.contains(resourceDescriptor)) {
62                  throw new PluginParseException("Duplicate resource with type '" + resourceDescriptor.getType() + "' and name '" + resourceDescriptor.getName() + "' found");
63              }
64  
65              templates.add(resourceDescriptor);
66          }
67  
68          return new Resources(element);
69      }
70  
71      /**
72       * Private constructor to create a Resources object from XML. Entry via fromXml.
73       *
74       * @param element
75       */
76      private Resources(final Element element) {
77          if (element != null) {
78              this.resourceDescriptors = Lists.newArrayList(Iterables.transform(element.elements("resource"), new Function<Element, ResourceDescriptor>() {
79  
80                  @Override
81                  public ResourceDescriptor apply(@Nullable final Element e) {
82                      return new ResourceDescriptor(e);
83                  }
84              }));
85          } else {
86              this.resourceDescriptors = Collections.emptyList();
87          }
88      }
89  
90      public List<ResourceDescriptor> getResourceDescriptors() {
91          return resourceDescriptors;
92      }
93  
94      public ResourceLocation getResourceLocation(final String type, final String name) {
95          for (final ResourceDescriptor resourceDescriptor : getResourceDescriptors()) {
96              if (resourceDescriptor.doesTypeAndNameMatch(type, name)) {
97                  return resourceDescriptor.getResourceLocationForName(name);
98              }
99          }
100         return null;
101     }
102 
103     public ResourceDescriptor getResourceDescriptor(final String type, final String name) {
104         for (final ResourceDescriptor resourceDescriptor : getResourceDescriptors()) {
105             if (resourceDescriptor.getType().equalsIgnoreCase(type) && resourceDescriptor.getName().equalsIgnoreCase(name)) {
106                 return resourceDescriptor;
107             }
108         }
109         return null;
110     }
111 
112     public static class TypeFilter implements Predicate<ResourceDescriptor> {
113         private final String type;
114 
115         public TypeFilter(final String type) {
116             this.type = notNull("type", type);
117         }
118 
119         public boolean apply(final ResourceDescriptor input) {
120             // TODO Auto-generated method stub
121             return type.equals(input.getType());
122         }
123     }
124 }