View Javadoc

1   package com.atlassian.plugin.osgi.factory.transform.stage;
2   
3   import com.atlassian.plugin.osgi.factory.transform.TransformStage;
4   import com.atlassian.plugin.osgi.factory.transform.TransformContext;
5   import com.atlassian.plugin.osgi.factory.transform.PluginTransformationException;
6   import org.dom4j.Document;
7   import org.dom4j.Element;
8   
9   import java.util.List;
10  import java.util.ArrayList;
11  
12  /**
13   * Transforms component imports into a Spring XML file
14   *
15   * @since 2.2.0
16   */
17  public class ComponentImportSpringStage implements TransformStage
18  {
19      /** Path of generated Spring XML file */
20      private static final String SPRING_XML = "META-INF/spring/atlassian-plugins-component-imports.xml";
21  
22      public void execute(TransformContext context) throws PluginTransformationException
23      {
24          if (SpringHelper.shouldGenerateFile(context, SPRING_XML))
25          {
26              Document springDoc = SpringHelper.createSpringDocument();
27              Element root = springDoc.getRootElement();
28              List<Element> elements = context.getDescriptorDocument().getRootElement().elements("component-import");
29              for (Element component : elements)
30              {
31                  Element osgiReference = root.addElement("osgi:reference");
32                  osgiReference.addAttribute("id", component.attributeValue("key"));
33                  String infName = component.attributeValue("interface");
34                  if (infName != null)
35                  {
36                      osgiReference.addAttribute("interface", infName);
37                      context.getExtraImports().add(infName.substring(0, infName.lastIndexOf('.')));
38                  }
39  
40  
41                  List<Element> compInterfaces = component.elements("interface");
42                  if (compInterfaces.size() > 0)
43                  {
44                      List<String> interfaceNames = new ArrayList<String>();
45                      for (Element inf : compInterfaces)
46                      {
47                          interfaceNames.add(inf.getTextTrim());
48                      }
49  
50                      Element interfaces = osgiReference.addElement("osgi:interfaces");
51                      for (String name : interfaceNames)
52                      {
53                          Element e = interfaces.addElement("beans:value");
54                          e.setText(name);
55                          context.getExtraImports().add(name.substring(0, name.lastIndexOf('.')));
56                      }
57                  }
58              }
59              if (root.elements().size() > 0)
60              {
61                  context.getFileOverrides().put(SPRING_XML, SpringHelper.documentToBytes(springDoc));
62              }
63          }
64      }
65  }