View Javadoc

1   package com.atlassian.plugin.webresource;
2   
3   import com.atlassian.plugin.Plugin;
4   import com.atlassian.plugin.PluginParseException;
5   import com.atlassian.plugin.descriptors.AbstractModuleDescriptor;
6   import org.dom4j.Attribute;
7   import org.dom4j.Element;
8   
9   import java.util.ArrayList;
10  import java.util.Collections;
11  import java.util.HashSet;
12  import java.util.List;
13  import java.util.Set;
14  
15  /**
16   * A way of linking to web 'resources', such as javascript or css.  This allows us to include resources once
17   * on any given page, as well as ensuring that plugins can declare resources, even if they are included
18   * at the bottom of a page.
19   */
20  public class WebResourceModuleDescriptor extends AbstractModuleDescriptor<Void>
21  {
22      private List<String> dependencies = Collections.emptyList();
23      private boolean disableMinification;
24      private Set<String> contexts = Collections.emptySet();
25      private List<WebResourceTransformation> webResourceTransformations = Collections.emptyList();
26  
27      @Override
28      public void init(final Plugin plugin, final Element element) throws PluginParseException
29      {
30          super.init(plugin, element);
31  
32          final List<String> deps = new ArrayList<String>();
33          for (Element dependency : (List<Element>) element.elements("dependency"))
34          {
35              deps.add(dependency.getTextTrim());
36          }
37          dependencies = Collections.unmodifiableList(deps);
38  
39          final Set<String> ctxs = new HashSet<String>();
40          for (Element contextElement : (List<Element>) element.elements("context"))
41          {
42              ctxs.add(contextElement.getTextTrim());
43          }
44          contexts = Collections.unmodifiableSet(ctxs);
45  
46          final List<WebResourceTransformation> trans = new ArrayList<WebResourceTransformation>();
47          for (Element e : (List<Element>) element.elements("transformation"))
48          {
49              trans.add(new WebResourceTransformation(e));
50          }
51          webResourceTransformations = Collections.unmodifiableList(trans);
52  
53          final Attribute minifiedAttribute = element.attribute("disable-minification");
54          disableMinification = minifiedAttribute == null ? false : Boolean.valueOf(minifiedAttribute.getValue());
55      }
56  
57      /**
58       * As this descriptor just handles resources, you should never call this
59       */
60      @Override
61      public Void getModule()
62      {
63          throw new UnsupportedOperationException("There is no module for Web Resources");
64      }
65  
66      /**
67       * Returns the web resource contexts this resource is associated with.
68       *
69       * @return  the web resource contexts this resource is associated with.
70       * @since 2.5.0
71       */
72      public Set<String> getContexts()
73      {
74          return contexts;
75      }
76  
77      /**
78       * Returns a list of dependencies on other web resources.
79       * @return a list of module complete keys
80       */
81      public List<String> getDependencies()
82      {
83          return dependencies;
84      }
85  
86      public List<WebResourceTransformation> getTransformations()
87      {
88          return webResourceTransformations;
89      }
90  
91      /**
92       * @return <code>true</code> if resource minification should be skipped, <code>false</code> otherwise.
93       */
94      public boolean isDisableMinification()
95      {
96          return disableMinification;
97      }
98  }