Giter Club home page Giter Club logo

schema-to-java's Introduction

schema-to-java

Generates Java classes from the SPDX Schema. Here we use the tool, JiBX to perform conversion.

Requirements

Either the Sun/Oracle JDK/JRE Variant or OpenJDK. I used OpenJDK 11.0.7.

Installation

  1. Clone or download the repository.
  2. Store the xsd file in src/main/config directory.

To generate modal classes from XSD

  1. Open the terminal and go to the repository.
  2. Run the project by command $ mvn jibx:schema-codegen.

Applying Global Customization

  1. Created GlobalCustomization.xml file in src/main/custom directory. Add the required custom attributes in schema element.
  2. Configure the custom file in pom.xml as shown below :
	<configuration>
         --------------

                    ------------
                    <customizations>
                                <customization>src/main/custom/GlobalCustomization.xml</customization>
                    </customizations>
                    ------------
         --------------
   </configuration>      
  1. Run the project by command $ mvn jibx:schema-codegen.

Applying Specific Customization

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.)

  1. 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
  1. 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 and https://github.com/jibx/core/blob/master/build/src/org/jibx/schema/codegen/extend/ClassDecorator.java respectively.

  2. Implement your customization codes in the same class of side project.

  3. Compile it, create its .jar and run $ mvn install.

  4. 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>
  1. Configure the SPDXClassDecorator along with its package in GlobalCustomization.xml in class-decorator element.

  2. Run the main-project by command: $ mvn jibx:schema-codegen.

A. Extend the generated classes by a superclass

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.

  1. 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

  2. Create a java class named ModelObject.java in main-project in src/main/source-code/org/spdx/library/model directory. This is our superclass.

  3. Add an attribute base-class in GlobalCustomization.xml to determine baseclass.

  4. Compile the SPDXClassDecorator.java and run $ mvn install.

  5. Switch to main-project and run(schema-to-java) by $ mvn jibx:schema-codegen.

B. Addition of constructors

Here, we add two parameterized constructors to all the non-enum classes. To do this:

  1. 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.

  2. The ModelObject.java class in src/main/source-code/org/spdx/library/model is to be modified. The new ModelObject.java class will be completely taken from https://github.com/spdx/Spdx-Java-Library/blob/master/src/main/java/org/spdx/library/model/ModelObject.java

  3. 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 in side-project/ClassHolderHelper.

  4. Compile the SPDXClassDecorator.java and run by $ mvn install.

  5. Switch to main-project and run(schema-to-java) by $ mvn jibx:schema-codegen.

C. Addition of getType() method

The getType() method is to be added to all the non-enum classes. To do this:

  1. 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().

  2. Compile the SPDXClassDecorator.java and run by $ mvn install.

  3. Switch to main-project and run(schema-to-java) by $ mvn jibx:schema-codegen.

D. Generation of Enum methods

This tasks deals with only enums. Here we add certain members and features to the enum classes. To do this:

  1. Add a new interface IndividualUriValue to src/main/source-code/org/spdx/library/model. This interface will implement all the non-enum classes.

  2. Add a new attribute interface in GlobalCustomization.xml under Class-Decorator element to locate IndividualUriValue, as this will be the implementing interface.

  3. Add two methods: getLongName() and getNameSpace() 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 in side-project/Decorator/org/spdx/jibx/SPDXClassDecorator.java.

  4. 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.

  5. Create a new SPDXNameConverter.java to modify the enum values of enum classes. This class will be invoked from GlobalCustomization.xml by adding name-converter attribute. For referrence view the code in side-project/SPDXNameConverter.java.

  6. Compile the SPDXClassDecorator.java and run by $ mvn install.

  7. Switch to main-project and run(schema-to-java) by $ mvn jibx:schema-codegen.

schema-to-java's People

Contributors

anishasrivastava1411 avatar goneall avatar rtgdk avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

schema-to-java's Issues

Restructure side-project code to conform with Java file naming practices

Most compile/IDE's expect the file names to be in a directory following the package name.

Following are the changes required:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.