Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.lib.spring; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.util.*; |
| 5 | import java.util.Map.Entry; |
| 6 | import java.util.regex.*; |
| 7 | |
| 8 | import javax.xml.transform.*; |
| 9 | import javax.xml.transform.stream.*; |
| 10 | |
Stuart McCulloch | 39cc9ac | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 11 | import aQute.bnd.header.*; |
| 12 | import aQute.bnd.osgi.*; |
| 13 | import aQute.bnd.osgi.Descriptors.PackageRef; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 14 | import aQute.bnd.service.*; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 15 | |
| 16 | /** |
| 17 | * This component is called when we find a resource in the META-INF/*.xml |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 18 | * pattern. We parse the resource and and the imports to the builder. Parsing is |
| 19 | * done with XSLT (first time I see the use of having XML for the Spring |
| 20 | * configuration files!). |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 21 | * |
| 22 | * @author aqute |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 23 | */ |
| 24 | public class SpringComponent implements AnalyzerPlugin { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 25 | static Transformer transformer; |
| 26 | static Pattern SPRING_SOURCE = Pattern.compile("META-INF/spring/.*\\.xml"); |
| 27 | static Pattern QN = Pattern.compile("[_A-Za-z$][_A-Za-z0-9$]*(\\.[_A-Za-z$][_A-Za-z0-9$]*)*"); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 28 | |
| 29 | public static Set<CharSequence> analyze(InputStream in) throws Exception { |
| 30 | if (transformer == null) { |
| 31 | TransformerFactory tf = TransformerFactory.newInstance(); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 32 | Source source = new StreamSource(SpringComponent.class.getResourceAsStream("extract.xsl")); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 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 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 66 | public boolean analyzeJar(Analyzer analyzer) throws Exception { |
| 67 | Jar jar = analyzer.getJar(); |
| 68 | Map<String,Resource> dir = jar.getDirectories().get("META-INF/spring"); |
| 69 | if (dir == null || dir.isEmpty()) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 70 | return false; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 71 | |
| 72 | for (Iterator<Entry<String,Resource>> i = dir.entrySet().iterator(); i.hasNext();) { |
| 73 | Entry<String,Resource> entry = i.next(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 74 | String path = entry.getKey(); |
| 75 | Resource resource = entry.getValue(); |
| 76 | if (SPRING_SOURCE.matcher(path).matches()) { |
| 77 | try { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 78 | InputStream in = resource.openInputStream(); |
| 79 | Set<CharSequence> set = analyze(in); |
| 80 | in.close(); |
| 81 | for (Iterator<CharSequence> r = set.iterator(); r.hasNext();) { |
| 82 | PackageRef pack = analyzer.getPackageRef((String) r.next()); |
| 83 | if (!QN.matcher(pack.getFQN()).matches()) |
| 84 | analyzer.warning("Package does not seem a package in spring resource (" + path + "): " |
| 85 | + pack); |
| 86 | if (!analyzer.getReferred().containsKey(pack)) |
| 87 | analyzer.getReferred().put(pack, new Attrs()); |
| 88 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 89 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 90 | catch (Exception e) { |
| 91 | analyzer.error("Unexpected exception in processing spring resources(" + path + "): " + e); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | } |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | } |