View Javadoc

1   package com.atlassian.plugin.web.descriptors;
2   
3   import com.atlassian.plugin.util.Assertions;
4   import com.atlassian.plugin.web.NoOpContextProvider;
5   import org.dom4j.Element;
6   
7   import com.atlassian.plugin.Plugin;
8   import com.atlassian.plugin.PluginParseException;
9   import com.atlassian.plugin.loaders.LoaderUtils;
10  import com.atlassian.plugin.web.ContextProvider;
11  import com.atlassian.plugin.web.WebFragmentHelper;
12  import com.atlassian.plugin.web.conditions.ConditionLoadingException;
13  
14  /**
15   * This class is used for constructing
16   * {@link com.atlassian.plugin.web.ContextProvider} objects from a module
17   * descriptor's XML element. Its functionality is used by both
18   * {@link com.atlassian.plugin.web.descriptors.AbstractWebFragmentModuleDescriptor}
19   * and
20   * {@link com.atlassian.plugin.web.descriptors.DefaultWebPanelModuleDescriptor}.
21   * 
22   * @since 2.5.0
23   */
24  class ContextProviderElementParser
25  {
26      private final WebFragmentHelper webFragmentHelper;
27  
28      public ContextProviderElementParser(final WebFragmentHelper webFragmentHelper)
29      {
30          this.webFragmentHelper = webFragmentHelper;
31      }
32  
33      /**
34       * @param element the module descriptor's XML element which contains the
35       *            nested <context-provider> element.
36       * @return the configured {@link ContextProvider} instance, or
37       *         <code>null</code> when the descriptor does not contain a
38       *         &lt;context-provider> element.
39       * @throws PluginParseException
40       */
41      public ContextProvider makeContextProvider(final Plugin plugin, final Element element) throws PluginParseException
42      {
43          Assertions.notNull("plugin == null", plugin);
44          try
45          {
46              final Element contextProviderElement = element.element("context-provider");
47              if (contextProviderElement == null)
48              {
49                  return new NoOpContextProvider();
50              }
51              else
52              {
53                  final ContextProvider context = webFragmentHelper.loadContextProvider(contextProviderElement.attributeValue("class"), plugin);
54                  context.init(LoaderUtils.getParams(contextProviderElement));
55                  return context;
56              }
57          }
58          catch (final ClassCastException e)
59          {
60              throw new PluginParseException("Configured context-provider class does not implement the ContextProvider interface", e);
61          }
62          catch (final ConditionLoadingException cle)
63          {
64              throw new PluginParseException("Unable to load the module's display conditions: " + cle.getMessage(), cle);
65          }
66      }
67  }