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