blob: 9fadb356d0c564a901d97d20752aca288cf87e32 [file] [log] [blame]
Stuart McCulloch26e7a5a2011-10-17 10:31:43 +00001package aQute.lib.spring;
2
3import java.io.*;
4import java.net.*;
5import java.util.*;
6import java.util.regex.*;
7
8import javax.xml.transform.*;
9import javax.xml.transform.stream.*;
10
11import aQute.lib.osgi.*;
12
13public class XMLType {
14
15 Transformer transformer;
16 Pattern paths;
17 String root;
18
19
20 static Pattern QN = Pattern
21 .compile("[_A-Za-z$][_A-Za-z0-9$]*(\\.[_A-Za-z$][_A-Za-z0-9$]*)*");
22
23 public XMLType(URL source, String root, String paths ) throws Exception {
24 transformer = getTransformer(source);
25 this.paths = Pattern.compile(paths);
26 this.root = root;
27 }
28
29 public Set<String> analyze(InputStream in) throws Exception {
30 Set<String> refers = new HashSet<String>();
31
32 ByteArrayOutputStream bout = new ByteArrayOutputStream();
33 Result r = new StreamResult(bout);
34 Source s = new StreamSource(in);
35 transformer.transform(s, r);
36
37 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
38 bout.close();
39
40 BufferedReader br = new BufferedReader(new InputStreamReader(bin, "UTF8"));
41
42 String line = br.readLine();
43 while (line != null) {
44 line = line.trim();
45 if (line.length() > 0) {
46 String parts[] = line.split("\\s*,\\s*");
47 for (int i = 0; i < parts.length; i++) {
48 int n = parts[i].lastIndexOf('.');
49 if (n > 0) {
50 refers.add(parts[i].subSequence(0, n).toString());
51 }
52 }
53 }
54 line = br.readLine();
55 }
56 br.close();
57 return refers;
58 }
59
60 public boolean analyzeJar(Analyzer analyzer) throws Exception {
61 Jar jar = analyzer.getJar();
62 Map<String,Resource> dir = jar.getDirectories().get(root);
63 if (dir == null || dir.isEmpty()) {
64 Resource resource = jar.getResource(root);
65 if ( resource != null )
66 process(analyzer, root, resource);
67 return false;
68 }
69
70 for (Iterator<Map.Entry<String,Resource>> i = dir.entrySet().iterator(); i.hasNext();) {
71 Map.Entry<String,Resource> entry = i.next();
72 String path = entry.getKey();
73 Resource resource = entry.getValue();
74 if (paths.matcher(path).matches()) {
75 process(analyzer, path, resource);
76 }
77 }
78 return false;
79 }
80
81 private void process(Analyzer analyzer, String path, Resource resource) {
82 try {
83 InputStream in = resource.openInputStream();
84 Set<String> set = analyze(in);
85 in.close();
86 for (Iterator<String> r = set.iterator(); r.hasNext();) {
87 String pack = r.next();
88 if (!QN.matcher(pack).matches())
89 analyzer
90 .warning("Package does not seem a package in spring resource ("
91 + path + "): " + pack);
92 if (!analyzer.getReferred().containsKey(pack))
93 analyzer.getReferred().put(pack,
94 new LinkedHashMap<String,String>());
95 }
96 } catch (Exception e) {
97 analyzer
98 .error("Unexpected exception in processing spring resources("
99 + path + "): " + e);
100 }
101 }
102
103 protected Transformer getTransformer(java.net.URL url) throws Exception {
104 TransformerFactory tf = TransformerFactory.newInstance();
105 Source source = new StreamSource(url.openStream());
106 return tf.newTransformer(source);
107 }
108}