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