2626import java .nio .charset .StandardCharsets ;
2727import java .util .LinkedHashMap ;
2828import java .util .Map ;
29+ import java .util .Objects ;
2930import java .util .Set ;
3031
3132public class SpdxXsdGenerator {
3233
33- //todo : automatically obtain latest release from: https://api.github.com/repos/spdx/license-list-data/releases
34- //todo : make configurable
35- private static final String SPDX_VERSION = "3.27.0" ;
34+ public static void main (String [] args ) throws Exception {
35+ String tagName = args .length == 0 || Objects .equals (args [0 ], "latest" )
36+ ? getLatestReleaseTagName ()
37+ : args [0 ];
38+ new SpdxXsdGenerator (tagName )
39+ .generateSchemas ();
40+ }
41+
42+ private static final String REPO = "spdx/license-list-data" ;
43+
44+ private static String getLatestReleaseTagName () throws Exception {
45+ String apiReleasesLatest = "https://api.github.com/repos/" + REPO + "/releases/latest" ;
46+ HttpResponse <JsonNode > apiResponse = Unirest .get (apiReleasesLatest ).asJson ();
47+ final JSONObject apiResponseRoot = apiResponse .getBody ().getObject ();
48+ return apiResponseRoot .getString ("tag_name" );
49+ }
50+
51+ private final String tagName ;
3652
37- public static void main (String args []) throws Exception {
38- String licenseUrl = "https://raw.githubusercontent.com/spdx/license-list-data/v" + SPDX_VERSION + "/json/licenses.json" ;
39- String exceptionsUrl = "https://raw.githubusercontent.com/spdx/license-list-data/v" + SPDX_VERSION + "/json/exceptions.json" ;
53+ public SpdxXsdGenerator (String tagName ) {
54+ this .tagName = tagName ;
55+ }
56+
57+ public void generateSchemas () throws Exception {
58+ System .out .println ("Generate Schemas for " + REPO + " tagName: " + tagName );
59+
60+ String licenseUrl = "https://raw.githubusercontent.com/" + REPO + "/" + tagName + "/json/licenses.json" ;
61+ String exceptionsUrl = "https://raw.githubusercontent.com/" + REPO + "/" + tagName + "/json/exceptions.json" ;
4062
4163 HttpResponse <JsonNode > licenseResponse = Unirest .get (licenseUrl ).asJson ();
4264 final JSONObject licenseRoot = licenseResponse .getBody ().getObject ();
@@ -62,15 +84,14 @@ public static void main(String args[]) throws Exception {
6284 createJsonSchema (licenseMap , exceptionMap );
6385 }
6486
65-
66- private static void createXmlSchema (Map <String , String > licenses , Map <String , String > exceptions ) throws IOException {
87+ private void createXmlSchema (Map <String , String > licenses , Map <String , String > exceptions ) throws IOException {
6788 StringBuilder sb = new StringBuilder ();
6889 sb
6990 .append ("<?xml version=\" 1.0\" encoding=\" utf-8\" ?>" ).append ("\n " )
7091 .append ("<xs:schema xmlns:xs=\" http://www.w3.org/2001/XMLSchema\" " ).append ("\n " )
7192 .append (indent (11 )).append ("elementFormDefault=\" qualified\" " ).append ("\n " )
7293 .append (indent (11 )).append ("targetNamespace=\" http://cyclonedx.org/schema/spdx\" " ).append ("\n " )
73- .append (indent (11 )).append ("version=\" 1.0-" + SPDX_VERSION + "\" >" ).append ("\n \n " )
94+ .append (indent (11 )).append ("version=\" 1.0-" + stripLeadingV ( tagName ) + "\" >" ).append ("\n \n " )
7495 .append (indent (4 )).append ("<xs:simpleType name=\" licenseId\" >" ).append ("\n " )
7596 .append (indent (8 )).append ("<xs:restriction base=\" xs:string\" >" ).append ("\n " );
7697
@@ -90,13 +111,13 @@ private static void createXmlSchema(Map<String, String> licenses, Map<String, St
90111 FileUtils .writeStringToFile (file , sb .toString (), StandardCharsets .UTF_8 );
91112 }
92113
93- private static void createJsonSchema (Map <String , String > licenses , Map <String , String > exceptions ) throws IOException {
114+ private void createJsonSchema (Map <String , String > licenses , Map <String , String > exceptions ) throws IOException {
94115 StringBuilder sb = new StringBuilder ();
95116 sb
96117 .append ("{" ).append ("\n " )
97118 .append (indent (2 )).append ("\" $schema\" : \" http://json-schema.org/draft-07/schema#\" ," ).append ("\n " )
98119 .append (indent (2 )).append ("\" $id\" : \" http://cyclonedx.org/schema/spdx.schema.json\" ," ).append ("\n " )
99- .append (indent (2 )).append ("\" $comment\" : \" v1.0-" + SPDX_VERSION + "\" ," ).append ("\n " )
120+ .append (indent (2 )).append ("\" $comment\" : \" v1.0-" + stripLeadingV ( tagName ) + "\" ," ).append ("\n " )
100121 .append (indent (2 )).append ("\" type\" : \" string\" ," ).append ("\n " )
101122 .append (indent (2 )).append ("\" enum\" : [" );
102123
@@ -144,4 +165,10 @@ private static String indent(int spaces) {
144165 return sb .toString ();
145166 }
146167
168+ public static String stripLeadingV (String input ) {
169+ if (input != null && input .length () > 1 && input .charAt (0 ) == 'v' ) {
170+ return input .substring (1 );
171+ }
172+ return input ;
173+ }
147174}
0 commit comments