View Javadoc

1   package com.atlassian.plugin.osgi.factory.transform.stage;
2   
3   import com.atlassian.plugin.osgi.factory.transform.PluginTransformationException;
4   import com.atlassian.plugin.osgi.factory.transform.TransformContext;
5   import org.dom4j.*;
6   import org.dom4j.io.OutputFormat;
7   import org.dom4j.io.XMLWriter;
8   import org.apache.log4j.Logger;
9   
10  import java.io.ByteArrayOutputStream;
11  import java.io.IOException;
12  
13  /**
14   * Helper class for generating a new Spring XML file
15   *
16   * @since 2.2.0
17   */
18  class SpringHelper
19  {
20  
21      private static final Logger log = Logger.getLogger(SpringHelper.class);
22  
23      /**
24       * Creates a basic spring document with the usual namespaces
25       *
26       * @return An empty spring XML configuration file with namespaces
27       */
28      static Document createSpringDocument()
29      {
30          Document springDoc = DocumentHelper.createDocument();
31          Element root = springDoc.addElement("beans");
32  
33          root.addNamespace("beans", "http://www.springframework.org/schema/beans");
34          root.addNamespace("osgi", "http://www.springframework.org/schema/osgi");
35          root.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
36          root.addAttribute(new QName("schemaLocation", new Namespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")),
37                  "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd\n" +
38                          "http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd");
39          root.setName("beans:beans");
40          root.addAttribute("default-autowire", "autodetect");
41          return springDoc;
42      }
43  
44      /**
45       * Converts an XML document into a byte array
46       * @param doc The document
47       * @return A byte array of the contents
48       */
49      static byte[] documentToBytes(Document doc)
50      {
51  
52          ByteArrayOutputStream bout = new ByteArrayOutputStream();
53          OutputFormat format = OutputFormat.createPrettyPrint();
54  
55          try
56          {
57              XMLWriter writer = new XMLWriter(bout, format);
58              writer.write(doc);
59          }
60          catch (IOException e)
61          {
62              throw new PluginTransformationException("Unable to print generated Spring XML", e);
63          }
64  
65          return bout.toByteArray();
66      }
67  
68      /**
69       * Determines if the file should be generated, based on whether it already exists in the context or not
70       *
71       * @param context The transformation context
72       * @param path The path of the file
73       * @return True if not present, false otherwise
74       */
75      static boolean shouldGenerateFile(TransformContext context, String path)
76      {
77          if (context.getPluginJar().getEntry(path) == null)
78          {
79              log.debug("File "+path+" not present, generating");
80              return true;
81          }
82          else
83          {
84              log.debug("File "+path+" already exists in jar, skipping generation");
85              return false;
86          }
87      }
88  }