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