diff options
author | 2020-07-31 17:07:48 +0800 | |
---|---|---|
committer | 2020-08-01 00:25:14 +0800 | |
commit | cc8aa4b89b5f9e268d4d40d04b25418e70955ea3 (patch) | |
tree | 04c06b2f819d4f38f5b0aae85b2f3400fdf11a7f | |
parent | write src_unpack everytime is not elegant, use java-pkg-maven to handle it (diff) | |
download | java-ebuilder-cc8aa4b89b5f9e268d4d40d04b25418e70955ea3.tar.gz java-ebuilder-cc8aa4b89b5f9e268d4d40d04b25418e70955ea3.tar.bz2 java-ebuilder-cc8aa4b89b5f9e268d4d40d04b25418e70955ea3.zip |
initial effort to make java-ebuilder parse licenses from pom.xml
Signed-off-by: zongyu <zzy2529420793@gmail.com>
5 files changed, 154 insertions, 0 deletions
diff --git a/src/main/java/org/gentoo/java/ebuilder/maven/MavenEbuilder.java b/src/main/java/org/gentoo/java/ebuilder/maven/MavenEbuilder.java index 37d45a4..3b5c3d6 100644 --- a/src/main/java/org/gentoo/java/ebuilder/maven/MavenEbuilder.java +++ b/src/main/java/org/gentoo/java/ebuilder/maven/MavenEbuilder.java @@ -702,6 +702,8 @@ public class MavenEbuilder { if (config.getLicense() != null) { writer.print(config.getLicense()); + } else { + writer.print(mavenProject.getLicenses()); } writer.println('"'); diff --git a/src/main/java/org/gentoo/java/ebuilder/maven/MavenLicenses.java b/src/main/java/org/gentoo/java/ebuilder/maven/MavenLicenses.java new file mode 100644 index 0000000..e2db62c --- /dev/null +++ b/src/main/java/org/gentoo/java/ebuilder/maven/MavenLicenses.java @@ -0,0 +1,62 @@ +package org.gentoo.java.ebuilder.maven; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +/** + * translate licenses from pom.xml to portage + * + * @author Zhang Zongyu + */ +public class MavenLicenses { + + /** + * Location of the resource file mapping licenses. + */ + private static final String licenseMapFile + = "/licenseMap.properties"; + + /** + * the Map that will convert license from maven + * to portage. + */ + private Map<String, String> licenseMap; + + /** + * Load cache from resource + */ + @SuppressWarnings({"unchecked", "rawtypes"}) + MavenLicenses() { + Properties mapProperty = new Properties(); + try { + mapProperty.load( + this.getClass().getResourceAsStream( + licenseMapFile)); + } catch (final IOException ex) { + throw new RuntimeException( + "Failed to read license map from resource", ex); + } + + licenseMap = (Map)mapProperty; + } + + /** + * query the LicenseMap + * + * @param licenseName the licenses/license/name in pom.xml + * + * @return license identifier that works with Portage + */ + public String getEquivalentLicense(String licenseName) { + final String portageLicense = licenseMap.get(licenseName); + + if (portageLicense == null) { + return "!!!equivalentPortageLicenseName-not-found!!!"; + } else { + return portageLicense; + } + } +} diff --git a/src/main/java/org/gentoo/java/ebuilder/maven/MavenParser.java b/src/main/java/org/gentoo/java/ebuilder/maven/MavenParser.java index 8abc0aa..8c64e37 100644 --- a/src/main/java/org/gentoo/java/ebuilder/maven/MavenParser.java +++ b/src/main/java/org/gentoo/java/ebuilder/maven/MavenParser.java @@ -16,6 +16,7 @@ import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import org.gentoo.java.ebuilder.Config; +import org.gentoo.java.ebuilder.maven.MavenLicenses; /** * Parser for parsing pom.xml into project collector class. @@ -419,6 +420,9 @@ public class MavenParser { case "groupId": mavenProject.setGroupId(reader.getElementText()); break; + case "licenses": + parseProjectLicenses(mavenProject, reader); + break; case "properties": parseProjectProperties(mavenProject, reader); break; @@ -573,6 +577,68 @@ public class MavenParser { } /** + * Parses project licenses. + * + * @param mavenProject maven project instance + * @param reader XML stream reader + * + * @throws XMLStreamException Thrown if problem occurred while reading the + * XML stream. + */ + private void parseProjectLicenses(final MavenProject mavenProject, + final XMLStreamReader reader) + throws XMLStreamException { + MavenLicenses mavenLic = new MavenLicenses(); + + while (reader.hasNext()) { + reader.next(); + + if (reader.isStartElement()) { + switch (reader.getLocalName()) { + case "license": + parseProjectLicense(mavenLic, mavenProject, reader); + break; + default: + consumeElement(reader); + } + } else if (reader.isEndElement()) { + return; + } + } + } + + /** + * Parses project license. + * + * @param mavenProject maven project instance + * @param reader XML stream reader + * + * @throws XMLStreamException Thrown if problem occurred while reading the + * XML stream. + */ + private void parseProjectLicense(final MavenLicenses mavenLicenses, + final MavenProject mavenProject, final XMLStreamReader reader) + throws XMLStreamException { + while (reader.hasNext()) { + reader.next(); + + if (reader.isStartElement()) { + switch (reader.getLocalName()) { + case "name": + mavenProject.addLicense( + mavenLicenses.getEquivalentLicense( + reader.getElementText())); + break; + default: + consumeElement(reader); + } + } else if (reader.isEndElement()) { + return; + } + } + } + + /** * Parses project properties. * * @param mavenProject maven project instance diff --git a/src/main/java/org/gentoo/java/ebuilder/maven/MavenProject.java b/src/main/java/org/gentoo/java/ebuilder/maven/MavenProject.java index 07fa60e..fa4958d 100644 --- a/src/main/java/org/gentoo/java/ebuilder/maven/MavenProject.java +++ b/src/main/java/org/gentoo/java/ebuilder/maven/MavenProject.java @@ -42,6 +42,10 @@ public class MavenProject { */ private Boolean hasTests; /** + * Lisences. + */ + private List<String> licenses = new ArrayList<>(10); + /** * Homepage URL. */ private String homepage; @@ -105,6 +109,15 @@ public class MavenProject { } /** + * Adds license to {@link #licenses}. + * + * @param license {@link #licenses} + */ + public void addLicense(final String license) { + licenses.add(license); + } + + /** * Adds path to {@link #resourceDirectories}. * * @param path resource path @@ -297,6 +310,15 @@ public class MavenProject { } /** + * Getter for {@link #licenses}. + * + * @return space separated licenses + */ + public String getLicenses() { + return String.join(" ", licenses); + } + + /** * Getter for {@link #mainClass}. * * @return {@link #mainClass} diff --git a/src/main/resources/licenseMap.properties b/src/main/resources/licenseMap.properties new file mode 100644 index 0000000..fe9262a --- /dev/null +++ b/src/main/resources/licenseMap.properties @@ -0,0 +1,2 @@ +#Fri Jul 31 23:28:29 CST 2020 +Apache\ License,\ Version\ 2.0=Apache-2.0 |