blob: 782c282ac1ceedd9889bed05f0839bb9c461c475 [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.*;
13import aQute.libg.version.*;
14
15public class PomFromManifest extends WriteResource {
16 final Manifest manifest;
17 private List<String> scm = new ArrayList<String>();
18 private List<String> developers = new ArrayList<String>();
19 final static Pattern NAME_URL = Pattern.compile("(.*)(http://.*)");
20 String xbsn;
21 String xversion;
22 String xgroupId;
23 String xartifactId;
24 private String projectURL;
25
26 public String getBsn() {
27 if (xbsn == null)
28 xbsn = manifest.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
29 if (xbsn == null)
30 throw new RuntimeException("Cannot create POM unless bsn is set");
31
32 xbsn = xbsn.trim();
33 int n = xbsn.lastIndexOf('.');
34 if (n < 0) {
35 n = xbsn.length();
36 xbsn = xbsn + "." + xbsn;
37 }
38
39 if (xgroupId == null)
40 xgroupId = xbsn.substring(0, n);
41 if (xartifactId == null) {
42 xartifactId = xbsn.substring(n + 1);
43 n = xartifactId.indexOf(';');
44 if (n > 0)
45 xartifactId = xartifactId.substring(0, n).trim();
46 }
47
48 return xbsn;
49 }
50
51 public String getGroupId() {
52 getBsn();
53 return xgroupId;
54 }
55
56 public String getArtifactId() {
57 getBsn();
58 return xartifactId;
59 }
60
61 public Version getVersion() {
62 if (xversion != null)
63 return new Version(xversion);
64 String version = manifest.getMainAttributes().getValue(Constants.BUNDLE_VERSION);
65 Version v = new Version(version);
66 return new Version(v.getMajor(), v.getMinor(), v.getMicro());
67 }
68
69 public PomFromManifest(Manifest manifest) {
70 this.manifest = manifest;
71 }
72
Stuart McCulloch2286f232012-06-15 13:27:53 +000073 @Override
74 public long lastModified() {
Stuart McCullochbb014372012-06-07 21:57:32 +000075 return 0;
76 }
77
Stuart McCulloch2286f232012-06-15 13:27:53 +000078 @Override
79 public void write(OutputStream out) throws IOException {
Stuart McCullochbb014372012-06-07 21:57:32 +000080 PrintWriter ps = IO.writer(out);
81
82 String name = manifest.getMainAttributes().getValue(Analyzer.BUNDLE_NAME);
83
84 String description = manifest.getMainAttributes().getValue(Constants.BUNDLE_DESCRIPTION);
85 String docUrl = manifest.getMainAttributes().getValue(Constants.BUNDLE_DOCURL);
86 String bundleVendor = manifest.getMainAttributes().getValue(Constants.BUNDLE_VENDOR);
87
88 String licenses = manifest.getMainAttributes().getValue(Constants.BUNDLE_LICENSE);
89
90 Tag project = new Tag("project");
91 project.addAttribute("xmlns", "http://maven.apache.org/POM/4.0.0");
92 project.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
93 project.addAttribute("xsi:schemaLocation",
94 "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd");
95
96 project.addContent(new Tag("modelVersion").addContent("4.0.0"));
97 project.addContent(new Tag("groupId").addContent(getGroupId()));
98 project.addContent(new Tag("artifactId").addContent(getArtifactId()));
99 project.addContent(new Tag("version").addContent(getVersion().toString()));
100
101 if (description != null) {
102 new Tag(project, "description").addContent(description);
103 }
104 if (name != null) {
105 new Tag(project, "name").addContent(name);
106 }
107
108 if (projectURL != null)
109 new Tag(project, "url").addContent(projectURL);
110 else if (docUrl != null) {
111 new Tag(project, "url").addContent(docUrl);
112 } else
113 new Tag(project, "url").addContent("http://no-url");
114
115 String scmheader = manifest.getMainAttributes().getValue("Bundle-SCM");
116 if (scmheader != null)
117 scm.add(scmheader);
118
119 Tag scmtag = new Tag(project, "scm");
120 if (scm != null && !scm.isEmpty()) {
121 for (String cm : this.scm) {
122 new Tag(scmtag, "url").addContent(cm);
123 new Tag(scmtag, "connection").addContent(cm);
124 new Tag(scmtag, "developerConnection").addContent(cm);
125 }
126 } else {
127 new Tag(scmtag, "url").addContent("private");
128 new Tag(scmtag, "connection").addContent("private");
129 new Tag(scmtag, "developerConnection").addContent("private");
130 }
131
132 if (bundleVendor != null) {
133 Matcher m = NAME_URL.matcher(bundleVendor);
134 String namePart = bundleVendor;
135 String urlPart = this.projectURL;
136 if (m.matches()) {
137 namePart = m.group(1);
138 urlPart = m.group(2);
139 }
140 Tag organization = new Tag(project, "organization");
141 new Tag(organization, "name").addContent(namePart.trim());
142 if (urlPart != null) {
143 new Tag(organization, "url").addContent(urlPart.trim());
144 }
145 }
146 if (!developers.isEmpty()) {
147 Tag d = new Tag(project, "developers");
148 for (String email : developers) {
149 String id = email;
150 String xname = email;
151 String organization = null;
152
Stuart McCulloch2286f232012-06-15 13:27:53 +0000153 Matcher m = Pattern.compile("([^@]+)@([\\d\\w\\-_\\.]+)\\.([\\d\\w\\-_\\.]+)").matcher(email);
Stuart McCullochbb014372012-06-07 21:57:32 +0000154 if (m.matches()) {
155 xname = m.group(1);
156 organization = m.group(2);
157 }
158
159 Tag developer = new Tag(d, "developer");
160 new Tag(developer, "id").addContent(id);
161 new Tag(developer, "name").addContent(xname);
162 new Tag(developer, "email").addContent(email);
163 if (organization != null)
164 new Tag(developer, "organization").addContent(organization);
165 }
166 }
167 if (licenses != null) {
168 Tag ls = new Tag(project, "licenses");
169
170 Parameters map = Processor.parseHeader(licenses, null);
Stuart McCulloch2286f232012-06-15 13:27:53 +0000171 for (Iterator<Entry<String,Attrs>> e = map.entrySet().iterator(); e.hasNext();) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000172
173 // Bundle-License:
174 // http://www.opensource.org/licenses/apache2.0.php; \
175 // description="${Bundle-Copyright}"; \
176 // link=LICENSE
177 //
178 //  <license>
179 //    <name>This material is licensed under the Apache
180 // Software License, Version 2.0</name>
181 //    <url>http://www.apache.org/licenses/LICENSE-2.0</url>
182 //    <distribution>repo</distribution>
183 //    </license>
184
Stuart McCulloch2286f232012-06-15 13:27:53 +0000185 Entry<String,Attrs> entry = e.next();
Stuart McCullochbb014372012-06-07 21:57:32 +0000186 Tag l = new Tag(ls, "license");
Stuart McCulloch2286f232012-06-15 13:27:53 +0000187 Map<String,String> values = entry.getValue();
Stuart McCullochbb014372012-06-07 21:57:32 +0000188 String url = entry.getKey();
189
190 if (values.containsKey("description"))
191 tagFromMap(l, values, "description", "name", url);
192 else
193 tagFromMap(l, values, "name", "name", url);
194
195 tagFromMap(l, values, "url", "url", url);
196 tagFromMap(l, values, "distribution", "distribution", "repo");
197 }
198 }
199 project.print(0, ps);
200 ps.flush();
201 }
202
203 /**
204 * Utility function to print a tag from a map
205 *
206 * @param ps
207 * @param values
208 * @param string
209 * @param tag
210 * @param object
211 */
Stuart McCulloch2286f232012-06-15 13:27:53 +0000212 private Tag tagFromMap(Tag parent, Map<String,String> values, String string, String tag, String object) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000213 String value = values.get(string);
214 if (value == null)
215 value = object;
216 if (value == null)
217 return parent;
218 new Tag(parent, tag).addContent(value.trim());
219 return parent;
220 }
221
222 public void setSCM(String scm) {
223 this.scm.add(scm);
224 }
225
226 public void setURL(String url) {
227 this.projectURL = url;
228 }
229
230 public void setBsn(String bsn) {
231 this.xbsn = bsn;
232 }
233
234 public void addDeveloper(String email) {
235 this.developers.add(email);
236 }
237
238 public void setVersion(String version) {
239 this.xversion = version;
240 }
241
242 public void setArtifact(String artifact) {
243 this.xartifactId = artifact;
244 }
245
246 public void setGroup(String group) {
247 this.xgroupId = group;
248 }
249}