Skip to content

Commit

Permalink
use the online SPDX license list for creating SPDX license IDs
Browse files Browse the repository at this point in the history
Signed-off-by: Gary O'Neall <[email protected]>
  • Loading branch information
goneall committed May 5, 2019
1 parent a8aae0a commit ddba576
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 166 deletions.
14 changes: 9 additions & 5 deletions src/main/java/org/spdx/maven/MavenToSpdxLicenseMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
*/
public class MavenToSpdxLicenseMapper
{
private static final String SPDX_LICENSE_URL_PREFIX = "http://spdx.org/licenses/";
private static final String SPDX_LICENSE_URL_PREFIX = "https://spdx.org/licenses/";
private static final String LISTED_LICENSE_JSON_URL = SPDX_LICENSE_URL_PREFIX + "licenses.json";
private static final String LISTED_LICENSE_JSON_PATH = "resources/licenses.json";

Expand All @@ -69,15 +69,18 @@ private MavenToSpdxLicenseMapper(Log log) throws LicenseMapperException {
try
{
URL listedLicenseJsonUrl = new URL( LISTED_LICENSE_JSON_URL );
//TODO: Uncomment the line below once the JSON file has been uploaded to the SPDX listed license website
//is = listedLicenseJsonUrl.openStream();
is = listedLicenseJsonUrl.openStream();
}
catch ( MalformedURLException e )
{
if (log != null) {
log.warn( "Invalid JSON URL for SPDX listed licenses. Using cached version" );
}
}
} catch (IOException e) {
if (log != null) {
log.warn( "IO Exception opening web page for JSON for SPDX listed licenses. Using cached version" );
}
}
if (is == null) {
// use the cached version
is = LicenseManager.class.getClassLoader().getResourceAsStream(LISTED_LICENSE_JSON_PATH);
Expand Down Expand Up @@ -156,6 +159,7 @@ private void initializeUrlMap( Reader jsonReader, Log log ) throws LicenseMapper
if ( urls != null ) {
for ( int j = 0; j < urls.size(); j++ ) {
String url = (String)urls.get( j );
url = url.replaceAll("https", "http");
if (this.urlStringToSpdxLicenseId.containsKey( url )) {
urlsWithMultipleIds.add( url );
} else {
Expand Down Expand Up @@ -231,7 +235,7 @@ private SpdxListedLicense mavenLicenseToSpdxListedLicense( License license )
if ( license.getUrl() == null || license.getUrl().isEmpty() ) {
return null;
}
String spdxId = this.urlStringToSpdxLicenseId.get( license.getUrl() );
String spdxId = this.urlStringToSpdxLicenseId.get( license.getUrl().replaceAll("https", "http") );
if (spdxId == null) {
return null;
}
Expand Down
7 changes: 3 additions & 4 deletions src/test/java/org/spdx/maven/TestLicenseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
public class TestLicenseManager
{
private static final String TEST_SPDX_DOCUMENT_URL = "http://www.spdx.org/documents/test";
static final String APACHE_CROSS_REF_URL1 = "http://www.opensource.org/licenses/Apache-2.0";
static final String APACHE_CROSS_REF_URL2 = "http://www.apache.org/licenses/LICENSE-2.0";
static final String APACHE_CROSS_REF_URL3 = "http://opensource.org/licenses/Apache-2.0";
static final String APACHE_LICENSE_ID = "Apache-2.0";
Expand Down Expand Up @@ -203,7 +202,7 @@ public void testMavenLicenseListToSpdxLicense() throws LicenseManagerException,

License apache = new License();
apache.setName( LICENSE1_NAME );
apache.setUrl( APACHE_CROSS_REF_URL1 );
apache.setUrl( APACHE_CROSS_REF_URL2 );
License apsl = new License();
apsl.setName( LICENSE2_NAME );
apsl.setUrl( APSL_CROSS_REF_URL );
Expand Down Expand Up @@ -244,7 +243,7 @@ public void testMavenLicenseToSpdxLicense() throws LicenseManagerException, Malf
final String LICENSE1_NAME = "Apachelicense1";
License apache = new License();
apache.setName( LICENSE1_NAME );
apache.setUrl( APACHE_CROSS_REF_URL1 );
apache.setUrl( APACHE_CROSS_REF_URL2 );
LicenseManager licenseManager = new LicenseManager( spdxDoc, log, true );

AnyLicenseInfo result = licenseManager.mavenLicenseToSpdxLicense( apache );
Expand Down Expand Up @@ -295,7 +294,7 @@ public void testSpdxLicenseToMavenLicense() throws InvalidLicenseStringException
License result = licenseManager.spdxLicenseToMavenLicense( licenseInfo );
assertEquals( result.getName(), ((SpdxListedLicense)licenseInfo).getName() );
String resultUrl = result.getUrl().replace("https", "http");
assertTrue( APACHE_CROSS_REF_URL1.equals(resultUrl) || APACHE_CROSS_REF_URL2.equals(resultUrl) || APACHE_CROSS_REF_URL3.equals(resultUrl));
assertTrue( APACHE_CROSS_REF_URL2.equals(resultUrl) || APACHE_CROSS_REF_URL3.equals(resultUrl));

// non standard license
final String LICENSE_ID = "LicenseRef-nonStd1";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class TestMavenToSpdxLicenseMapper
private static final String APACHE2_URL = "http://www.apache.org/licenses/LICENSE-2.0";
private static final String APACHE_SPDX_ID = "Apache-2.0";

private static final String MIT_URL = "http://www.opensource.org/licenses/MIT";
private static final String MIT_URL = "http://opensource.org/licenses/MIT";
private static final String MIT_SPDX_ID = "MIT";

@Before
Expand Down
Loading

0 comments on commit ddba576

Please sign in to comment.