Generates Java classes from the SPDX Schema. Here we use the tool, JiBX to perform conversion.
Either the Sun/Oracle JDK/JRE Variant or OpenJDK. I used OpenJDK 11.0.7.
- Clone or download the repository.
- Store the xsd file in
src/main/config
directory.
- Open the terminal and go to the repository.
- Run the project by command
$ mvn jibx:schema-codegen
.
- Created GlobalCustomization.xml file in
src/main/custom
directory. Add the required custom attributes in schema element. - Configure the custom file in pom.xml as shown below :
<configuration>
--------------
------------
<customizations>
<customization>src/main/custom/GlobalCustomization.xml</customization>
</customizations>
------------
--------------
</configuration>
- Run the project by command
$ mvn jibx:schema-codegen
.
To apply specific decorations over generated classes we need to create a decor java class and write codes to perform changes.(For reference the decor file named SPDXClassDecorator.java is stored in src/main/main/Decorator/org/spdx/jibx
directory of schema-to-java.)
- Create a new side-project to have a separated pom.xml. We use this project to create jar file and use it as a dependency in our main project. We used specific groupId, artifactId and version in our new pom.xml. As shown below:
<groupId>org.spdx.jibx</groupId>
<artifactId>spdxclassdecorator</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>main
-
Create a new java class that extends NameMatchDecoratorBase implements ClassDecorator from
https://github.com/jibx/core/blob/master/build/src/org/jibx/schema/codegen/extend/NameMatchDecoratorBase.java
andhttps://github.com/jibx/core/blob/master/build/src/org/jibx/schema/codegen/extend/ClassDecorator.java
respectively. -
Implement your customization codes in the same class of side project.
-
Compile it, create its
.jar
and run$ mvn install
. -
Add the newly created plugin in main-project as dependency. As shown below :
<plugin>
---------
<dependencies>
<dependency>
<groupId>org.spdx.jibx</groupId>
<artifactId>spdxclassdecorator</artifactId>
<version>1.0-SNAPSHOT</versio
<scope>compile</scope>
<type>jar</type>
</dependency>
</dependencies>
---------
</plugin>
-
Configure the SPDXClassDecorator along with its package in GlobalCustomization.xml in class-decorator element.
-
Run the main-project by command:
$ mvn jibx:schema-codegen
.
Here we only extend java classes by a superclass. During java class generation, we also got some enums generated. Enums don't extend baseclass. The classes are to be extended by
ModelObject.java
provided the generated classes already don't extend any baseclass.
-
We implement start() of SPDXClassDecorator.java to extend classes which do not have pre determined baseclass in schema. This method also checks for enum. If class is enum it do not extend
-
Create a java class named ModelObject.java in main-project in
src/main/source-code/org/spdx/library/model
directory. This is our superclass. -
Add an attribute
base-class
in GlobalCustomization.xml to determine baseclass. -
Compile the SPDXClassDecorator.java and run
$ mvn install
. -
Switch to main-project and run(schema-to-java) by
$ mvn jibx:schema-codegen
.
Here, we add two parameterized constructors to all the non-enum classes. To do this:
-
Add the lines of code to the start() of SPDXClassDecorator to add constructor. For referrence view the code in
side-project/Decorator/ogr/spdx/jibx/SPDXClassDecorator.java
. -
The
ModelObject.java
class insrc/main/source-code/org/spdx/library/model
is to be modified. The new ModelObject.java class will be completely taken fromhttps://github.com/spdx/Spdx-Java-Library/blob/master/src/main/java/org/spdx/library/model/ModelObject.java
-
Create a class
ClassHolderHelper
to access methods of AST. This will help in calling and reusing the AST methods to complete our above task. For referrence view the code inside-project/ClassHolderHelper
. -
Compile the SPDXClassDecorator.java and run by
$ mvn install
. -
Switch to main-project and run(schema-to-java) by
$ mvn jibx:schema-codegen
.
The
getType()
method is to be added to all the non-enum classes.
To do this:
-
Add more codes to SPDXClassDecorator to add method. Here again we call many methods using ClassHolderHelper. We add return type of method, access modifiers, block and return statement to the method getType(). This piece of code is also written in start().
-
Compile the SPDXClassDecorator.java and run by
$ mvn install
. -
Switch to main-project and run(schema-to-java) by
$ mvn jibx:schema-codegen
.
This tasks deals with only enums. Here we add certain members and features to the enum classes. To do this:
-
Add a new interface
IndividualUriValue
tosrc/main/source-code/org/spdx/library/model
. This interface will implement all the non-enum classes. -
Add a new attribute
interface
inGlobalCustomization.xml
under Class-Decorator element to locate IndividualUriValue, as this will be the implementing interface. -
Add two methods:
getLongName()
andgetNameSpace()
with String as return type. Add return statements to these methods. This can be done be adding codes to the SPDXClassDecorator.java. For referrence view the code inside-project/Decorator/org/spdx/jibx/SPDXClassDecorator.java
. -
Add a new method
getIndividualURI()
with String as return type. It's return statement calls both previously created methods ie, getLongName() and getNameSpace(). These methods are created by writting codes in start() of SPDXClassDecorator.java. -
Create a new
SPDXNameConverter.java
to modify the enum values of enum classes. This class will be invoked fromGlobalCustomization.xml
by addingname-converter
attribute. For referrence view the code inside-project/SPDXNameConverter.java
. -
Compile the SPDXClassDecorator.java and run by
$ mvn install
. -
Switch to main-project and run(schema-to-java) by
$ mvn jibx:schema-codegen
.