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
15
16
17
18 class SpringHelper
19 {
20
21 private static final Logger log = Logger.getLogger(SpringHelper.class);
22
23
24
25
26
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
46
47
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
70
71
72
73
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 }