blob: b722d0f1ebec97b796ae4be744e7fe562acc92e0 [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +00001package aQute.bnd.maven;
2
3import java.io.*;
4import java.util.*;
5import java.util.Map.Entry;
6import java.util.jar.*;
7import java.util.regex.*;
8
9import aQute.lib.io.*;
10import aQute.lib.osgi.*;
11import aQute.lib.tag.*;
12import aQute.libg.header.*;
13
14public class PomResource extends WriteResource {
15 final Manifest manifest;
Stuart McCulloch2286f232012-06-15 13:27:53 +000016 private Map<String,String> scm;
Stuart McCullochbb014372012-06-07 21:57:32 +000017 final static Pattern NAME_URL = Pattern.compile("(.*)(http://.*)");
18
19 public PomResource(Manifest manifest) {
20 this.manifest = manifest;
21 }
22
Stuart McCulloch2286f232012-06-15 13:27:53 +000023 @Override
24 public long lastModified() {
Stuart McCullochbb014372012-06-07 21:57:32 +000025 return 0;
26 }
27
Stuart McCulloch2286f232012-06-15 13:27:53 +000028 @Override
29 public void write(OutputStream out) throws IOException {
Stuart McCullochbb014372012-06-07 21:57:32 +000030 PrintWriter ps = IO.writer(out);
31
32 String name = manifest.getMainAttributes().getValue(Analyzer.BUNDLE_NAME);
33
34 String description = manifest.getMainAttributes().getValue(Constants.BUNDLE_DESCRIPTION);
35 String docUrl = manifest.getMainAttributes().getValue(Constants.BUNDLE_DOCURL);
36 String version = manifest.getMainAttributes().getValue(Constants.BUNDLE_VERSION);
37 String bundleVendor = manifest.getMainAttributes().getValue(Constants.BUNDLE_VENDOR);
38
39 String bsn = manifest.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
40 String licenses = manifest.getMainAttributes().getValue(Constants.BUNDLE_LICENSE);
41
42 if (bsn == null) {
43 throw new RuntimeException("Cannot create POM unless bsn is set");
44 }
45
46 bsn = bsn.trim();
47 int n = bsn.lastIndexOf('.');
48 if (n <= 0)
49 throw new RuntimeException(
50 "Can not create POM unless Bundle-SymbolicName contains a . to separate group and artifact id");
51
52 if (version == null)
53 version = "0";
54
55 String groupId = bsn.substring(0, n);
56 String artifactId = bsn.substring(n + 1);
57 n = artifactId.indexOf(';');
58 if (n > 0)
59 artifactId = artifactId.substring(0, n).trim();
60
61 Tag project = new Tag("project");
62 project.addAttribute("xmlns", "http://maven.apache.org/POM/4.0.0");
63 project.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
64 project.addAttribute("xmlns:xsi", "");
65 project.addAttribute("xsi:schemaLocation",
66 "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd");
67
68 project.addContent(new Tag("modelVersion").addContent("4.0.0"));
69 project.addContent(new Tag("groupId").addContent(groupId));
70 project.addContent(new Tag("artifactId").addContent(artifactId));
71 project.addContent(new Tag("version").addContent(version));
72
73 if (description != null) {
74 new Tag(project, "description").addContent(description);
75 }
76 if (name != null) {
77 new Tag(project, "name").addContent(name);
78 }
79 if (docUrl != null) {
80 new Tag(project, "url").addContent(docUrl);
81 }
82
83 if (scm != null) {
84 Tag scm = new Tag(project, "scm");
Stuart McCulloch2286f232012-06-15 13:27:53 +000085 for (Map.Entry<String,String> e : this.scm.entrySet()) {
Stuart McCullochbb014372012-06-07 21:57:32 +000086 new Tag(scm, e.getKey()).addContent(e.getValue());
87 }
88 }
89
90 if (bundleVendor != null) {
91 Matcher m = NAME_URL.matcher(bundleVendor);
92 String namePart = bundleVendor;
93 String urlPart = null;
94 if (m.matches()) {
95 namePart = m.group(1);
96 urlPart = m.group(2);
97 }
98 Tag organization = new Tag(project, "organization");
99 new Tag(organization, "name").addContent(namePart.trim());
100 if (urlPart != null) {
101 new Tag(organization, "url").addContent(urlPart.trim());
102 }
103 }
104 if (licenses != null) {
105 Tag ls = new Tag(project, "licenses");
106
107 Parameters map = Processor.parseHeader(licenses, null);
Stuart McCulloch2286f232012-06-15 13:27:53 +0000108 for (Iterator<Entry<String,Attrs>> e = map.entrySet().iterator(); e.hasNext();) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000109
110 // Bundle-License:
111 // http://www.opensource.org/licenses/apache2.0.php; \
112 // description="${Bundle-Copyright}"; \
113 // link=LICENSE
114 //
115 //  <license>
116 //    <name>This material is licensed under the Apache
117 // Software License, Version 2.0</name>
118 //    <url>http://www.apache.org/licenses/LICENSE-2.0</url>
119 //    <distribution>repo</distribution>
120 //    </license>
121
Stuart McCulloch2286f232012-06-15 13:27:53 +0000122 Entry<String,Attrs> entry = e.next();
Stuart McCullochbb014372012-06-07 21:57:32 +0000123 Tag l = new Tag(ls, "license");
Stuart McCulloch2286f232012-06-15 13:27:53 +0000124 Map<String,String> values = entry.getValue();
Stuart McCullochbb014372012-06-07 21:57:32 +0000125 String url = entry.getKey();
126
127 if (values.containsKey("description"))
128 tagFromMap(l, values, "description", "name", url);
129 else
130 tagFromMap(l, values, "name", "name", url);
131
132 tagFromMap(l, values, "url", "url", url);
133 tagFromMap(l, values, "distribution", "distribution", "repo");
134 }
135 }
136 project.print(0, ps);
137 ps.flush();
138 }
139
140 /**
141 * Utility function to print a tag from a map
142 *
143 * @param ps
144 * @param values
145 * @param string
146 * @param tag
147 * @param object
148 */
Stuart McCulloch2286f232012-06-15 13:27:53 +0000149 private Tag tagFromMap(Tag parent, Map<String,String> values, String string, String tag, String object) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000150 String value = values.get(string);
151 if (value == null)
152 value = object;
153 if (value == null)
154 return parent;
155 new Tag(parent, tag).addContent(value.trim());
156 return parent;
157 }
158
Stuart McCulloch2286f232012-06-15 13:27:53 +0000159 public void setProperties(Map<String,String> scm) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000160 this.scm = scm;
161 }
162}