Repository navigation
Generate Java SPDX Model Classes from XML XSD file, Google Summer of Code 2020
Generate Java Model Classes from XML Schema
Student Details: Anisha Srivastava | GitHub | LinkedIn
Mentors: Gary O'Neall and Rohit Lodha
The project was aimed at generating Java Model Classes from proposed XML Schema files. This involved mapping the elements of XML Schema to members of the java class using the JiBX Tool. Conversion is important for the sharing capability of the specifications. Importing packages and customization of JiBX enabled modification in generated classes brought them closely similar to the existing Spdx-java-library.
-
My first task was to generate model classes from the schema. To do this JiBX tool was used. A setup was created to install all plugins and dependencies for the project. JiBX’s
schema-codegenplugins enabled conversion. The XSD file was added as an input and the output directory was changed to keep all relevant classes in a single directory for upcoming tasks. This successfully generated the classes. -
The upcoming tasks were mostly focused on the customization of codes to make them more functional. To do this the JAR file was created to extend the Decorator classes of the tool(JiBX). The number of JiBX methods and eclipse packages were used in the code. This helped in modifying the respective Decorator class to complete every preferable customization. The JAR created was later added as a dependency to the project to reflect changes of Decorator classes into generated model classes. The XML file was created to enforce customization over every class. This file required time to time modification as per the tasks.
Global Customization:
This collectively customized the classes to be functional and utilizable. This included removal of schema snippets, enabled importing docs, a preferred inline decorator, etc in each generated class. The simple customizations of JiBX provide many aspects to do so.
Specific Customization:
This helped in applying customizations either to a particular schema or to a set of schemas. The JiBX CodeGen Extension enabled to use various additional attributes in GlobalCustomization.xml and various methods of JiBX Classes in SPDXClassDecorator.java
A. For non-enum classes:
-
Extended each class by a superclass: The classes which didn't have a predetermined superclass were extended by a common superclass, ModelObject.java. Since enums don’t extend any base class, in this task enums and non-enum classes were bifurcated.
-
Added Constructors: The task was to add two par constructors to each generated non-enum classes. Along with the invocation of super() in constructors to access the methods of the superclass.
-
Added getType() : A new method getType() was added to each class with String as return type and a block with the return statement.
-
Change in list return Type: This task included changing the return type and data member type from List to Collection. JIBX has an existing extension to change the type used for implementing lists - ListImplementationDecorator.
B. For Enums:
- Generated methods of Enums: Added an interface
IndividualUriValue.javato the project directory which every Enum will implement. The methods were similarly added to the enums as did for non-enum classes. And last but not the least, modified enumeration values by removing Enum name from each value. This could be done by addingSPDXNameConverter.javaclass which extends DefaultNameConverter.java
I did a detailed analysis of the generated classes and the existing classes to divide them into categories. This helped in understanding the difference and finding out the points that needed modification and customization.
Here is the link of the spreadsheet: https://docs.google.com/spreadsheets/d/1TUfUuYZEjt6Wqp0Erc2l_GmC0cWODDjkkHTai9llpEU/edit?usp=sharing
-
Generated classes: (except ModelObject and IndividualUriValue ) Java Classes
-
Decorator classes: Decorators
PR for classes generation: https://github.com/spdx/schema-to-java/pull/1
PR for Decorator classes: (except #PR1) https://github.com/spdx/schema-to-java/pulls?q=is%3Apr+is%3Aclosed+author%3AAnishaSrivastava1411
List of Commits: https://github.com/spdx/schema-to-java/commits?author=AnishaSrivastava1411
List of Issues: https://github.com/spdx/schema-to-java/issues?q=is%3Aopen+is%3Aissue
The close analysis of classes demands some more changes in generated classes.
- The customization over getters and setters which call methods to get and set more features.
- Addition of a few more methods in generated classes which are present in classes of spdx-java-library but not in schema-to-java classes.
- The changes in list return type.
The issues has been raised for some of the leftovers- Issues
A Pull Request still open- PR
To have the idea of project installation and working in detail please refer to the README.md
Contributing to SPDX has been an incredible journey. I would like to extend my sincere gratitude to my mentors Gary and Rohit, they were very responsive and I learned a lot while working with them. Also, I'm thankful to the community for providing me with such a great opportunity.
Thank you SPDX for an amazing GSoC 2020.