blob: 3318a0e4aa3c332564c79c44d50eace7d5d59259 [file] [log] [blame]
Stuart McCulloch26e7a5a2011-10-17 10:31:43 +00001package aQute.lib.spring;
2
3import java.io.*;
4import java.util.*;
5import java.util.regex.*;
6
7import javax.xml.transform.*;
8import javax.xml.transform.stream.*;
9
10import aQute.bnd.service.*;
11import aQute.lib.osgi.*;
12
13/**
14 * This component is called when we find a resource in the META-INF/*.xml
15 * pattern. We parse the resource and and the imports to the builder.
16 *
17 * Parsing is done with XSLT (first time I see the use of having XML for the
18 * Spring configuration files!).
19 *
20 * @author aqute
21 *
22 */
23public class SpringComponent implements AnalyzerPlugin {
24 static Transformer transformer;
25 static Pattern SPRING_SOURCE = Pattern.compile("META-INF/spring/.*\\.xml");
26 static Pattern QN = Pattern.compile("[_A-Za-z$][_A-Za-z0-9$]*(\\.[_A-Za-z$][_A-Za-z0-9$]*)*");
27
28 public static Set<CharSequence> analyze(InputStream in) throws Exception {
29 if (transformer == null) {
30 TransformerFactory tf = TransformerFactory.newInstance();
31 Source source = new StreamSource(SpringComponent.class
32 .getResourceAsStream("extract.xsl"));
33 transformer = tf.newTransformer(source);
34 }
35
36 Set<CharSequence> refers = new HashSet<CharSequence>();
37
38 ByteArrayOutputStream bout = new ByteArrayOutputStream();
39 Result r = new StreamResult(bout);
40 Source s = new StreamSource(in);
41 transformer.transform(s, r);
42
43 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
44 bout.close();
45
46 BufferedReader br = new BufferedReader(new InputStreamReader(bin, "UTF8"));
47
48 String line = br.readLine();
49 while (line != null) {
50 line = line.trim();
51 if (line.length() > 0) {
52 String parts[] = line.split("\\s*,\\s*");
53 for (int i = 0; i < parts.length; i++) {
54 int n = parts[i].lastIndexOf('.');
55 if (n > 0) {
56 refers.add(parts[i].subSequence(0, n));
57 }
58 }
59 }
60 line = br.readLine();
61 }
62 br.close();
63 return refers;
64 }
65
66 @SuppressWarnings("unchecked")
67 public boolean analyzeJar(Analyzer analyzer) throws Exception {
68 Jar jar = analyzer.getJar();
69 Map dir = (Map) jar.getDirectories().get("META-INF/spring");
70 if ( dir == null || dir.isEmpty())
71 return false;
72
73 for (Iterator i = dir.entrySet().iterator(); i.hasNext();) {
74 Map.Entry entry = (Map.Entry) i.next();
75 String path = (String) entry.getKey();
76 Resource resource = (Resource) entry.getValue();
77 if (SPRING_SOURCE.matcher(path).matches()) {
78 try {
79 InputStream in = resource.openInputStream();
80 Set set = analyze(in);
81 in.close();
82 for (Iterator r = set.iterator(); r.hasNext();) {
83 String pack = (String) r.next();
84 if ( !QN.matcher(pack).matches())
85 analyzer.warning("Package does not seem a package in spring resource ("+path+"): " + pack );
86 if (!analyzer.getReferred().containsKey(pack))
87 analyzer.getReferred().put(pack, new LinkedHashMap());
88 }
89 } catch( Exception e ) {
90 analyzer.error("Unexpected exception in processing spring resources("+path+"): " + e );
91 }
92 }
93 }
94 return false;
95 }
96
97}